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