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