Files
go-eve-pi/routes/data.go
2025-10-11 11:05:06 +02:00

254 lines
8.9 KiB
Go

// Package routes provides data processing functions for EVE PI endpoints.
package routes
import (
"context"
"fmt"
"time"
"go-eve-pi/esi"
logger "git.site.quack-lab.dev/dave/cylogger"
)
// ExtractorInfo represents extractor information for a planet
type ExtractorInfo struct {
PlanetName string `json:"planet_name"`
ExtractorNumber int `json:"extractor_number"`
ExpiryDate string `json:"expiry_date"`
}
// StorageInfo represents storage information for a planet
type StorageInfo struct {
PlanetName string `json:"planet_name"`
StorageType string `json:"storage_type"`
Utilization float64 `json:"utilization"`
}
// GetExtractorsForCharacter retrieves extractor information for a character
func GetExtractorsForCharacter(esiClient esi.ESIInterface, characterID int, accessToken string, planetIDs []int64) ([]ExtractorInfo, error) {
funclog := logger.Default.WithPrefix("GetExtractorsForCharacter").WithPrefix(fmt.Sprintf("characterID=%d", characterID))
funclog.Info("Starting")
funclog.Info("Found %d planets", len(planetIDs))
var extractors []ExtractorInfo
for i, planetID := range planetIDs {
planetlog := funclog.WithPrefix(fmt.Sprintf("planetID=%d", planetID))
planetlog.Info("Processing planet %d/%d", i+1, len(planetIDs))
// Get planet details
details, err := esiClient.GetPlanetPI(context.Background(), characterID, planetID, accessToken)
if err != nil {
planetlog.Warning("Failed to fetch details: %v", err)
continue
}
if details == nil {
planetlog.Warning("Planet details are nil")
continue
}
if details.Pins == nil {
planetlog.Warning("Planet details have no pins")
continue
}
planetlog.Info("Got planet details with %d pins", len(details.Pins))
// Get planet name from universe endpoint
planetNameData, err := esiClient.GetPlanetName(context.Background(), planetID)
if err != nil {
planetlog.Error("Failed to get planet name: %v", err)
continue
}
planetlog.Info("Planet name: %s", planetNameData.Name)
// Count extractors and get expiry dates
extractorCount := 0
for j, pin := range details.Pins {
pinlog := planetlog.WithPrefix(fmt.Sprintf("pinID=%d", pin.PinID)).WithPrefix(fmt.Sprintf("pinTypeID=%d", pin.TypeID))
pinlog.Info("Processing pin %d/%d", j+1, len(details.Pins))
if pin.ExpiryTime == nil {
pinlog.Info("Pin %d has no expiry time", j+1)
continue
}
pinlog.Info("Pin has expiry time: %s", pin.ExpiryTime.Format(time.RFC3339))
expiryDate := pin.ExpiryTime.Format(time.RFC3339)
pinlog.Info("Expiry date: %s", expiryDate)
extractors = append(extractors, ExtractorInfo{
PlanetName: planetNameData.Name,
ExtractorNumber: extractorCount,
ExpiryDate: expiryDate,
})
extractorCount++
pinlog.Info("Added extractor %d to results", extractorCount)
}
planetlog.Info("Found %d extractors", extractorCount)
}
funclog.Info("Returning %d total extractors", len(extractors))
return extractors, nil
}
// GetStorageForCharacter retrieves storage information for a character
// func GetStorageForCharacter(esiClient esi.ESIInterface, characterID int, accessToken string) ([]StorageInfo, error) {
// funclog := logger.Default.WithPrefix("GetStorageForCharacter").WithPrefix(fmt.Sprintf("characterID=%d", characterID))
// funclog.Info("Starting")
// // Get character planets
// planets, err := esiClient.GetCharacterPlanets(context.Background(), characterID, accessToken)
// if err != nil {
// funclog.Error("Failed to get planets: %v", err)
// return nil, err
// }
// funclog.Info("Found %d planets", len(planets))
// var storage []StorageInfo
// for i, planet := range planets {
// planetlog := funclog.WithPrefix(fmt.Sprintf("planetID=%d", planet.PlanetID))
// planetlog.Info("Processing planet %d/%d", i+1, len(planets))
// // Get planet details
// details, err := esiClient.GetPlanetPI(context.Background(), characterID, planet.PlanetID, accessToken)
// if err != nil {
// planetlog.Warning("Failed to fetch details: %v", err)
// continue
// }
// planetlog.Info("Got planet details with %d pins", len(details.Pins))
// if details == nil {
// planetlog.Warning("Planet details are nil")
// continue
// }
// // Get planet name from universe endpoint
// planetNameData, err := esiClient.GetPlanetName(context.Background(), planet.PlanetID)
// if err != nil {
// planetlog.Error("Failed to get planet name: %v", err)
// continue
// }
// planetlog = planetlog.WithPrefix(fmt.Sprintf("planetName=%s", planetNameData.Name))
// planetlog.Info("Planet name: %s", planetNameData.Name)
// // Analyze storage utilization
// storageCount := 0
// for j, pin := range details.Pins {
// planetlog.Info("Processing pin %d/%d (TypeID: %d) on planet %s", j+1, len(details.Pins), pin.TypeID, planetNameData.Name)
// // Check if this pin is actually a storage facility
// _, isStorage := constants.StorageCapacities[pin.TypeID]
// if !isStorage {
// planetlog.Info("Pin %d is not a storage facility (TypeID: %d), skipping", j+1, pin.TypeID)
// continue
// }
// planetlog.Info("Pin %d is a storage facility (TypeID: %d)", j+1, pin.TypeID)
// storageType, exists := constants.PITypesMap[pin.TypeID]
// if !exists {
// planetlog.Error("Unknown storage type ID %d for planet %s - this should not happen", pin.TypeID, planetNameData.Name)
// return nil, fmt.Errorf("unknown storage type ID %d", pin.TypeID)
// }
// planetlog.Info("Storage type: %s", storageType)
// totalVolume := 0.0
// planetlog.Info("Processing %d contents for storage facility %s", len(pin.Contents), storageType)
// for k, content := range pin.Contents {
// planetlog.Info("Processing content %d/%d (TypeID: %d, Amount: %d)", k+1, len(pin.Contents), content.TypeID, content.Amount)
// volume, exists := constants.PIProductVolumes[content.TypeID]
// if !exists {
// planetlog.Error("Missing product volume data for type ID %d - cannot calculate storage utilization", content.TypeID)
// return nil, fmt.Errorf("missing product volume data for type ID %d", content.TypeID)
// }
// itemVolume := float64(content.Amount) * volume
// totalVolume += itemVolume
// planetlog.Info("Content TypeID %d: Amount %d, Volume %f, ItemVolume %f", content.TypeID, content.Amount, volume, itemVolume)
// }
// storageCapacity := constants.StorageCapacities[pin.TypeID]
// utilization := (totalVolume / float64(storageCapacity)) * 100.0
// planetlog.Info("Storage calculation: %s on %s - TotalVolume: %f, Capacity: %d, Utilization: %f%%", storageType, planetNameData.Name, totalVolume, storageCapacity, utilization)
// storage = append(storage, StorageInfo{
// PlanetName: planetNameData.Name,
// StorageType: storageType,
// Utilization: utilization,
// })
// storageCount++
// planetlog.Info("Added storage facility %d to results", storageCount)
// }
// planetlog.Info("Found %d storage facilities on planet %s", storageCount, planetNameData.Name)
// }
// funclog.Info("Returning %d total storage facilities", len(storage))
// return storage, nil
// }
// CheckStorageThresholds checks storage utilization against thresholds
// func CheckStorageThresholds(storage []StorageInfo, warningThreshold, criticalThreshold float64) []StorageInfo {
// var alerts []StorageInfo
// for _, s := range storage {
// if s.Utilization >= criticalThreshold {
// alerts = append(alerts, s)
// } else if s.Utilization >= warningThreshold {
// alerts = append(alerts, s)
// }
// }
// return alerts
// }
// CheckExtractorExpiry checks extractor expiry against thresholds
func CheckExtractorExpiry(extractors []ExtractorInfo, warningDuration, criticalDuration time.Duration) []ExtractorInfo {
var alerts []ExtractorInfo
now := time.Now()
for _, e := range extractors {
if e.ExpiryDate == "N/A" {
continue
}
expiryTime, err := time.Parse(time.RFC3339, e.ExpiryDate)
if err != nil {
logger.Warning("Failed to parse expiry date in ISO 8601 (RFC3339) format %s: %v", e.ExpiryDate, err)
continue
}
timeUntilExpiry := expiryTime.Sub(now)
if timeUntilExpiry <= criticalDuration {
alerts = append(alerts, e)
} else if timeUntilExpiry <= warningDuration {
alerts = append(alerts, e)
}
}
return alerts
}
// FormatStorageAlert formats a storage alert message
// func FormatStorageAlert(storage StorageInfo, isCritical bool) string {
// severity := "WARNING"
// if isCritical {
// severity = "CRITICAL"
// }
// return fmt.Sprintf("%s: %s almost full on %s (%.1f%% utilized)",
// severity, storage.StorageType, storage.PlanetName, storage.Utilization)
// }
// FormatExtractorAlert formats an extractor alert message
func FormatExtractorAlert(extractor ExtractorInfo, isCritical bool) string {
severity := "WARNING"
if isCritical {
severity = "CRITICAL"
}
status := "expiring soon"
if isCritical {
status = "expired"
}
return fmt.Sprintf("%s: Extractor #%d %s on %s (expires: %s)",
severity, extractor.ExtractorNumber, status, extractor.PlanetName, extractor.ExpiryDate)
}