forked from ChrisBoesch/feedback-fire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.html
482 lines (370 loc) · 16.2 KB
/
patterns.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.8.0/angularfire.min.js"></script>
<!-- Anagular-char data -->
<link rel="stylesheet" href=
"https://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.css" type=
"text/css" />
<script src="https://cdn.rawgit.com/nnnick/Chart.js/master/Chart.min.js" type=
"text/javascript">
</script>
<script src=
"https://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.js"
type="text/javascript">
</script>
<!-- Setting the size of the charts -->
<style>
.chart {
width: 500px;
height: 500px;
}
</style>
</head>
<body ng-controller="userController" >
<div class="container">
<h3> Counters and Timers and Queues </h3>
Demonstraton of how to eempliment couters, timers, and task queues on Firebase. <br><br>
<button class="btn btn-success" ng-click="addCounter(theKey)">
Add Counter
</button> with the key <input ng-model="theKey" type="input" ng-init="theKey='awesome'">
<br>
Counters: <br>
<table>
<tr>
<th>Counter</th>
<th>Count</th>
</tr>
<tr ng-repeat="(k,v) in counters">
<td>{{k}}</td>
<td>{{v}}</td>
<td>
<button class="btn btn-success" ng-click="incrementCounter(k)">
<span class="glyphicon"></span> Increment
</button>
<button class="btn btn-success" ng-click="addCounter(k)">
<span class="glyphicon"></span> Reset
</button>
<button class="btn btn-success" ng-click="deleteCounter(k)">
<span class="glyphicon"></span> Delete
</button>
</td>
</tr>
</table>
<hr>
<button class="btn btn-success" ng-click="addTimer(theKey,duration)">
Add Timer
</button> with the key <input ng-model="theKey" type="input"> and duration
<input type="input" ng-model="duration" ng-init="duration=60000"> milliseconds.
<br>
<button class="btn btn-success" ng-click="updateCurrentTime()">
<span class="glyphicon"></span> Update Server Time
</button>
Refreshed at: {{server.currentTime}}
<br>
Timers:
<ul>
<li ng-repeat="(k,v) in timers">
<b>{{k}}</b> timer duration {{ v['duration']/1000 }}
seconds since start <b>{{ (server.currentTime - v['start']) /1000}}</b>
seconds remaining <b>{{(v['duration'] - (server.currentTime - v['start']) )/1000}}</b>
seconds
<button class="btn btn-success" ng-click="resetTimer(k)">
<span class="glyphicon"></span> Reset
</button>
<button class="btn btn-success" ng-click="deleteTimer(k)">
<span class="glyphicon"></span> Delete
</button>
</li>
</ul>
<hr>
<div ng-init="intervalCounter={'name':'awesome', 'interval':3000,'target':5, 'timer':'awesome'}">
Increment the counter
<input ng-model="intervalCounter.name" type="text">
every
<input ng-model="intervalCounter.interval" type="text">
milliseconds for until the counter reaches
<input ng-model="intervalCounter.target" type="text">.
<button class="btn btn-success" ng-click="updateUntilCounter()">
<span class="glyphicon"></span> Start Updates
</button><br>
Increment the counter
<input ng-model="intervalCounter.name" type="text">
every
<input ng-model="intervalCounter.interval" type="text">
milliseconds for until the timer
<input ng-model="intervalCounter.timer" type="text">
expires.
<button class="btn btn-success" ng-click="updateCounterUntilTimerExpires()">
<span class="glyphicon"></span> Start Updates
</button><br>
</div>
<hr>
Local Interval Loops:
<ol>
<li ng-repeat="(k,interval) in localIntervals">
{{k}},
Increment counter <b> {{interval["counterKey"]}}</b>
every <b>{{interval["interval"]}}</b> milliseconds
until
<span ng-show="interval.timer"> timer <b>{{interval["timer"]}}</b> expires. </span>
<span ng-show="interval.maxCount"> counter reaches <b>{{interval["maxCount"]}}</b>.</span>
<button class="btn btn-success" ng-click="stopInterval(k)">
<span class="glyphicon"></span> Stop Interval
</button>
</li>
</ol>
<hr>
Add a task to task queue
<input ng-model="taskQueue" ng-init="taskQueue='funQueue'" type="text">
to update counter <input ng-model="theCounter" ng-init="theCounter='fun'" type="text">. <br>
<button class="btn btn-success" ng-click="addTask(taskQueue,theCounter)">
<span class="glyphicon"></span> Add task
</button>
Task Queues:
<ul>
<li ng-repeat="(k,v) in queues">
{{k}} queue tasks
<ol>
<li ng-repeat="(taskID, task) in v">
Task: {{task.task}}, data: {{task.data}}
<button class="btn btn-success" ng-click="processTask(k,taskID)">
<span class="glyphicon"></span> Process Task
</button>
</li>
</ol>
<button class="btn btn-success" ng-click="addTask(k,theCounter)">
<span class="glyphicon"></span> Add task
</button>
<button class="btn btn-success" ng-click="deleteQueue(k)">
<span class="glyphicon"></span> Delete
</button>
</li>
</ul>
<hr>
<div ng-init="intervalCounter={'queue':'funQueue', 'interval':3000,'target':5, 'counter':'awesome'}">
Process tasks in queue
<input ng-model="intervalCounter.queue" type="text">
every
<input ng-model="intervalCounter.interval" type="text">
milliseconds while the counter
<input ng-model="intervalCounter.counter" type="text">.
is less than
<input ng-model="intervalCounter.target" type="text">.
<button class="btn btn-success" ng-click="processTasksUntilCounter(intervalCounter.counter,intervalCounter.target,intervalCounter.queue,intervalCounter.interval)">
<span class="glyphicon"></span> Start Processing Tasks in Queue
</button><br>
</div>
<br>
Todo: <b>Process tasks in a queue from the server.</b> <br>
<hr>
<button class="btn btn-success" ng-click="update_the_chart()">
<span class="glyphicon"></span> Update Chart
</button><br>
<div class="panel panel-default">
<div class="panel-heading">
Chart
</div>
<!--
class="chart chart-line"
class="chart chart-bar"
-->
<div class="panel-body">
<canvas id="line" class="chart chart-bar" data="data" labels="labels" legend="true" click="onClick" series="series"></canvas>
</div>
</div>
</div> <!-- container -->
<script>
var myApp = angular.module("myApp", ["firebase","chart.js"]);
myApp.controller('userController', ['$scope', '$firebase',
function($scope, $firebase) {
$scope.newuser = {};
// Here is where you update your Firebase URL.
var theFirebaseURL = "https://counters-and-timers.firebaseio.com";
var ref = new Firebase(theFirebaseURL);
//Counters and timers development
$scope.counters = $firebase(ref.child("counters")).$asObject();
$scope.timers = $firebase(ref.child("timers")).$asObject();
$scope.server = $firebase(ref.child("server")).$asObject();
$scope.queues = $firebase(ref.child("queues")).$asObject();
//Sometime it is easier to work with data as an array.
$scope.theCounters = $firebase(ref.child("counters")).$asArray();
$scope.updateCurrentTime = function() {
ref.child("server").transaction(function(currentValue) {
return {'currentTime': Firebase.ServerValue.TIMESTAMP}
});
};
$scope.updateCurrentTime();
$scope.timerCount = 0;
$scope.localIntervals = {};
//Start a maxCount interval and add it to the interval tracking list.
$scope.updateUntilCounter = function() {
maxCount = $scope.intervalCounter.target;
counterKey = $scope.intervalCounter.name;
interval = $scope.intervalCounter.interval;
var intervalToStop = setInterval(function () {myCounterTimer( counterKey,maxCount,intervalToStop)}, interval);
//Add to local object for tracking.
var temp = {'counterKey':counterKey,'maxCount':maxCount,'interval':interval}
$scope.localIntervals[intervalToStop] = temp;
}
//If the maxCount has been reached, stop the interval
function myCounterTimer(counterName, maxCounter, intervalToStop) {
//You could automatically update the chart as you are locally incrementing counters.
//$scope.update_the_chart();
if($scope.counters[counterName]<maxCounter){
console.log("updating counter "+counterName+" from "+$scope.counters[counterName]);
$scope.incrementCounter(counterName);
}
else { $scope.stopInterval(intervalToStop);}
};
//Start an update until counter expires interval
$scope.updateCounterUntilTimerExpires = function(){
timer = $scope.intervalCounter.timer;
counterKey = $scope.intervalCounter.name;
interval = $scope.intervalCounter.interval;
var intervalToStop = setInterval(function () {myCounterTimeoutTimer( counterKey,timer,intervalToStop)}, interval);
var temp = {'counterKey':counterKey,'timer':timer,'interval':interval}
$scope.localIntervals[intervalToStop] = temp;
}
//If the timer has expired, then stop the interval.
function myCounterTimeoutTimer(counterName, timer, intervalToStop) {
//update current server time.
$scope.updateCurrentTime();
//Determine of the timer has expired.
var timeToGo = $scope.timers[timer].duration - ($scope.server.currentTime - $scope.timers[timer].start);
if(timeToGo > 0 ){
console.log("updating counter "+counterName+" from "+$scope.counters[counterName] +" time to go "+timeToGo);
$scope.incrementCounter(counterName);
}
else { $scope.stopInterval(intervalToStop); }
}
$scope.stopInterval = function(intervalToStop){
console.log("stopping counter timer.");
clearInterval(intervalToStop);
delete $scope.localIntervals[intervalToStop];
$scope.$digest(); //In case the GUI does not update.
};
$scope.addCounter = function(counterName) {
ref.child("counters/"+counterName).transaction(function(currentValue) {
return 0;
});
};
$scope.incrementCounter = function(counterName) {
ref.child("counters/"+counterName).transaction(function(currentValue) {
return currentValue+1;
});
};
$scope.deleteCounter = function(counterName) {
console.log("deleting");
ref.child("counters/"+counterName).remove();
};
$scope.addTimer = function(timerName,duration) {
ref.child("timers/"+timerName).transaction(function(currentValue) {
return {'start': Firebase.ServerValue.TIMESTAMP, 'duration':duration};
});
$scope.updateCurrentTime();
};
$scope.resetTimer = function(timerName) {
ref.child("timers/"+timerName).transaction(function(currentValue) {
return {'start': Firebase.ServerValue.TIMESTAMP,'duration':currentValue.duration};
});
// need to pass duration.
$scope.updateCurrentTime();
};
$scope.deleteTimer = function(timerName) {
console.log("deleting");
ref.child("timers/"+timerName).remove();
$scope.updateCurrentTime();
};
$scope.addTask = function(queueName, counterName){
var task = {'task':'Update Counter','data':counterName};
theQueue = $firebase(ref.child("queues").child(queueName)).$asArray();
theQueue.$add(task);
};
$scope.deleteQueue = function(queueName) {
ref.child("queues/"+queueName).remove();
};
$scope.processTask = function(queueName,taskName) {
//execute the task here before deleteing.
//Do something with this task.
if($scope.queues[queueName][taskName].task=="Update Counter"){
$scope.incrementCounter($scope.queues[queueName][taskName].data)
ref.child("queues/"+queueName+"/"+taskName).remove();
} else {
console.log("Checking the empty queue "+queueName);
}
};
$scope.processTasksUntilCounter = function(counterKey,maxCount,queue,interval){
console.log("here");
var intervalToStop = setInterval(function () {myProcessingTimer( counterKey,maxCount,queue,intervalToStop)}, interval);
//Add to local object for tracking.
var temp = {'counterKey':counterKey,'maxCount':maxCount,'interval':interval}
$scope.localIntervals[intervalToStop] = temp;
}
//If the maxCount has been reached, stop the interval
function myProcessingTimer(counterName, maxCounter, queueName,intervalToStop) {
if($scope.counters[counterName]<maxCounter){
console.log("Todo: Processing task in queue"+queueName+" while counter "+counterName);
//$scope.theQueue = $firebase(ref.child("queues").child(queueName)).$asArray();
$scope.incrementCounter(counterName);
//Find task name of task 0 if any in the queue.
if ( (queueName in $scope.queues) && Object.keys($scope.queues[queueName]).length >0){
console.log(Object.keys($scope.queues[queueName]));
$scope.processTask(queueName,Object.keys($scope.queues[queueName])[0]);
//Update the counter.
}else{
//If there are no more tasks.
//$scope.stopInterval(intervalToStop);
}
}
else {
//If counter has exceeded limit.
$scope.stopInterval(intervalToStop);
}
};
// angular-charts starter data shown before first click.
//$scope.labels = ["Click", "to", "Update","Counters", "!"];
//$scope.series = ['Counters', 'Queue Lengths'];
//$scope.data = [
// [10,20,30,40,35],
// [15,5,25,30,25]
// ];
$scope.update_the_chart = function(){
console.log("Updating the chart.");
$scope.labels = [];
$scope.myCounters = [];
$scope.data = [$scope.myCounters];
//you can have multiple lines or bars for each X value.
//$scope.data = [$scope.myCounters, $scope.myCounters];
for(var i=0, len = $scope.theCounters.length; i < len; i++) {
$scope.labels.push($scope.theCounters[i].$id);
$scope.data[0].push($scope.theCounters[i].$value);
}
};
$scope.onClick = function (points, evt) {
$scope.update_the_chart();
};
$scope.counters.$loaded().then(function() {
$scope.update_the_chart();
});
var unwatch_counters = $scope.counters.$watch(function() {
console.log("counter data changed!");
$scope.update_the_chart();
});
//unwatch_counters();
}]); //close the controller and module.
</script>
<!-- Google Tracking code. Change the UA-XXXXXXX key to be yours -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-32403946-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>