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