Showing
2 changed files
with
29 additions
and
58 deletions
| ... | @@ -4,11 +4,8 @@ import ( | ... | @@ -4,11 +4,8 @@ import ( |
| 4 | "context" | 4 | "context" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "net/http" | 6 | "net/http" |
| 7 | - "strconv" | ||
| 8 | - "strings" | ||
| 9 | "time" | 7 | "time" |
| 10 | 8 | ||
| 11 | - "github.com/jmoiron/sqlx" | ||
| 12 | "github.com/labstack/echo/v4" | 9 | "github.com/labstack/echo/v4" |
| 13 | "github.com/olivere/elastic/v7" | 10 | "github.com/olivere/elastic/v7" |
| 14 | 11 | ||
| ... | @@ -231,80 +228,51 @@ func (app *App) GetMessagesAnalyses(c echo.Context) error { | ... | @@ -231,80 +228,51 @@ func (app *App) GetMessagesAnalyses(c echo.Context) error { |
| 231 | } | 228 | } |
| 232 | 229 | ||
| 233 | type Process struct { | 230 | type Process struct { |
| 234 | - UID string `json:"uid" db:"UID"` | 231 | + PID int `json:"pid" db:"pid"` |
| 235 | - PID int `json:"pid" db:"PID"` | 232 | + UID string `json:"uid" db:"uid"` |
| 236 | - PPID int `json:"ppid" db:"PPID"` | 233 | + PPID int `json:"ppid" db:"ppid"` |
| 237 | - STIME string `json:"stime" db:"STIME"` | 234 | + STime string `json:"stime" db:"stime"` |
| 238 | - TIME string `json:"time" db:"TIME"` | 235 | + Time string `json:"time" db:"time"` |
| 239 | - CMD string `json:"cmd" db:"CMD"` | 236 | + CMD string `json:"cmd" db:"cmd"` |
| 240 | } | 237 | } |
| 241 | 238 | ||
| 242 | func (app *App) GetProcesses(c echo.Context) error { | 239 | func (app *App) GetProcesses(c echo.Context) error { |
| 243 | processes := []Process{} | 240 | processes := []Process{} |
| 244 | - db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file"))) | ||
| 245 | - if err != nil { | ||
| 246 | - return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file") | ||
| 247 | - } | ||
| 248 | - defer db.Close() | ||
| 249 | 241 | ||
| 250 | - 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` | 242 | + query := "SELECT * FROM processes WHERE `extraction_no`=? AND `uid` LIKE 'u%' ORDER BY `time` DESC" |
| 251 | - db.Select(&processes, query) | 243 | + fmt.Println(app.db.Unsafe().Select(&processes, query, c.Param("no"))) |
| 252 | 244 | ||
| 253 | return c.JSON(http.StatusOK, processes) | 245 | return c.JSON(http.StatusOK, processes) |
| 254 | } | 246 | } |
| 255 | 247 | ||
| 256 | type Alarm struct { | 248 | type Alarm struct { |
| 257 | - ID string `json:"id"` | 249 | + ID string `json:"id" db:"id"` |
| 258 | - When time.Time `json:"when"` | 250 | + When time.Time `json:"when" db:"when"` |
| 259 | - History []AlarmHistory `json:"history"` | 251 | + History []AlarmHistory `json:"history" db:"-"` |
| 260 | } | 252 | } |
| 261 | 253 | ||
| 262 | type AlarmHistory struct { | 254 | type AlarmHistory struct { |
| 263 | - Type string `json:"type"` | 255 | + Type string `json:"type" db:"type"` |
| 264 | - When time.Time `json:"when"` | 256 | + When time.Time `json:"when" db:"when"` |
| 265 | } | 257 | } |
| 266 | 258 | ||
| 267 | func (app *App) GetAlarms(c echo.Context) error { | 259 | func (app *App) GetAlarms(c echo.Context) error { |
| 268 | - db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file"))) | 260 | + alarms := []Alarm{} |
| 269 | - if err != nil { | 261 | + query := "SELECT * FROM alarms WHERE `extraction_no`=?" |
| 270 | - return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file") | ||
| 271 | - } | ||
| 272 | - defer db.Close() | ||
| 273 | - | ||
| 274 | - alarms := map[string]Alarm{} | ||
| 275 | - rows, _ := db.Queryx("SELECT * FROM alarm ORDER BY TIME") | ||
| 276 | - | ||
| 277 | - for rows.Next() { | ||
| 278 | - var tm string | ||
| 279 | - var typ string | ||
| 280 | - var detail string | ||
| 281 | - | ||
| 282 | - rows.Scan(&tm, &typ, &detail) | ||
| 283 | 262 | ||
| 284 | - detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")] | 263 | + extNo := c.Param("no") |
| 285 | - s := strings.Split(detail, " ") | 264 | + if err := app.db.Unsafe().Select(&alarms, query, extNo); err != nil { |
| 286 | - timestamp, _ := strconv.ParseInt(s[4], 10, 64) | 265 | + return err |
| 287 | - timestamp /= 1000 | ||
| 288 | - | ||
| 289 | - if _, ok := alarms[s[0]]; !ok { | ||
| 290 | - alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)} | ||
| 291 | - } | ||
| 292 | - | ||
| 293 | - when, _ := time.Parse("2006-01-02 15:04:05", tm) | ||
| 294 | - alarm := alarms[s[0]] | ||
| 295 | - alarm.History = append(alarms[s[0]].History, AlarmHistory{ | ||
| 296 | - Type: typ, | ||
| 297 | - When: when, | ||
| 298 | - }) | ||
| 299 | - alarms[s[0]] = alarm | ||
| 300 | } | 266 | } |
| 301 | 267 | ||
| 302 | - results := []Alarm{} | 268 | + query = "SELECT * FROM alarm_histories WHERE `extraction_no`=? AND `id`=?" |
| 303 | - for _, v := range alarms { | 269 | + for i := range alarms { |
| 304 | - results = append(results, v) | 270 | + if err := app.db.Unsafe().Select(&alarms[i].History, query, extNo, alarms[i].ID); err != nil { |
| 271 | + return err | ||
| 272 | + } | ||
| 305 | } | 273 | } |
| 306 | 274 | ||
| 307 | - return c.JSON(http.StatusOK, results) | 275 | + return c.JSON(http.StatusOK, alarms) |
| 308 | } | 276 | } |
| 309 | 277 | ||
| 310 | type Schedule struct { | 278 | type Schedule struct { | ... | ... |
| ... | @@ -8,6 +8,9 @@ import ( | ... | @@ -8,6 +8,9 @@ import ( |
| 8 | "net/http" | 8 | "net/http" |
| 9 | "os" | 9 | "os" |
| 10 | "os/exec" | 10 | "os/exec" |
| 11 | + "strconv" | ||
| 12 | + "strings" | ||
| 13 | + "time" | ||
| 11 | 14 | ||
| 12 | "github.com/dgrijalva/jwt-go" | 15 | "github.com/dgrijalva/jwt-go" |
| 13 | "github.com/jmoiron/sqlx" | 16 | "github.com/jmoiron/sqlx" |
| ... | @@ -138,7 +141,7 @@ func (app *App) PostExtractions(c echo.Context) error { | ... | @@ -138,7 +141,7 @@ func (app *App) PostExtractions(c echo.Context) error { |
| 138 | } | 141 | } |
| 139 | } | 142 | } |
| 140 | 143 | ||
| 141 | - /*alarms := map[string]Alarm{} | 144 | + alarms := map[string]Alarm{} |
| 142 | rows, _ = db.Queryx("SELECT * FROM alarm ORDER BY TIME") | 145 | rows, _ = db.Queryx("SELECT * FROM alarm ORDER BY TIME") |
| 143 | 146 | ||
| 144 | for rows.Next() { | 147 | for rows.Next() { |
| ... | @@ -172,7 +175,7 @@ func (app *App) PostExtractions(c echo.Context) error { | ... | @@ -172,7 +175,7 @@ func (app *App) PostExtractions(c echo.Context) error { |
| 172 | for _, h := range v.History { | 175 | for _, h := range v.History { |
| 173 | tx.Exec("INSERT INTO alarm_histories VALUES (?, ?, ?, ?)", extNo, v.ID, h.Type, h.When) | 176 | tx.Exec("INSERT INTO alarm_histories VALUES (?, ?, ?, ?)", extNo, v.ID, h.Type, h.When) |
| 174 | } | 177 | } |
| 175 | - }*/ | 178 | + } |
| 176 | 179 | ||
| 177 | tx.Commit() | 180 | tx.Commit() |
| 178 | 181 | ... | ... |
-
Please register or login to post a comment