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