diff --git a/enrich.go b/enrich.go index be2e82b..63b2b3a 100644 --- a/enrich.go +++ b/enrich.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "path/filepath" - "strings" "sync" "time" @@ -294,7 +293,11 @@ func flattenVictim(ctx context.Context, db DB, victim Victim, flat *FlatKillmail } else { flat.VictimCharacterName = name } - flog.Debug("Got character name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got character name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Character name empty (took %v)", time.Since(start)) + } return nil }) } @@ -314,7 +317,11 @@ func flattenVictim(ctx context.Context, db DB, victim Victim, flat *FlatKillmail } else { flat.VictimCorporationName = name } - flog.Debug("Got corporation name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got corporation name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Corporation name empty (took %v)", time.Since(start)) + } return nil }) } @@ -334,7 +341,11 @@ func flattenVictim(ctx context.Context, db DB, victim Victim, flat *FlatKillmail } else { flat.VictimAllianceName = name } - flog.Debug("Got alliance name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got alliance name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Alliance name empty (took %v)", time.Since(start)) + } return nil }) } @@ -419,7 +430,11 @@ func flattenAttacker(ctx context.Context, db DB, killmailID int64, attacker Atta } else { flat.CharacterName = name } - flog.Debug("Got character name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got character name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Character name empty (took %v)", time.Since(start)) + } return nil }) } @@ -439,7 +454,11 @@ func flattenAttacker(ctx context.Context, db DB, killmailID int64, attacker Atta } else { flat.CorporationName = name } - flog.Debug("Got corporation name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got corporation name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Corporation name empty (took %v)", time.Since(start)) + } return nil }) } @@ -459,7 +478,11 @@ func flattenAttacker(ctx context.Context, db DB, killmailID int64, attacker Atta } else { flat.AllianceName = name } - flog.Debug("Got alliance name: %s (took %v)", name, time.Since(start)) + if name != "" { + flog.Debug("Got alliance name: %s (took %v)", name, time.Since(start)) + } else { + flog.Debug("Alliance name empty (took %v)", time.Since(start)) + } return nil }) } @@ -660,13 +683,21 @@ func flattenMarketGroupName(ctx context.Context, db DB, typeID int32) (string, e var errNotFound = fmt.Errorf("not found") -func getCharacterName(characterID int64) (string, error) { - flog := logger.Default.WithPrefix("getCharacterName").WithPrefix(fmt.Sprintf("character_%d", characterID)) +type namedEntity interface { + GetName() string +} - esiURL := fmt.Sprintf("https://esi.evetech.net/characters/%d", characterID) +func (c Character) GetName() string { return c.Name } +func (c Corporation) GetName() string { return c.Name } +func (a Alliance) GetName() string { return a.Name } + +func getName[T namedEntity](entityType, cachePrefix string, entityID int64) (string, error) { + flog := logger.Default.WithPrefix(fmt.Sprintf("get%sName", entityType)).WithPrefix(fmt.Sprintf("%s_%d", cachePrefix, entityID)) + + esiURL := fmt.Sprintf("https://esi.evetech.net/%s/%d", cachePrefix, entityID) proxyURL := fmt.Sprintf("https://proxy.site.quack-lab.dev?url=%s", esiURL) - flog.Debug("Fetching character name from ESI") + flog.Debug("Fetching %s name from ESI", entityType) flog.Debug("ESI URL: %s", esiURL) flog.Debug("Proxy URL: %s", proxyURL) @@ -676,82 +707,25 @@ func getCharacterName(characterID int64) (string, error) { return "", fmt.Errorf("failed to create request: %w", err) } - cacheFile := filepath.Join(".cache", fmt.Sprintf("character_%d.json", characterID)) - char, err := cyutils.RequestCached[Character](req, cacheFile) + cacheFile := filepath.Join(".cache", fmt.Sprintf("%s_%d.json", cachePrefix, entityID)) + entity, err := cyutils.RequestCached[T](req, cacheFile) if err != nil { - errStr := err.Error() - if strings.Contains(errStr, "404") || strings.Contains(errStr, "not found") { - flog.Debug("Character not found (404)") - return "", fmt.Errorf("character %d: %w", characterID, errNotFound) - } - flog.Error("Failed to get character: %v", err) + flog.Error("Failed to get %s: %v", entityType, err) return "", err } - flog.Debug("Successfully got character name: %s", char.Name) - return char.Name, nil + flog.Debug("Successfully got %s name: %s", entityType, entity.GetName()) + return entity.GetName(), nil +} + +func getCharacterName(characterID int64) (string, error) { + return getName[Character]("Character", "character", characterID) } func getCorporationName(corporationID int64) (string, error) { - flog := logger.Default.WithPrefix("getCorporationName").WithPrefix(fmt.Sprintf("corporation_%d", corporationID)) - - esiURL := fmt.Sprintf("https://esi.evetech.net/corporations/%d", corporationID) - proxyURL := fmt.Sprintf("https://proxy.site.quack-lab.dev?url=%s", esiURL) - - flog.Debug("Fetching corporation name from ESI") - flog.Debug("ESI URL: %s", esiURL) - flog.Debug("Proxy URL: %s", proxyURL) - - req, err := http.NewRequest("GET", proxyURL, nil) - if err != nil { - flog.Error("Failed to create request: %v", err) - return "", fmt.Errorf("failed to create request: %w", err) - } - - cacheFile := filepath.Join(".cache", fmt.Sprintf("corporation_%d.json", corporationID)) - corp, err := cyutils.RequestCached[Corporation](req, cacheFile) - if err != nil { - errStr := err.Error() - if strings.Contains(errStr, "404") || strings.Contains(errStr, "not found") { - flog.Debug("Corporation not found (404)") - return "", fmt.Errorf("corporation %d: %w", corporationID, errNotFound) - } - flog.Error("Failed to get corporation: %v", err) - return "", err - } - - flog.Debug("Successfully got corporation name: %s", corp.Name) - return corp.Name, nil + return getName[Corporation]("Corporation", "corporation", corporationID) } func getAllianceName(allianceID int64) (string, error) { - flog := logger.Default.WithPrefix("getAllianceName").WithPrefix(fmt.Sprintf("alliance_%d", allianceID)) - - esiURL := fmt.Sprintf("https://esi.evetech.net/alliances/%d", allianceID) - proxyURL := fmt.Sprintf("https://proxy.site.quack-lab.dev?url=%s", esiURL) - - flog.Debug("Fetching alliance name from ESI") - flog.Debug("ESI URL: %s", esiURL) - flog.Debug("Proxy URL: %s", proxyURL) - - req, err := http.NewRequest("GET", proxyURL, nil) - if err != nil { - flog.Error("Failed to create request: %v", err) - return "", fmt.Errorf("failed to create request: %w", err) - } - - cacheFile := filepath.Join(".cache", fmt.Sprintf("alliance_%d.json", allianceID)) - alliance, err := cyutils.RequestCached[Alliance](req, cacheFile) - if err != nil { - errStr := err.Error() - if strings.Contains(errStr, "404") || strings.Contains(errStr, "not found") { - flog.Debug("Alliance not found (404)") - return "", fmt.Errorf("alliance %d: %w", allianceID, errNotFound) - } - flog.Error("Failed to get alliance: %v", err) - return "", err - } - - flog.Debug("Successfully got alliance name: %s", alliance.Name) - return alliance.Name, nil + return getName[Alliance]("Alliance", "alliance", allianceID) }