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