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