Skip to content

Commit

Permalink
Merge pull request #323 from isucon/backend/263_remove-graph-table-fix
Browse files Browse the repository at this point in the history
[Backend]グラフをGET時に生成するように
  • Loading branch information
ryoha000 authored Jul 9, 2021
2 parents cce814b + 7129fd5 commit 7d4e94d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 213 deletions.
1 change: 0 additions & 1 deletion development/mysql-backend/1_Constraint.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ALTER TABLE isu ADD CONSTRAINT `isu_user_id` FOREIGN KEY (jia_user_id) REFERENCES user(jia_user_id);
ALTER TABLE isu_condition ADD CONSTRAINT `isu_condition_isu_uuid` FOREIGN KEY (jia_isu_uuid) REFERENCES isu(jia_isu_uuid);
ALTER TABLE graph ADD CONSTRAINT `graph_isu_uuid` FOREIGN KEY (jia_isu_uuid) REFERENCES isu(jia_isu_uuid);
10 changes: 0 additions & 10 deletions development/mysql-backend/2_Init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Dumping data for table `graph`
--

LOCK TABLES `graph` WRITE;
/*!40000 ALTER TABLE `graph` DISABLE KEYS */;
INSERT INTO `graph` VALUES ('0694e4d7-dfce-4aec-b7ca-887ac42cfb8f','2021-06-16 00:00:00','{\"score\": 70, \"detail\": {\"dirty\": 0, \"over_weight\": 0}, \"sitting\": 0}','2021-06-16 02:33:40.596857','2021-06-16 02:33:40.596857'),('0694e4d7-dfce-4aec-b7ca-887ac42cfb8f','2021-06-16 01:00:00','{\"score\": 50, \"detail\": {\"dirty\": -10, \"over_weight\": -10}, \"sitting\": 100}','2021-06-16 02:33:40.598760','2021-06-16 02:33:40.598760'),('f012233f-c50e-4349-9473-95681becff1e','2021-06-16 00:00:00','{\"score\": 50, \"detail\": {\"dirty\": -10, \"over_weight\": -10}, \"sitting\": 100}','2021-06-16 02:33:40.600613','2021-06-16 02:33:40.600613'),('f012233f-c50e-4349-9473-95681becff1e','2021-06-16 01:00:00','{\"score\": 30, \"detail\": {\"dirty\": -10, \"over_weight\": -10}, \"sitting\": 100}','2021-06-16 02:33:40.602674','2021-06-16 02:33:40.602674');
/*!40000 ALTER TABLE `graph` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping data for table `isu`
--
Expand Down
162 changes: 0 additions & 162 deletions extra/initial-data/graph/graph.go

This file was deleted.

4 changes: 0 additions & 4 deletions extra/initial-data/models/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,5 @@ func (c Condition) Create() error {
return fmt.Errorf("insert user: %w", err)
}

// INSERT INTO graph
if err := graph.UpdateGraph(db, c.Isu.JIAIsuUUID, c.CreatedAt); err != nil {
log.Fatal(err)
}
return nil
}
64 changes: 28 additions & 36 deletions webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,9 @@ type GraphData struct {

//グラフ表示用 一時間のsummry
type Graph struct {
JIAIsuUUID string `db:"jia_isu_uuid"`
StartAt time.Time `db:"start_at"`
Data string `db:"data"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
JIAIsuUUID string
StartAt time.Time
Data string
}

type User struct {
Expand Down Expand Up @@ -1137,11 +1135,9 @@ func getIsuGraph(c echo.Context) error {
return c.String(http.StatusNotFound, "isu not found")
}

var graphList []Graph
err = tx.Select(&graphList, "SELECT * FROM `graph` WHERE `jia_isu_uuid` = ? AND ? <= `start_at` AND `start_at` <= ? ORDER BY `start_at` ASC ",
jiaIsuUUID, date, date.Add(time.Hour*24))
graphList, err := getGraphDataList(tx, jiaIsuUUID, date)
if err != nil {
c.Logger().Errorf("db error: %v", err)
c.Logger().Errorf("cannot get graph: %v", err)
return c.NoContent(http.StatusInternalServerError)
}

Expand Down Expand Up @@ -1542,13 +1538,6 @@ func postIsuCondition(c echo.Context) error {

}

// getGraph用のデータを計算し、DBを更新する
err = updateGraph(tx, jiaIsuUUID)
if err != nil {
c.Logger().Errorf("failed to update graph: %v", err)
return c.NoContent(http.StatusInternalServerError)
}

// トランザクション終了
err = tx.Commit()
if err != nil {
Expand All @@ -1559,32 +1548,31 @@ func postIsuCondition(c echo.Context) error {
return c.NoContent(http.StatusCreated)
}

// getGraph用のデータを計算し、DBを更新する
func updateGraph(tx *sqlx.Tx, jiaIsuUUID string) error {
func getGraphDataList(tx *sqlx.Tx, jiaIsuUUID string, date time.Time) ([]Graph, error) {
// IsuConditionを一時間ごとの区切りに分け、区切りごとにスコアを計算する
IsuConditionCluster := []IsuCondition{} // 一時間ごとの纏まり
var tmpIsuCondition IsuCondition
valuesForUpdate := []interface{}{} //3個1組、更新するgraphの各行のデータ
rows, err := tx.Queryx("SELECT * FROM `isu_condition` WHERE `jia_isu_uuid` = ? ORDER BY `timestamp` ASC", jiaIsuUUID)
if err != nil {
return err
return nil, err
}
graphDatas := []Graph{}
//一時間ごとに区切る
var startTime time.Time
for rows.Next() {
err = rows.StructScan(&tmpIsuCondition)
if err != nil {
return err
return nil, err
}
tmpTime := truncateAfterHours(tmpIsuCondition.Timestamp)
if startTime != tmpTime {
if len(IsuConditionCluster) > 0 {
//tmpTimeは次の一時間なので、それ以外を使ってスコア計算
data, err := calculateGraphData(IsuConditionCluster)
if err != nil {
return fmt.Errorf("failed to calculate graph: %v", err)
return nil, fmt.Errorf("failed to calculate graph: %v", err)
}
valuesForUpdate = append(valuesForUpdate, jiaIsuUUID, startTime, data)
graphDatas = append(graphDatas, Graph{JIAIsuUUID: jiaIsuUUID, StartAt: startTime, Data: string(data)})
}

//次の一時間の探索
Expand All @@ -1597,24 +1585,28 @@ func updateGraph(tx *sqlx.Tx, jiaIsuUUID string) error {
//最後の一時間分
data, err := calculateGraphData(IsuConditionCluster)
if err != nil {
return fmt.Errorf("failed to calculate graph: %v", err)
return nil, fmt.Errorf("failed to calculate graph: %v", err)
}
valuesForUpdate = append(valuesForUpdate, jiaIsuUUID, startTime, data)
graphDatas = append(graphDatas, Graph{JIAIsuUUID: jiaIsuUUID, StartAt: startTime, Data: string(data)})
}

//insert or update
params := strings.Repeat("(?,?,?),", len(valuesForUpdate)/3)
params = params[:len(params)-1]
_, err = tx.Exec("INSERT INTO `graph` (`jia_isu_uuid`, `start_at`, `data`) VALUES "+
params+
" ON DUPLICATE KEY UPDATE `data` = VALUES(`data`)",
valuesForUpdate...,
)
if err != nil {
return err
// 24時間分のグラフデータだけを取り出す処理
endDate := date.Add(time.Hour * 24)
startIndex := 0
endNextIndex := len(graphDatas)
for i, graph := range graphDatas {
if startIndex == 0 && !graph.StartAt.Before(date) {
startIndex = i
}
if endNextIndex == len(graphDatas) && graph.StartAt.After(endDate) {
endNextIndex = i
}
}
if endNextIndex < startIndex {
return []Graph{}, nil
}

return nil
return graphDatas[startIndex : endNextIndex], nil
}

//分以下を切り捨て、一時間単位にする関数
Expand Down

0 comments on commit 7d4e94d

Please sign in to comment.