Tasks.js
[gitlive] / Tasks.js
1
2 XObject = imports.XObject.XObject;
3 XMLHttpRequest = imports.XMLHttpRequest.XMLHttpRequest;
4 Netrc = imports.Netrc.Netrc;
5 Date = imports.Date.Date;
6 /**
7  *
8  * Tasks
9  *
10  *  Flow
11  *
12  *    Commit
13  *      => Tasks.notify(commit)
14  *
15  *
16  *
17  *
18  */
19
20 Tasks = {
21     
22     curTask : false,
23     commitRepo : false, // the DB version of repo info..
24     lastCommit : false,
25     
26     nextPrompt : false, // time when the system last prompted a confirmation that a task is being worked on.
27     
28     
29     notifyIdle : function()
30     {
31         
32     },
33     
34     notify : function(commit)
35     {
36         if (this.inQuery && this.inQuery > (new Date())) {
37             // ignore the notification.. we are currently checking what the current
38             // status is.
39             
40             // we need to handle a WTF situation where something below failed... so
41             
42             return; 
43         }
44         this.inQuery = (new Date()).add(Date.MINUTE, 5);
45         this.lastCommit = commit;
46         this.commitRepo = false;
47         this.curTask = false;
48         this.fetchTask();
49     },
50     
51     
52     
53     fetchTask: function()
54     {
55         // have we got the status in the last 15 mins..
56         // we should not need to get it again... - it's probably not changed.
57         if (this.curTask && !this.curTask.hasExpired()) {
58             this.fetchRepo();
59         }
60         _this = this;
61         // do the request to get the task..
62         var r = new XMLHttpRequest({
63             onreadystatechange : function() {
64                 print("Got result.");
65                 if (this.status != 4) {
66                     return;
67                 }
68                 
69                   
70                 var res = JSON.parse(this.responseText);
71                 
72                 //print(JSON.stringify(res,null,4))
73                 //print([ res.success , res.data.length ]);
74                 _this.curTask = (res.success && res.data.length) ? (new Task(res.data[0])) : false;
75                 
76                 print("Current task:" + JSON.stringify(_this.curTask,null,4));
77                 _this.fetchRepo();
78             }
79             
80         });
81         var netrc  = Netrc.forHost('git.roojs.com');
82         
83         r.open('GET',
84                "http://roojs.com/admin.php/Roo/cash_invoice_entry?_current_task=1"
85                ,true, netrc.login, netrc.password  );
86          print("Getting current task: "  +  "http://roojs.com/admin.php/Roo/cash_invoice_entry?_current_task=1");        
87         r.send();
88         
89     },
90     
91     list: function(repo, callback)
92     {
93         // have we got the status in the last 15 mins..
94         // we should not need to get it again... - it's probably not changed.
95          
96         _this = this;
97         // do the request to get the task..
98         var r = new XMLHttpRequest({
99             onreadystatechange : function() {
100                 print("Got result.");
101                 if (this.status != 4) {
102                     return;
103                 }
104                 
105                   
106                 var res = JSON.parse(this.responseText);
107                 
108                 //print(JSON.stringify(res,null,4))
109                 //print([ res.success , res.data.length ]);
110                 if (!res.success || !res.data.length)  {
111                     print("NO tasks returned");
112                     callback([]);
113                     return;
114                 }
115                 
116                 //print("Current task:" + JSON.stringify(_this.curTask,null,4));
117                 callback(res.data);
118             }
119             
120         });
121         var netrc  = Netrc.forHost('git.roojs.com');
122         
123         r.open('GET',
124                "http://roojs.com/admin.php/Roo/mtrack_ticket?repo_shortname=" + repo.name
125                ,true, netrc.login, netrc.password  );
126          print("Getting current task: "  +  "http://roojs.com/admin.php/Roo/mtrack_ticket?repo_shortname=" + repo.name);        
127         r.send();
128         
129     },
130     
131     
132     
133     
134     fetchRepo: function()
135     {
136         
137         var repo = this.lastCommit.repo;
138         
139          _this = this;
140         var r = new XMLHttpRequest({
141             onreadystatechange : function() {
142                 print("Got result.");
143                 if (this.status != 4) {
144                     return;
145                 }
146                 
147                   
148                 var res = JSON.parse(this.responseText);
149                 
150                 print(JSON.stringify(res,null,4))
151                 //print([ res.success , res.data.length ]);
152                 _this.commitRepo = (res.success && res.data.length) ? res.data[0] : false;
153                 print(JSON.stringify(_this.commitRepo))
154                 _this.verifyCommit();
155             }
156             
157         });
158         var netrc  = Netrc.forHost('git.roojs.com');
159         
160         r.open('GET',
161                "http://roojs.com/admin.php/Roo/mtrack_repos?shortname=" + repo.name
162                ,true, netrc.login, netrc.password  );
163         //print("SEding request");        
164         r.send();
165         
166         
167     },
168     
169         //---------- end fetching - now verifying..
170
171     
172     
173     verifyCommit : function()
174     {
175         // using curTask + lastCommit decide what to do.
176         this.inQuery = 0;
177         //tests:::
178        
179         // check to see if current task is being planned for too long..
180         // you should only enter task, and allow it to span over an hour.
181         // if you do the whole day on a task, then it will need to verify with you every so often that you
182         // need to confirm that you are still working on it..
183         
184         /*
185           
186            Example:
187            
188             Start at 10am, marked working on it till 3pm.
189             
190             So:
191                 at 11am, the system will pop up a warning - are you still working on it?
192                 -> if yes pressed, then next warning will be at 11pm
193                 
194           
195          */
196         
197         if (!this.currTask) {
198              this.promptForTask();
199             return;
200         }
201         
202          if (!this.currTask.project_id) {
203             this.promptForTask();
204             return;
205         }
206         
207         // are we working on the same project!!!!
208         if (this.currTask.project_id != this.commitRepo.project_id) {
209             this.promptForTask();
210             return;
211         }
212   
213         // has the ticket been closed...
214         
215         var is_project=  this.curTask.project_id_code[0] != '*' ;
216         // is there a ticket?
217         if (is_project && !this.currTask.ticket_id) {
218             this.promptForTask();
219             return;
220         }
221         if (is_project && this.currTask.ticket_status) {
222             //TODO - if status is closed.. then we need to prompt..
223             
224             
225         }
226         
227         
228         // we now working on same project..
229         
230         
231         var now = new Date();
232         
233         print(JSON.stringify(this.curTask));
234         
235         var endoftask = this.curTask.action_datetime.add(Date.HOUR, this.curTask.qtyvalue);
236         print("END OF TASK: " + endoftask);
237         
238         var max_stretch = now.add(Date.HOUR, 1);
239          print("Max stretch: " + max_stretch);
240         if (endoftask > max_stretch) {
241             this.fixEndCurrTask(); //
242             
243         }
244         // still needs to be verified..
245        
246         
247         
248         
249
250         
251         if (!this.nextPrompt && this.curTask) {
252             //var use_start = this.curTask.action_datetime < now ? now : 
253             // if we have a task, then the next verification should be 1 hour after it started.
254             // even if we have only just seen it.. so we could already need verification.
255             this.nextPrompt = this.curTask.action_datetime; // the start time recorded in the database.
256         }
257         
258         
259         if (!this.nextPrompt || (this.nextPrompt < (new Date()))) {
260             
261             this.promptForTask();
262             return;
263             
264         }
265         
266         // ok verified basic time...
267         //let's check to see if project matches..'
268         
269         
270         
271         
272     },
273     
274     
275     
276     
277     //---------- end verifying - now prompting..
278     
279     
280     fixEndCurrTask: function()
281     {
282         // set the end time of the current task to be now + 1 hours at most...
283         var now = new Date();
284         var eot = now.add(Date.HOUR, 1);
285         // now round it down to nearest 15 minutes..
286         var min = Math.round((eot.format('i')*1) / 15) * 15;
287         var reot = Date.parseDate(eot.format('Y-m-d H:') + (min ? min : '00') + ':00', 'Y-m-d H:i:s');
288         
289         print("REAL END TIME" + reot.format('Y-m-d H:i:s'))
290         print("Current start time" + this.curTask.action_datetime.format('Y-m-d H:i:s'))
291         
292         // how long between start and reot...
293         var hours = (reot - this.curTask.action_datetime) / (60*60 * 1000 );
294         var rounded =  Math.round(hours * 4) / 4.0;
295         print("Rounded : "  + rounded);
296         //return;
297         this.updateTask({ qtyvalue : rounded });
298         this.curTask.qtyvalue = rounded;
299         
300     },
301     
302     promptForTask : function()
303     {
304         /// fixme...
305         this.nextPrompt = (new Date()).add(Date.MINUTE, 60);
306         
307         
308     },
309     
310     updateTask: function(setv)
311     {
312         var args = {};
313         XObject.extend(args,setv);
314         args.id = _this.curTask.id;
315         
316         
317         //print(JSON.stringify(args));
318         //return;
319     
320     
321          _this = this;
322          // do the request to get the task..
323         var r = new XMLHttpRequest({
324             onreadystatechange : function() {
325                 print("Got result.");
326                 if (this.status != 4) {
327                     return;
328                 }
329                 
330                  print( this.responseText)
331                  
332                 var res = JSON.parse(this.responseText);
333                 
334                 print(JSON.stringify(res,null,4))
335                 //print([ res.success , res.data.length ]);
336             }
337             
338         });
339         var netrc  = Netrc.forHost('git.roojs.com');
340         
341         r.open('POST',
342                "http://roojs.com/admin.php/Roo/cash_invoice_entry?_current_task=1"
343                ,true, netrc.login, netrc.password  );
344         //print("SEding request");        
345         r.send(args);
346         
347         
348         
349         
350     }
351     
352 };
353
354
355
356
357
358 Task = XObject.define(
359     function(cfg) {
360         // cal parent?
361         if (typeof(cfg) != 'object') {
362             print("CFG not oboject?");
363             return;
364         } 
365         XObject.extend(this,cfg);
366  
367         // fix up the values.
368         this.action_datetime = Date.parseDate(this.action_dt,'Y-m-d H:i:s');
369       // print("ACT DT: " + this.action_dt);
370         
371     },
372     Object,
373     {
374         /**
375          * This is similar to the cash_invoice_entry data..
376          * 
377          */
378         action_dt: '', //"2012-11-23 11:00:00"
379         description: '', //"QA on new site"
380         qtyvalue: 0, //"2.25"
381         
382         hasExpired : function()
383         {
384             
385             var exp = this.action_datetime.add(Date.HOUR, this.qtyvalue);
386             return (new Date()) > exp;  
387             
388         }
389     }
390 );
391
392
393
394
395
396
397
398
399
400
401 //-------------- testing
402 Gtk = imports.gi.Gtk;
403 Gtk.init(Seed.argv);
404 Tasks.notify( { repo : imports.Scm.Repo.Repo.get('gitlive') } );
405 Gtk.main();