forked from schollz/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
executable file
·261 lines (242 loc) · 7.13 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
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"
)
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)
}
}
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)
}
}
func slashLoginPOST(c *gin.Context) {
loginGroup := sessions.Default(c)
group := strings.ToLower(c.PostForm("group"))
if _, err := os.Stat(path.Join("data", 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.",
})
}
}
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.",
})
}
}
func slashDashboard(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
}
ps, _ := openParameters(group)
users := getUsers(group)
people := make(map[string]UserPositionJSON)
for _, user := range users {
people[user] = getPositionBreakdown(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{
"Group": group,
"Dash": dash,
"Users": people,
})
}
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 := getPositionBreakdown(group, user)
c.JSON(http.StatusOK, userJSON)
}
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,
})
}
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)
lookUpLocation := true
if strings.Count(location, ":") > 4 {
lookUpLocation = false // location is actuall mac
Debug.Println("GOT LOCATION")
}
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 {
Debug.Println(loc, m)
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,
})
}
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),
})
}