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         var totals = {};
66         
67         for (var i = 0; i < lines.length; i++) {
68             var line = lines[i];
69             var hour = line.start.format('H');
70             
71             if (line.project == 'IDLE' && line.spanMin >= 5 ) {
72                 line.project = 'LONGIDLE';
73             }
74             if (line.project == 'IDLE' || line.project == 'LONGIDLE') {
75                 line.desc = line.project;
76             }
77             
78             var project = line.project;
79             hours[hour] = (typeof(hours[hour]) == 'undefined') ? {} : hours[hour];
80             hours[hour][project] = (typeof(hours[hour][project]) == 'undefined') ? 
81                     { total : 0, items : [] } 
82                     : hours[hour][project];
83             hours[hour][project].total += line.span;
84             hours[hour][project].items.push(line);
85             
86             shours[hour] = (typeof(shours[hour]) == 'undefined') ? {} : shours[hour];
87             shours[hour][line.desc] = (typeof(shours[hour][line.desc] ) == 'undefined') ? 0 : shours[hour][line.desc] ;
88             shours[hour][line.desc] += line.span;
89             
90         }
91         this.shours = shours;
92         return hours;
93
94     },
95     parseLine : function(l) 
96     {
97         var ret = { cmd : false,  line : l, span : 0 };
98         var ar = l.split(/\s+/);
99         //print(JSON.stringify(ar));
100         
101             
102         var time = ar.shift();
103         
104         ret.desc = ar.join(' ');
105         
106         //print("time: " + time);
107         
108         //ret.start = xDate.Date.parseDate(this.date.format('Y-m-d') + ' ' + time, 'Y-m-d H:i:s');
109         ret.start = xDate.Date.parseDate(
110                             (this.date ? this.date.format('Y-m-d')  :'2013-01-01') +
111                             ' ' + time, 'Y-m-d H:i:s');
112
113         while (ret.cmd === false) {
114             var ta = ar.pop();
115             //print("TA:"+ta)
116             if (ta[0] !=  '-') { //hopfully withc catch stuff.
117                 ret.cmd = ta;
118                 break;
119             }
120             if (!ar.length) {
121                 // just assume it's the last bit..
122                 //print(line);
123                 throw "invalid line: " + l;
124             }
125
126         }
127         //print(ret.cmd);
128         
129         
130         ret.title = ar.join(' ');
131         if (ret.title == 'IDLE') {
132             ret.project = 'IDLE';
133             return ret;        
134         }
135         ret.project = 'Unknown';
136         
137         for(var proj in this.regexes) {
138             for(var r in this.regexes[proj]) {
139                 if (ret.line.match(r)) {
140                     ret.project = proj;
141                     break;
142                 }
143             }
144             if (ret.project != 'Unknown') {
145                 break;
146             }
147         }
148         return ret;
149         /*
150
151         if (typeof(this[ret.cmd])=='undefined') {
152              ret.project = 'Unknown';
153             return ret;
154             print( "Unknown application: " + ret.line);
155             throw { error : "TEST"};
156         }
157         
158         //print(ret.cmd);
159         //print(ret.title);
160         if (typeof(this[ret.cmd])=='string') {
161             ret.project = this[ret.cmd]  
162         } else {
163
164             this[ret.cmd](ret);
165         }
166
167
168         return ret;
169         */
170
171     },
172     
173     regexes : {
174         'HPA' : [ /web.hpasite/, ],
175         'MediaOut' : [ /app\.WorkerBee/, /web.MediaOut/,
176                         /media-outreach/ , /Press Release Image Portal/, /Media Outreach Feed/ , /PressRelease/],
177         'Habito' : [ /Interspire/ ],
178         'Hebe' :[ /web.hhyc_membership_system/ ]
179         
180         
181         
182     },
183     
184     '/usr/bin/perl' : function(ret) {
185         if (ret.title.match(/^PAC/)) {
186             ret.project = 'Unknown';
187             return  'Unknown';
188         }
189         return 'Unknown';
190         throw "Unknown match: " + ret.line;
191     },
192     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
193     '/usr/lib/chromium/chromium' : function (ret) {
194
195           switch(true) {
196
197                 case (ret.title.match(/Media Clipping Portal/)):
198                     ret.project = 'Media Outreach';
199                     return;
200                 
201                 default:
202                     ret.project = 'Browsing';
203                     return;
204           }
205
206     },
207     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
208         var l = ret.title.match(/Project\s+(^\)+)/);
209         if (!l) {
210             ret.project="Unknown";
211             return;
212         }
213         throw "Unknown match: " + ret.line;
214     },
215     'guake' : 'Local Terminal',
216     'mono' : 'mono?'
217     
218
219 }
220 //print(Seed.argv[2]);Seed.quit();
221 if (typeof(Seed.argv[2]) == 'undefined') {
222     print("pick a date");
223     Seed.quit();
224 }
225  
226 var res = GitLogParser.parse( Seed.argv[2][0] == '/' ? Seed.argv[2] : xDate.Date.parseDate(Seed.argv[2], 'Y-m-d'));
227 var totals = { work : 0 , idle: 0, shortidle : 0}
228 for (var h in res) {
229     for (var p in res[h]) {
230         if (p == 'LONGIDLE') {
231             var idletime = Math.floor(res[h][p].total/60000) ;
232             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m LONGIDLE' );
233             totals.idle += idletime;
234             
235              
236             continue;
237         }
238         if (p == 'IDLE') {
239             var idletime = Math.floor(res[h][p].total/60000) ;
240             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m SHORT IDLE' );
241             totals.shortidle += idletime;
242             
243              
244             continue;
245         }
246         
247         
248         //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m ' + p );  
249         totals.work += Math.floor(res[h][p].total/60000) ;
250         for (var k in res[h][p].items) {
251              
252             //print( '     ' + Math.floor(res[h][p].items[k].span/60000) +'m ' + res[h][p].items[k].line );
253                  
254         }
255         
256         
257     }
258     
259 }
260
261
262 print("\nLONGIDLE : " +(totals.idle/60).toFixed(2) +"h" );
263 print("\nShort Idle : " +(totals.shortidle/60).toFixed(2) +"h" );
264
265 print("Worked: " + (totals.work/60).toFixed(2) +"h\n" );
266  
267 var totals = {};
268
269 for (var h in GitLogParser.shours) {
270     var hsum = [];
271     var htot = 0;
272     for (var desc in GitLogParser.shours[h]) {
273         htot += (GitLogParser.shours[h][desc]/60000).toFixed(2)*1;
274         hsum.push({ desc : desc, tot : (GitLogParser.shours[h][desc]/60000).toFixed(2)*1 })
275         totals[desc] = (typeof(totals[desc]) == 'undefined' ? 0 : totals[desc]) + ((GitLogParser.shours[h][desc]/60000).toFixed(2)*1);
276     }
277     hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
278     print("\n\n" + h+': Total (' + htot +')');
279     hsum.forEach(function(r) {
280         print ("  " + r.tot + "   : " + r.desc);
281     });
282 }
283 print("\n\n" + h+': Totals (' + htot +')');
284  
285 var hsum = [];
286 for (var desc in totals) {    
287     hsum.push({ desc : desc, tot : totals[desc] });
288 }
289 hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
290 print("\n\nDay Total");
291 hsum.forEach(function(r) {
292     print ("  " + (r.tot.toFixed(2)*1) + "   : " + r.desc);
293 });
294
295 //print(JSON.stringify(GitLogParser.shours,null,4));
296
297
298
299 // open file..
300
301 // read lines
302
303 // summarize each hour
304
305 //convert line into 'Project / filename'
306
307