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