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