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