/
matches.go
74 lines (68 loc) · 1.83 KB
/
matches.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package sqldbexport
import (
"database/sql"
"github.com/RoboCup-SSL/ssl-match-stats/pkg/matchstats"
"github.com/google/uuid"
"github.com/pkg/errors"
"log"
)
func (p *SqlDbExporter) FindMatchId(logFileName string) *uuid.UUID {
id := new(uuid.UUID)
err := p.db.QueryRow(
"SELECT id FROM matches WHERE file_name=$1",
logFileName).Scan(id)
if err == sql.ErrNoRows {
return nil
}
if err != nil {
log.Print("Could not query matches:", err)
}
return id
}
func (p *SqlDbExporter) WriteMatches(matchStatsCollection *matchstats.MatchStatsCollection, tournamentId *uuid.UUID, division string) error {
for _, matchStats := range matchStatsCollection.MatchStats {
logFileName := matchStats.Name
matchId := p.FindMatchId(logFileName)
if matchId == nil {
matchId = new(uuid.UUID)
*matchId = uuid.New()
}
if _, err := p.db.Exec(
`INSERT INTO matches
(
id,
file_name,
tournament_id_fk,
division,
start_time,
duration,
extra_time,
shootout
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT ON CONSTRAINT matches_pkey DO UPDATE SET
division=excluded.division,
start_time=excluded.start_time,
duration=excluded.duration,
extra_time=excluded.extra_time,
shootout=excluded.shootout`,
matchId,
logFileName,
tournamentId,
division,
convertTime(matchStats.StartTime),
convertDuration(matchStats.MatchDuration),
matchStats.ExtraTime,
matchStats.Shootout,
); err != nil {
return errors.Wrap(err, "Could not insert match")
}
if err := p.WriteTeamStats(matchStats, matchId); err != nil {
return errors.Wrap(err, "Could not insert team stats")
}
if err := p.WriteGamePhases(matchStats, matchId); err != nil {
return errors.Wrap(err, "Could not insert game phases")
}
}
return nil
}