Files
go-eve-pi/routes/data.go

218 lines
6.4 KiB
Go

// Package routes provides data processing functions for EVE PI endpoints.
package routes
import (
"context"
"fmt"
"time"
"go-eve-pi/constants"
"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) ([]ExtractorInfo, error) {
logger.Debug("Getting extractors for character ID %d", characterID)
// Get character planets
planets, err := esiClient.GetCharacterPlanets(context.Background(), characterID, accessToken)
if err != nil {
logger.Error("Failed to get planets for character %d: %v", characterID, err)
return nil, err
}
var extractors []ExtractorInfo
for _, planet := range planets {
// Get planet details
details, err := esiClient.GetPlanetDetails(context.Background(), characterID, planet.PlanetID, accessToken)
if err != nil {
logger.Warning("Failed to fetch details for planet %d: %v", planet.PlanetID, err)
continue
}
if details != nil {
// Get planet name from universe endpoint
planetNameData, err := esiClient.GetPlanetName(context.Background(), planet.PlanetID)
if err != nil {
logger.Error("Failed to get planet name for planet ID %d: %v", planet.PlanetID, err)
continue
}
// Count extractors and get expiry dates
extractorCount := 0
for _, pin := range details.Pins {
if pin.ExtractorDetails != nil {
extractorCount++
expiryDate := "N/A"
if pin.ExpiryTime != nil {
expiryDate = *pin.ExpiryTime
}
extractors = append(extractors, ExtractorInfo{
PlanetName: planetNameData.Name,
ExtractorNumber: extractorCount,
ExpiryDate: expiryDate,
})
}
}
}
}
return extractors, nil
}
// GetStorageForCharacter retrieves storage information for a character
func GetStorageForCharacter(esiClient esi.ESIInterface, characterID int, accessToken string) ([]StorageInfo, error) {
logger.Debug("Getting storage for character ID %d", characterID)
// Get character planets
planets, err := esiClient.GetCharacterPlanets(context.Background(), characterID, accessToken)
if err != nil {
logger.Error("Failed to get planets for character %d: %v", characterID, err)
return nil, err
}
var storage []StorageInfo
for _, planet := range planets {
// Get planet details
details, err := esiClient.GetPlanetDetails(context.Background(), characterID, planet.PlanetID, accessToken)
if err != nil {
logger.Warning("Failed to fetch details for planet %d: %v", planet.PlanetID, err)
continue
}
if details != nil {
// Get planet name from universe endpoint
planetNameData, err := esiClient.GetPlanetName(context.Background(), planet.PlanetID)
if err != nil {
logger.Error("Failed to get planet name for planet ID %d: %v", planet.PlanetID, err)
continue
}
// Analyze storage utilization
for _, pin := range details.Pins {
// Check if this pin is actually a storage facility
_, isStorage := constants.StorageCapacities[pin.TypeID]
if !isStorage {
// Skip non-storage facilities (like industry facilities)
continue
}
totalVolume := 0.0
for _, content := range pin.Contents {
volume, exists := constants.PIProductVolumes[content.TypeID]
if !exists {
logger.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)
}
totalVolume += float64(content.Amount) * volume
}
storageType, exists := constants.PI_TYPES_MAP[pin.TypeID]
if !exists {
logger.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)
}
storageCapacity := constants.StorageCapacities[pin.TypeID]
utilization := (totalVolume / float64(storageCapacity)) * 100.0
storage = append(storage, StorageInfo{
PlanetName: planetNameData.Name,
StorageType: storageType,
Utilization: utilization,
})
}
}
}
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("2006-01-02 15:04:05", e.ExpiryDate)
if err != nil {
logger.Warning("Failed to parse expiry date %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)
}