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         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         // summarize data...
36         var hours = {};
37         for (var i = 0; i < lines.length; i++) {
38             var line = lines[i];
39             var hour = hours[line].start.format('H');
40             hours[hour] = (typeof(hours[hour]) == 'undefined') ? {} : hours[hour];
41             hours[hour][project] = (typeof(hours[project]) == 'undefined') ? 
42                     { total : 0, items : [] } 
43                     : hours[project];
44             hours[hour][project].total += line.span;
45             hours[hour][project].items.push(line);
46         }
47         return hours;
48
49     },
50     parseLine : function(l) 
51     {
52         var ret = { cmd : false,  line : l };
53         var ar = l.split(/\s+/);
54         print(JSON.stringify(ar));
55             
56         var time = ar.shift();
57         print(time);
58         
59         ret.start = xDate.Date.parseDate(this.date.format('Y-m-d') + ' ' + time, 'Y-m-d H:i:s');
60
61
62         while (ret.cmd !== false) {
63             var ta = ar.pop();
64             if (ta[0] !=  '-') { //hopfully withc catch stuff.
65                 ret.cmd = ta;
66                 break;
67             }
68             if (!ar.length) {
69                 // just assume it's the last bit..
70                 print(line);
71                 throw "invalid line: " + l;
72             }
73
74         }
75         
76         
77         ret.title = ar.join(' ');
78         if (ret.title == 'IDLE') {
79             ret.project = 'IDLE';
80             return ret;        
81         }
82                 
83         
84
85         if (typeof(this[ret.cmd])=='undefined') {
86             throw "Unknown application: " + ret.line;
87         }
88         if (typeof(this[ret.cmd])=='string') {
89             ret.project = this[ret.cmd]  
90         } else {
91
92             this[ret.cmd](ret);
93         }
94
95
96         return ret;
97
98     },
99
100     '/usr/bin/perl' : function(ret) {
101         if (ret.title.match(/^PAC/)) {
102             ret.project = 'Unknown';
103             return;
104         }
105         throw "Unknown match: " + ret.line;
106     },
107     '/usr/lib/icedove/icedove-bin' : 'Checking Mail',
108     '/usr/lib/chromium/chromium' : function (ret) {
109
110           switch(true) {
111
112                 case (ret.title.match(/Media Clipping Portal/)):
113                     ret.project = 'Media Outreach';
114                     return;
115                 
116                 default:
117                     ret.project = 'Browsing';
118                     return;
119           }
120
121     },
122     '/usr/lib/Komodo-Edit-7/lib/mozilla/komodo' : function(ret) {
123           var l = ret.title.match(/Project\s+(^\)+)/);
124           print(l);
125               
126          throw "Unknown match: " + ret.line;
127     },
128     'guake' : 'Local Terminal',
129
130
131 }
132
133 var res = GitLogParser.parse(xDate.Date.parseDate('2012-07-31', 'Y-m-d'));
134 print(JSON.stringify(res,null,4));
135
136 // open file..
137
138 // read lines
139
140 // summarize each hour
141
142 //convert line into 'Project / filename'
143
144