forked from sporiyano/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
executable file
·305 lines (282 loc) · 8.58 KB
/
routes.go
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
// Copyright 2015-2016 Zack Scholl. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.
// routes.go contains the functions that handle the web page views
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
// slash returns the dashboard, if logged in, else it redirects to login page.
func slash(c *gin.Context) {
var group string
loginGroup := sessions.Default(c)
groupCookie := loginGroup.Get("group")
if groupCookie == nil {
c.Redirect(302, "/login")
} else {
group = groupCookie.(string)
c.Redirect(302, "/dashboard/"+group)
}
}
// slashLogin handles a login with url parameter and returns dashboard if successful, else login.
func slashLogin(c *gin.Context) {
loginGroup := sessions.Default(c)
group := c.DefaultQuery("group", "noneasdf")
if group == "noneasdf" {
c.HTML(http.StatusOK, "login.tmpl", gin.H{})
} else {
loginGroup.Set("group", group)
loginGroup.Save()
c.Redirect(302, "/dashboard/"+group)
}
}
// slashLogin handles a POST login and returns dashboard if successful, else login.
func slashLoginPOST(c *gin.Context) {
loginGroup := sessions.Default(c)
group := strings.ToLower(c.PostForm("group"))
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); err == nil {
loginGroup.Set("group", group)
loginGroup.Save()
c.Redirect(302, "/dashboard/"+group)
} else {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "Incorrect login.",
})
}
}
// slashLogout handles a logout
func slashLogout(c *gin.Context) {
var group string
loginGroup := sessions.Default(c)
groupCookie := loginGroup.Get("group")
if groupCookie == nil {
c.Redirect(302, "/login")
} else {
group = groupCookie.(string)
fmt.Println(group)
loginGroup.Clear()
loginGroup.Save()
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"Message": "You are now logged out.",
})
}
}
// slashDashboard displays the dashboard
func slashDashboard(c *gin.Context) {
// skipUsers := c.DefaultQuery("skip", "")
// skipAllUsers := false
// if len(skipUsers) > 0 {
// skipAllUsers = true
// }
filterUser := c.DefaultQuery("user", "")
filterUsers := c.DefaultQuery("users", "")
filterUserMap := make(map[string]bool)
if len(filterUser) > 0 {
u := strings.Replace(strings.TrimSpace(filterUser), ":", "", -1)
filterUserMap[u] = true
}
if len(filterUsers) > 0 {
for _, user := range strings.Split(filterUsers, ",") {
u := strings.Replace(strings.TrimSpace(user), ":", "", -1)
filterUserMap[u] = true
}
}
group := c.Param("group")
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); os.IsNotExist(err) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "First download the app or CLI program to insert some fingerprints.",
})
return
}
ps, _ := openParameters(group)
var users []string
for user := range filterUserMap {
users = append(users, user)
}
people := make(map[string]UserPositionJSON)
if len(users) == 0 {
people = getCurrentPositionOfAllUsers(group)
} else {
for _, user := range users {
people[user] = getCurrentPositionOfUser(group, user)
}
}
type DashboardData struct {
Networks []string
Locations map[string][]string
LocationAccuracy map[string]int
LocationCount map[string]int
Mixin map[string]float64
VarabilityCutoff map[string]float64
Users map[string]UserPositionJSON
}
var dash DashboardData
dash.Networks = []string{}
dash.Locations = make(map[string][]string)
dash.LocationAccuracy = make(map[string]int)
dash.LocationCount = make(map[string]int)
dash.Mixin = make(map[string]float64)
dash.VarabilityCutoff = make(map[string]float64)
for n := range ps.NetworkLocs {
dash.Mixin[n] = ps.Priors[n].Special["MixIn"]
dash.VarabilityCutoff[n] = ps.Priors[n].Special["VarabilityCutoff"]
dash.Networks = append(dash.Networks, n)
dash.Locations[n] = []string{}
for loc := range ps.NetworkLocs[n] {
dash.Locations[n] = append(dash.Locations[n], loc)
dash.LocationAccuracy[loc] = ps.Results[n].Accuracy[loc]
dash.LocationCount[loc] = ps.Results[n].TotalLocations[loc]
}
}
c.HTML(http.StatusOK, "dashboard.tmpl", gin.H{
"Message": RuntimeArgs.Message,
"Group": group,
"Dash": dash,
"Users": people,
})
}
// slash Location returns location (to be deprecated)
func slashLocation(c *gin.Context) {
group := c.Param("group")
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); os.IsNotExist(err) {
c.JSON(http.StatusOK, gin.H{"success": "false", "message": "First download the app or CLI program to insert some fingerprints."})
return
}
user := c.Param("user")
userJSON := getCurrentPositionOfUser(group, user)
c.JSON(http.StatusOK, userJSON)
}
// slashExplore returns a chart of the data
func slashExplore(c *gin.Context) {
group := c.Param("group")
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); os.IsNotExist(err) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "First download the app or CLI program to insert some fingerprints.",
})
return
}
network := c.Param("network")
location := c.Param("location")
ps, _ := openParameters(group)
// TODO: check if network and location exists in the ps, if not return 404
datas := []template.JS{}
names := []template.JS{}
indexNames := []template.JS{}
// Sort locations
macs := []string{}
for m := range ps.Priors[network].P[location] {
if float64(ps.MacVariability[m]) > ps.Priors[network].Special["VarabilityCutoff"] {
macs = append(macs, m)
}
}
sort.Strings(macs)
it := 0
for _, m := range macs {
n := ps.Priors[network].P[location][m]
names = append(names, template.JS(string(m)))
jsonByte, _ := json.Marshal(n)
datas = append(datas, template.JS(string(jsonByte)))
indexNames = append(indexNames, template.JS(strconv.Itoa(it)))
it++
}
rsiRange, _ := json.Marshal(RssiRange)
c.HTML(http.StatusOK, "plot.tmpl", gin.H{
"RssiRange": template.JS(string(rsiRange)),
"Datas": datas,
"Names": names,
"IndexNames": indexNames,
})
}
// slashExplore returns a chart of the data (canvas.js)
func slashExplore2(c *gin.Context) {
group := c.Param("group")
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); os.IsNotExist(err) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "First download the app or CLI program to insert some fingerprints.",
})
return
}
network := c.Param("network")
location := c.Param("location")
ps, _ := openParameters(group)
fmt.Println(ps.UniqueLocs)
lookUpLocation := false
for _, loc := range ps.UniqueLocs {
if location == loc {
lookUpLocation = true
}
}
type macDatum struct {
Name string `json:"name"`
Points []float32 `json:"data"`
}
type macDatas struct {
Macs []macDatum `json:"macs"`
}
var data macDatas
data.Macs = []macDatum{}
if lookUpLocation {
// Sort locations
macs := []string{}
for m := range ps.Priors[network].P[location] {
if float64(ps.MacVariability[m]) > ps.Priors[network].Special["VarabilityCutoff"] {
macs = append(macs, m)
}
}
sort.Strings(macs)
for _, m := range macs {
n := ps.Priors[network].P[location][m]
data.Macs = append(data.Macs, macDatum{Name: m, Points: n})
}
} else {
m := location
for loc := range ps.Priors[network].P {
n := ps.Priors[network].P[loc][m]
data.Macs = append(data.Macs, macDatum{Name: strings.Replace(loc, " ", "%20", -1), Points: n})
}
}
c.HTML(http.StatusOK, "plot2.tmpl", gin.H{
"Data": data,
"Rssi": RssiRange,
"Title": group + "/" + network + "/" + location,
"Group": strings.Replace(group, " ", "%20", -1),
"Network": strings.Replace(network, " ", "%20", -1),
"Legend": !lookUpLocation,
})
}
// slashPie returns a Pie chart
func slashPie(c *gin.Context) {
group := c.Param("group")
if _, err := os.Stat(path.Join(RuntimeArgs.SourcePath, group+".db")); os.IsNotExist(err) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ErrorMessage": "First download the app or CLI program to insert some fingerprints.",
})
return
}
network := c.Param("network")
location := c.Param("location")
ps, _ := openParameters(group)
vals := []int{}
names := []string{}
fmt.Println(ps.Results[network].Guess[location])
for guessloc, val := range ps.Results[network].Guess[location] {
names = append(names, guessloc)
vals = append(vals, val)
}
namesJSON, _ := json.Marshal(names)
valsJSON, _ := json.Marshal(vals)
c.HTML(http.StatusOK, "pie.tmpl", gin.H{
"Names": template.JS(namesJSON),
"Vals": template.JS(valsJSON),
})
}