Ma Suhyeon

Implement proccesses and alarms

......@@ -39,6 +39,8 @@ func NewApp(config Config) *App {
app.router.HandleFunc("/extractions/{file}/calls/analyses", app.GetCallsAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/apps/analyses", app.GetAppsAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/messages/analyses", app.GetMessagesAnalyses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/processes", app.GetProcesses).Methods("GET")
app.router.HandleFunc("/extractions/{file}/alarms", app.GetAlarms).Methods("GET")
return app
}
......
......@@ -3,6 +3,8 @@ package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
......@@ -145,7 +147,90 @@ func (app *App) GetMessagesAnalyses(w http.ResponseWriter, r *http.Request) {
(SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=1) receive,
(SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=2) send
FROM sms s GROUP BY address ORDER BY receive + send DESC`
fmt.Println(db.Select(&messages, query))
db.Select(&messages, query)
WriteJson(w, messages)
}
type Process struct {
UID string `json:"uid" db:"UID"`
PID int `json:"pid" db:"PID"`
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(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
processes := []Process{}
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", vars["file"]))
if err != nil {
WriteError(w, http.StatusInternalServerError, "Could not open db file")
return
}
defer db.Close()
query := `SELECT UID, CAST(PID AS INTEGER) PID, CAST(PPID AS INTEGER) PPID, STIME, TIME, CMD FROM process WHERE UID LIKE 'u%' ORDER BY TIME DESC`
db.Select(&processes, query)
WriteJson(w, processes)
}
type Alarm struct {
ID string `json:"id"`
When time.Time `json:"when"`
History []AlarmHistory `json:"history"`
}
type AlarmHistory struct {
Type string `json:"type"`
When time.Time `json:"when"`
}
func (app *App) GetAlarms(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", vars["file"]))
if err != nil {
WriteError(w, http.StatusInternalServerError, "Could not open db file")
return
}
defer db.Close()
alarms := map[string]Alarm{}
rows, _ := db.Queryx("SELECT * FROM alarm ORDER BY TIME")
for rows.Next() {
var tm string
var typ string
var detail string
rows.Scan(&tm, &typ, &detail)
detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")]
s := strings.Split(detail, " ")
timestamp, _ := strconv.ParseInt(s[4], 10, 64)
timestamp /= 1000
if _, ok := alarms[s[0]]; !ok {
alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)}
}
when, _ := time.Parse("2006-01-02 15:04:05", tm)
alarm := alarms[s[0]]
alarm.History = append(alarms[s[0]].History, AlarmHistory{
Type: typ,
When: when,
})
alarms[s[0]] = alarm
}
results := []Alarm{}
for _, v := range alarms {
results = append(results, v)
}
WriteJson(w, results)
}
......