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