Skip to content

Commit

Permalink
Merge branch 'condition_generated'
Browse files Browse the repository at this point in the history
  • Loading branch information
wtks committed Aug 21, 2021
2 parents 3549777 + 928c0dc commit 7981a77
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
50 changes: 22 additions & 28 deletions app/webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type IsuCondition struct {
Timestamp time.Time `db:"timestamp"`
IsSitting bool `db:"is_sitting"`
Condition string `db:"condition"`
Level string `db:"level"`
Message string `db:"message"`
CreatedAt time.Time `db:"created_at"`
}
Expand Down Expand Up @@ -1048,24 +1049,29 @@ func getIsuConditions(c echo.Context) error {
func getIsuConditionsFromDB(db *sqlx.DB, jiaIsuUUID string, endTime time.Time, conditionLevel map[string]interface{}, startTime time.Time,
limit int, isuName string) ([]*GetIsuConditionResponse, error) {

levels := []string{}
for s := range conditionLevel {
levels = append(levels, "'"+s+"'")
}

conditions := []IsuCondition{}
var err error

sdb := selectDB(jiaIsuUUID)
if startTime.IsZero() {
err = sdb.Select(&conditions,
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ?"+
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? AND level IN ("+strings.Join(levels, ",")+")"+
" AND `timestamp` < ?"+
" ORDER BY `timestamp` DESC",
jiaIsuUUID, endTime,
" ORDER BY `timestamp` DESC LIMIT ?",
jiaIsuUUID, endTime, limit,
)
} else {
err = sdb.Select(&conditions,
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ?"+
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? AND level IN ("+strings.Join(levels, ",")+")"+
" AND `timestamp` < ?"+
" AND ? <= `timestamp`"+
" ORDER BY `timestamp` DESC",
jiaIsuUUID, endTime, startTime,
" ORDER BY `timestamp` DESC LIMIT ?",
jiaIsuUUID, endTime, startTime, limit,
)
}
if err != nil {
Expand All @@ -1074,27 +1080,16 @@ func getIsuConditionsFromDB(db *sqlx.DB, jiaIsuUUID string, endTime time.Time, c

conditionsResponse := []*GetIsuConditionResponse{}
for _, c := range conditions {
cLevel, err := calculateConditionLevel(c.Condition)
if err != nil {
continue
}

if _, ok := conditionLevel[cLevel]; ok {
data := GetIsuConditionResponse{
JIAIsuUUID: c.JIAIsuUUID,
IsuName: isuName,
Timestamp: c.Timestamp.Unix(),
IsSitting: c.IsSitting,
Condition: c.Condition,
ConditionLevel: cLevel,
Message: c.Message,
}
conditionsResponse = append(conditionsResponse, &data)
data := GetIsuConditionResponse{
JIAIsuUUID: c.JIAIsuUUID,
IsuName: isuName,
Timestamp: c.Timestamp.Unix(),
IsSitting: c.IsSitting,
Condition: c.Condition,
ConditionLevel: c.Level,
Message: c.Message,
}
}

if len(conditionsResponse) > limit {
conditionsResponse = conditionsResponse[:limit]
conditionsResponse = append(conditionsResponse, &data)
}

return conditionsResponse, nil
Expand Down Expand Up @@ -1231,7 +1226,7 @@ func getTrend(c echo.Context) error {
}

var (
isuExistsMap = make(map[string]interface{})
isuExistsMap = make(map[string]interface{})
isuExistsMapMux = sync.RWMutex{}
)

Expand Down Expand Up @@ -1281,7 +1276,6 @@ func postIsuCondition(c echo.Context) error {
return c.String(http.StatusBadRequest, "bad request body")
}


if ok, err := isIsuExists(jiaIsuUUID); err != nil {
c.Logger().Errorf("db error: %v", err)
return c.NoContent(http.StatusInternalServerError)
Expand Down
9 changes: 9 additions & 0 deletions app/webapp/sql/0_Schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ CREATE TABLE `isu_condition` (
`timestamp` DATETIME NOT NULL,
`is_sitting` TINYINT(1) NOT NULL,
`condition` VARCHAR(255) NOT NULL,
`level` VARCHAR(10) GENERATED ALWAYS AS (
CASE CAST((LENGTH(`condition`) - LENGTH(REPLACE(`condition`, '=true' , ''))) / 5 AS INT)
WHEN 0 THEN 'info'
WHEN 1 THEN 'warning'
WHEN 2 THEN 'warning'
WHEN 3 THEN 'critical'
ELSE ''
END
) STORED,
`message` VARCHAR(255) NOT NULL,
`created_at` DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY(`id`)
Expand Down

0 comments on commit 7981a77

Please sign in to comment.