sync mechanism complete
This commit is contained in:
parent
40f16346f5
commit
d3803dead3
3 changed files with 142 additions and 105 deletions
|
|
@ -104,6 +104,7 @@ func (a apiUser) toUser() *User {
|
|||
ChapterID: a.ChapterID,
|
||||
Department: departmentOf(a.CustomProps),
|
||||
AppointmentTypes: optionLabels(a.CustomProps, "appointment-type"),
|
||||
IsRep: isRep(a.CustomProps),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
19
server.go
19
server.go
|
|
@ -3,8 +3,8 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
|
@ -19,6 +19,7 @@ type User struct {
|
|||
ChapterID int64
|
||||
Department string
|
||||
AppointmentTypes []string
|
||||
IsRep bool
|
||||
}
|
||||
|
||||
// STClient is what the handler needs from the Solidarity Tech API.
|
||||
|
|
@ -30,6 +31,10 @@ type STClient interface {
|
|||
type Store interface {
|
||||
AcquireLock(chapterID int64, dept string, repID int64) (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
|
||||
|
|
@ -86,7 +91,6 @@ func (s *Server) Routes() *http.ServeMux {
|
|||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%d users\n", len(users))
|
||||
counts := map[string]int{}
|
||||
eligible := 0
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
51
store.go
51
store.go
|
|
@ -3,29 +3,42 @@ package main
|
|||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"modernc.org/sqlite"
|
||||
sqlite3 "modernc.org/sqlite/lib"
|
||||
)
|
||||
|
||||
const schema = `
|
||||
CREATE TABLE IF NOT EXISTS people (
|
||||
var schemaStatements = []string{
|
||||
`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,
|
||||
email TEXT,
|
||||
chapter_id INTEGER,
|
||||
department TEXT,
|
||||
appointment_type TEXT,
|
||||
is_rep INTEGER NOT NULL DEFAULT 0,
|
||||
is_eligible INTEGER NOT NULL DEFAULT 0,
|
||||
synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sync_state (
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department)`,
|
||||
`CREATE TABLE IF NOT EXISTS sync_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
)`,
|
||||
}
|
||||
|
||||
type SQLStore struct{ db *sql.DB }
|
||||
|
||||
|
|
@ -36,8 +49,10 @@ func OpenStore(path string) (*SQLStore, error) {
|
|||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.Exec(schema); err != nil {
|
||||
return nil, err
|
||||
for i, stmt := range schemaStatements {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
return nil, fmt.Errorf("schema statement %d: %w", i, err)
|
||||
}
|
||||
}
|
||||
return &SQLStore{db: db}, nil
|
||||
}
|
||||
|
|
@ -77,29 +92,35 @@ func (s *SQLStore) UpsertPeople(users []User, eligible map[string]bool) error {
|
|||
defer tx.Rollback()
|
||||
|
||||
stmt, err := tx.Prepare(`
|
||||
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, synced_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, is_eligible, synced_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
email=excluded.email,
|
||||
chapter_id=excluded.chapter_id,
|
||||
department=excluded.department,
|
||||
appointment_type=excluded.appointment_type,
|
||||
is_rep=excluded.is_rep,
|
||||
is_rep=excluded.is_rep,
|
||||
synced_at=CURRENT_TIMESTAMP`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
//loop body
|
||||
|
||||
for _, u := range users {
|
||||
rep := 0
|
||||
rep, elig := 0, 0
|
||||
if u.IsRep {
|
||||
rep = 1
|
||||
}
|
||||
if u.Eligible(eligible) {
|
||||
elig = 1
|
||||
}
|
||||
_, err := stmt.Exec(u.ID, u.Email, u.ChapterID,
|
||||
normalizeDept(u.Department),
|
||||
strings.Join(u.AppointmentTypes, ","),
|
||||
rep)
|
||||
rep, elig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("upserting user %d: %w", u.ID, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue