-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (123 loc) · 2.88 KB
/
app.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
var restify = require('restify');
var clone = require('node-v8-clone').clone;
var memArray = [];
var genListArr = [];
setInterval(procList, 100*60*1);
function addView(req, res, next)
{
console.log(".");
//Add new view to array
var newVisitArray = [];
newVisitArray["url"] = req.params.url;
newVisitArray["title"]=req.params.title;
newVisitArray["img"]=req.params.img;
newVisitArray["domain"]=req.params.domain.toLowerCase();
newVisitArray["nodeid"]=req.params.nodeid;
newVisitArray["expired"]=0;
memArray.push(newVisitArray);
//console.log(memArray);
res.send("ok");
}
function procList()
{
//Clears views that have been flagged afte 5 min timmer
//Replace all 0s with 1s
//console.log(memArray);
var len = memArray.length;
for(i=0; i<len; i++)
{
if(memArray[i]["expired"]==0)
{
memArray[i]["expired"] = 1;
}
else
{
memArray.splice(i,1);
i--;
len--;
}
}
//Generate the response List
genList();
//console.log(genListArr);
}
function genList()
{
genListArr = [];
var memClone = clone(memArray,true);
var resList = {};
for(var i =0; i<memClone.length; i++)
{
var item = clone(memClone[i],true);
if(item["domain"] in resList)
{
if(item["nodeid"] in resList[item["domain"]])
{
//Add 1
resList[item["domain"]][item["nodeid"]].count ++;
}
else
{
//Create with 1 count
var newObjItem = {};
newObjItem.url = item["url"];
newObjItem.title = item["title"];
newObjItem.img = item["img"];
newObjItem.domain = item["domain"];
newObjItem.count = 1;
resList[item["domain"]][item["nodeid"]] = newObjItem;
}
}
else
{
//Create the Domain and the Item we are on as well
var newObjItem = {};
newObjItem.url = item["url"];
newObjItem.title = item["title"];
newObjItem.img = item["img"];
newObjItem.domain = item["domain"];
newObjItem.count = 1;
resList[item["domain"]] = {};
resList[item["domain"]][item["nodeid"]] = newObjItem;
}
}
var sorted = [];
for(domainItem in resList)
{
sorted[domainItem] = [];
for(item in resList[domainItem])
{
sorted[domainItem].push(resList[domainItem][item]);
}
//Do the sort for last added Domain
sorted[domainItem].sort(
function(a,b)
{
return b.count - a.count;
}
);
}
genListArr = clone(sorted, true);
}
function getTopList(req, res, next)
{
var domain = req.params.domain.toLowerCase();
if(domain in genListArr)
{
res.send(JSON.parse(JSON.stringify(genListArr[domain])));
}
else
{
res.send("{}");
}
res.end();
}
var server = restify.createServer();
server.use(restify.CORS({origins: ['*'] }));
server.use(restify.fullResponse());
server.use(restify.bodyParser()); // mapped in req.params
server.get('/at/:url/:title/:img/:domain/:nodeid', addView);
server.get('top/:domain', getTopList);
server.listen(8099, function() {
console.log('%s listening at %s', server.name, server.url);
});