Ma Suhyeon

Implement proccesses and alarms

...@@ -39,6 +39,8 @@ func NewApp(config Config) *App { ...@@ -39,6 +39,8 @@ func NewApp(config Config) *App {
39 app.router.HandleFunc("/extractions/{file}/calls/analyses", app.GetCallsAnalyses).Methods("GET") 39 app.router.HandleFunc("/extractions/{file}/calls/analyses", app.GetCallsAnalyses).Methods("GET")
40 app.router.HandleFunc("/extractions/{file}/apps/analyses", app.GetAppsAnalyses).Methods("GET") 40 app.router.HandleFunc("/extractions/{file}/apps/analyses", app.GetAppsAnalyses).Methods("GET")
41 app.router.HandleFunc("/extractions/{file}/messages/analyses", app.GetMessagesAnalyses).Methods("GET") 41 app.router.HandleFunc("/extractions/{file}/messages/analyses", app.GetMessagesAnalyses).Methods("GET")
42 + app.router.HandleFunc("/extractions/{file}/processes", app.GetProcesses).Methods("GET")
43 + app.router.HandleFunc("/extractions/{file}/alarms", app.GetAlarms).Methods("GET")
42 44
43 return app 45 return app
44 } 46 }
......
...@@ -3,6 +3,8 @@ package main ...@@ -3,6 +3,8 @@ package main
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "net/http" 5 "net/http"
6 + "strconv"
7 + "strings"
6 "time" 8 "time"
7 9
8 "github.com/gorilla/mux" 10 "github.com/gorilla/mux"
...@@ -145,7 +147,90 @@ func (app *App) GetMessagesAnalyses(w http.ResponseWriter, r *http.Request) { ...@@ -145,7 +147,90 @@ func (app *App) GetMessagesAnalyses(w http.ResponseWriter, r *http.Request) {
145 (SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=1) receive, 147 (SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=1) receive,
146 (SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=2) send 148 (SELECT COUNT(1) FROM sms m WHERE m.address=s.address AND m.type=2) send
147 FROM sms s GROUP BY address ORDER BY receive + send DESC` 149 FROM sms s GROUP BY address ORDER BY receive + send DESC`
148 - fmt.Println(db.Select(&messages, query)) 150 + db.Select(&messages, query)
149 151
150 WriteJson(w, messages) 152 WriteJson(w, messages)
151 } 153 }
154 +
155 +type Process struct {
156 + UID string `json:"uid" db:"UID"`
157 + PID int `json:"pid" db:"PID"`
158 + PPID int `json:"ppid" db:"PPID"`
159 + STIME string `json:"stime" db:"STIME"`
160 + TIME string `json:"time" db:"TIME"`
161 + CMD string `json:"cmd" db:"CMD"`
162 +}
163 +
164 +func (app *App) GetProcesses(w http.ResponseWriter, r *http.Request) {
165 + vars := mux.Vars(r)
166 +
167 + processes := []Process{}
168 + db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", vars["file"]))
169 + if err != nil {
170 + WriteError(w, http.StatusInternalServerError, "Could not open db file")
171 + return
172 + }
173 + defer db.Close()
174 +
175 + 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`
176 + db.Select(&processes, query)
177 +
178 + WriteJson(w, processes)
179 +}
180 +
181 +type Alarm struct {
182 + ID string `json:"id"`
183 + When time.Time `json:"when"`
184 + History []AlarmHistory `json:"history"`
185 +}
186 +
187 +type AlarmHistory struct {
188 + Type string `json:"type"`
189 + When time.Time `json:"when"`
190 +}
191 +
192 +func (app *App) GetAlarms(w http.ResponseWriter, r *http.Request) {
193 + vars := mux.Vars(r)
194 +
195 + db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", vars["file"]))
196 + if err != nil {
197 + WriteError(w, http.StatusInternalServerError, "Could not open db file")
198 + return
199 + }
200 + defer db.Close()
201 +
202 + alarms := map[string]Alarm{}
203 + rows, _ := db.Queryx("SELECT * FROM alarm ORDER BY TIME")
204 +
205 + for rows.Next() {
206 + var tm string
207 + var typ string
208 + var detail string
209 +
210 + rows.Scan(&tm, &typ, &detail)
211 +
212 + detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")]
213 + s := strings.Split(detail, " ")
214 + timestamp, _ := strconv.ParseInt(s[4], 10, 64)
215 + timestamp /= 1000
216 +
217 + if _, ok := alarms[s[0]]; !ok {
218 + alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)}
219 + }
220 +
221 + when, _ := time.Parse("2006-01-02 15:04:05", tm)
222 + alarm := alarms[s[0]]
223 + alarm.History = append(alarms[s[0]].History, AlarmHistory{
224 + Type: typ,
225 + When: when,
226 + })
227 + alarms[s[0]] = alarm
228 + }
229 +
230 + results := []Alarm{}
231 + for _, v := range alarms {
232 + results = append(results, v)
233 + }
234 +
235 + WriteJson(w, results)
236 +}
......