GitLogParser.js
[gitlive] / GitLogParser.js
1
2 var File = imports.File.File;
3 xDate = imports.Date;
4
5
6
7
8
9 GitLogParser = {
10     shours : false,
11     date : false,
12     parse : function(date)
13     {
14         var filename = date;
15         if (typeof(date) == 'object') {
16             this.date  = date;        
17             var home  = GLib.get_home_dir();
18             //print( "READING FILE");
19             filename = home + '/.gitlog' + date.format('/Y/m/d') + '.log';
20         
21         
22         }
23         
24         var flines = File.read(  filename ).split("\n");        
25         //print("loaded");
26         // first just convert them..
27         // we had an old bug that did not put line breaks in there..
28         // however 00:00:00: is pretty distinct, so let'st try and split on it..
29         
30         
31         
32         lines = [];
33         
34         // read the lines, and fill in the 'spans'
35         
36         for (var i = 0; i < flines.length; i++) {
37             
38             var xl = flines[i].split(/([0-9]{2}:[0-9]{2}:[0-9]{2})/);
39             
40             //print(JSON.stringify(xl));
41             for (var ii=1; ii< xl.length; ii+=2) {
42                 var p = lines.length;
43                 lines.push( this.parseLine(xl[ii] + ' ' + xl[ii+1])); 
44                 if (p > 0) {
45                     lines[p-1].span = lines[p].start - lines[p-1].start; // should be seconds..?
46                     lines[p-1].spanMin = lines[p-1].span/60000;
47                     
48                     
49                 }
50             
51             }
52              
53             
54         }
55         //print("parsed");
56         //print(JSON.stringify(lines,null,4));
57         
58         // summarize data...
59         var hours = {};
60         var shours = {};
61         
62         // shours should be:
63         // hour : [ ]
64         
65          
66         for (var i = 0; i < lines.length; i++) {
67             var line = lines[i];
68             var hour = line.start.format('H');
69             
70             if (line.project == 'IDLE' && line.spanMin >= 5 ) {
71                 line.project = 'LONGIDLE';
72             }
73             if (line.project == 'IDLE' || line.project == 'LONGIDLE') {
74                 line.desc = line.project;
75             }
76             
77             var project = line.project;
78             hours[hour] = (typeof(hours[hour]) == 'undefined') ? {} : hours[hour];
79             hours[hour][project] = (typeof(hours[hour][project]) == 'undefined') ? 
80                     { total : 0, items : [] } 
81                     : hours[hour][project];
82             hours[hour][project].total += line.span;
83             hours[hour][project].items.push(line);
84             
85             shours[hour] = (typeof(shours[hour]) == 'undefined') ? {} : shours[hour];
86             shours[hour][line.desc] = (typeof(shours[hour][line.desc] ) == 'undefined') ? 0 : shours[hour][line.desc] ;
87             shours[hour][line.desc] += line.span;
88              
89             
90             
91         }
92         this.total = total;
93         this.shours = shours;
94         return hours;
95
96     },
97     parseLine : function(l) 
98     {
99         var ret = { cmd : false,  line : l, span : 0 };
100         var ar = l.split(/\s+/);
101         //print(JSON.stringify(ar));
102         
103             
104         var time = ar.shift();
105         
106         ret.desc = ar.join(' ');
107         
108         //print("time: " + time);
109         
110         //ret.start = xDate.Date.parseDate(this.date.format('Y-m-d') + ' ' + time, 'Y-m-d H:i:s');
111         ret.start = xDate.Date.parseDate(
112                             (this.date ? this.date.format('Y-m-d')  :'2013-01-01') +
113                             ' ' + time, 'Y-m-d H:i:s');
114
115         while (ret.cmd === false) {
116             var ta = ar.pop();
117             //print("TA:"+ta)
118             if (ta[0] !=  '-') { //hopfully withc catch stuff.
119                 ret.cmd = ta;
120                 break;
121             }
122             if (!ar.length) {
123                 // just assume it's the last bit..
124                 //print(line);
125                 throw "invalid line: " + l;
126             }
127
128         }
129         //print(ret.cmd);
130         
131         
132         ret.title = ar.join(' ');
133         if (ret.title == 'IDLE') {
134             ret.project = 'IDLE';
135             return ret;        
136         }
137         ret.project = 'Unknown';
138         
139         for(var proj in this.regexes) {
140             for(var r in this.regexes[proj]) {
141                 if (ret.line.match(r)) {
142                     ret.project = proj;
143                     break;
144                 }
145             }
146             if (ret.project != 'Unknown') {
147                 break;
148             }
149         }
150         return ret;
151         /*
152
153         if (typeof(this[ret.cmd])=='undefined') {
154              ret.project = 'Unknown';
155             return ret;
156             print( "Unknown application: " + ret.line);
157             throw { error : "TEST"};
158         }
159         
160         //print(ret.cmd);
161         //print(ret.title);
162         if (typeof(this[ret.cmd])=='string') {
163             ret.project = this[ret.cmd]  
164         } else {
165
166             this[ret.cmd](ret);
167         }
168
169
170         return ret;
171         */
172
173     },
174     
175     regexes : {
176         'HPA' : [ /web.hpasite/, ],
177         'MediaOut' : [ /app\.WorkerBee/, /web.MediaOut/,
178                         /media-outreach/ , /Press Release Image Portal/, /Media Outreach Feed/ , /PressRelease/],
179         'Habito' : [ /Interspire/ ],
180         'Hebe' :[ /web.hhyc_membership_system/ ]
181         
182         
183         
184     },
185     
186     '/usr/bin/perl' : function(ret) {
187         if (ret.title.match(/^PAC/)) {
188             ret.project = 'Unknown';
189             return  'Unknown';
190         }
191         return 'Unknown';
192         throw "Unknown match: " + ret.line;
193     },
194     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
195     '/usr/lib/chromium/chromium' : function (ret) {
196
197           switch(true) {
198
199                 case (ret.title.match(/Media Clipping Portal/)):
200                     ret.project = 'Media Outreach';
201                     return;
202                 
203                 default:
204                     ret.project = 'Browsing';
205                     return;
206           }
207
208     },
209     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
210         var l = ret.title.match(/Project\s+(^\)+)/);
211         if (!l) {
212             ret.project="Unknown";
213             return;
214         }
215         throw "Unknown match: " + ret.line;
216     },
217     'guake' : 'Local Terminal',
218     'mono' : 'mono?'
219     
220
221 }
222 //print(Seed.argv[2]);Seed.quit();
223 if (typeof(Seed.argv[2]) == 'undefined') {
224     print("pick a date");
225     Seed.quit();
226 }
227  
228 var res = GitLogParser.parse( Seed.argv[2][0] == '/' ? Seed.argv[2] : xDate.Date.parseDate(Seed.argv[2], 'Y-m-d'));
229 var totals = { work : 0 , idle: 0, shortidle : 0}
230 for (var h in res) {
231     for (var p in res[h]) {
232         if (p == 'LONGIDLE') {
233             var idletime = Math.floor(res[h][p].total/60000) ;
234             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m LONGIDLE' );
235             totals.idle += idletime;
236             
237              
238             continue;
239         }
240         if (p == 'IDLE') {
241             var idletime = Math.floor(res[h][p].total/60000) ;
242             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m SHORT IDLE' );
243             totals.shortidle += idletime;
244             
245              
246             continue;
247         }
248         
249         
250         //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m ' + p );  
251         totals.work += Math.floor(res[h][p].total/60000) ;
252         for (var k in res[h][p].items) {
253              
254             //print( '     ' + Math.floor(res[h][p].items[k].span/60000) +'m ' + res[h][p].items[k].line );
255                  
256         }
257         
258         
259     }
260     
261 }
262
263
264 print("\nLONGIDLE : " +(totals.idle/60).toFixed(2) +"h" );
265 print("\nShort Idle : " +(totals.shortidle/60).toFixed(2) +"h" );
266
267 print("Worked: " + (totals.work/60).toFixed(2) +"h\n" );
268  
269 var totals = {};
270
271 for (var h in GitLogParser.shours) {
272     var hsum = [];
273     var htot = 0;
274     for (var desc in GitLogParser.shours[h]) {
275         htot += (GitLogParser.shours[h][desc]/60000).toFixed(2)*1;
276         hsum.push({ desc : desc, tot : (GitLogParser.shours[h][desc]/60000).toFixed(2)*1 })
277         totals[desc] = (typeof(totals[desc]) == 'undefined' ? 0 : totals[desc]) + ((GitLogParser.shours[h][desc]/60000).toFixed(2)*1);
278     }
279     hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
280     print("\n\n" + h+': Total (' + htot +')');
281     hsum.forEach(function(r) {
282         print ("  " + r.tot + "   : " + r.desc);
283     });
284 }
285 print("\n\n" + h+': Totals (' + htot +')');
286  
287 var hsum = [];
288 for (var desc in totals) {    
289     hsum.push({ desc : desc, tot : totals[desc] });
290 }
291 hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
292 print("\n\nDay Total");
293 hsum.forEach(function(r) {
294     print ("  " + (r.tot.toFixed(2)*1) + "   : " + r.desc);
295 });
296
297 //print(JSON.stringify(GitLogParser.shours,null,4));
298
299
300
301 // open file..
302
303 // read lines
304
305 // summarize each hour
306
307 //convert line into 'Project / filename'
308
309