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         
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((this.date ? this.date.format('Y-m-d')  : (new Date()).format('Y-m-d') +
104                                             ' ' + time, 'Y-m-d H:i:s');
105
106         while (ret.cmd === false) {
107             var ta = ar.pop();
108             //print("TA:"+ta)
109             if (ta[0] !=  '-') { //hopfully withc catch stuff.
110                 ret.cmd = ta;
111                 break;
112             }
113             if (!ar.length) {
114                 // just assume it's the last bit..
115                 //print(line);
116                 throw "invalid line: " + l;
117             }
118
119         }
120         //print(ret.cmd);
121         
122         
123         ret.title = ar.join(' ');
124         if (ret.title == 'IDLE') {
125             ret.project = 'IDLE';
126             return ret;        
127         }
128                 
129         
130
131         if (typeof(this[ret.cmd])=='undefined') {
132              ret.project = 'Unknown';
133             return ret;
134             print( "Unknown application: " + ret.line);
135             throw { error : "TEST"};
136         }
137         
138         //print(ret.cmd);
139         //print(ret.title);
140         if (typeof(this[ret.cmd])=='string') {
141             ret.project = this[ret.cmd]  
142         } else {
143
144             this[ret.cmd](ret);
145         }
146
147
148         return ret;
149
150     },
151  
152     '/usr/bin/perl' : function(ret) {
153         if (ret.title.match(/^PAC/)) {
154             ret.project = 'Unknown';
155             return  'Unknown';
156         }
157         return 'Unknown';
158         throw "Unknown match: " + ret.line;
159     },
160     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
161     '/usr/lib/chromium/chromium' : function (ret) {
162
163           switch(true) {
164
165                 case (ret.title.match(/Media Clipping Portal/)):
166                     ret.project = 'Media Outreach';
167                     return;
168                 
169                 default:
170                     ret.project = 'Browsing';
171                     return;
172           }
173
174     },
175     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
176         var l = ret.title.match(/Project\s+(^\)+)/);
177         if (!l) {
178             ret.project="Unknown";
179             return;
180         }
181         throw "Unknown match: " + ret.line;
182     },
183     'guake' : 'Local Terminal',
184     'mono' : 'mono?'
185     
186
187 }
188 //print(Seed.argv[2]);Seed.quit();
189 if (typeof(Seed.argv[2]) == 'undefined') {
190     print("pick a date");
191     Seed.quit();
192 }
193  
194 var res = GitLogParser.parse( Seed.argv[2][0] == '/' ? Seed.argv[2] : xDate.Date.parseDate(Seed.argv[2], 'Y-m-d'));
195 var totals = { work : 0 , idle: 0, shortidle : 0}
196 for (var h in res) {
197     for (var p in res[h]) {
198         if (p == 'LONGIDLE') {
199             var idletime = Math.floor(res[h][p].total/60000) ;
200             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m LONGIDLE' );
201             totals.idle += idletime;
202             
203              
204             continue;
205         }
206         if (p == 'IDLE') {
207             var idletime = Math.floor(res[h][p].total/60000) ;
208             //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m SHORT IDLE' );
209             totals.shortidle += idletime;
210             
211              
212             continue;
213         }
214         
215         
216         //print(h + ' ' + Math.floor(res[h][p].total/60000) +'m ' + p );  
217         totals.work += Math.floor(res[h][p].total/60000) ;
218         for (var k in res[h][p].items) {
219              
220             //print( '     ' + Math.floor(res[h][p].items[k].span/60000) +'m ' + res[h][p].items[k].line );
221                  
222         }
223         
224         
225     }
226     
227 }
228 for (var h in GitLogParser.shours) {
229     var hsum = [];
230     var htot = 0;
231     for (var desc in GitLogParser.shours[h]) {
232         htot += (GitLogParser.shours[h][desc]/60000).toFixed(2)*1;
233         hsum.push({ desc : desc, tot : (GitLogParser.shours[h][desc]/60000).toFixed(2)*1 })
234     }
235     hsum.sort(function(a,b) { if (a.tot == b.tot) { return 0; } return a.tot < b.tot ? 1 : -1 });
236     print(h+': Total (' + htot +')');
237     hsum.forEach(function(r) {
238         print ("  " + r.tot + "   : " + r.desc);
239     });
240 }
241
242
243
244 //print(JSON.stringify(GitLogParser.shours,null,4));
245
246
247
248 print("\nLONGIDLE : " +(totals.idle/60).toFixed(2) +"h" );
249 print("\nShort Idle : " +(totals.shortidle/60).toFixed(2) +"h" );
250
251 print("Worked: " + (totals.work/60).toFixed(2) +"h\n" );
252  
253
254 // open file..
255
256 // read lines
257
258 // summarize each hour
259
260 //convert line into 'Project / filename'
261
262