-
Notifications
You must be signed in to change notification settings - Fork 0
/
now.js
184 lines (163 loc) · 5.52 KB
/
now.js
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
var asynk = require('async');
var moment = require('moment');
var Now = {};
Now.common = {};
Now.common.taxonomyAliases = {
'programming' : ['javascript', 'HTML', 'css', 'nodejs']
}
Now.common.taxonomies = {
'programming' : [
'engineering:computer science:implementation'
]
}
Now.common.types = ['home', 'chore', 'design', 'development']
Now.defaults = {
"activity" : '',
"iteration" : '', //default : 0
"complete" : '', //default : 0
"total" : '', //default : 0
"taxonomies" : [],
"links" : {}, //default : {}
"meta" : {}, //default : {} untracked data
"magnitude" : 1, //default : 1 the granularity of a task
"type" : '', //any string: design, development, etc.
"scope" : 'personal', // personal, public, private or a custom string
}
Now.I = function(opts){
var options = opts || {};
this.options = {};
this.data = {};
if(options.activity) this.data.activity = options.activity;
if(options.iteration) this.data.iteration = options.iteration;
if(options.complete) this.data.complete = options.complete;
if(options.date) this.data.date = moment(options.date);
if(options.total) this.data.total = options.total;
if(options.taxonomies) this.data.taxonomies = options.taxonomies;
if(options.meta) this.data.links = options.links;
if(options.magnitude) this.data.magnitude = options.magnitude;
if(options.type) this.data.type = options.type;
if(options.scope) this.data.scope = options.scope;
}
Now.I.prototype.set = function(name, value){
return (this.data[name] = value);
}
Now.I.prototype.get = function(name, value){
return this.data[name];
}
Now.I.prototype.reconcile = function(object, analytics){
//todo: support analytics gathering, too
var data = this.data;
Object.keys(this.data).forEach(function(key){
object[key] = Now.collations[key](data[key], object[key], data, object);
});
return object;
}
Now.Collection = function(set, opts){ //a grouping of events to analyze
this.collations = {};
this.set = set;
this.options = opts || {};
}
//todo: progressive collation
var val = function(ob, key){
return ob[key] || Now.defaults[key];
}
Now.Collection.prototype.collate = function(gp, cb){
var grouper = (cb && gp) || function(item){
return val(item.data,'activity')+':'+val(item.data,'scope')};
var callback = (gp && cb) || gp;
var ob = this;
var set = this.set;
var options = this.options;
var name;
//todo: sort by date
asynk.eachOfSeries(set.sort(function(a, b){
return a.data.date.unix() - b.data.date.unix();
}), function(item, index, done){
name = grouper(item);
if(!ob.collations[name]) ob.collations[name] = new Now.Collation();
ob.collations[name].now(item);
done();
}, function(){
//data is now assembled and ready to process
var results = {};
var history = {};
asynk.eachOfSeries(Object.keys(ob.collations), function(key, index, done){
ob.collations[key].collate(function(){
results[key] = ob.collations[key].data;
history[key] = ob.collations[key].history;
done();
});
}, function(){
callback(undefined, results, history, grouper);
});
})
}
Now.Collation = function(){ //the current state of any particular thing
this.events = [];
this.unreconciled = [];
this.history = [];
this.data = {};
}
Now.Collation.prototype.now = function(event){
this.events.push(event);
this.unreconciled.push(event);
}
Now.Collation.prototype.collate = function(cb){
var event;
var ob = this;
while(this.unreconciled.length){ //safest for recovery
event = this.unreconciled.shift();
ob.data = event.reconcile(ob.data);
ob.history.push(JSON.parse(JSON.stringify(ob.data)));
}
cb();
}
var preferCurrent = function(newValue, currentValue, newObject, existingObject){
return currentValue || newValue;
}
var preferNew = function(newValue, currentValue, newObject, existingObject){
return newValue || currentValue;
}
var keepAllUnique = function(newValue, currentValue, newObject, existingObject){
if(newValue && currentValue){
newValue.forEach(function(value, index){
if(currentValue.indexOf(value) === -1){
currentValue.push(value);
}
});
return currentValue;
}else return newValue || currentValue;
};
var integrate = function(newValue, currentValue, newObject, existingObject){
if(newValue && currentValue){
Object.keys(newValue).forEach(function(key, index){
currentValue[key] = newValue;
});
return currentValue;
}else return newValue || currentValue;
};
var dateHandler = function(newValue, currentValue, newObject, existingObject){
if(
(
existingObject['creation-date'] > newValue
) || !existingObject['creation-date']
) existingObject['creation-date'] = newValue;
if(
existingObject['modification-date'] < newValue ||
!existingObject['modification-date']
)existingObject['modification-date'] = newValue;
return newValue;
};
Now.collations = {
activity : preferCurrent, //shouldn't change past first set
iteration : preferNew,
complete : preferNew,
date : dateHandler,
total : preferNew,
taxonomies : keepAllUnique,
links : integrate,
meta : integrate,
magnitude : preferNew,
scope : preferCurrent //shouldn't change past first set
}
module.exports = Now;