fix: Added LOTW badge for Lotw users colored depending on their last upload
This commit is contained in:
@@ -202,6 +202,23 @@ func (db *DB) Resolve(call string, date time.Time) (Exception, bool) {
|
||||
return Exception{}, false
|
||||
}
|
||||
|
||||
// HasException reports whether the callsign has ANY full-call exception (at any
|
||||
// date). Used to tell a genuinely date-sensitive call (G1T: Scotland only from
|
||||
// 2024) — where cty.dat's date-blind exact-call override is wrong for older
|
||||
// QSOs — from an ordinary call cty.dat handles fine.
|
||||
func (db *DB) HasException(call string) bool {
|
||||
if db == nil {
|
||||
return false
|
||||
}
|
||||
c := strings.ToUpper(strings.TrimSpace(call))
|
||||
for _, key := range candidates(c) {
|
||||
if len(db.exceptions[key]) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// candidates yields the call and a version with one trailing affix removed.
|
||||
func candidates(c string) []string {
|
||||
out := []string{c}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package clublog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func d(s string) time.Time {
|
||||
t, _ := time.Parse("2006-01-02", s)
|
||||
return t
|
||||
}
|
||||
|
||||
// G1T is a contest call: Scotland ONLY from 2024-02-21; before that it's England
|
||||
// by prefix. cty.dat's date-blind "=G1T → Scotland" override is wrong for a 2012
|
||||
// QSO — the date-aware ClubLog cascade must give England then, Scotland now.
|
||||
func TestG1TDatedException(t *testing.T) {
|
||||
db := &DB{
|
||||
exceptions: map[string][]Exception{
|
||||
"G1T": {{Call: "G1T", Entity: "Scotland", ADIF: 279, Start: d("2024-02-21")}},
|
||||
},
|
||||
prefixes: map[string][]Exception{
|
||||
"G": {{Entity: "England", ADIF: 223}},
|
||||
},
|
||||
}
|
||||
|
||||
// 2012 QSO: exception doesn't cover it, but the call HAS an exception →
|
||||
// caller should use the prefix (England).
|
||||
if _, ok := db.Resolve("G1T", d("2012-07-29")); ok {
|
||||
t.Fatalf("2012: exception should NOT cover a pre-2024 date")
|
||||
}
|
||||
if !db.HasException("G1T") {
|
||||
t.Fatalf("HasException(G1T) should be true")
|
||||
}
|
||||
pe, pok := db.ResolvePrefix("G1T", d("2012-07-29"))
|
||||
if !pok || pe.Entity != "England" {
|
||||
t.Fatalf("2012 prefix: got %q ok=%v, want England", pe.Entity, pok)
|
||||
}
|
||||
|
||||
// 2025 QSO: the exception covers it → Scotland.
|
||||
e, ok := db.Resolve("G1T", d("2025-01-01"))
|
||||
if !ok || e.Entity != "Scotland" {
|
||||
t.Fatalf("2025: got %q ok=%v, want Scotland", e.Entity, ok)
|
||||
}
|
||||
|
||||
// An ordinary call with no exception → HasException false (caller leaves it
|
||||
// to cty.dat).
|
||||
if db.HasException("G4ABC") {
|
||||
t.Fatalf("HasException(G4ABC) should be false")
|
||||
}
|
||||
}
|
||||
@@ -125,3 +125,19 @@ func (m *Manager) Resolve(call string, date time.Time) (Exception, bool) {
|
||||
}
|
||||
return db.Resolve(call, date)
|
||||
}
|
||||
|
||||
// ResolvePrefix resolves a call by ClubLog's date-ranged PREFIX table.
|
||||
func (m *Manager) ResolvePrefix(call string, date time.Time) (Exception, bool) {
|
||||
m.mu.RLock()
|
||||
db := m.db
|
||||
m.mu.RUnlock()
|
||||
return db.ResolvePrefix(call, date)
|
||||
}
|
||||
|
||||
// HasException reports whether the call has any full-call exception (any date).
|
||||
func (m *Manager) HasException(call string) bool {
|
||||
m.mu.RLock()
|
||||
db := m.db
|
||||
m.mu.RUnlock()
|
||||
return db.HasException(call)
|
||||
}
|
||||
|
||||
+13
-5
@@ -339,17 +339,25 @@ func normalizeCallsign(s string) string {
|
||||
// which can change the DXCC entity (HD5MW/8 → HD8MW → Galápagos, not
|
||||
// Ecuador). This is the same class of rule as KG4 and /MM.
|
||||
if areaDigit != 0 {
|
||||
main = replaceFirstDigit(main, areaDigit)
|
||||
main = replaceAreaDigit(main, areaDigit)
|
||||
}
|
||||
return main
|
||||
}
|
||||
|
||||
// replaceFirstDigit substitutes the first 0-9 digit of a call with d (used to
|
||||
// apply a "/N" call-area change). Returns the call unchanged if it has no digit.
|
||||
func replaceFirstDigit(call string, d byte) string {
|
||||
// replaceAreaDigit substitutes the CALL-AREA digit of a call with d (used to
|
||||
// apply a "/N" call-area change). The area digit is the first digit that comes
|
||||
// AFTER a prefix letter — NOT a leading digit that is part of a digit-first
|
||||
// prefix (7X, 3A, 4X, 9A, 2E…). So 7X2ARA/4 → 7X4ARA (still Algeria, area 4),
|
||||
// NOT 4X2ARA (which is Israel — the old first-digit bug). Returns the call
|
||||
// unchanged if it has no such digit.
|
||||
func replaceAreaDigit(call string, d byte) string {
|
||||
b := []byte(call)
|
||||
seenLetter := false
|
||||
for i := range b {
|
||||
if b[i] >= '0' && b[i] <= '9' {
|
||||
switch {
|
||||
case b[i] >= 'A' && b[i] <= 'Z':
|
||||
seenLetter = true
|
||||
case b[i] >= '0' && b[i] <= '9' && seenLetter:
|
||||
b[i] = d
|
||||
return string(b)
|
||||
}
|
||||
|
||||
@@ -219,6 +219,9 @@ func TestNormalize(t *testing.T) {
|
||||
"F4BPO/M": "F4BPO", // plain mobile keeps the home entity
|
||||
"F4BPO/5": "F5BPO", // "/5" re-homes to call area 5
|
||||
"HD5MW/8": "HD8MW", // "/8" → Galápagos call area (HD8)
|
||||
"7X2ARA/4": "7X4ARA", // "/4" changes the AREA digit (2→4), NOT the 7 of the 7X prefix (was wrongly 4X2ARA = Israel)
|
||||
"3A2ABC/6": "3A6ABC", // digit-first prefix 3A: area digit is the 2, not the 3
|
||||
"4X1AB/2": "4X2AB", // 4X (Israel) area 1→2, keep the leading 4
|
||||
"DL/F4BPO": "DL",
|
||||
"MM/KA9P": "MM", // leading MM = Scotland operating prefix
|
||||
"MM/LY3X/P": "MM",
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
// Package lotwusers tracks which callsigns are LoTW users and when they last
|
||||
// uploaded, from ARRL's public "lotw-user-activity.csv" feed. OpsLog shows a
|
||||
// colour-coded badge next to the entered callsign (recent upload = likely to
|
||||
// confirm on LoTW). The list is cached on disk so it survives restarts.
|
||||
package lotwusers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// feedURL is ARRL's public LoTW last-upload activity list (CSV:
|
||||
// "CALLSIGN,YYYY-MM-DD,HH:MM:SS", one line per registered LoTW callsign).
|
||||
const feedURL = "https://lotw.arrl.org/lotw-user-activity.csv"
|
||||
|
||||
const cacheFile = "lotw-user-activity.csv"
|
||||
|
||||
// Info is a lookup result for one callsign.
|
||||
type Info struct {
|
||||
IsUser bool `json:"is_user"`
|
||||
LastUpload string `json:"last_upload,omitempty"` // YYYY-MM-DD
|
||||
DaysAgo int `json:"days_ago"` // days since last upload (-1 if unknown)
|
||||
}
|
||||
|
||||
// Manager holds the parsed list + cache location.
|
||||
type Manager struct {
|
||||
mu sync.RWMutex
|
||||
users map[string]time.Time // UPPER(callsign) → last-upload date (UTC)
|
||||
updated time.Time // when the cache was last refreshed
|
||||
dir string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// NewManager loads any on-disk cache and returns a ready manager.
|
||||
func NewManager(dataDir string) *Manager {
|
||||
m := &Manager{
|
||||
users: map[string]time.Time{},
|
||||
dir: dataDir,
|
||||
client: &http.Client{Timeout: 90 * time.Second},
|
||||
}
|
||||
m.loadCache()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Manager) path() string { return filepath.Join(m.dir, cacheFile) }
|
||||
|
||||
func (m *Manager) loadCache() {
|
||||
data, err := os.ReadFile(m.path())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m.parse(data)
|
||||
if fi, e := os.Stat(m.path()); e == nil {
|
||||
m.mu.Lock()
|
||||
m.updated = fi.ModTime()
|
||||
m.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Download fetches the latest list, caches it to disk and replaces the in-memory
|
||||
// map. Returns the number of callsigns loaded.
|
||||
func (m *Manager) Download(ctx context.Context) (int, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, feedURL, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "OpsLog")
|
||||
resp, err := m.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("lotwusers: request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, fmt.Errorf("lotwusers: http %d", resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 64*1024*1024))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("lotwusers: read: %w", err)
|
||||
}
|
||||
n := m.parse(body)
|
||||
if n == 0 {
|
||||
return 0, fmt.Errorf("lotwusers: feed parsed to 0 callsigns")
|
||||
}
|
||||
_ = os.WriteFile(m.path(), body, 0o644) // best-effort cache
|
||||
m.mu.Lock()
|
||||
m.updated = time.Now()
|
||||
m.mu.Unlock()
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// parse loads the CSV bytes into the map and returns the count.
|
||||
func (m *Manager) parse(data []byte) int {
|
||||
users := make(map[string]time.Time, 1<<20)
|
||||
sc := bufio.NewScanner(bytes.NewReader(data))
|
||||
sc.Buffer(make([]byte, 1024*1024), 1024*1024)
|
||||
for sc.Scan() {
|
||||
line := strings.TrimSpace(sc.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(line, ",")
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
call := strings.ToUpper(strings.TrimSpace(parts[0]))
|
||||
if call == "" || strings.EqualFold(call, "callsign") {
|
||||
continue // header row
|
||||
}
|
||||
t, err := time.Parse("2006-01-02", strings.TrimSpace(parts[1]))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
users[call] = t
|
||||
}
|
||||
if len(users) == 0 {
|
||||
return 0
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.users = users
|
||||
m.mu.Unlock()
|
||||
return len(users)
|
||||
}
|
||||
|
||||
// Lookup reports whether callsign is a LoTW user and how long ago it uploaded.
|
||||
// Tries the exact call, then (for portable calls like "EA8/DL1ABC" or "F5ABC/P")
|
||||
// the longest slash-separated segment — the base call LoTW is keyed on.
|
||||
func (m *Manager) Lookup(callsign string) Info {
|
||||
c := strings.ToUpper(strings.TrimSpace(callsign))
|
||||
if c == "" {
|
||||
return Info{DaysAgo: -1}
|
||||
}
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if len(m.users) == 0 {
|
||||
return Info{DaysAgo: -1}
|
||||
}
|
||||
t, ok := m.users[c]
|
||||
if !ok && strings.Contains(c, "/") {
|
||||
base := ""
|
||||
for _, seg := range strings.Split(c, "/") {
|
||||
if len(seg) > len(base) {
|
||||
base = seg
|
||||
}
|
||||
}
|
||||
t, ok = m.users[base]
|
||||
}
|
||||
if !ok {
|
||||
return Info{DaysAgo: -1}
|
||||
}
|
||||
days := int(time.Since(t).Hours() / 24)
|
||||
if days < 0 {
|
||||
days = 0
|
||||
}
|
||||
return Info{IsUser: true, LastUpload: t.Format("2006-01-02"), DaysAgo: days}
|
||||
}
|
||||
|
||||
// Count returns how many callsigns are loaded.
|
||||
func (m *Manager) Count() int {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return len(m.users)
|
||||
}
|
||||
|
||||
// Updated returns when the list was last refreshed (zero if never).
|
||||
func (m *Manager) Updated() time.Time {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return m.updated
|
||||
}
|
||||
Reference in New Issue
Block a user