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
|
|
@ -12,7 +12,7 @@ import (
|
|||
type CallsignManager struct {
|
||||
db *sql.DB
|
||||
mutex sync.RWMutex
|
||||
|
||||
|
||||
// Compiled regex patterns for callsign parsing
|
||||
airlinePattern *regexp.Regexp
|
||||
flightPattern *regexp.Regexp
|
||||
|
|
@ -42,14 +42,14 @@ func (cm *CallsignManager) ParseCallsign(callsign string) *CallsignParseResult {
|
|||
ParsedTime: time.Now(),
|
||||
IsValid: false,
|
||||
}
|
||||
|
||||
|
||||
if callsign == "" {
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// Clean and normalize the callsign
|
||||
normalized := strings.TrimSpace(strings.ToUpper(callsign))
|
||||
|
||||
|
||||
// Try airline pattern first (most common for commercial flights)
|
||||
if matches := cm.airlinePattern.FindStringSubmatch(normalized); len(matches) == 3 {
|
||||
result.AirlineCode = matches[1]
|
||||
|
|
@ -57,7 +57,7 @@ func (cm *CallsignManager) ParseCallsign(callsign string) *CallsignParseResult {
|
|||
result.IsValid = true
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// Fall back to general flight pattern
|
||||
if matches := cm.flightPattern.FindStringSubmatch(normalized); len(matches) == 3 {
|
||||
result.AirlineCode = matches[1]
|
||||
|
|
@ -65,24 +65,24 @@ func (cm *CallsignManager) ParseCallsign(callsign string) *CallsignParseResult {
|
|||
result.IsValid = true
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (cm *CallsignManager) GetCallsignInfo(callsign string) (*CallsignInfo, error) {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
|
||||
if callsign == "" {
|
||||
return nil, fmt.Errorf("empty callsign")
|
||||
}
|
||||
|
||||
|
||||
// First check the cache
|
||||
cached, err := cm.getCallsignFromCache(callsign)
|
||||
if err == nil && cached != nil {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
|
||||
// Parse the callsign
|
||||
parsed := cm.ParseCallsign(callsign)
|
||||
if !parsed.IsValid {
|
||||
|
|
@ -91,13 +91,13 @@ func (cm *CallsignManager) GetCallsignInfo(callsign string) (*CallsignInfo, erro
|
|||
IsValid: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
// Look up airline information
|
||||
airline, err := cm.getAirlineByCode(parsed.AirlineCode)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("failed to lookup airline %s: %w", parsed.AirlineCode, err)
|
||||
}
|
||||
|
||||
|
||||
// Build the result
|
||||
info := &CallsignInfo{
|
||||
OriginalCallsign: callsign,
|
||||
|
|
@ -106,7 +106,7 @@ func (cm *CallsignManager) GetCallsignInfo(callsign string) (*CallsignInfo, erro
|
|||
IsValid: true,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
|
||||
if airline != nil {
|
||||
info.AirlineName = airline.Name
|
||||
info.AirlineCountry = airline.Country
|
||||
|
|
@ -114,7 +114,7 @@ func (cm *CallsignManager) GetCallsignInfo(callsign string) (*CallsignInfo, erro
|
|||
} else {
|
||||
info.DisplayName = fmt.Sprintf("%s %s", parsed.AirlineCode, parsed.FlightNumber)
|
||||
}
|
||||
|
||||
|
||||
// Cache the result (fire and forget)
|
||||
go func() {
|
||||
if err := cm.cacheCallsignInfo(info); err != nil {
|
||||
|
|
@ -122,7 +122,7 @@ func (cm *CallsignManager) GetCallsignInfo(callsign string) (*CallsignInfo, erro
|
|||
fmt.Printf("Warning: failed to cache callsign info for %s: %v\n", callsign, err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
|
|
@ -133,10 +133,10 @@ func (cm *CallsignManager) getCallsignFromCache(callsign string) (*CallsignInfo,
|
|||
FROM callsign_cache
|
||||
WHERE callsign = ? AND expires_at > datetime('now')
|
||||
`
|
||||
|
||||
|
||||
var info CallsignInfo
|
||||
var cacheExpires time.Time
|
||||
|
||||
|
||||
err := cm.db.QueryRow(query, callsign).Scan(
|
||||
&info.OriginalCallsign,
|
||||
&info.AirlineCode,
|
||||
|
|
@ -148,25 +148,25 @@ func (cm *CallsignManager) getCallsignFromCache(callsign string) (*CallsignInfo,
|
|||
&info.LastUpdated,
|
||||
&cacheExpires,
|
||||
)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (cm *CallsignManager) cacheCallsignInfo(info *CallsignInfo) error {
|
||||
// Cache for 24 hours by default
|
||||
cacheExpires := time.Now().Add(24 * time.Hour)
|
||||
|
||||
|
||||
query := `
|
||||
INSERT OR REPLACE INTO callsign_cache
|
||||
(callsign, airline_icao, flight_number, airline_name,
|
||||
airline_country, cached_at, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
|
||||
_, err := cm.db.Exec(query,
|
||||
info.OriginalCallsign,
|
||||
info.AirlineCode,
|
||||
|
|
@ -176,7 +176,7 @@ func (cm *CallsignManager) cacheCallsignInfo(info *CallsignInfo) error {
|
|||
info.LastUpdated,
|
||||
cacheExpires,
|
||||
)
|
||||
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -190,7 +190,7 @@ func (cm *CallsignManager) getAirlineByCode(code string) (*AirlineRecord, error)
|
|||
name
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
|
||||
var airline AirlineRecord
|
||||
err := cm.db.QueryRow(query, code, code, code).Scan(
|
||||
&airline.ICAOCode,
|
||||
|
|
@ -199,31 +199,31 @@ func (cm *CallsignManager) getAirlineByCode(code string) (*AirlineRecord, error)
|
|||
&airline.Country,
|
||||
&airline.Active,
|
||||
)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
return &airline, nil
|
||||
}
|
||||
|
||||
func (cm *CallsignManager) GetAirlinesByCountry(country string) ([]AirlineRecord, error) {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
|
||||
query := `
|
||||
SELECT icao_code, iata_code, name, country, active
|
||||
FROM airlines
|
||||
WHERE country = ? AND active = 1
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
|
||||
rows, err := cm.db.Query(query, country)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var airlines []AirlineRecord
|
||||
for rows.Next() {
|
||||
var airline AirlineRecord
|
||||
|
|
@ -239,14 +239,14 @@ func (cm *CallsignManager) GetAirlinesByCountry(country string) ([]AirlineRecord
|
|||
}
|
||||
airlines = append(airlines, airline)
|
||||
}
|
||||
|
||||
|
||||
return airlines, rows.Err()
|
||||
}
|
||||
|
||||
func (cm *CallsignManager) SearchAirlines(query string) ([]AirlineRecord, error) {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
|
||||
searchQuery := `
|
||||
SELECT icao_code, iata_code, name, country, active
|
||||
FROM airlines
|
||||
|
|
@ -265,11 +265,11 @@ func (cm *CallsignManager) SearchAirlines(query string) ([]AirlineRecord, error)
|
|||
name
|
||||
LIMIT 50
|
||||
`
|
||||
|
||||
|
||||
searchTerm := "%" + strings.ToUpper(query) + "%"
|
||||
exactTerm := strings.ToUpper(query)
|
||||
|
||||
rows, err := cm.db.Query(searchQuery,
|
||||
|
||||
rows, err := cm.db.Query(searchQuery,
|
||||
searchTerm, searchTerm, searchTerm, searchTerm,
|
||||
exactTerm, exactTerm, exactTerm,
|
||||
)
|
||||
|
|
@ -277,7 +277,7 @@ func (cm *CallsignManager) SearchAirlines(query string) ([]AirlineRecord, error)
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var airlines []AirlineRecord
|
||||
for rows.Next() {
|
||||
var airline AirlineRecord
|
||||
|
|
@ -293,14 +293,14 @@ func (cm *CallsignManager) SearchAirlines(query string) ([]AirlineRecord, error)
|
|||
}
|
||||
airlines = append(airlines, airline)
|
||||
}
|
||||
|
||||
|
||||
return airlines, rows.Err()
|
||||
}
|
||||
|
||||
func (cm *CallsignManager) ClearExpiredCache() error {
|
||||
cm.mutex.Lock()
|
||||
defer cm.mutex.Unlock()
|
||||
|
||||
|
||||
query := `DELETE FROM callsign_cache WHERE expires_at <= datetime('now')`
|
||||
_, err := cm.db.Exec(query)
|
||||
return err
|
||||
|
|
@ -309,9 +309,9 @@ func (cm *CallsignManager) ClearExpiredCache() error {
|
|||
func (cm *CallsignManager) GetCacheStats() (map[string]interface{}, error) {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
|
||||
stats := make(map[string]interface{})
|
||||
|
||||
|
||||
// Total cached entries
|
||||
var totalCached int
|
||||
err := cm.db.QueryRow(`SELECT COUNT(*) FROM callsign_cache`).Scan(&totalCached)
|
||||
|
|
@ -319,7 +319,7 @@ func (cm *CallsignManager) GetCacheStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["total_cached"] = totalCached
|
||||
|
||||
|
||||
// Valid (non-expired) entries
|
||||
var validCached int
|
||||
err = cm.db.QueryRow(`SELECT COUNT(*) FROM callsign_cache WHERE expires_at > datetime('now')`).Scan(&validCached)
|
||||
|
|
@ -327,10 +327,10 @@ func (cm *CallsignManager) GetCacheStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["valid_cached"] = validCached
|
||||
|
||||
|
||||
// Expired entries
|
||||
stats["expired_cached"] = totalCached - validCached
|
||||
|
||||
|
||||
// Total airlines in database
|
||||
var totalAirlines int
|
||||
err = cm.db.QueryRow(`SELECT COUNT(*) FROM airlines WHERE active = 1`).Scan(&totalAirlines)
|
||||
|
|
@ -338,7 +338,7 @@ func (cm *CallsignManager) GetCacheStats() (map[string]interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
stats["total_airlines"] = totalAirlines
|
||||
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
|
|
@ -349,14 +349,14 @@ func (cm *CallsignManager) LoadEmbeddedData() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
if count > 0 {
|
||||
// Data already loaded
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// For now, we'll implement this as a placeholder
|
||||
// In a full implementation, this would load embedded airline data
|
||||
// from embedded files or resources
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue