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