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