GitLogParser.js
[gitlive] / GitLogParser.js
1
2 var File = imports.File.File;
3 xDate = imports.Date;
4
5
6 GitLogParser = { 
7
8     parse : function(date)
9     {
10         this.date  = date;        
11         var home  = GLib.get_home_dir();
12         
13         var flines = File.read(  home + '/.gitlog' + date.format('/Y/m/d') + '.log').split("\n");
14         // first just convert them..
15         // we had an old bug that did not put line breaks in there..
16         // however 00:00:00: is pretty distinct, so let'st try and split on it..
17         
18         
19         
20         lines = [];
21         for (var i = 0; i < flines.length; i++) {
22             var xl = flines[i].split(/([0-9]{2}:[0-9]{2}:[0-9]{2})/);
23             //print(JSON.stringify(xl));
24             for (var ii=1; ii< xl.length; ii+=2) {
25                 var p = lines.length;
26                 lines.push( this.parseLine(xl[ii] + ' ' + xl[ii+1])); 
27                 if (p > 0) {
28                     lines[p-1].span = lines[p].start - lines[p-1].start; // should be seconds..?
29                 }
30             
31             }
32              
33             
34         };
35         // summarize data...
36         var hours = {};
37         for (var i = 0; i < lines.length; i++) {
38             var line = lines[i];
39             var hour = hours[line].start.format('H');
40             hours[hour] = (typeof(hours[hour]) == 'undefined') ? {} : hours[hour];
41             hours[hour][project] = (typeof(hours[project]) == 'undefined') ? 
42                     { total : 0, items : [] } 
43                     : hours[project];
44             hours[hour][project].total += line.span;
45             hours[hour][project].items.push(line);
46         }
47         return hours;
48
49     },
50     parseLine : function(l) 
51     {
52         var ret = { cmd : false,  line : l };
53         var ar = l.split(/\s+/);
54         print(JSON.stringify(ar));
55             
56         var time = ar.shift();
57         print("time: " + time);
58         
59         ret.start = xDate.Date.parseDate(this.date.format('Y-m-d') + ' ' + time, 'Y-m-d H:i:s');
60         
61
62         while (ret.cmd !== false) {
63             var ta = ar.pop();
64             print("TA:"+ta)
65             if (ta[0] !=  '-') { //hopfully withc catch stuff.
66                 ret.cmd = ta;
67                 break;
68             }
69             if (!ar.length) {
70                 // just assume it's the last bit..
71                 print(line);
72                 throw "invalid line: " + l;
73             }
74
75         }
76         print(ret.cmd);
77         
78         
79         ret.title = ar.join(' ');
80         if (ret.title == 'IDLE') {
81             ret.project = 'IDLE';
82             return ret;        
83         }
84                 
85         
86
87         if (typeof(this[ret.cmd])=='undefined') {
88             throw "Unknown application: " + ret.line;
89         }
90         if (typeof(this[ret.cmd])=='string') {
91             ret.project = this[ret.cmd]  
92         } else {
93
94             this[ret.cmd](ret);
95         }
96
97
98         return ret;
99
100     },
101  
102     '/usr/bin/perl' : function(ret) {
103         if (ret.title.match(/^PAC/)) {
104             ret.project = 'Unknown';
105             return;
106         }
107         throw "Unknown match: " + ret.line;
108     },
109     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
110     '/usr/lib/chromium/chromium' : function (ret) {
111
112           switch(true) {
113
114                 case (ret.title.match(/Media Clipping Portal/)):
115                     ret.project = 'Media Outreach';
116                     return;
117                 
118                 default:
119                     ret.project = 'Browsing';
120                     return;
121           }
122
123     },
124     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
125           var l = ret.title.match(/Project\s+(^\)+)/);
126           print(l);
127               
128          throw "Unknown match: " + ret.line;
129     },
130     'guake' : 'Local Terminal',
131
132
133 }
134
135 var res = GitLogParser.parse(xDate.Date.parseDate('2012-07-31', 'Y-m-d'));
136 print(JSON.stringify(res,null,4));
137
138 // open file..
139
140 // read lines
141
142 // summarize each hour
143
144 //convert line into 'Project / filename'
145
146