-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolve.js
More file actions
333 lines (250 loc) · 10.7 KB
/
evolve.js
File metadata and controls
333 lines (250 loc) · 10.7 KB
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
/**
* @author Helena
*/
var s3db = {
//to invoke rules + collections and then statement + items sequencially. simply fill the s3db structur and invoke s3db.getData(): s3db.login = {'url':'http://uab.s3db.org/s3db/', 'key':'mudamseostempos','project_id':'457'}; s3db.getData()
completedCall : 0,
simultCalls : 3,
getData : function (x){
var collectioncall = s3db.login.url+'S3QL.php?key='+s3db.login.key+'&query=<S3QL><select>*</select><from>collections</from><where><project_id>'+s3db.login.project_id+'</project_id></where></S3QL>';
$.getJSON(collectioncall+'&format=json&callback=?', function (collections) {
s3db.collections = collections;
s3db.completedCall++;
if(s3db.completedCall==s3db.simultCalls){
dataactions.rollback();
if(s3db.retrieveDataTrigger){
s3db.callItemsAndStatements = 0;
s3db.getItems();
//queriable rules also include deleted rules
s3db.getStatements();
}
}
})
var rulecall = s3db.login.url+'S3QL.php?key='+s3db.login.key+'&query=<S3QL><select>rule_id,subject,verb,object,subject_id,verb_id,object_id,created_on,modified_on</select><from>rules</from><where><project_id>'+s3db.login.project_id+'</project_id></where></S3QL>';
$.getJSON(rulecall+'&format=json&callback=?', function (rules) {
s3db.rules = rules;
s3db.completedCall++;
if(s3db.completedCall==s3db.simultCalls){
dataactions.rollback();
if(s3db.retrieveDataTrigger){
s3db.callItemsAndStatements = 0;
s3db.getItems();
//queriable rules also include deleted rules
s3db.getStatements();
}
}
})
var rulelog = s3db.login.url+'S3QL.php?key='+s3db.login.key+'&query=<S3QL><select>*</select><from>rulelog</from><where><project_id>'+s3db.login.project_id+'</project_id></where></S3QL>';
$.getJSON(rulelog+'&format=json&callback=?', function (rulelog) {
s3db.rulelog = rulelog;
s3db.completedCall++;
if(s3db.completedCall==s3db.simultCalls){
dataactions.rollback();
}
})
},
getStatements : function (){
//retrieving statements per rule; some log rules (the has UID ones) require gathering items as well; so we gather all items
//for collecting items
s3db.completedStatsCall = 0;
s3db.simultStatsCalls = s3db.rules.length;
s3db.statements = {};
s3db.rules.map( function (a) {
var statscall = s3db.login.url+'S3QL.php?key='+s3db.login.key+'&query=<S3QL><select>*</select><from>statements</from><where><rule_id>'+a.rule_id+'</rule_id></where></S3QL>';
$.getJSON(statscall+'&format=json&callback=?', function (stats) {
if(typeof(stats[0])!='undefined'){
ridguess = stats[0].rule_id;
s3db.statements[ridguess] = stats;
}
s3db.completedStatsCall++;
if(s3db.completedStatsCall==s3db.simultStatsCalls){
s3db.callItemsAndStatements++
dataactions.countEntries();
}
})
})
},
getItems : function (){
//for collecting items
s3db.completedItemsCall = 0;
s3db.simultItemsCalls = s3db.collections.length;
s3db.items = {};
s3db.collections.map( function (a) {
var itemscall = s3db.login.url+'S3QL.php?key='+s3db.login.key+'&query=<S3QL><select>*</select><from>items</from><where><collection_id>'+a.collection_id+'</collection_id></where></S3QL>';
$.getJSON(itemscall+'&format=json&callback=?', function (items) {
if(typeof(items[0])!='undefined'){
cidguess = items[0].collection_id;
s3db.items[cidguess] = items;
}
s3db.completedItemsCall++;
if(s3db.completedItemsCall==s3db.simultItemsCalls){
s3db.callItemsAndStatements++
dataactions.countEntries();
}
})
})
}
}
var data = {};dataactions = {};
dataactions.getListDays = function (){
data.dailyActions = {};
data.allRulesDays = [];
data.rulesPerDay = {};
/*namesNodes = s3db.rules.map( function (a) { return a.object } );
idsNodes = s3db.rules.map( function (a) { return a.rule_id } );
combinedIdNames = {};
for (var i=0; i < idsNodes.length; i++) {
combinedIdNames[idsNodes[i]] = namesNodes[i];
};
*/
//captue all the dates where actions occurred and all the actions
var datesLog = s3db.rulelog.map( function (a) { return a.action_timestamp.substr(0,10) } );
var actionsLog = s3db.rulelog.map( function (a) { return a.action } );
var idsLog = s3db.rulelog.map( function (a) { return a.rule_id } );
//find create and delete actions per day
datesLog.map ( function (date,i) {
if(typeof(data.dailyActions[date])=='undefined'){
data.dailyActions[date] = {created:[], changed: [], deleted: []};
data.allRulesDays.push(date);
}
if($.trim(actionsLog[i]) == 'create') {
data.dailyActions[date].created.push(idsLog[i]);
} else if($.trim(actionsLog[i]) == 'edit') {
data.dailyActions[date].changed.push(idsLog[i]);
} else if($.trim(actionsLog[i]) == 'delete') {
data.dailyActions[date].deleted.push(idsLog[i]);
//whenever a rule is deleted, we lose track of its description, so its description must be collected the the rulelog
}
//if not rules were added, but one was deleted, this should be -1; will make sense for comulative display
data.rulesPerDay[date] = data.dailyActions[date].created.length-data.dailyActions[date].deleted.length
} ) ;
}
dataactions.rebuildOntologies = function (){
//a day ontollogy is the set of rules created before or on this day minus the rules deleted before or on this day
data.dayOntology = {};var allCreatedBefore = [];data.dayOntologyEdges = {};data.dayOntologyNodes ={};
data.countRules = {};
var allDeletedBefore = [];
for(var day in data.dailyActions){
//intersept all allcreted before this day
var allCreatedBefore = $.merge(allCreatedBefore, data.dailyActions[day].created);
var allDeletedBefore = $.merge(allDeletedBefore, data.dailyActions[day].deleted);
data.dayOntology[day] = allCreatedBefore.diff(allDeletedBefore);
data.countRules[day] = data.dayOntology[day].length;
//all edges and nodes for the day
data.dayOntologyEdges[day] = data.dayOntology[day].map ( function (rule) {
var ind = data.alledges_ind.indexOf(rule);
if(ind!=-1){
return data.alledges[ind];
}
})
data.dayOntologyEdges[day] = data.dayOntologyEdges[day].clean();
}
}
dataactions.ruleEdges = function (){
data.alledges = s3db.rules.map ( function (data) { return {'rule_id': data.rule_id, 'source':data.subject, 'sink':data.object, 'label':data.verb };});
data.alledges_ind = s3db.rules.map(function (data){return data.rule_id})
//complete alledges with stuff deleted
s3db.rulelog.map ( function (log)
{
if($.trim(log.action) == 'delete' || log.old_object=='UID'){
if(data.alledges_ind.indexOf(log.rule_id)==-1){
data.alledges.push( {'rule_id': log.rule_id, 'source':log.old_subject, 'sink':log.old_object, 'label':log.old_verb })
data.alledges_ind.push(log.rule_id)
}
}
}
)
//var allsources = data.alledges.map ( function (data) { return data.source; })
//var allsinks = data.alledges.map ( function (data) { return data.sink} );
//data.allnodes = $.unique(allsources.concat(allsinks));
}
dataactions.rollback = function () {
//an ontology in a day is the combination of all the ruules that existed before that day minus the ones that were deleted
//will assume the ontology remained the same in the intervals betwen the existing days
dataactions.ruleEdges();
dataactions.getListDays();
dataactions.rebuildOntologies();
//now a bit of a hack because the rulelog logs collections via "hasUID" rules; need to match that to a collection ID
s3db.C2R_lookup = {};s3db.R2C_lookup = {};
for (var r in s3db.rulelog){
if(s3db.rulelog[r].old_object=="UID"){
for (var c in s3db.collections){
if (s3db.collections[c].name==s3db.rulelog[r].old_subject){
s3db.C2R_lookup[s3db.collections[c].id] = s3db.rulelog[r].rule_id;
s3db.R2C_lookup[s3db.rulelog[r].rule_id] = s3db.collections[c].id;
}
}
}
}
if(s3db.retrieveDataTrigger){
s3db.callItemsAndStatements = 0;
s3db.getItems();
//queriable rules also include deleted rules
s3db.getStatements();
}
else {
graph.buildRadial();
graph.drawTopology();
}
}
dataactions.countEntries = function () {
if(s3db.callItemsAndStatements==2){
//we are now ready to count entries per day
//Count for statements; this code can be made cleaner but I'm not feeling very inspired today
data.createdStatements = {};data.statementsCreatedDate = [];data.statementsDatePerRule = {};
for (var rid in s3db.statements){
data.statementsDatePerRule[rid] = {}
var s_crated_by_rule = s3db.statements[rid].map( function (s){ return s.created_on.substr(0,10);})
s_crated_by_rule.map( function (date) {
if(typeof(data.statementsDatePerRule[rid][date])=='undefined'){
data.statementsDatePerRule[rid][date] = 0
}
data.statementsDatePerRule[rid][date]++;
if(typeof(data.createdStatements[date])=='undefined'){
data.createdStatements[date]=0;
}
data.createdStatements[date]++;
})
data.statementsCreatedDate = data.statementsCreatedDate.concat(s_crated_by_rule);
}
//for items, we first need to look at the matched between the hasUID rule and the entity
data.createdItems = {};data.itemsCreatedDate = [];data.itemsDatePerRule = {};
for (var cid in s3db.items){
data.itemsDatePerRule[cid] = {};
var rid = s3db.C2R_lookup[cid];data.statementsDatePerRule[rid] = {};
var s_crated_by_collection = s3db.items[cid].map( function (s){ return s.created_on.substr(0,10);})
s_crated_by_collection.map( function (date) {
if(typeof(data.itemsDatePerRule[cid][date])=='undefined'){
data.itemsDatePerRule[cid][date] = 0;
data.statementsDatePerRule[rid][date] = 0;
}
data.itemsDatePerRule[cid][date]++;
data.statementsDatePerRule[rid][date]++;
if(typeof(data.createdItems[date])=='undefined'){
data.createdItems[date]=0;
}
data.createdItems[date]++;
})
data.itemsCreatedDate = data.itemsCreatedDate.concat(s_crated_by_collection);
}
var allStatItemsDates = $.unique(data.itemsCreatedDate.concat(data.statementsCreatedDate));
data.allStatsDays = allStatItemsDates;
//now, merge the items with the statements as if the items themselves were statements
data.createdStatementsItems = {};
for(var i in allStatItemsDates){
var date = allStatItemsDates[i];
if(typeof(data.createdItems[date])=='undefined'){
data.createdItems[date] = 0;
}
if(typeof(data.createdStatements[date])=='undefined'){
data.createdStatements[date] = 0;
}
data.createdStatementsItems[date]=data.createdStatements[date] + data.createdItems[date];
}
//ready to move on
store = {}; store[s3db.login.url+"&key="+s3db.login.key+"&project_id="+s3db.login.project_id] = {'s3db':JSON.stringify(s3db), 'data':JSON.stringify(data)};
localStorage.evolve = JSON.stringify(store);
graph.buildRadial();
graph.drawTopology();
}
}