From 08dc21d9cdf0b9a70bc0153b8ad819692171dc5c Mon Sep 17 00:00:00 2001 From: Topvennie Date: Tue, 10 Dec 2024 02:09:47 +0100 Subject: [PATCH] feat(zess): bar colors --- db/migrations/20241210002653_add_scan_id.sql | 15 +++++++ db/queries/scan.sql | 10 ++--- internal/pkg/db/dto/scan.go | 10 ++++- internal/pkg/db/sqlc/models.go | 1 + internal/pkg/db/sqlc/scan.sql.go | 46 +++++++++++--------- internal/pkg/zess/zess.go | 7 ++- tui/view/event/style.go | 1 + tui/view/event/view.go | 5 +++ tui/view/zess/style.go | 29 ++++++++++-- tui/view/zess/view.go | 2 +- tui/view/zess/zess.go | 13 +++++- 11 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 db/migrations/20241210002653_add_scan_id.sql diff --git a/db/migrations/20241210002653_add_scan_id.sql b/db/migrations/20241210002653_add_scan_id.sql new file mode 100644 index 0000000..9a9adf2 --- /dev/null +++ b/db/migrations/20241210002653_add_scan_id.sql @@ -0,0 +1,15 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE scan +ADD COLUMN scan_id INTEGER NOT NULL DEFAULT 0; + +ALTER TABLE scan +ALTER COLUMN scan_id +DROP DEFAULT; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE scan +DROP COLUMN scan_id; +-- +goose StatementEnd diff --git a/db/queries/scan.sql b/db/queries/scan.sql index 7c9665a..706ffc5 100644 --- a/db/queries/scan.sql +++ b/db/queries/scan.sql @@ -10,14 +10,14 @@ FROM scan WHERE id = $1; -- name: CreateScan :one -INSERT INTO scan (scan_time) -VALUES ($1) +INSERT INTO scan (scan_id, scan_time) +VALUES ($1, $2) RETURNING *; -- name: UpdateScan :one UPDATE scan -SET scan_time = $1 -WHERE id = $2 +SET scan_id = $1, scan_time = $2 +WHERE id = $3 RETURNING *; -- name: DeleteScan :execrows @@ -38,7 +38,7 @@ LIMIT 1; SELECT * FROM scan WHERE id > $1 -ORDER BY scan_time ASC; +ORDER BY scan_id, scan_time ASC; -- name: GetScansInCurrentSeason :one SELECT COUNT(*) AS amount diff --git a/internal/pkg/db/dto/scan.go b/internal/pkg/db/dto/scan.go index 03eb265..5622e24 100644 --- a/internal/pkg/db/dto/scan.go +++ b/internal/pkg/db/dto/scan.go @@ -10,6 +10,7 @@ import ( // Scan is the DTO for the scan type Scan struct { ID int32 `json:"id"` + ScanID int32 `json:"scan_id"` ScanTime time.Time `json:"scan_time" validate:"required"` } @@ -17,19 +18,24 @@ type Scan struct { func ScanDTO(scan sqlc.Scan) *Scan { return &Scan{ ID: scan.ID, + ScanID: scan.ScanID, ScanTime: scan.ScanTime.Time, } } // CreateParams converts a Scan to sqlc.CreateScanParams -func (s *Scan) CreateParams() pgtype.Timestamptz { - return pgtype.Timestamptz{Time: s.ScanTime, Valid: true} +func (s *Scan) CreateParams() sqlc.CreateScanParams { + return sqlc.CreateScanParams{ + ScanID: s.ScanID, + ScanTime: pgtype.Timestamptz{Time: s.ScanTime, Valid: true}, + } } // UpdateParams converts a Scan to sqlc.UpdateScanParams func (s *Scan) UpdateParams() sqlc.UpdateScanParams { return sqlc.UpdateScanParams{ ID: s.ID, + ScanID: s.ScanID, ScanTime: pgtype.Timestamptz{Time: s.ScanTime, Valid: true}, } } diff --git a/internal/pkg/db/sqlc/models.go b/internal/pkg/db/sqlc/models.go index 4083a8a..f50e025 100644 --- a/internal/pkg/db/sqlc/models.go +++ b/internal/pkg/db/sqlc/models.go @@ -35,6 +35,7 @@ type Message struct { type Scan struct { ID int32 ScanTime pgtype.Timestamptz + ScanID int32 } type Season struct { diff --git a/internal/pkg/db/sqlc/scan.sql.go b/internal/pkg/db/sqlc/scan.sql.go index 6782c7f..38a6499 100644 --- a/internal/pkg/db/sqlc/scan.sql.go +++ b/internal/pkg/db/sqlc/scan.sql.go @@ -12,15 +12,20 @@ import ( ) const createScan = `-- name: CreateScan :one -INSERT INTO scan (scan_time) -VALUES ($1) -RETURNING id, scan_time +INSERT INTO scan (scan_id, scan_time) +VALUES ($1, $2) +RETURNING id, scan_time, scan_id ` -func (q *Queries) CreateScan(ctx context.Context, scanTime pgtype.Timestamptz) (Scan, error) { - row := q.db.QueryRow(ctx, createScan, scanTime) +type CreateScanParams struct { + ScanID int32 + ScanTime pgtype.Timestamptz +} + +func (q *Queries) CreateScan(ctx context.Context, arg CreateScanParams) (Scan, error) { + row := q.db.QueryRow(ctx, createScan, arg.ScanID, arg.ScanTime) var i Scan - err := row.Scan(&i.ID, &i.ScanTime) + err := row.Scan(&i.ID, &i.ScanTime, &i.ScanID) return i, err } @@ -39,7 +44,7 @@ func (q *Queries) DeleteScan(ctx context.Context, id int32) (int64, error) { const getAllScans = `-- name: GetAllScans :many -SELECT id, scan_time +SELECT id, scan_time, scan_id FROM scan ` @@ -53,7 +58,7 @@ func (q *Queries) GetAllScans(ctx context.Context) ([]Scan, error) { var items []Scan for rows.Next() { var i Scan - if err := rows.Scan(&i.ID, &i.ScanTime); err != nil { + if err := rows.Scan(&i.ID, &i.ScanTime, &i.ScanID); err != nil { return nil, err } items = append(items, i) @@ -65,10 +70,10 @@ func (q *Queries) GetAllScans(ctx context.Context) ([]Scan, error) { } const getAllScansSinceID = `-- name: GetAllScansSinceID :many -SELECT id, scan_time +SELECT id, scan_time, scan_id FROM scan WHERE id > $1 -ORDER BY scan_time ASC +ORDER BY scan_id, scan_time ASC ` func (q *Queries) GetAllScansSinceID(ctx context.Context, id int32) ([]Scan, error) { @@ -80,7 +85,7 @@ func (q *Queries) GetAllScansSinceID(ctx context.Context, id int32) ([]Scan, err var items []Scan for rows.Next() { var i Scan - if err := rows.Scan(&i.ID, &i.ScanTime); err != nil { + if err := rows.Scan(&i.ID, &i.ScanTime, &i.ScanID); err != nil { return nil, err } items = append(items, i) @@ -94,7 +99,7 @@ func (q *Queries) GetAllScansSinceID(ctx context.Context, id int32) ([]Scan, err const getLastScan = `-- name: GetLastScan :one -SELECT id, scan_time +SELECT id, scan_time, scan_id FROM scan ORDER BY id DESC LIMIT 1 @@ -104,12 +109,12 @@ LIMIT 1 func (q *Queries) GetLastScan(ctx context.Context) (Scan, error) { row := q.db.QueryRow(ctx, getLastScan) var i Scan - err := row.Scan(&i.ID, &i.ScanTime) + err := row.Scan(&i.ID, &i.ScanTime, &i.ScanID) return i, err } const getScanByID = `-- name: GetScanByID :one -SELECT id, scan_time +SELECT id, scan_time, scan_id FROM scan WHERE id = $1 ` @@ -117,7 +122,7 @@ WHERE id = $1 func (q *Queries) GetScanByID(ctx context.Context, id int32) (Scan, error) { row := q.db.QueryRow(ctx, getScanByID, id) var i Scan - err := row.Scan(&i.ID, &i.ScanTime) + err := row.Scan(&i.ID, &i.ScanTime, &i.ScanID) return i, err } @@ -137,19 +142,20 @@ func (q *Queries) GetScansInCurrentSeason(ctx context.Context) (int64, error) { const updateScan = `-- name: UpdateScan :one UPDATE scan -SET scan_time = $1 -WHERE id = $2 -RETURNING id, scan_time +SET scan_id = $1, scan_time = $2 +WHERE id = $3 +RETURNING id, scan_time, scan_id ` type UpdateScanParams struct { + ScanID int32 ScanTime pgtype.Timestamptz ID int32 } func (q *Queries) UpdateScan(ctx context.Context, arg UpdateScanParams) (Scan, error) { - row := q.db.QueryRow(ctx, updateScan, arg.ScanTime, arg.ID) + row := q.db.QueryRow(ctx, updateScan, arg.ScanID, arg.ScanTime, arg.ID) var i Scan - err := row.Scan(&i.ID, &i.ScanTime) + err := row.Scan(&i.ID, &i.ScanTime, &i.ScanID) return i, err } diff --git a/internal/pkg/zess/zess.go b/internal/pkg/zess/zess.go index eac8f8a..f3800de 100644 --- a/internal/pkg/zess/zess.go +++ b/internal/pkg/zess/zess.go @@ -12,6 +12,7 @@ import ( "github.com/zeusWPI/scc/internal/pkg/db/sqlc" "github.com/zeusWPI/scc/pkg/config" "github.com/zeusWPI/scc/pkg/util" + "go.uber.org/zap" ) // Zess represents a zess instance @@ -69,7 +70,7 @@ func (z *Zess) UpdateScans() error { return err } - lastScan = sqlc.Scan{ID: -1} + lastScan = sqlc.Scan{ScanID: -1} } // Get all scans @@ -78,9 +79,11 @@ func (z *Zess) UpdateScans() error { return err } + zap.S().Info(lastScan) + errs := make([]error, 0) for _, scan := range *zessScans { - if lastScan.ID >= scan.ID { + if scan.ScanID <= lastScan.ScanID { continue } diff --git a/tui/view/event/style.go b/tui/view/event/style.go index 6c2f928..bfbfbc9 100644 --- a/tui/view/event/style.go +++ b/tui/view/event/style.go @@ -43,6 +43,7 @@ var ( // Styles overview var ( + sOverviewTotal = base.AlignVertical(lipgloss.Center) sOverviewTitle = base.Bold(true).Foreground(cWarning).Width(widthOverview).Align(lipgloss.Center) sOverview = base.Border(lipgloss.NormalBorder(), true, false, false, false).BorderForeground(cBorder).Width(widthOverview).MarginRight(mOverview) sPassedName = base.Foreground(cZeus).Faint(true).Width(widthOverviewName) diff --git a/tui/view/event/view.go b/tui/view/event/view.go index 79b75fc..7d3a16c 100644 --- a/tui/view/event/view.go +++ b/tui/view/event/view.go @@ -60,6 +60,11 @@ func (m *Model) viewNormal() string { title := sOverviewTitle.Render("Events") overview = lipgloss.JoinVertical(lipgloss.Left, title, overview) + // Center the overview + if lipgloss.Height(im) > lipgloss.Height(overview) { + overview = sOverviewTotal.Height(lipgloss.Height(im)).Render(overview) + } + // Combine image and overview view := lipgloss.JoinHorizontal(lipgloss.Top, overview, im) diff --git a/tui/view/zess/style.go b/tui/view/zess/style.go index 97e7986..fe6f3ad 100644 --- a/tui/view/zess/style.go +++ b/tui/view/zess/style.go @@ -21,16 +21,39 @@ var ( // Colors var ( - cBarChart = lipgloss.Color("#32012F") - cBorder = lipgloss.Color("#383838") cZeus = lipgloss.Color("#FF7F00") cStatsTitle = lipgloss.Color("#EE4B2B") ) +// Message colors +var colors = []string{ + "#FAF500", // Yellow + "#3AFA00", // Green + "#FAD700", // Yellow Green + "#FAA600", // Orange + "#FAE200", // Yellow Orange + "#FA7200", // Orange Red + "#FA4600", // Red + "#FA0400", // Real Red + "#FA0079", // Pink Red + "#FA00FA", // Pink + "#EE00FA", // Purple + "#8300FA", // Purple Blue + "#3100FA", // Blue + "#00FAFA", // Light Blue + "#00FAA5", // Green Blue + "#00FA81", // IDK + "#F8FA91", // Weird Light Green + "#FAD392", // Light Orange + "#FA9E96", // Salmon + "#DEA2F9", // Fuchsia + "#B3D2F9", // Boring Blue +} + // Styles chart var ( - sBar = base.Foreground(cBarChart) + sBar = base ) // Styles stats diff --git a/tui/view/zess/view.go b/tui/view/zess/view.go index f00d1c0..6828146 100644 --- a/tui/view/zess/view.go +++ b/tui/view/zess/view.go @@ -16,7 +16,7 @@ func (m *Model) viewChart() string { Values: []barchart.BarValue{{ Name: scan.label, Value: float64(scan.amount), - Style: sBar, + Style: sBar.Foreground(lipgloss.Color(scan.color)), }}, } diff --git a/tui/view/zess/zess.go b/tui/view/zess/zess.go index 429dbbd..a8d8c9d 100644 --- a/tui/view/zess/zess.go +++ b/tui/view/zess/zess.go @@ -3,6 +3,7 @@ package zess import ( "context" + "math/rand/v2" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -23,6 +24,7 @@ type weekScan struct { time yearWeek amount int64 label string + color string } // Model represents the Model for the zess view @@ -227,7 +229,12 @@ func updateScans(view view.View) (tea.Msg, error) { } if !found { - zessScanMsg.scans = append(zessScanMsg.scans, weekScan{time: newTime, amount: 1, label: newScan.ScanTime.Time.Format("02/01")}) + zessScanMsg.scans = append(zessScanMsg.scans, weekScan{ + time: newTime, + amount: 1, + label: newScan.ScanTime.Time.Format("02/01"), + color: randomColor(), + }) } // Update scan ID @@ -276,3 +283,7 @@ func (z *yearWeek) after(z2 yearWeek) bool { return z.week > z2.week } + +func randomColor() string { + return colors[rand.IntN(len(colors))] +}