data.go
11 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/labstack/echo/v4"
"github.com/olivere/elastic/v7"
_ "github.com/mattn/go-sqlite3"
)
type Call struct {
ID int `json:"id" db:"id"`
Type int `json:"type" db:"type"`
Name *string `json:"name" db:"name"`
Number string `json:"number" db:"number"`
Duration int `json:"duration" db:"duration"`
Date time.Time `json:"date" db:"date"`
}
func (app *App) GetCalls(c echo.Context) error {
calls := []Call{}
query := `SELECT * FROM calls WHERE extraction_no=?`
vals := []interface{}{c.Param("no")}
number := c.QueryParam("number")
if len(number) > 0 {
query += " AND `number`=?"
vals = append(vals, NormalizeNumber(number))
}
app.db.Unsafe().Select(&calls, query, vals...)
return c.JSON(http.StatusOK, calls)
}
func (app *App) PostCall(c echo.Context) error {
call := Call{}
if err := c.Bind(&call); err != nil {
return err
}
query := "INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(query, c.Param("no"), call.ID, call.Type, call.Name, call.Number, call.Duration, call.Date)
if err != nil {
return nil
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) PutCall(c echo.Context) error {
call := Call{}
if err := c.Bind(&call); err != nil {
return err
}
query := "UPDATE calls SET `id`=?, `type`=?, `name`=?, `number`=?, `duration`=?, `date`=? " +
"WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, call.ID, call.Type, call.Name, call.Number, call.Duration, call.Date, c.Param("no"), c.Param("id"))
if err != nil {
return nil
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the call")
} else {
return c.NoContent(http.StatusNoContent)
}
}
func (app *App) DeleteCall(c echo.Context) error {
query := "DELETE FROM calls WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("id"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the call")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type CallStats struct {
Number string `json:"number" db:"number"`
Name *string `json:"name" db:"name"`
Incoming int `json:"incoming" db:"incoming"`
Outgoing int `json:"outgoing" db:"outgoing"`
Duration int `json:"duration" db:"duration"`
}
func (app *App) GetCallsAnalyses(c echo.Context) error {
calls := []CallStats{}
query := `SELECT number, name,
(SELECT COUNT(1) FROM calls s WHERE s.extraction_no=c.extraction_no AND s.number=c.number AND s.type=1) incoming,
(SELECT COUNT(1) FROM calls s WHERE s.extraction_no=c.extraction_no AND s.number=c.number AND s.type=2) outgoing,
SUM(duration) duration
FROM calls c WHERE extraction_no=? GROUP BY number ORDER BY duration DESC`
app.db.Select(&calls, query, c.Param("no"))
return c.JSON(http.StatusOK, calls)
}
type AppInfo struct {
PackageName string `json:"package_name" db:"package_name"`
Name string `json:"name" db:"name"`
Version string `json:"version" db:"version"`
WifiUsage int `json:"wifi_usage" db:"wifi_usage"`
CellularUsage int `json:"cellular_usage" db:"cellular_usage"`
LastUsed time.Time `json:"last_used" db:"last_used"`
ForegroundTime int64 `json:"foreground_time" db:"foreground_time"`
}
func (app *App) GetAppsAnalyses(c echo.Context) error {
apps := []AppInfo{}
query := `SELECT * FROM apps WHERE extraction_no=? ORDER BY foreground_time DESC LIMIT 0, 100`
app.db.Unsafe().Select(&apps, query, c.Param("no"))
return c.JSON(http.StatusOK, apps)
}
func (app *App) PostAppAnalysis(c echo.Context) error {
info := AppInfo{}
if err := c.Bind(&info); err != nil {
return err
}
query := "INSERT INTO apps VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(
query,
c.Param("no"), info.PackageName, info.Name, info.Version,
info.WifiUsage, info.CellularUsage, info.LastUsed, info.ForegroundTime,
)
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) PutAppAnalysis(c echo.Context) error {
info := AppInfo{}
if err := c.Bind(&info); err != nil {
return err
}
query := "UPDATE apps SET `package_name`=?, `name`=?, `version`=?, " +
"`wifi_usage`=?, `cellular_usage`=?, `last_used`=?, `foreground_time`=? " +
"WHERE `extraction_no`=? AND `package_name`=?"
res, err := app.db.Exec(
query,
info.PackageName, info.Name, info.Version,
info.WifiUsage, info.CellularUsage, info.LastUsed, info.ForegroundTime,
c.Param("no"), c.Param("package"),
)
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the app")
} else {
return c.NoContent(http.StatusNoContent)
}
}
func (app *App) DeleteAppAnalysis(c echo.Context) error {
query := "DELETE FROM apps WHERE `extraction_no`=? AND `package_name`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("package"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the app")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type Message struct {
ID int `json:"id" db:"id"`
Type int `json:"type" db:"type"`
Address string `json:"address" db:"address"`
Body string `json:"body" db:"body"`
Date time.Time `json:"date" db:"date"`
}
func (app *App) GetMessages(c echo.Context) error {
messages := []Message{}
no := c.Param("no")
query := "SELECT * FROM messages WHERE extraction_no=?"
vals := []interface{}{no}
number := c.QueryParam("number")
if len(number) > 0 {
query += " AND `address`=?"
vals = append(vals, NormalizeNumber(number))
}
q := c.QueryParam("q")
if len(q) > 0 {
res, err := app.es.Search("messages-" + no).
Query(elastic.NewMatchQuery("content", q)).
Do(context.Background())
if err != nil {
return err
}
query += " AND `id`=?"
for _, hit := range res.Hits.Hits {
message := Message{}
err := app.db.Unsafe().Get(
&message,
query, append(vals, hit.Id)...,
)
if err == nil {
messages = append(messages, message)
}
}
} else {
app.db.Unsafe().Select(&messages, query, vals...)
}
return c.JSON(http.StatusOK, messages)
}
func (app *App) PostMessage(c echo.Context) error {
message := Message{}
if err := c.Bind(&message); err != nil {
return err
}
query := "INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(query, c.Param("no"), message.ID, message.Type, message.Address, message.Body, message.Date)
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) PutMessage(c echo.Context) error {
message := Message{}
if err := c.Bind(&message); err != nil {
return err
}
query := "UPDATE messages SET `id`=?, `type`=?, `address`=?, `body`=?, `date`=? " +
"WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, message.ID, message.Type, message.Address, message.Body, message.Date, c.Param("no"), c.Param("id"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the message")
} else {
return c.NoContent(http.StatusNoContent)
}
}
func (app *App) DeleteMessage(c echo.Context) error {
query := "DELETE FROM messages WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("id"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the message")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type MessageStats struct {
Address string `json:"address" db:"address"`
Receive int `json:"receive" db:"receive"`
Send int `json:"send" db:"send"`
}
func (app *App) GetMessagesAnalyses(c echo.Context) error {
messages := []MessageStats{}
query := `SELECT address,
(SELECT COUNT(1) FROM messages m WHERE m.extraction_no=s.extraction_no AND m.address=s.address AND m.type=1) receive,
(SELECT COUNT(1) FROM messages m WHERE m.extraction_no=s.extraction_no AND m.address=s.address AND m.type=2) send
FROM messages s WHERE extraction_no=? GROUP BY address ORDER BY receive + send DESC`
app.db.Select(&messages, query, c.Param("no"))
return c.JSON(http.StatusOK, messages)
}
type Process struct {
PID int `json:"pid" db:"pid"`
UID string `json:"uid" db:"uid"`
PPID int `json:"ppid" db:"ppid"`
STime string `json:"stime" db:"stime"`
Time string `json:"time" db:"time"`
CMD string `json:"cmd" db:"cmd"`
}
func (app *App) GetProcesses(c echo.Context) error {
processes := []Process{}
query := "SELECT * FROM processes WHERE `extraction_no`=? AND `uid` LIKE 'u%' ORDER BY `time` DESC"
fmt.Println(app.db.Unsafe().Select(&processes, query, c.Param("no")))
return c.JSON(http.StatusOK, processes)
}
type Alarm struct {
ID string `json:"id" db:"id"`
When time.Time `json:"when" db:"when"`
History []AlarmHistory `json:"history" db:"-"`
}
type AlarmHistory struct {
Type string `json:"type" db:"type"`
When time.Time `json:"when" db:"when"`
}
func (app *App) GetAlarms(c echo.Context) error {
alarms := []Alarm{}
query := "SELECT * FROM alarms WHERE `extraction_no`=?"
extNo := c.Param("no")
if err := app.db.Unsafe().Select(&alarms, query, extNo); err != nil {
return err
}
query = "SELECT * FROM alarm_histories WHERE `extraction_no`=? AND `id`=?"
for i := range alarms {
if err := app.db.Unsafe().Select(&alarms[i].History, query, extNo, alarms[i].ID); err != nil {
return err
}
}
return c.JSON(http.StatusOK, alarms)
}
type Schedule struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Begin time.Time `json:"begin" db:"begin"`
End time.Time `json:"end" db:"end"`
}
func (app *App) GetSchedules(c echo.Context) error {
schedules := []Schedule{}
query := "SELECT * FROM schedules WHERE `extraction_no`=?"
if err := app.db.Unsafe().Select(&schedules, query, c.Param("no")); err != nil {
return err
}
return c.JSON(http.StatusOK, schedules)
}
type DailyContact struct {
Date time.Time `json:"date" db:"date"`
Calls int `json:"calls" db:"calls"`
Messages int `json:"messages" db:"messages"`
}
func (app *App) GetDailyContacts(c echo.Context) error {
query := "SELECT `date`, `calls`, `messages` FROM daily_contacts WHERE `extraction_no`=?"
result := []DailyContact{}
if err := app.db.Select(&result, query, c.Param("no")); err != nil {
fmt.Println(err)
return err
}
return c.JSON(http.StatusOK, result)
}
func NormalizeNumber(number string) string {
if len(number) > 0 && number[0] == '+' {
return number
}
integer, err := strconv.Atoi(number)
if err != nil {
return number
}
return fmt.Sprintf("+82%d", integer)
}