Uncommited changes synced
[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             hours[-1] = (typeof(hours[-1]) == 'undefined') ? {} : hours[-1];
86             hours[-1][project] = (typeof(hours[-1][project]) == 'undefined') ? 
87                     { total : 0, items : {} } 
88                     : hours[-1][project];
89             
90             hours[-1][project].total += line.span;
91             hours[-1][project].items[line.desc] = (typeof(hours[-1][project].items[line.desc]) == 'undefined' ?
92                 0 : hours[-1][project].items[line.desc]) + line.span;
93             
94             shours[hour] = (typeof(shours[hour]) == 'undefined') ? {} : shours[hour];
95             shours[hour][line.desc] = (typeof(shours[hour][line.desc] ) == 'undefined') ? 0 : shours[hour][line.desc] ;
96             shours[hour][line.desc] += line.span;
97              
98             
99             
100         }
101          this.shours = shours;
102         return hours;
103
104     },
105     parseLine : function(l) 
106     {
107         var ret = { cmd : false,  line : l, span : 0 };
108         var ar = l.split(/\s+/);
109         //print(JSON.stringify(ar));
110         
111             
112         var time = ar.shift();
113         
114         ret.desc = ar.join(' ');
115         
116         //print("time: " + time);
117         
118         //ret.start = xDate.Date.parseDate(this.date.format('Y-m-d') + ' ' + time, 'Y-m-d H:i:s');
119         ret.start = xDate.Date.parseDate(
120                             (this.date ? this.date.format('Y-m-d')  :'2013-01-01') +
121                             ' ' + time, 'Y-m-d H:i:s');
122
123         while (ret.cmd === false) {
124             var ta = ar.pop();
125             //print("TA:"+ta)
126             if (ta[0] !=  '-') { //hopfully withc catch stuff.
127                 ret.cmd = ta;
128                 break;
129             }
130             if (!ar.length) {
131                 // just assume it's the last bit..
132                 //print(line);
133                 throw "invalid line: " + l;
134             }
135
136         }
137         //print(ret.cmd);
138         
139         
140         ret.title = ar.join(' ');
141         if (ret.title == 'IDLE') {
142             ret.project = 'IDLE';
143             return ret;        
144         }
145         ret.project = 'Unknown';
146         
147         for(var proj in this.regexes) {
148             for(var r in this.regexes[proj]) {
149                 if (ret.line.match(this.regexes[proj][r])) {
150                     //print("Match " + proj + " on " + r + ret.line + )
151                     ret.project = proj;
152                     break;
153                 }
154             }
155             if (ret.project != 'Unknown') {
156                 break;
157             }
158         }
159         return ret;
160         /*
161
162         if (typeof(this[ret.cmd])=='undefined') {
163              ret.project = 'Unknown';
164             return ret;
165             print( "Unknown application: " + ret.line);
166             throw { error : "TEST"};
167         }
168         
169         //print(ret.cmd);
170         //print(ret.title);
171         if (typeof(this[ret.cmd])=='string') {
172             ret.project = this[ret.cmd]  
173         } else {
174
175             this[ret.cmd](ret);
176         }
177
178
179         return ret;
180         */
181
182     },
183     
184     regexes : {
185         'HPA' : [ /web.hpasite/, /Roo Application Builder - Site/, /HP Intranet/, /Hpasite/ ],
186         'MediaOut' : [ /app\.WorkerBee/, /web\.MediaOut/, /workerbee/,
187                         /media-outreach/ , /Press Release Image Portal/, /Media Outreach Feed/ , /PressRelease/,
188                         /Clipping Portal/, /Media OutReach/,
189                         /Auto Searching/ , // These are the excel files they send over..
190                         /sitemap\.xml/,
191                         /Builder - Clipping/
192                         ],
193         'Habito' : [ /Interspire/ ],
194         'Hebe' :[ /web.hhyc_membership_system/ ,/Builder - Hebe/, /Hebe Haven Membership Managment/],
195         
196         'RIS' : [ /Project web.Ris/ , /RIS HK/ , /Hydra/, /hydrajobs/ ],
197         // for who thoug...
198         'Mailfort' : [ /web.FlexySpam/ ],
199         'Emily' : [ /greengarden/ ,/Inventory \| Warehouse/, /Emily/ ],
200         'GPUK' : [/gp-wordpress/, /Greenpeace UK/], 
201         
202         'COBA'  : [/Veridate/, /web\.coba/, /COBA/ ,/Pipeline - DS view/, /Coba/, /Pman.BAdmin/ , /SoapUI/ ],
203         'Regulation Asia' : [/Regulation Asia/, /comocracy/],
204         'Unknown Project' : [ /PAC/ , /Guake/ , /WhatsApp/ ,/Mozilla Thunderbird/],
205         'Admin Work' : [ /roojs.com\/admin/, /RooJSolutions Management Portal/ ]
206             
207          
208     },
209     
210     match_project : function(line) {
211         for(var proj in this.regexes) {
212             for(var r in this.regexes[proj]) {
213                 if (line.match(this.regexes[proj][r])) {
214                     //print("Match " + proj + " on " + r + ret.line + )
215                     return  proj;
216
217                 }
218             }
219             
220         }
221         return "Unknown";
222     },
223     
224     
225     '/usr/bin/perl' : function(ret) {
226         if (ret.title.match(/^PAC/)) {
227             ret.project = 'Unknown';
228             return  'Unknown';
229         }
230         return 'Unknown';
231         throw "Unknown match: " + ret.line;
232     },
233     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
234     '/usr/lib/chromium/chromium' : function (ret) {
235
236           switch(true) {
237
238                 case (ret.title.match(/Media Clipping Portal/)):
239                     ret.project = 'Media Outreach';
240                     return;
241                 
242                 default:
243                     ret.project = 'Browsing';
244                     return;
245           }
246
247     },
248     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
249         var l = ret.title.match(/Project\s+(^\)+)/);
250         if (!l) {
251             ret.project="Unknown";
252             return;
253         }
254         throw "Unknown match: " + ret.line;
255     },
256     'guake' : 'Local Terminal',
257     'mono' : 'mono?'
258     
259
260 }
261 //print(Seed.argv[2]);Seed.quit();
262 if (typeof(Seed.argv[2]) == 'undefined') {
263     print("pick a date");
264     Seed.quit();
265 }
266  
267 var res = GitLogParser.parse( Seed.argv[2][0] == '/' ? Seed.argv[2] : xDate.Date.parseDate(Seed.argv[2], 'Y-m-d'));
268 var totals = { work : 0 , idle: 0, shortidle : 0};
269 var unknown = [];
270 for (var h in res) {
271     for (var p in res[h]) {
272         if (p == 'LONGIDLE') {
273             var idletime = Math.floor(res[h][p].total/60000) ;
274             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m LONGIDLE' );
275             totals.idle += idletime;
276             
277              
278             continue;
279         }
280         if (p == 'IDLE') {
281             var idletime = Math.floor(res[h][p].total/60000) ;
282             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m SHORT IDLE' );
283             totals.shortidle += idletime;
284             
285              
286             continue;
287         }
288         
289         
290         //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m ' + p );  
291         totals.work += Math.floor(res[h][p].total/60000) ;
292        
293         if (h == -1) {
294             
295             print( "\n" + p + ':' + Math.floor(res[h][p].total/60000) + 'm');
296             
297             if (p != 'Unknown') {
298         
299                 for (var k in res[h][p].items) {
300                      
301                     print( '     ' +   Math.floor(res[h][p].items[k]/60000) +'m ' + k );
302                          
303                 }
304             } else {
305                 
306                 for (var k in res[h][p].items) {
307                     unknown.push({ desc : k , tot : (res[h][p].items[k]/60000).toFixed(2)*1 }) ;
308                     
309                          
310                 }
311             }
312         }
313         
314     }
315     
316 }
317
318
319 print("\nLONGIDLE : " +(totals.idle/60).toFixed(2) +"h" );
320 print("\nShort Idle : " +(totals.shortidle/60).toFixed(2) +"h" );
321
322 print("Worked: " + (totals.work/60).toFixed(2) +"h\n" );
323  
324 var totals = {};
325
326 for (var h in GitLogParser.shours) {
327     var hsum = [];
328     var htot = 0;
329     for (var desc in GitLogParser.shours[h]) {
330         htot += (GitLogParser.shours[h][desc]/60000).toFixed(2)*1;
331         hsum.push({ desc : desc, tot : (GitLogParser.shours[h][desc]/60000).toFixed(2)*1 })
332         totals[desc] = (typeof(totals[desc]) == 'undefined' ? 0 : totals[desc]) + ((GitLogParser.shours[h][desc]/60000).toFixed(2)*1);
333     }
334     hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
335     print("\n\n" + h+': Total (' + htot +')');
336     hsum.forEach(function(r) {
337         var pr = r.desc.match(/IDLE/) ? '' : (GitLogParser.match_project(r.desc) == "Unknown" ? '??? - ' : '');
338         
339         print ("  " + r.tot + "   : " + pr +   r.desc);
340     });
341 }
342   
343  
344 unknown.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
345 print("\n\nUnclassified");
346 unknown.forEach(function(r) {
347     print ("  " + (r.tot.toFixed(2)*1) + "   : " + r.desc);
348 });
349
350 //print(JSON.stringify(GitLogParser.shours,null,4));
351
352
353
354 // open file..
355
356 // read lines
357
358 // summarize each hour
359
360 //convert line into 'Project / filename'
361
362