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