sync mechanism complete

This commit is contained in:
Miloš Jovanović 2026-09-10 00:20:56 +02:00
parent 40f16346f5
commit d3803dead3
3 changed files with 142 additions and 105 deletions

View file

@ -104,6 +104,7 @@ func (a apiUser) toUser() *User {
ChapterID: a.ChapterID, ChapterID: a.ChapterID,
Department: departmentOf(a.CustomProps), Department: departmentOf(a.CustomProps),
AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"), AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"),
IsRep: isRep(a.CustomProps),
} }
} }

View file

@ -3,8 +3,8 @@ package main
import ( import (
"context" "context"
"crypto/subtle" "crypto/subtle"
"fmt"
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -19,6 +19,7 @@ type User struct {
ChapterID int64 ChapterID int64
Department string Department string
AppointmentTypes []string AppointmentTypes []string
IsRep bool
} }
// STClient is what the handler needs from the Solidarity Tech API. // STClient is what the handler needs from the Solidarity Tech API.
@ -30,6 +31,10 @@ type STClient interface {
type Store interface { type Store interface {
AcquireLock(chapterID int64, dept string, repID int64) (int64, error) AcquireLock(chapterID int64, dept string, repID int64) (int64, error)
ReleaseLock(runID int64) error ReleaseLock(runID int64) error
UpsertPeople(users []User, eligible map[string]bool) error
LastSync() (int64, error)
SetLastSync(ts int64) error
} }
// ErrLocked is returned by AcquireLock when a run is already in flight // ErrLocked is returned by AcquireLock when a run is already in flight
@ -86,7 +91,6 @@ func (s *Server) Routes() *http.ServeMux {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
fmt.Fprintf(w, "%d users\n", len(users))
counts := map[string]int{} counts := map[string]int{}
eligible := 0 eligible := 0
for _, u := range users { for _, u := range users {
@ -102,6 +106,17 @@ func (s *Server) Routes() *http.ServeMux {
} }
}) })
// TEMPORARY: remove before deploy.
mux.HandleFunc("POST /debug/sync", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
if err := s.SyncRoster(ctx); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusOK)
})
return mux return mux
} }

View file

@ -3,29 +3,42 @@ package main
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"fmt"
"strconv"
"strings"
"modernc.org/sqlite" "modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib" sqlite3 "modernc.org/sqlite/lib"
) )
const schema = ` var schemaStatements = []string{
CREATE TABLE IF NOT EXISTS people ( `CREATE TABLE IF NOT EXISTS runs (
id INTEGER PRIMARY KEY,
chapter_id INTEGER NOT NULL,
department TEXT NOT NULL,
rep_user_id INTEGER NOT NULL,
status TEXT NOT NULL,
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
ended_at TEXT
)`,
`CREATE UNIQUE INDEX IF NOT EXISTS one_run_per_dept
ON runs(chapter_id, department) WHERE status = 'running'`,
`CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
email TEXT, email TEXT,
chapter_id INTEGER, chapter_id INTEGER,
department TEXT, department TEXT,
appointment_type TEXT, appointment_type TEXT,
is_rep INTEGER NOT NULL DEFAULT 0, is_rep INTEGER NOT NULL DEFAULT 0,
is_eligible INTEGER NOT NULL DEFAULT 0,
synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
); )`,
`CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department)`,
CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department); `CREATE TABLE IF NOT EXISTS sync_state (
CREATE TABLE IF NOT EXISTS sync_state (
key TEXT PRIMARY KEY, key TEXT PRIMARY KEY,
value TEXT NOT NULL value TEXT NOT NULL
); )`,
` }
type SQLStore struct{ db *sql.DB } type SQLStore struct{ db *sql.DB }
@ -36,8 +49,10 @@ func OpenStore(path string) (*SQLStore, error) {
return nil, err return nil, err
} }
db.SetMaxOpenConns(1) db.SetMaxOpenConns(1)
if _, err := db.Exec(schema); err != nil { for i, stmt := range schemaStatements {
return nil, err if _, err := db.Exec(stmt); err != nil {
return nil, fmt.Errorf("schema statement %d: %w", i, err)
}
} }
return &SQLStore{db: db}, nil return &SQLStore{db: db}, nil
} }
@ -77,29 +92,35 @@ func (s *SQLStore) UpsertPeople(users []User, eligible map[string]bool) error {
defer tx.Rollback() defer tx.Rollback()
stmt, err := tx.Prepare(` stmt, err := tx.Prepare(`
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, synced_at) INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, is_eligible, synced_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(id) DO UPDATE SET ON CONFLICT(id) DO UPDATE SET
email=excluded.email, email=excluded.email,
chapter_id=excluded.chapter_id, chapter_id=excluded.chapter_id,
department=excluded.department, department=excluded.department,
appointment_type=excluded.appointment_type, appointment_type=excluded.appointment_type,
is_rep=excluded.is_rep, is_rep=excluded.is_rep,
is_rep=excluded.is_rep,
synced_at=CURRENT_TIMESTAMP`) synced_at=CURRENT_TIMESTAMP`)
if err != nil { if err != nil {
return err return err
} }
defer stmt.Close() defer stmt.Close()
//loop body
for _, u := range users { for _, u := range users {
rep := 0 rep, elig := 0, 0
if u.IsRep { if u.IsRep {
rep = 1 rep = 1
} }
if u.Eligible(eligible) {
elig = 1
}
_, err := stmt.Exec(u.ID, u.Email, u.ChapterID, _, err := stmt.Exec(u.ID, u.Email, u.ChapterID,
normalizeDept(u.Department), normalizeDept(u.Department),
strings.Join(u.AppointmentTypes, ","), strings.Join(u.AppointmentTypes, ","),
rep) rep, elig)
if err != nil { if err != nil {
return fmt.Errorf("upserting user %d: %w", u.ID, err) return fmt.Errorf("upserting user %d: %w", u.ID, err)
} }