fix: QSL/recording no longer revert a QSO's upload status + faster logging
Status clobber: sending an OpsLog QSL card (or stamping a recording) read the QSO, did slow work (e-mail send), then wrote the WHOLE row back from that now- stale copy — silently reverting clublog/qrz_qso_upload_status from Y to R that a concurrent auto-upload had just set. That's why QSOs "showed R again, but only with QSL sending on", intermittently. New Repo.SetExtra does a targeted `UPDATE qso SET extras = ?` that touches only the extras column, so an app-field stamp can never clobber another column. Used by SendEQSL, markRecordingSent and the recording-path stamp. Logging speed: the log path no longer runs a callsign lookup at all — the e-mail already arrives on the QSO from the entry lookup, so the second one was redundant and stalled logging on a slow/not-found QRZ. And the entry lookup (LookupCallsign) is now bounded to ~2 s: providers get 2 s, then it falls through to cty.dat instead of spinning 10 s+ on a call that isn't in QRZ.
This commit is contained in:
+49
-10
@@ -781,6 +781,38 @@ func (r *Repo) BulkSetField(ctx context.Context, ids []int64, column, value stri
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// SetExtra merges ONE key into a QSO's extras JSON without rewriting the rest of
|
||||
// the row. A slow caller that only wants to stamp its own app field (e-mailing a
|
||||
// QSL card, saving a recording) must not do a full-row Update: the row it read may
|
||||
// be seconds stale, and writing it all back silently reverts any column another
|
||||
// action changed meanwhile — e.g. an auto-upload flipping clublog_qso_upload_status
|
||||
// from R to Y. Touching only `extras` makes that impossible. Empty value deletes
|
||||
// the key.
|
||||
func (r *Repo) SetExtra(ctx context.Context, id int64, key, value string) error {
|
||||
if id == 0 || strings.TrimSpace(key) == "" {
|
||||
return fmt.Errorf("missing id or key")
|
||||
}
|
||||
var extrasJSON sql.NullString
|
||||
if err := r.db.QueryRowContext(ctx, `SELECT extras FROM qso WHERE id = ?`, id).Scan(&extrasJSON); err != nil {
|
||||
return fmt.Errorf("load extras: %w", err)
|
||||
}
|
||||
m := decodeExtras(extrasJSON.String)
|
||||
if m == nil {
|
||||
m = map[string]string{}
|
||||
}
|
||||
if value == "" {
|
||||
delete(m, key)
|
||||
} else {
|
||||
m[key] = value
|
||||
}
|
||||
if _, err := r.db.ExecContext(ctx,
|
||||
`UPDATE qso SET extras = ?, updated_at = ? WHERE id = ?`,
|
||||
encodeExtras(m), db.NowISO(), id); err != nil {
|
||||
return fmt.Errorf("set extra %s: %w", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update overwrites all editable fields of an existing QSO. updated_at is bumped.
|
||||
func (r *Repo) Update(ctx context.Context, q QSO) error {
|
||||
if q.ID == 0 {
|
||||
@@ -1864,25 +1896,32 @@ func (r *Repo) Count(ctx context.Context) (int64, error) {
|
||||
}
|
||||
|
||||
// RecentRate counts QSOs whose start time falls within each trailing window from
|
||||
// `now` — the live "QSO rate" meter shown in the header. It scans only the most
|
||||
// recently inserted rows (ORDER BY id DESC LIMIT), since any QSO in the last hour
|
||||
// was inserted recently; that keeps it cheap even on a large log. qso_date is the
|
||||
// repo's text column, parsed with parseTimeLoose (backend-format agnostic).
|
||||
func (r *Repo) RecentRate(ctx context.Context, now time.Time, windows ...time.Duration) ([]int, error) {
|
||||
// `now` — the live "QSO rate" meter shown in the header. When operator is non-empty
|
||||
// (multi-op on a shared logbook) only that operator's QSOs are counted, so each op
|
||||
// sees their OWN performance, not the cumulative rate; empty operator matches every
|
||||
// QSO. It scans only the most recently inserted rows (ORDER BY id DESC LIMIT), since
|
||||
// any QSO in the last hour was inserted recently; that keeps it cheap even on a large
|
||||
// log. qso_date is the repo's text column, parsed with parseTimeLoose (backend-format
|
||||
// agnostic).
|
||||
func (r *Repo) RecentRate(ctx context.Context, now time.Time, operator string, windows ...time.Duration) ([]int, error) {
|
||||
counts := make([]int, len(windows))
|
||||
// 400 rows covers a full hour even at a blistering contest rate (>300/h); any
|
||||
// QSO inside the trailing windows is among the most recently inserted.
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT qso_date FROM qso ORDER BY id DESC LIMIT 400`)
|
||||
// 2000 rows covers a full hour for one operator even in a busy multi-op run
|
||||
// (other operators' rows are discarded before counting).
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT operator, qso_date FROM qso ORDER BY id DESC LIMIT 2000`)
|
||||
if err != nil {
|
||||
return counts, err
|
||||
}
|
||||
defer rows.Close()
|
||||
now = now.UTC()
|
||||
opFilter := strings.ToUpper(strings.TrimSpace(operator))
|
||||
for rows.Next() {
|
||||
var dateStr sql.NullString
|
||||
if err := rows.Scan(&dateStr); err != nil {
|
||||
var oper, dateStr sql.NullString
|
||||
if err := rows.Scan(&oper, &dateStr); err != nil {
|
||||
return counts, err
|
||||
}
|
||||
if strings.ToUpper(strings.TrimSpace(oper.String)) != opFilter {
|
||||
continue // a different operator's QSO — not part of my rate
|
||||
}
|
||||
t := parseTimeLoose(dateStr.String).UTC()
|
||||
if t.IsZero() || t.After(now) {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user