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