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