Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions core/aircraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,23 @@ func (pg *postgres) updateDatabase(nowEpoch float64, aircrafts []Aircraft) {
func getAircraftsRecentlySeen(pg *postgres, nowEpoch float64, aircrafts []Aircraft) map[string]*Aircraft {

existingAircrafts := make(map[string]*Aircraft)
now := time.Now()
newExpiry := now.Add(10 * time.Minute)

var uncachedHexes []string

var hexValues []string
for _, a := range aircrafts {
hexValues = append(hexValues, a.Hex)
entry, ok := pg.recentAircraftCache[a.Hex]
if ok && now.Before(entry.expiresAt) {
entry.expiresAt = newExpiry
existingAircrafts[a.Hex] = &entry.aircraft
} else {
uncachedHexes = append(uncachedHexes, a.Hex)
}
}

if len(uncachedHexes) == 0 {
return existingAircrafts
}

recentThreshold := int(nowEpoch) - 600
Expand All @@ -114,39 +127,43 @@ func getAircraftsRecentlySeen(pg *postgres, nowEpoch float64, aircrafts []Aircra
ias,
tas
FROM aircraft_data
WHERE hex = ANY($1::text[])
WHERE hex = ANY($1::text[])
AND last_seen_epoch > $2
ORDER BY hex, last_seen DESC;
`

rows, err := pg.db.Query(context.Background(), query, hexValues, recentThreshold)
rows, err := pg.db.Query(context.Background(), query, uncachedHexes, recentThreshold)
if err != nil {
fmt.Println("getAircraftsRecentlySeen() - Error querying db: ", err)
return nil
}
defer rows.Close()

for rows.Next() {
var existingAircraft Aircraft
var a Aircraft
err := rows.Scan(
&existingAircraft.Id,
&existingAircraft.Hex,
&existingAircraft.LastSeenEpoch,
&existingAircraft.LastSeenLat,
&existingAircraft.LastSeenLon,
&existingAircraft.LastSeenDistance,
&existingAircraft.AltBaro,
&existingAircraft.AltGeom,
&existingAircraft.Gs,
&existingAircraft.Ias,
&existingAircraft.Tas)
&a.Id,
&a.Hex,
&a.LastSeenEpoch,
&a.LastSeenLat,
&a.LastSeenLon,
&a.LastSeenDistance,
&a.AltBaro,
&a.AltGeom,
&a.Gs,
&a.Ias,
&a.Tas)

if err != nil {
log.Error().Err(err).Msg("getAircraftsRecentlySeen() - error scanning rows")
continue
}

existingAircrafts[existingAircraft.Hex] = &existingAircraft
pg.recentAircraftCache[a.Hex] = &aircraftCacheEntry{
aircraft: a,
expiresAt: newExpiry,
}
existingAircrafts[a.Hex] = &pg.recentAircraftCache[a.Hex].aircraft
}

return existingAircrafts
Expand Down
14 changes: 12 additions & 2 deletions core/db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import (
"context"
"os"
"sync"
"time"

"github.com/jackc/pgx/v5/pgxpool"
"github.com/rs/zerolog/log"
)

type aircraftCacheEntry struct {
aircraft Aircraft
expiresAt time.Time
}

type postgres struct {
db *pgxpool.Pool
db *pgxpool.Pool
recentAircraftCache map[string]*aircraftCacheEntry
}

var (
Expand All @@ -25,7 +32,10 @@ func NewPG(ctx context.Context, connString string) (*postgres, error) {
log.Error().Err(err).Msg("Unable to connect to database")
}

pgInstance = &postgres{db}
pgInstance = &postgres{
db: db,
recentAircraftCache: make(map[string]*aircraftCacheEntry),
}
})

return pgInstance, nil
Expand Down
Loading