Skip to content

Commit

Permalink
level
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumack committed Aug 21, 2021
1 parent acc8584 commit 45a149d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
61 changes: 43 additions & 18 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type IsuCondition struct {
JIAIsuUUID string `db:"jia_isu_uuid"`
Timestamp time.Time `db:"timestamp"`
IsSitting bool `db:"is_sitting"`
Level int `db:"level"`
Condition string `db:"condition"`
Message string `db:"message"`
CreatedAt time.Time `db:"created_at"`
Expand Down Expand Up @@ -1106,24 +1107,28 @@ 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) {

var levels []int
for key, _ := range conditionLevel {
lv, _ := convertConditionLevel(key)
levels = append(levels, lv)
}

conditions := []IsuCondition{}
var err error

if startTime.IsZero() {
err = db.Select(&conditions,
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ?"+
" AND `timestamp` < ?"+
" ORDER BY `timestamp` DESC LIMIT ?",
jiaIsuUUID, endTime, limit,
)
q := "SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? AND level IN (?) " +
" AND `timestamp` < ?" +
" ORDER BY `timestamp` DESC LIMIT ?"
q, params, _ := sqlx.In(q, jiaIsuUUID, levels, endTime, limit)
err = db.Select(&conditions, q, params...)
} else {
err = db.Select(&conditions,
"SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ?"+
" AND `timestamp` < ?"+
" AND ? <= `timestamp`"+
" ORDER BY `timestamp` DESC LIMIT ?",
jiaIsuUUID, endTime, startTime, limit,
)
q := "SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? AND level IN (?) " +
" AND `timestamp` < ?" +
" AND ? <= `timestamp`" +
" ORDER BY `timestamp` DESC LIMIT ?"
q, params, _ := sqlx.In(q, jiaIsuUUID, levels, endTime, startTime, limit)
err = db.Select(&conditions, q, params...)
}
if err != nil {
return nil, fmt.Errorf("db error: %v", err)
Expand Down Expand Up @@ -1176,6 +1181,21 @@ func calculateConditionLevel(condition string) (string, error) {
return conditionLevel, nil
}

func convertConditionLevel(condition string) (int, error) {
switch condition {
case conditionLevelCritical:
return 3, nil
case conditionLevelWarning:
return 2, nil
case conditionLevelInfo:
return 1, nil
default:
return 0, fmt.Errorf("unexpected warn count")
}

return 0, nil
}

// GET /api/trend
// ISUの性格毎の最新のコンディション情報
func getTrend(c echo.Context) error {
Expand Down Expand Up @@ -1297,9 +1317,9 @@ func postIsuCondition(c echo.Context) error {
return c.String(http.StatusNotFound, "not found: isu")
}

var lastTime string;
var perHourCount int;
perHourCount = 0;
var lastTime string
var perHourCount int
perHourCount = 0

var rows []*IsuCondition
for _, cond := range req {
Expand All @@ -1320,18 +1340,23 @@ func postIsuCondition(c echo.Context) error {
return c.String(http.StatusBadRequest, "bad request body")
}

conditionLevel, _ := calculateConditionLevel(cond.Condition)
level, _ := convertConditionLevel(conditionLevel)

rows = append(rows, &IsuCondition{
JIAIsuUUID: jiaIsuUUID,
Timestamp: timestamp,
IsSitting: cond.IsSitting,
Level: level,
Condition: cond.Condition,
Message: cond.Message,
})
}

_, err = tx.NamedExec(
"INSERT INTO `isu_condition`"+
" (`jia_isu_uuid`, `timestamp`, `is_sitting`, `condition`, `message`)"+
" VALUES (:jia_isu_uuid, :timestamp, :is_sitting, :condition, :message)", rows)
" (`jia_isu_uuid`, `timestamp`, `is_sitting`, `condition`, `level`, `message`)"+
" VALUES (:jia_isu_uuid, :timestamp, :is_sitting, :condition, :level, :message)", rows)
if err != nil {
c.Logger().Errorf("db error: %v", err)
return c.NoContent(http.StatusInternalServerError)
Expand Down
2 changes: 2 additions & 0 deletions sql/0_Schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ CREATE TABLE `isu_condition` (
`jia_isu_uuid` CHAR(36) NOT NULL,
`timestamp` DATETIME NOT NULL,
`is_sitting` TINYINT(1) NOT NULL,
`level` TINYINT(1) NOT NULL,
`condition` VARCHAR(255) NOT NULL,
`message` VARCHAR(255) NOT NULL,
`created_at` DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4;
create index i1 on isu_condition(`jia_isu_uuid`, `timestamp`);
create index i2 on isu_condition(`jia_isu_uuid`, `level`, `timestamp`);

CREATE TABLE `user` (
`jia_user_id` VARCHAR(255) PRIMARY KEY,
Expand Down

0 comments on commit 45a149d

Please sign in to comment.