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     
92     fetchRepo: function()
93     {
94         
95         var repo = this.lastCommit.repo;
96         
97          _this = this;
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                 _this.commitRepo = (res.success && res.data.length) ? res.data[0] : false;
111                 print(JSON.stringify(_this.commitRepo))
112                 _this.verifyCommit();
113             }
114             
115         });
116         var netrc  = Netrc.forHost('git.roojs.com');
117         
118         r.open('GET',
119                "http://roojs.com/admin.php/Roo/mtrack_repos?shortname=" + repo.name
120                ,true, netrc.login, netrc.password  );
121         //print("SEding request");        
122         r.send();
123         
124         
125     },
126     
127         //---------- end fetching - now verifying..
128
129     
130     
131     verifyCommit : function()
132     {
133         // using curTask + lastCommit decide what to do.
134         this.inQuery = 0;
135         //tests:::
136         this.verifyTaskTime();
137         this.verifyTaskProject();
138           
139         
140         
141         
142     },
143     
144     verifyTaskTime : function()
145     {
146         // check to see if current task is being planned for too long..
147         // you should only enter task, and allow it to span over an hour.
148         // if you do the whole day on a task, then it will need to verify with you every so often that you
149         // need to confirm that you are still working on it..
150         
151         /*
152           
153            Example:
154            
155             Start at 10am, marked working on it till 3pm.
156             
157             So:
158                 at 11am, the system will pop up a warning - are you still working on it?
159                 -> if yes pressed, then next warning will be at 11pm
160                 
161           
162          */
163         var now = new Date();
164         if ( this.curTask) {
165             print(JSON.stringify(this.curTask));
166             
167             var endoftask = this.curTask.action_datetime.add(Date.HOUR, this.curTask.qtyvalue);
168             print("END OF TASK: " + endoftask);
169             
170             var max_stretch = now.add(Date.HOUR, 1);
171              print("Max stretch: " + max_stretch);
172             if (endoftask > max_stretch) {
173                 this.fixEndCurrTask(); //
174                  
175             }
176             
177             
178         }
179         
180         
181         
182         
183
184         
185         if (!this.nextPrompt && this.curTask) {
186             //var use_start = this.curTask.action_datetime < now ? now : 
187             // if we have a task, then the next verification should be 1 hour after it started.
188             // even if we have only just seen it.. so we could already need verification.
189             this.nextPrompt = this.curTask.action_datetime; // the start time recorded in the database.
190         }
191         
192         
193         if (!this.nextPrompt || (this.nextPrompt < (new Date()))) {
194             
195             this.promptForTask();
196             return;
197             
198         }
199         
200         
201         
202         
203     },
204     
205     //---------- end verifying - now prompting..
206     
207     
208     fixEndCurrTask: function()
209     {
210         // set the end time of the current task to be now + 1 hours at most...
211         var now = new Date();
212         var eot = now.add(Date.HOUR, 1);
213         // now round it down to nearest 15 minutes..
214         var min = Math.round((eot.format('i')*1) / 15) * 15;
215         var reot = Date.parseDate(eot.format('Y-m-d H:') + (min ? min : '00') + ':00', 'Y-m-d H:i:s');
216         
217         print("REAL END TIME" + reot.format('Y-m-d H:i:s'))
218         print("Current start time" + this.curTask.action_datetime.format('Y-m-d H:i:s'))
219         
220         // how long between start and reot...
221         var hours = (reot - this.curTask.action_datetime) / (60*60 * 1000 );
222         var rounded =  Math.round(hours * 4) / 4.0;
223         print("Rounded : "  + rounded);
224         //return;
225         this.updateTask({ qtyvalue : rounded });
226         this.curTask.qtyvalue = rounded;
227         
228     },
229     
230     promptForTask : function()
231     {
232         /// fixme...
233         this.nextPrompt = (new Date()).add(Date.MINUTE, 60);
234         
235         
236     },
237     
238     updateTask: function(setv)
239     {
240         var args = {};
241         XObject.extend(args,setv);
242         args.id = _this.curTask.id;
243         
244         
245         //print(JSON.stringify(args));
246         //return;
247     
248     
249          _this = this;
250          // do the request to get the task..
251         var r = new XMLHttpRequest({
252             onreadystatechange : function() {
253                 print("Got result.");
254                 if (this.status != 4) {
255                     return;
256                 }
257                 
258                  print( this.responseText)
259                  
260                 var res = JSON.parse(this.responseText);
261                 
262                 print(JSON.stringify(res,null,4))
263                 //print([ res.success , res.data.length ]);
264             }
265             
266         });
267         var netrc  = Netrc.forHost('git.roojs.com');
268         
269         r.open('POST',
270                "http://roojs.com/admin.php/Roo/cash_invoice_entry?_current_task=1"
271                ,true, netrc.login, netrc.password  );
272         //print("SEding request");        
273         r.send(args);
274         
275         
276         
277         
278     }
279     
280 };
281
282
283
284
285
286 Task = XObject.define(
287     function(cfg) {
288         // cal parent?
289         if (typeof(cfg) != 'object') {
290             print("CFG not oboject?");
291             return;
292         } 
293         XObject.extend(this,cfg);
294  
295         // fix up the values.
296         this.action_datetime = Date.parseDate(this.action_dt,'Y-m-d H:i:s');
297       // print("ACT DT: " + this.action_dt);
298         
299     },
300     Object,
301     {
302         /**
303          * This is similar to the cash_invoice_entry data..
304          * 
305          */
306         action_dt: '', //"2012-11-23 11:00:00"
307         description: '', //"QA on new site"
308         qtyvalue: 0, //"2.25"
309         
310         hasExpired : function()
311         {
312             
313             var exp = this.action_datetime.add(Date.HOUR, this.qtyvalue);
314             return (new Date()) > exp;  
315             
316         }
317     }
318 );
319
320
321
322
323
324
325
326
327
328
329 //-------------- testing
330 Gtk = imports.gi.Gtk;
331 Gtk.init(Seed.argv);
332 Tasks.notify( { repo : imports.Scm.Repo.Repo.get('gitlive') } );
333 Gtk.main();