Ma Suhyeon

Recover APIs

......@@ -4,11 +4,8 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/olivere/elastic/v7"
......@@ -231,80 +228,51 @@ func (app *App) GetMessagesAnalyses(c echo.Context) error {
}
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"`
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{}
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file")))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
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)
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"`
When time.Time `json:"when"`
History []AlarmHistory `json:"history"`
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"`
When time.Time `json:"when"`
Type string `json:"type" db:"type"`
When time.Time `json:"when" db:"when"`
}
func (app *App) GetAlarms(c echo.Context) error {
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file")))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
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
alarms := []Alarm{}
query := "SELECT * FROM alarms WHERE `extraction_no`=?"
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)}
extNo := c.Param("no")
if err := app.db.Unsafe().Select(&alarms, query, extNo); err != nil {
return err
}
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
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
}
results := []Alarm{}
for _, v := range alarms {
results = append(results, v)
}
return c.JSON(http.StatusOK, results)
return c.JSON(http.StatusOK, alarms)
}
type Schedule struct {
......
......@@ -8,6 +8,9 @@ import (
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/jmoiron/sqlx"
......@@ -138,7 +141,7 @@ func (app *App) PostExtractions(c echo.Context) error {
}
}
/*alarms := map[string]Alarm{}
alarms := map[string]Alarm{}
rows, _ = db.Queryx("SELECT * FROM alarm ORDER BY TIME")
for rows.Next() {
......@@ -172,7 +175,7 @@ func (app *App) PostExtractions(c echo.Context) error {
for _, h := range v.History {
tx.Exec("INSERT INTO alarm_histories VALUES (?, ?, ?, ?)", extNo, v.ID, h.Type, h.When)
}
}*/
}
tx.Commit()
......