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