feat: materialize award references into the QSO row (award_refs column)

Award refs are now computed once and stored on the QSO as a JSON column
(award_refs, e.g. {"DDFM":"74","WAJA":"12"}) instead of recomputing the whole
award engine on every grid page load. Written on log/edit/UDP-import and
bulk-recomputed when an award definition or reference list changes; a one-time
per-logbook backfill materializes pre-existing QSOs. The grid reads the column
directly like any other field, so it's fast and available everywhere (including
the shared MySQL logbook).

Also fix per-profile grid column layout: the DB copy was already per-profile,
but the localStorage cache used one global namespace and shadowed it, so a
profile switch kept the previous profile's columns/widths. gridPrefs now scopes
the cache by active profile id and the grids remount on profile:changed.
This commit is contained in:
2026-07-20 11:51:49 +02:00
parent 9cc72c7575
commit 64b746f007
9 changed files with 396 additions and 87 deletions
+233 -54
View File
@@ -812,6 +812,7 @@ func (a *App) startup(ctx context.Context) {
applog.Printf("startup: logbook backend = %s", backend)
a.logDb = logbookConn
a.qso = qso.NewRepo(logbookConn)
a.backfillAwardRefsOnce() // one-time: materialise award_refs for pre-existing QSOs
go a.rebuildWorkedIndex() // in-memory worked-index for per-spot alert checks
go a.adifMonitorLoop() // watch external ADIF files (fldigi, N1MM…) for new QSOs
a.relayAutoOn.Store(a.GetRelayAuto().Enabled) // prime the relay auto-control hot-path flag
@@ -1873,6 +1874,7 @@ func (a *App) AddQSO(q qso.QSO) (id int64, err error) {
q.ID = id
a.noteWorked(q.Callsign, q.Band, q.Mode) // keep the alert worked-index fresh
a.noteLiveQSO() // multi-op: flip this operator back "online"
a.materializeAwardRefs(q) // stamp award_refs so the grid columns show at once
// Announce the log so UI widgets can react (e.g. the Flex panel zeroing RIT).
wruntime.EventsEmit(a.ctx, "qso:logged", id)
a.saveQSORecording(&q)
@@ -2515,6 +2517,7 @@ func (a *App) SaveAwardDefs(defs []award.Def) error {
return err
}
go a.mirrorAwardsToFolder(defs)
a.recomputeAwardRefsAsync() // definitions changed → refresh every row's award_refs
return nil
}
@@ -3639,71 +3642,224 @@ func (a *App) ComputeQSOAwardRefs(q qso.QSO) ([]QSOAwardRef, error) {
return out, nil
}
// AwardRefsForQSOs returns, per QSO id, a map of award code → the reference(s)
// that QSO contributes to (joined when several). Powers the per-award columns in
// the Recent QSOs / Worked-before grids. The reference metadata is computed ONCE
// for the whole batch so a page of QSOs stays cheap.
func (a *App) AwardRefsForQSOs(ids []int64) (map[int64]map[string]string, error) {
out := map[int64]map[string]string{}
if a.qso == nil || len(ids) == 0 {
return out, nil
}
// awardMatCtx bundles the precompiled award state needed to derive a QSO's
// materialised references. Built ONCE (newAwardMatCtx) and reused across a batch
// or a full recompute so award.Compute's per-pattern compilation isn't repeated.
type awardMatCtx struct {
defs []award.Def
metas map[string][]award.RefMeta
fieldByCode map[string]string
dispByCode map[string]string
nameOf award.NameResolver
}
func (a *App) newAwardMatCtx() awardMatCtx {
defs := a.awardDefs()
metas := a.awardRefMetas(defs)
fieldByCode := map[string]string{}
dispByCode := map[string]string{}
for _, d := range defs {
fieldByCode[strings.ToUpper(d.Code)] = strings.ToLower(strings.TrimSpace(d.Field))
dispByCode[strings.ToUpper(d.Code)] = strings.ToLower(strings.TrimSpace(d.RefDisplay))
}
nameOf := func(field, ref string) string {
switch field {
case "dxcc":
if n, err := strconv.Atoi(ref); err == nil {
return dxcc.NameForDXCC(n)
return awardMatCtx{
defs: defs,
metas: a.awardRefMetas(defs),
fieldByCode: fieldByCode,
dispByCode: dispByCode,
nameOf: func(field, ref string) string {
switch field {
case "dxcc":
if n, err := strconv.Atoi(ref); err == nil {
return dxcc.NameForDXCC(n)
}
case "cont":
return continentName(ref)
}
case "cont":
return continentName(ref)
return ""
},
}
}
// awardRefLabels derives, for one QSO, the map of award code → the reference(s)
// that QSO contributes to (joined when several), formatted per the award's
// RefDisplay choice (ref / name / both; DXCC shows the country name by default).
func (a *App) awardRefLabels(ac awardMatCtx, q qso.QSO) map[string]string {
a.enrichQSOForAwards(&q)
results := award.Compute(ac.defs, []qso.QSO{q}, ac.metas, ac.nameOf)
m := map[string]string{}
for i := range results {
r := &results[i]
code := strings.ToUpper(r.Code)
dxccField := ac.fieldByCode[code] == "dxcc"
var refs []string
for _, rf := range r.Refs {
if !rf.Worked {
continue
}
label := rf.Ref
switch ac.dispByCode[code] {
case "name":
if rf.Name != "" {
label = rf.Name
}
case "both":
if rf.Name != "" {
label = rf.Ref + " — " + rf.Name
}
default: // "" or "ref"
if dxccField && rf.Name != "" {
label = rf.Name
}
}
refs = append(refs, label)
}
if len(refs) > 0 {
m[code] = strings.Join(refs, ", ")
}
}
return m
}
// awardRefsJSONFor returns awardRefLabels marshalled to a compact JSON object
// keyed by award code, or "" when the QSO contributes to no award (blank column).
func (a *App) awardRefsJSONFor(ac awardMatCtx, q qso.QSO) string {
m := a.awardRefLabels(ac, q)
if len(m) == 0 {
return ""
}
b, err := json.Marshal(m)
if err != nil {
return ""
}
return string(b)
}
// materializeAwardRefs computes ONE QSO's award references and stores them on the
// row (the award_refs column) so the grid columns read them straight from the DB.
// Cheap: an in-memory award.Compute plus one targeted UPDATE. Called on every
// log / edit / UDP-import. Never fatal — a failure just leaves the column stale
// until the next recompute.
func (a *App) materializeAwardRefs(q qso.QSO) {
if a.qso == nil || q.ID == 0 {
return
}
ac := a.newAwardMatCtx()
if err := a.qso.SetAwardRefs(a.ctx, q.ID, a.awardRefsJSONFor(ac, q)); err != nil {
applog.Printf("award_refs: store for qso %d failed: %v", q.ID, err)
}
}
// materializeAwardRefsForIDs recomputes award_refs for a specific set of QSOs
// (e.g. after a bulk cty/QRZ/Club Log update or a bulk edit changed fields that
// feed awards). Writes only the changed rows, in one transaction.
func (a *App) materializeAwardRefsForIDs(ids []int64) {
if a.qso == nil || len(ids) == 0 {
return
}
ac := a.newAwardMatCtx()
changes := map[int64]string{}
err := a.qso.IterateByIDs(a.ctx, ids, func(q qso.QSO) error {
a.enrichQSOForAwards(&q)
results := award.Compute(defs, []qso.QSO{q}, metas, nameOf)
m := map[string]string{}
for i := range results {
r := &results[i]
code := strings.ToUpper(r.Code)
dxccField := fieldByCode[code] == "dxcc"
var refs []string
for _, rf := range r.Refs {
if !rf.Worked {
continue
}
// Per-award display choice: ref (default), name (description), or
// both. DXCC keeps showing the country name under the default.
label := rf.Ref
switch dispByCode[code] {
case "name":
if rf.Name != "" {
label = rf.Name
}
case "both":
if rf.Name != "" {
label = rf.Ref + " — " + rf.Name
}
default: // "" or "ref"
if dxccField && rf.Name != "" {
label = rf.Name
}
}
refs = append(refs, label)
}
if len(refs) > 0 {
m[code] = strings.Join(refs, ", ")
}
if js := a.awardRefsJSONFor(ac, q); js != q.AwardRefs {
changes[q.ID] = js
}
if len(m) > 0 {
return nil
})
if err != nil {
applog.Printf("award_refs: recompute for ids failed: %v", err)
return
}
if err := a.qso.SetAwardRefsBatch(a.ctx, changes); err != nil {
applog.Printf("award_refs: batch write for ids failed: %v", err)
}
}
// RecomputeAllAwardRefs rebuilds every QSO's materialised award references. Run
// when an award definition or reference list changes (stored labels would
// otherwise go stale) and once after upgrade to backfill existing rows. Rows
// whose result is unchanged are skipped; the changed ones are written in a single
// transaction (SetAwardRefsBatch) so even a large logbook on a remote MySQL is
// one round-trip's worth of work rather than N. Returns how many rows changed.
func (a *App) RecomputeAllAwardRefs() (int, error) {
if a.qso == nil {
return 0, fmt.Errorf("db not initialized")
}
ac := a.newAwardMatCtx()
// Collect updates during the scan; DON'T write mid-iteration (a nested query on
// the same connection can deadlock SQLite / trip MySQL "commands out of sync").
changes := map[int64]string{}
err := a.qso.IterateAll(a.ctx, func(q qso.QSO) error {
if js := a.awardRefsJSONFor(ac, q); js != q.AwardRefs {
changes[q.ID] = js
}
return nil
})
if err != nil {
return 0, err
}
if err := a.qso.SetAwardRefsBatch(a.ctx, changes); err != nil {
return 0, err
}
return len(changes), nil
}
// recomputeAwardRefsAsync runs a full recompute off the UI goroutine and, when
// done, tells the frontend to reload so the refreshed award columns show. Used
// wherever the set of matches could shift for MANY rows at once: an award
// definition / reference-list change, or a bulk ADIF import.
func (a *App) recomputeAwardRefsAsync() {
go func() {
n, err := a.RecomputeAllAwardRefs()
if err != nil {
applog.Printf("award_refs: bulk recompute failed: %v", err)
return
}
if a.ctx != nil {
wruntime.EventsEmit(a.ctx, "awards:recomputed", n)
}
}()
}
// keyAwardsMaterialized marks (per profile / logbook) that the one-time award_refs
// backfill has run, so it doesn't re-scan the whole logbook on every launch.
const keyAwardsMaterialized = "awards.materialized.v1"
// backfillAwardRefsOnce populates award_refs for QSOs that predate the feature
// (or were logged by an older client that didn't materialise them). It runs at
// most once per logbook — guarded by a per-profile flag — in the background so it
// never delays startup, even on a large remote MySQL logbook.
func (a *App) backfillAwardRefsOnce() {
if a.qso == nil || a.settings == nil {
return
}
if done, _ := a.settings.Get(a.ctx, keyAwardsMaterialized); done == "1" {
return
}
go func() {
n, err := a.RecomputeAllAwardRefs()
if err != nil {
applog.Printf("award_refs: initial backfill failed: %v", err)
return
}
_ = a.settings.Set(a.ctx, keyAwardsMaterialized, "1")
applog.Printf("award_refs: backfilled %d QSO(s)", n)
if a.ctx != nil && n > 0 {
wruntime.EventsEmit(a.ctx, "awards:recomputed", n)
}
}()
}
// AwardRefsForQSOs returns, per QSO id, the award code → reference(s) map. It is
// the live (non-materialised) path, kept for callers that want fresh values
// without touching the DB. The grid now reads the stored award_refs column
// instead, but this stays available and is the single source of the label logic.
func (a *App) AwardRefsForQSOs(ids []int64) (map[int64]map[string]string, error) {
out := map[int64]map[string]string{}
if a.qso == nil || len(ids) == 0 {
return out, nil
}
ac := a.newAwardMatCtx()
err := a.qso.IterateByIDs(a.ctx, ids, func(q qso.QSO) error {
if m := a.awardRefLabels(ac, q); len(m) > 0 {
out[q.ID] = m
}
return nil
@@ -3749,6 +3905,17 @@ func (a *App) GetAwardReferenceMeta() ([]AwardRefMeta, error) {
// UpdateAwardReferenceList downloads the latest reference list for an award and
// replaces the stored set. Returns the new reference count.
func (a *App) UpdateAwardReferenceList(code string) (AwardRefMeta, error) {
meta, err := a.updateAwardReferenceList(code)
if err == nil {
a.recomputeAwardRefsAsync() // new reference list → labels change → refresh rows
}
return meta, err
}
// updateAwardReferenceList is the recompute-free core, so DownloadAllReferenceLists
// can update several lists in a loop and trigger ONE bulk recompute at the end
// rather than one per list.
func (a *App) updateAwardReferenceList(code string) (AwardRefMeta, error) {
if a.awardRefs == nil {
return AwardRefMeta{}, fmt.Errorf("db not initialized")
}
@@ -3785,7 +3952,7 @@ func (a *App) DownloadAllReferenceLists() (string, error) {
if !awardref.CanUpdate(code) {
continue
}
meta, err := a.UpdateAwardReferenceList(code)
meta, err := a.updateAwardReferenceList(code)
if err != nil {
parts = append(parts, fmt.Sprintf("%s ✗", code))
if firstErr == nil {
@@ -3795,6 +3962,7 @@ func (a *App) DownloadAllReferenceLists() (string, error) {
}
parts = append(parts, fmt.Sprintf("%s %d", code, meta.Count))
}
a.recomputeAwardRefsAsync() // one bulk recompute after all lists updated
return strings.Join(parts, " · "), firstErr
}
@@ -3838,6 +4006,7 @@ func (a *App) DeleteAwardReference(code, refCode string) error {
}
a.markAwardEdited(code)
a.mirrorAwards()
a.recomputeAwardRefsAsync()
return nil
}
@@ -3881,6 +4050,7 @@ func (a *App) ReplaceAwardReferences(code string, refs []awardref.Ref) (int, err
}
a.markAwardEdited(code)
a.mirrorAwards()
a.recomputeAwardRefsAsync() // reference list replaced → refresh every row's award_refs
return n, nil
}
@@ -4485,6 +4655,7 @@ func (a *App) UpdateQSO(q qso.QSO) error {
err := a.qso.Update(a.ctx, q)
if err == nil {
a.invalidateAwardStats()
a.materializeAwardRefs(q) // fields may have changed → refresh award_refs
}
return err
}
@@ -4812,6 +4983,7 @@ func (a *App) BulkUpdateField(ids []int64, field, value string) (int64, error) {
}
if n > 0 {
a.invalidateAwardStats()
a.materializeAwardRefsForIDs(ids) // the edited field may feed an award → refresh
}
return n, nil
}
@@ -4961,7 +5133,11 @@ func (a *App) ImportADIF(path string, dupMode string, applyCty bool, applyStatio
im.OnProgress = func(processed, total int) {
wruntime.EventsEmit(a.ctx, "import:progress", map[string]int{"processed": processed, "total": total})
}
return im.ImportFile(a.ctx, path)
res, err := im.ImportFile(a.ctx, path)
if err == nil && (res.Imported > 0 || res.Updated > 0) {
a.recomputeAwardRefsAsync() // materialise award_refs for the imported rows
}
return res, err
}
// SaveADIFFile shows a native Save-As dialog suggesting a timestamped
@@ -8760,6 +8936,7 @@ func (a *App) UpdateQSOsFromCty(ids []int64) (int, error) {
}
if changed > 0 {
a.invalidateAwardStats()
a.materializeAwardRefsForIDs(ids) // entity fields changed → refresh award_refs
}
return changed, nil
}
@@ -8835,6 +9012,7 @@ func (a *App) UpdateQSOsFromQRZ(ids []int64) (int, error) {
}
if changed > 0 {
a.invalidateAwardStats()
a.materializeAwardRefsForIDs(ids) // entity/geo fields changed → refresh award_refs
}
return changed, nil
}
@@ -9290,6 +9468,7 @@ func (a *App) LogUDPLoggedADIF(adifText string) (int64, error) {
}
q.ID = id
a.noteLiveQSO() // multi-op: flip this operator back "online"
a.materializeAwardRefs(q)
a.saveQSORecording(&q)
if a.extsvc != nil {
a.extsvc.OnQSOLogged(id)