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