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