-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.coffee
146 lines (116 loc) · 3.43 KB
/
app.coffee
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
express = require "express"
serveStatic = require "serve-static"
mongoose = require "mongoose"
bodyParser = require "body-parser"
cors = require "express-cors"
_ = require "lodash"
app = express()
# MongoDB configuration
connectionString = "mongodb://localhost:27017/fp"
mongoose.connect connectionString
rawDataSchema = new mongoose.Schema
key: Number
data: Array
HeatmapSchema = new mongoose.Schema
url: String
type: String
videoSrc: String
maxValue: Number
data: Array
rawData: [rawDataSchema]
Heatmap = mongoose.model "Heatmap", HeatmapSchema
# Application
app.use bodyParser.json()
app.use bodyParser.urlencoded extended: true
app.use cors
allowedOrigins: [
"fp.dev:*"
"*.sabov.me:*"
"localhost:*"
"*.rwth-aachen.de:*"
]
app.use serveStatic "./dist"
# Helpers
getValueCoefficient = (a, b) -> Math.min Math.pow(a + (b - a) / 2, 1/2) * 4, 1
mergeData = (a, b) -> _.map _.zip(a, b), _.sum
prepareData = (data, length = 100) ->
data = _.map data, ({value, a, b}) ->
value = value * getValueCoefficient +a, +b
value: value.toFixed 2
a: +a
b: +b
flatData = new Array length
flatData[i] = 0 for i in [0...length]
for {a, b, value} in data
from = Math.round a * length
to = Math.round b * length
break if to - from < 1 or not (isFinite(a) and isFinite(b))
for i in [from..to]
flatData[i] += +value
flatData
# Routes
app.get "/get", (req, res) ->
{ videoSrc } = req.query
searchObj =
url: req.headers.referer
if videoSrc
searchObj.videoSrc = videoSrc
Heatmap.find searchObj, (err, result) ->
result = result[0]
res.json {
result
code: 200
status: "OK"
}
app.post "/save", (req, res, next) ->
data = req.body
{ videoSrc } = req.query
data.url = req.headers.referer
key = data.key
delete data.key
searchObj =
url: data.url
if data.videoSrc
searchObj.videoSrc = data.videoSrc
heatmap = new Heatmap data
Heatmap.find searchObj, (err, result) ->
if result.length > 1
console.log "More than one object was found"
else
preparedData = prepareData data.data
if result.length is 1
heatmap = result[0]
heatmap.data = mergeData heatmap.data, preparedData
else
heatmap.data = preparedData
heatmap.maxValue = _.max heatmap.data
rawData = _.find heatmap.rawData, key: +key
if rawData
rawData.data = mergeData rawData.data, preparedData
else
heatmap.rawData.push
key: key
data: preparedData
if heatmap.data.length
heatmap.save (err) ->
res.json
code: 200
status: "OK"
id: heatmap.id
else
res.json
code: 503
status: "Bad request"
app.get "/calc", (req, res) ->
Heatmap.find {}, (err, heatmaps) ->
_.each heatmaps, (heatmap) ->
data = _.pluck heatmap.rawData, "data"
zipped = _.zip.apply _, data
heatmap.data = _.map zipped, _.sum
heatmap.save()
res.json {
result: _.pluck heatmaps, "url"
code: 200
status: "OK"
}
app.listen 8080