// 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, planetIDs []int64) ([]ExtractorInfo, error) { logger.Info("GetExtractorsForCharacter: Starting for character ID %d", characterID) logger.Info("GetExtractorsForCharacter: Found %d planets for character %d", len(planetIDs), characterID) var extractors []ExtractorInfo for i, planetID := range planetIDs { logger.Info("GetExtractorsForCharacter: Processing planet %d/%d (ID: %d)", i+1, len(planetIDs), planetID) // Get planet details details, err := esiClient.GetPlanetPI(context.Background(), characterID, planetID, accessToken) if err != nil { logger.Warning("GetExtractorsForCharacter: Failed to fetch details for planet %d: %v", planetID, err) continue } logger.Info("GetExtractorsForCharacter: Got planet details for planet %d with %d pins", planetID, len(details.Pins)) if details != nil { // Get planet name from universe endpoint planetNameData, err := esiClient.GetPlanetName(context.Background(), planetID) if err != nil { logger.Error("GetExtractorsForCharacter: Failed to get planet name for planet ID %d: %v", planetID, err) continue } logger.Info("GetExtractorsForCharacter: Planet %d name: %s", planetID, planetNameData.Name) // Count extractors and get expiry dates extractorCount := 0 // for j, pin := range details.Pins { // logger.Info("GetExtractorsForCharacter: Processing pin %d/%d (TypeID: %d) on planet %s", j+1, len(details.Pins), pin.TypeID, planetNameData.Name) // if pin.ExtractorDetails != nil { // logger.Info("GetExtractorsForCharacter: Found extractor (TypeID: %d) on planet %s", pin.TypeID, planetNameData.Name) // extractorCount++ // expiryDate := "N/A" // if pin.ExpiryTime != nil { // expiryDate = *pin.ExpiryTime // logger.Info("GetExtractorsForCharacter: Extractor expiry: %s", expiryDate) // } else { // logger.Info("GetExtractorsForCharacter: Extractor has no expiry time") // } // extractors = append(extractors, ExtractorInfo{ // PlanetName: planetNameData.Name, // ExtractorNumber: extractorCount, // ExpiryDate: expiryDate, // }) // logger.Info("GetExtractorsForCharacter: Added extractor %d to results", extractorCount) // } else { // logger.Info("GetExtractorsForCharacter: Pin %d is not an extractor (TypeID: %d)", j+1, pin.TypeID) // } // } logger.Info("GetExtractorsForCharacter: Found %d extractors on planet %s", extractorCount, planetNameData.Name) } } logger.Info("GetExtractorsForCharacter: Returning %d total extractors for character %d", len(extractors), characterID) return extractors, nil } // GetStorageForCharacter retrieves storage information for a character func GetStorageForCharacter(esiClient esi.ESIInterface, characterID int, accessToken string) ([]StorageInfo, error) { logger.Info("GetStorageForCharacter: Starting for character ID %d", characterID) // Get character planets planets, err := esiClient.GetCharacterPlanets(context.Background(), characterID, accessToken) if err != nil { logger.Error("GetStorageForCharacter: Failed to get planets for character %d: %v", characterID, err) return nil, err } logger.Info("GetStorageForCharacter: Found %d planets for character %d", len(planets), characterID) var storage []StorageInfo for i, planet := range planets { logger.Info("GetStorageForCharacter: Processing planet %d/%d (ID: %d)", i+1, len(planets), planet.PlanetID) // Get planet details details, err := esiClient.GetPlanetPI(context.Background(), characterID, planet.PlanetID, accessToken) if err != nil { logger.Warning("GetStorageForCharacter: Failed to fetch details for planet %d: %v", planet.PlanetID, err) continue } logger.Info("GetStorageForCharacter: Got planet details for planet %d with %d pins", planet.PlanetID, len(details.Pins)) if details != nil { // Get planet name from universe endpoint planetNameData, err := esiClient.GetPlanetName(context.Background(), planet.PlanetID) if err != nil { logger.Error("GetStorageForCharacter: Failed to get planet name for planet ID %d: %v", planet.PlanetID, err) continue } logger.Info("GetStorageForCharacter: Planet %d name: %s", planet.PlanetID, planetNameData.Name) // Analyze storage utilization storageCount := 0 for j, pin := range details.Pins { logger.Info("GetStorageForCharacter: 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 { logger.Info("GetStorageForCharacter: Pin %d is not a storage facility (TypeID: %d), skipping", j+1, pin.TypeID) continue } logger.Info("GetStorageForCharacter: Pin %d is a storage facility (TypeID: %d)", j+1, pin.TypeID) storageType, exists := constants.PITypesMap[pin.TypeID] if !exists { logger.Error("GetStorageForCharacter: 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) } logger.Info("GetStorageForCharacter: Storage type: %s", storageType) totalVolume := 0.0 logger.Info("GetStorageForCharacter: Processing %d contents for storage facility %s", len(pin.Contents), storageType) for k, content := range pin.Contents { logger.Info("GetStorageForCharacter: 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 { logger.Error("GetStorageForCharacter: 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 logger.Info("GetStorageForCharacter: 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 logger.Info("GetStorageForCharacter: 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++ logger.Info("GetStorageForCharacter: Added storage facility %d to results", storageCount) } logger.Info("GetStorageForCharacter: Found %d storage facilities on planet %s", storageCount, planetNameData.Name) } } logger.Info("GetStorageForCharacter: Returning %d total storage facilities for character %d", len(storage), characterID) 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) }