style: Apply code formatting with go fmt
- Run 'make format' to ensure all Go code follows standard formatting - Maintains consistent code style across the entire codebase - No functional changes, only whitespace and formatting improvements
This commit is contained in:
parent
4fd0846127
commit
2bffa2c418
19 changed files with 543 additions and 527 deletions
|
|
@ -10,7 +10,7 @@ import (
|
|||
type HistoryManager struct {
|
||||
db *sql.DB
|
||||
mutex sync.RWMutex
|
||||
|
||||
|
||||
// Configuration
|
||||
maxHistoryDays int
|
||||
cleanupTicker *time.Ticker
|
||||
|
|
@ -23,11 +23,11 @@ func NewHistoryManager(db *sql.DB, maxHistoryDays int) *HistoryManager {
|
|||
maxHistoryDays: maxHistoryDays,
|
||||
stopCleanup: make(chan bool),
|
||||
}
|
||||
|
||||
|
||||
// Start periodic cleanup (every hour)
|
||||
hm.cleanupTicker = time.NewTicker(1 * time.Hour)
|
||||
go hm.periodicCleanup()
|
||||
|
||||
|
||||
return hm
|
||||
}
|
||||
|
||||
|
|
@ -56,14 +56,14 @@ func (hm *HistoryManager) periodicCleanup() {
|
|||
func (hm *HistoryManager) RecordAircraft(record *AircraftHistoryRecord) error {
|
||||
hm.mutex.Lock()
|
||||
defer hm.mutex.Unlock()
|
||||
|
||||
|
||||
query := `
|
||||
INSERT INTO aircraft_history
|
||||
(icao, callsign, squawk, latitude, longitude, altitude,
|
||||
vertical_rate, speed, track, source_id, signal_strength, timestamp)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
|
||||
_, err := hm.db.Exec(query,
|
||||
record.ICAO,
|
||||
record.Callsign,
|
||||
|
|
@ -78,7 +78,7 @@ func (hm *HistoryManager) RecordAircraft(record *AircraftHistoryRecord) error {
|
|||
record.SignalStrength,
|
||||
record.Timestamp,
|
||||
)
|
||||
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -86,16 +86,16 @@ func (hm *HistoryManager) RecordAircraftBatch(records []AircraftHistoryRecord) e
|
|||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
hm.mutex.Lock()
|
||||
defer hm.mutex.Unlock()
|
||||
|
||||
|
||||
tx, err := hm.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
||||
stmt, err := tx.Prepare(`
|
||||
INSERT INTO aircraft_history
|
||||
(icao, callsign, squawk, latitude, longitude, altitude,
|
||||
|
|
@ -106,7 +106,7 @@ func (hm *HistoryManager) RecordAircraftBatch(records []AircraftHistoryRecord) e
|
|||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
|
||||
for _, record := range records {
|
||||
_, err := stmt.Exec(
|
||||
record.ICAO,
|
||||
|
|
@ -126,16 +126,16 @@ func (hm *HistoryManager) RecordAircraftBatch(records []AircraftHistoryRecord) e
|
|||
return fmt.Errorf("failed to insert record for ICAO %s: %w", record.ICAO, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (hm *HistoryManager) GetAircraftHistory(icao string, hours int) ([]AircraftHistoryRecord, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
since := time.Now().Add(-time.Duration(hours) * time.Hour)
|
||||
|
||||
|
||||
query := `
|
||||
SELECT icao, callsign, squawk, latitude, longitude, altitude,
|
||||
vertical_rate, speed, track, source_id, signal_strength, timestamp
|
||||
|
|
@ -144,13 +144,13 @@ func (hm *HistoryManager) GetAircraftHistory(icao string, hours int) ([]Aircraft
|
|||
ORDER BY timestamp DESC
|
||||
LIMIT 1000
|
||||
`
|
||||
|
||||
|
||||
rows, err := hm.db.Query(query, icao, since)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var records []AircraftHistoryRecord
|
||||
for rows.Next() {
|
||||
var record AircraftHistoryRecord
|
||||
|
|
@ -173,16 +173,16 @@ func (hm *HistoryManager) GetAircraftHistory(icao string, hours int) ([]Aircraft
|
|||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
|
||||
return records, rows.Err()
|
||||
}
|
||||
|
||||
func (hm *HistoryManager) GetAircraftTrack(icao string, hours int) ([]TrackPoint, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
since := time.Now().Add(-time.Duration(hours) * time.Hour)
|
||||
|
||||
|
||||
query := `
|
||||
SELECT latitude, longitude, altitude, timestamp
|
||||
FROM aircraft_history
|
||||
|
|
@ -191,13 +191,13 @@ func (hm *HistoryManager) GetAircraftTrack(icao string, hours int) ([]TrackPoint
|
|||
ORDER BY timestamp ASC
|
||||
LIMIT 500
|
||||
`
|
||||
|
||||
|
||||
rows, err := hm.db.Query(query, icao, since)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var track []TrackPoint
|
||||
for rows.Next() {
|
||||
var point TrackPoint
|
||||
|
|
@ -212,16 +212,16 @@ func (hm *HistoryManager) GetAircraftTrack(icao string, hours int) ([]TrackPoint
|
|||
}
|
||||
track = append(track, point)
|
||||
}
|
||||
|
||||
|
||||
return track, rows.Err()
|
||||
}
|
||||
|
||||
func (hm *HistoryManager) GetRecentAircraft(hours int, limit int) ([]string, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
since := time.Now().Add(-time.Duration(hours) * time.Hour)
|
||||
|
||||
|
||||
query := `
|
||||
SELECT DISTINCT icao
|
||||
FROM aircraft_history
|
||||
|
|
@ -229,13 +229,13 @@ func (hm *HistoryManager) GetRecentAircraft(hours int, limit int) ([]string, err
|
|||
ORDER BY MAX(timestamp) DESC
|
||||
LIMIT ?
|
||||
`
|
||||
|
||||
|
||||
rows, err := hm.db.Query(query, since, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var icaos []string
|
||||
for rows.Next() {
|
||||
var icao string
|
||||
|
|
@ -245,20 +245,20 @@ func (hm *HistoryManager) GetRecentAircraft(hours int, limit int) ([]string, err
|
|||
}
|
||||
icaos = append(icaos, icao)
|
||||
}
|
||||
|
||||
|
||||
return icaos, rows.Err()
|
||||
}
|
||||
|
||||
func (hm *HistoryManager) GetAircraftLastSeen(icao string) (time.Time, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
query := `
|
||||
SELECT MAX(timestamp)
|
||||
FROM aircraft_history
|
||||
WHERE icao = ?
|
||||
`
|
||||
|
||||
|
||||
var lastSeen time.Time
|
||||
err := hm.db.QueryRow(query, icao).Scan(&lastSeen)
|
||||
return lastSeen, err
|
||||
|
|
@ -267,24 +267,24 @@ func (hm *HistoryManager) GetAircraftLastSeen(icao string) (time.Time, error) {
|
|||
func (hm *HistoryManager) CleanupOldHistory() error {
|
||||
hm.mutex.Lock()
|
||||
defer hm.mutex.Unlock()
|
||||
|
||||
|
||||
if hm.maxHistoryDays <= 0 {
|
||||
return nil // No cleanup if maxHistoryDays is 0 or negative
|
||||
}
|
||||
|
||||
|
||||
cutoff := time.Now().AddDate(0, 0, -hm.maxHistoryDays)
|
||||
|
||||
|
||||
query := `DELETE FROM aircraft_history WHERE timestamp < ?`
|
||||
result, err := hm.db.Exec(query, cutoff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err == nil && rowsAffected > 0 {
|
||||
fmt.Printf("Cleaned up %d old aircraft history records\n", rowsAffected)
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -295,9 +295,9 @@ func (hm *HistoryManager) GetStatistics() (map[string]interface{}, error) {
|
|||
func (hm *HistoryManager) GetHistoryStats() (map[string]interface{}, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
stats := make(map[string]interface{})
|
||||
|
||||
|
||||
// Total records
|
||||
var totalRecords int
|
||||
err := hm.db.QueryRow(`SELECT COUNT(*) FROM aircraft_history`).Scan(&totalRecords)
|
||||
|
|
@ -305,7 +305,7 @@ func (hm *HistoryManager) GetHistoryStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["total_records"] = totalRecords
|
||||
|
||||
|
||||
// Unique aircraft
|
||||
var uniqueAircraft int
|
||||
err = hm.db.QueryRow(`SELECT COUNT(DISTINCT icao) FROM aircraft_history`).Scan(&uniqueAircraft)
|
||||
|
|
@ -313,7 +313,7 @@ func (hm *HistoryManager) GetHistoryStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["unique_aircraft"] = uniqueAircraft
|
||||
|
||||
|
||||
// Recent records (last 24 hours)
|
||||
var recentRecords int
|
||||
since := time.Now().Add(-24 * time.Hour)
|
||||
|
|
@ -322,7 +322,7 @@ func (hm *HistoryManager) GetHistoryStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["recent_records_24h"] = recentRecords
|
||||
|
||||
|
||||
// Oldest and newest record timestamps (only if records exist)
|
||||
if totalRecords > 0 {
|
||||
var oldestTimestamp, newestTimestamp time.Time
|
||||
|
|
@ -333,18 +333,18 @@ func (hm *HistoryManager) GetHistoryStats() (map[string]interface{}, error) {
|
|||
stats["history_days"] = int(time.Since(oldestTimestamp).Hours() / 24)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (hm *HistoryManager) GetActivitySummary(hours int) (map[string]interface{}, error) {
|
||||
hm.mutex.RLock()
|
||||
defer hm.mutex.RUnlock()
|
||||
|
||||
|
||||
since := time.Now().Add(-time.Duration(hours) * time.Hour)
|
||||
|
||||
|
||||
summary := make(map[string]interface{})
|
||||
|
||||
|
||||
// Aircraft count in time period
|
||||
var aircraftCount int
|
||||
err := hm.db.QueryRow(`
|
||||
|
|
@ -356,7 +356,7 @@ func (hm *HistoryManager) GetActivitySummary(hours int) (map[string]interface{},
|
|||
return nil, err
|
||||
}
|
||||
summary["aircraft_count"] = aircraftCount
|
||||
|
||||
|
||||
// Message count in time period
|
||||
var messageCount int
|
||||
err = hm.db.QueryRow(`
|
||||
|
|
@ -368,7 +368,7 @@ func (hm *HistoryManager) GetActivitySummary(hours int) (map[string]interface{},
|
|||
return nil, err
|
||||
}
|
||||
summary["message_count"] = messageCount
|
||||
|
||||
|
||||
// Most active sources
|
||||
query := `
|
||||
SELECT source_id, COUNT(*) as count
|
||||
|
|
@ -378,13 +378,13 @@ func (hm *HistoryManager) GetActivitySummary(hours int) (map[string]interface{},
|
|||
ORDER BY count DESC
|
||||
LIMIT 5
|
||||
`
|
||||
|
||||
|
||||
rows, err := hm.db.Query(query, since)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
sources := make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
var sourceID string
|
||||
|
|
@ -399,7 +399,7 @@ func (hm *HistoryManager) GetActivitySummary(hours int) (map[string]interface{},
|
|||
})
|
||||
}
|
||||
summary["top_sources"] = sources
|
||||
|
||||
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
|
|
@ -408,4 +408,4 @@ type TrackPoint struct {
|
|||
Longitude float64 `json:"longitude"`
|
||||
Altitude *int `json:"altitude,omitempty"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue