704 lines
19 KiB
Go
704 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"zkillsusser/models"
|
|
|
|
"git.site.quack-lab.dev/dave/cylogger"
|
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
|
"github.com/ClickHouse/clickhouse-go/v2"
|
|
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
type DB interface {
|
|
Init() error
|
|
Get() *gorm.DB
|
|
|
|
SaveKillmails(killmails []Killmail) error
|
|
QueryFits(params QueryParams) (*FitStatistics, error)
|
|
SearchShips(query string, limit int) ([]models.InvType, error)
|
|
SearchSystems(query string, limit int) ([]models.MapSolarSystem, error)
|
|
SearchModules(query string, limit int) ([]models.InvType, error)
|
|
SearchGroups(query string, limit int) ([]models.InvGroup, error)
|
|
|
|
// Non retarded APIs below
|
|
GetItemTypes(itemIDs []int64) ([]models.InvType, error)
|
|
GetSolarSystems(systemIDs []int64) ([]models.MapSolarSystem, error)
|
|
ExpandGroupsIntoItemTypeIds(groups []int64) ([]int64, error)
|
|
GetModuleSlots(moduleIDs []int64) (map[int64]ModuleSlot, error)
|
|
CacheSet(key string, data []byte) error
|
|
CacheGet(key string) ([]byte, error)
|
|
CacheClean() error
|
|
}
|
|
|
|
type DBWrapper struct {
|
|
ch driver.Conn
|
|
db *gorm.DB // For SQLite (EVE static data)
|
|
}
|
|
|
|
var db *DBWrapper
|
|
|
|
func GetDB() (DB, error) {
|
|
if db != nil {
|
|
return db, nil
|
|
}
|
|
|
|
sdb, err := GetDBSqlite()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to SQLite: %w", err)
|
|
}
|
|
|
|
conn, err := GetDBClickhouse()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to ClickHouse: %w", err)
|
|
}
|
|
|
|
db = &DBWrapper{
|
|
ch: conn,
|
|
db: sdb,
|
|
}
|
|
err = db.Init()
|
|
return db, err
|
|
}
|
|
|
|
func GetDBSqlite() (*gorm.DB, error) {
|
|
return gorm.Open(sqlite.Open("sqlite-latest.sqlite"), &gorm.Config{
|
|
NamingStrategy: schema.NamingStrategy{
|
|
NoLowerCase: true,
|
|
},
|
|
})
|
|
}
|
|
|
|
func GetDBClickhouse() (driver.Conn, error) {
|
|
options := &clickhouse.Options{
|
|
Addr: []string{"clickhouse.site.quack-lab.dev"},
|
|
Auth: clickhouse.Auth{
|
|
Database: "zkill",
|
|
Username: "default",
|
|
Password: "",
|
|
},
|
|
Protocol: clickhouse.HTTP,
|
|
Settings: clickhouse.Settings{
|
|
"max_query_size": 100000000,
|
|
},
|
|
}
|
|
return clickhouse.Open(options)
|
|
}
|
|
|
|
func (db *DBWrapper) Get() *gorm.DB {
|
|
return db.db
|
|
}
|
|
|
|
func (db *DBWrapper) Init() error {
|
|
ctx := context.Background()
|
|
|
|
// Migrate unified cache table
|
|
// Use raw SQL to create table and index with IF NOT EXISTS to avoid errors
|
|
// For 404s, we store a special marker byte sequence instead of NULL
|
|
err := db.db.AutoMigrate(&CacheEntry{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to migrate cache_entries table: %w", err)
|
|
}
|
|
|
|
// Create killmails table
|
|
createFlatKillmails := `
|
|
CREATE TABLE IF NOT EXISTS killmails (
|
|
killmail_id Int64,
|
|
killmail_time DateTime,
|
|
solar_system_id Int64,
|
|
killmail_hash String,
|
|
victim_ship_type_id Int64,
|
|
victim_character_id Int64,
|
|
victim_corporation_id Int64,
|
|
victim_alliance_id Int64,
|
|
victim_damage_taken Int64,
|
|
victim_pos_x Float64,
|
|
victim_pos_y Float64,
|
|
victim_pos_z Float64,
|
|
attacker_count UInt16,
|
|
total_damage_done Int64,
|
|
final_blow_ship_type Int64,
|
|
attackers Array(Tuple(
|
|
Int64, -- character_id
|
|
Int64, -- corporation_id
|
|
Int64, -- alliance_id
|
|
Int64, -- ship_type_id
|
|
Int64, -- weapon_type_id
|
|
Int64, -- damage_done
|
|
UInt8, -- final_blow
|
|
Float64 -- security_status
|
|
)),
|
|
items Array(Tuple(
|
|
Int64, -- flag
|
|
Int64, -- item_type_id
|
|
Int64, -- quantity_destroyed
|
|
Int64, -- quantity_dropped
|
|
Int64 -- singleton
|
|
))
|
|
) ENGINE = MergeTree()
|
|
ORDER BY (killmail_id)
|
|
PRIMARY KEY (killmail_id)`
|
|
err = db.ch.Exec(ctx, createFlatKillmails)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create killmails table: %w", err)
|
|
}
|
|
|
|
// Create modules table
|
|
createFittedModules := `
|
|
CREATE TABLE IF NOT EXISTS modules (
|
|
killmail_id Int64,
|
|
item_type_id Int64,
|
|
slot String
|
|
) ENGINE = MergeTree()
|
|
ORDER BY (killmail_id, item_type_id)
|
|
PRIMARY KEY (killmail_id, item_type_id)`
|
|
err = db.ch.Exec(ctx, createFittedModules)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create modules table: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (db *DBWrapper) QueryFits(params QueryParams) (*FitStatistics, error) {
|
|
ctx := context.Background()
|
|
flog := logger.Default.WithPrefix("QueryFits").WithPrefix(fmt.Sprintf("%+v", params))
|
|
flog.Info("Starting query")
|
|
|
|
// Expand groups into item type IDs
|
|
newItemTypes, err := db.ExpandGroupsIntoItemTypeIds(params.Groups)
|
|
if err != nil {
|
|
flog.Error("Failed to expand groups: %v", err)
|
|
return nil, err
|
|
}
|
|
params.Modules = append(params.Modules, newItemTypes...)
|
|
modules := deduplicateInt64(params.Modules)
|
|
flog.Debug("Deduplicated modules: %d -> %d", len(params.Modules), len(modules))
|
|
|
|
// Build the base query - start with all killmails
|
|
baseQuery := `
|
|
SELECT
|
|
fk.killmail_id,
|
|
fk.solar_system_id,
|
|
fk.victim_ship_type_id
|
|
FROM killmails fk`
|
|
|
|
args := []interface{}{}
|
|
whereClauses := []string{}
|
|
|
|
// Apply filters
|
|
if params.Ship > 0 {
|
|
whereClauses = append(whereClauses, "fk.victim_ship_type_id = ?")
|
|
args = append(args, params.Ship)
|
|
}
|
|
|
|
if len(params.Systems) > 0 {
|
|
placeholders := make([]string, len(params.Systems))
|
|
for i := range params.Systems {
|
|
placeholders[i] = "?"
|
|
args = append(args, params.Systems[i])
|
|
}
|
|
whereClauses = append(whereClauses, "fk.solar_system_id IN ("+strings.Join(placeholders, ",")+")")
|
|
}
|
|
|
|
// For module filters, we need to join with modules
|
|
var moduleJoin string
|
|
if len(modules) > 0 {
|
|
placeholders := make([]string, len(modules))
|
|
for i := range modules {
|
|
placeholders[i] = "?"
|
|
args = append(args, modules[i])
|
|
}
|
|
moduleJoin = `
|
|
INNER JOIN modules fm ON fk.killmail_id = fm.killmail_id`
|
|
whereClauses = append(whereClauses, "fm.item_type_id IN ("+strings.Join(placeholders, ",")+")")
|
|
}
|
|
|
|
// Build final query
|
|
query := baseQuery + moduleJoin
|
|
if len(whereClauses) > 0 {
|
|
query += " WHERE " + strings.Join(whereClauses, " AND ")
|
|
}
|
|
|
|
flog.Debug("Executing query: %s", query)
|
|
rows, err := db.ch.Query(ctx, query, args...)
|
|
if err != nil {
|
|
flog.Error("Failed to execute query: %v", err)
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
// Collect results
|
|
var killmailIDs []int64
|
|
var systemIDs []int64
|
|
var shipTypeIDs []int64
|
|
|
|
for rows.Next() {
|
|
var killmailID, systemID, shipTypeID int64
|
|
if err := rows.Scan(&killmailID, &systemID, &shipTypeID); err != nil {
|
|
flog.Error("Failed to scan row: %v", err)
|
|
return nil, err
|
|
}
|
|
killmailIDs = append(killmailIDs, killmailID)
|
|
systemIDs = append(systemIDs, systemID)
|
|
shipTypeIDs = append(shipTypeIDs, shipTypeID)
|
|
}
|
|
|
|
totalKillmails := int64(len(killmailIDs))
|
|
flog.Info("Found %d killmails after filtering", totalKillmails)
|
|
|
|
if totalKillmails == 0 {
|
|
return &FitStatistics{
|
|
TotalKillmails: 0,
|
|
ShipBreakdown: make(map[int64]Stats),
|
|
SystemBreakdown: make(map[int64]Stats),
|
|
HighSlotModules: make(map[int32]Stats),
|
|
MidSlotModules: make(map[int32]Stats),
|
|
LowSlotModules: make(map[int32]Stats),
|
|
Rigs: make(map[int32]Stats),
|
|
Drones: make(map[int32]Stats),
|
|
KillmailIDs: []int64{},
|
|
}, nil
|
|
}
|
|
|
|
// Calculate ship breakdown
|
|
shipCounts := make(map[int64]int64)
|
|
for _, shipTypeID := range shipTypeIDs {
|
|
shipCounts[shipTypeID]++
|
|
}
|
|
|
|
shipBreakdown := make(map[int64]Stats)
|
|
for shipTypeID, count := range shipCounts {
|
|
percentage := float64(count) / float64(totalKillmails) * 100.0
|
|
shipBreakdown[shipTypeID] = Stats{
|
|
Count: count,
|
|
Percentage: percentage,
|
|
}
|
|
}
|
|
|
|
// Calculate system breakdown
|
|
systemCounts := make(map[int64]int64)
|
|
for _, systemID := range systemIDs {
|
|
systemCounts[systemID]++
|
|
}
|
|
|
|
systemBreakdown := make(map[int64]Stats)
|
|
for systemID, count := range systemCounts {
|
|
percentage := float64(count) / float64(totalKillmails) * 100.0
|
|
systemBreakdown[systemID] = Stats{
|
|
Count: count,
|
|
Percentage: percentage,
|
|
}
|
|
}
|
|
|
|
// Calculate module statistics
|
|
stats := &FitStatistics{
|
|
TotalKillmails: totalKillmails,
|
|
ShipBreakdown: shipBreakdown,
|
|
SystemBreakdown: systemBreakdown,
|
|
HighSlotModules: make(map[int32]Stats),
|
|
MidSlotModules: make(map[int32]Stats),
|
|
LowSlotModules: make(map[int32]Stats),
|
|
Rigs: make(map[int32]Stats),
|
|
Drones: make(map[int32]Stats),
|
|
KillmailIDs: limitKillmails(killmailIDs, params.KillmailLimit),
|
|
}
|
|
|
|
// Get module statistics for the filtered killmails
|
|
if err := db.calculateModuleStats(killmailIDs, stats, flog); err != nil {
|
|
flog.Error("Failed to calculate module stats: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
flog.Info("Statistics calculated: %d high, %d mid, %d low, %d rigs, %d drones",
|
|
len(stats.HighSlotModules), len(stats.MidSlotModules), len(stats.LowSlotModules),
|
|
len(stats.Rigs), len(stats.Drones))
|
|
|
|
return stats, nil
|
|
}
|
|
|
|
func (db *DBWrapper) ExpandGroupsIntoItemTypeIds(groups []int64) ([]int64, error) {
|
|
var groupTypeIDs []int64
|
|
result := db.db.Model(&models.InvType{}).
|
|
Select("typeID").
|
|
Where("groupID IN ?", groups).
|
|
Pluck("typeID", &groupTypeIDs)
|
|
return groupTypeIDs, result.Error
|
|
}
|
|
|
|
func (db *DBWrapper) SearchShips(query string, limit int) ([]models.InvType, error) {
|
|
var ships []models.InvType
|
|
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
err := db.db.Table("invTypes").
|
|
Joins("INNER JOIN invGroups ON invTypes.groupID = invGroups.groupID").
|
|
Where("LOWER(invTypes.\"typeName\") LIKE ? AND invGroups.categoryID IN (6)", searchPattern).
|
|
Limit(limit).
|
|
Find(&ships).Error
|
|
return ships, err
|
|
}
|
|
|
|
func (db *DBWrapper) SearchSystems(query string, limit int) ([]models.MapSolarSystem, error) {
|
|
var systems []models.MapSolarSystem
|
|
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
err := db.db.Table("mapSolarSystems").
|
|
Where("LOWER(\"solarSystemName\") LIKE ?", searchPattern).
|
|
Limit(limit).
|
|
Find(&systems).Error
|
|
return systems, err
|
|
}
|
|
|
|
func (db *DBWrapper) SearchModules(query string, limit int) ([]models.InvType, error) {
|
|
var modules []models.InvType
|
|
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
err := db.db.Table("invTypes").
|
|
Joins("INNER JOIN invGroups ON invTypes.groupID = invGroups.groupID").
|
|
Where("LOWER(invTypes.\"typeName\") LIKE ? AND invGroups.categoryID IN (7, 66)", searchPattern).
|
|
Limit(limit).
|
|
Find(&modules).Error
|
|
return modules, err
|
|
}
|
|
|
|
func (db *DBWrapper) SearchGroups(query string, limit int) ([]models.InvGroup, error) {
|
|
var groups []models.InvGroup
|
|
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
err := db.db.Table("invGroups").
|
|
Where("LOWER(\"groupName\") LIKE ?", searchPattern).
|
|
Limit(limit).
|
|
Find(&groups).Error
|
|
return groups, err
|
|
}
|
|
|
|
func (db *DBWrapper) GetItemTypes(itemIDs []int64) ([]models.InvType, error) {
|
|
var itemTypes []models.InvType
|
|
res := db.db.Model(&models.InvType{}).
|
|
Where("typeID IN ?", itemIDs).
|
|
Find(&itemTypes)
|
|
return itemTypes, res.Error
|
|
}
|
|
|
|
func (db *DBWrapper) GetSolarSystems(systemIDs []int64) ([]models.MapSolarSystem, error) {
|
|
var systems []models.MapSolarSystem
|
|
res := db.db.Model(&models.MapSolarSystem{}).
|
|
Where("solarSystemID IN ?", systemIDs).
|
|
Find(&systems)
|
|
return systems, res.Error
|
|
}
|
|
|
|
func deduplicateInt64(slice []int64) []int64 {
|
|
seen := make(map[int64]bool)
|
|
result := make([]int64, 0, len(slice))
|
|
for _, v := range slice {
|
|
if !seen[v] {
|
|
seen[v] = true
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (db *DBWrapper) calculateStats(params QueryParams, shipTypeIDs []int64, killmailIDs []int64, stats *FitStatistics, total int64, flog *logger.Logger) error {
|
|
// if total == 0 {
|
|
// return nil
|
|
// }
|
|
|
|
// ctx := context.Background()
|
|
|
|
// var query string
|
|
// var args []interface{}
|
|
|
|
// if len(killmailIDs) > 0 && len(killmailIDs) < 100000 {
|
|
// placeholders := make([]string, len(killmailIDs))
|
|
// for i := range killmailIDs {
|
|
// placeholders[i] = "?"
|
|
// args = append(args, killmailIDs[i])
|
|
// }
|
|
// query = "SELECT item_type_id, flag, count(DISTINCT killmail_id) as count FROM modules WHERE killmail_id IN (" + strings.Join(placeholders, ",") + ")"
|
|
// } else {
|
|
// var shipPlaceholders []string
|
|
// if len(shipTypeIDs) > 0 {
|
|
// shipPlaceholders = make([]string, len(shipTypeIDs))
|
|
// for i, shipID := range shipTypeIDs {
|
|
// shipPlaceholders[i] = "?"
|
|
// args = append(args, shipID)
|
|
// }
|
|
// } else if params.Ship != 0 {
|
|
// shipPlaceholders = []string{"?"}
|
|
// args = []interface{}{params.Ship}
|
|
// }
|
|
|
|
// if len(shipPlaceholders) > 0 {
|
|
// query = "SELECT item_type_id, flag, count(DISTINCT killmail_id) as count FROM modules WHERE victim_ship_type_id IN (" + strings.Join(shipPlaceholders, ",") + ")"
|
|
// } else {
|
|
// query = "SELECT item_type_id, flag, count(DISTINCT killmail_id) as count FROM modules"
|
|
// }
|
|
|
|
// if len(params.Systems) > 0 {
|
|
// sysPlaceholders := make([]string, len(params.Systems))
|
|
// for i := range params.Systems {
|
|
// sysPlaceholders[i] = "?"
|
|
// args = append(args, params.Systems[i])
|
|
// }
|
|
// if len(shipPlaceholders) > 0 {
|
|
// query += " AND solar_system_id IN (" + strings.Join(sysPlaceholders, ",") + ")"
|
|
// } else {
|
|
// query += " WHERE solar_system_id IN (" + strings.Join(sysPlaceholders, ",") + ")"
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// query += " GROUP BY item_type_id, flag"
|
|
|
|
// rows, err := db.ch.Query(ctx, query, args...)
|
|
// if err != nil {
|
|
// flog.Error("Failed to query module stats: %v", err)
|
|
// return err
|
|
// }
|
|
// defer rows.Close()
|
|
|
|
// // Map to aggregate counts per item_type_id (not per flag)
|
|
// itemCounts := make(map[int64]uint64)
|
|
// itemFlags := make(map[int64]int64)
|
|
|
|
// for rows.Next() {
|
|
// var itemTypeID, flag int64
|
|
// var count uint64
|
|
// if err := rows.Scan(&itemTypeID, &flag, &count); err != nil {
|
|
// flog.Error("Failed to scan module stat: %v", err)
|
|
// return err
|
|
// }
|
|
// // Only count fitted modules - ignore cargo (flag 5) and other non-module flags
|
|
// if flag < 11 || (flag > 34 && flag != 87 && (flag < 92 || flag > 99)) {
|
|
// continue
|
|
// }
|
|
// // Aggregate: if we've seen this item_type_id before, use the max count (should be same, but just in case)
|
|
// if existing, exists := itemCounts[itemTypeID]; !exists || count > existing {
|
|
// itemCounts[itemTypeID] = count
|
|
// itemFlags[itemTypeID] = flag
|
|
// }
|
|
// }
|
|
|
|
// // For filtered modules, they should be in 100% of fits - ADD THEM FIRST
|
|
// filteredModules := make(map[int64]bool)
|
|
// moduleSlots := make(map[int64]string)
|
|
// if len(params.Modules) > 0 {
|
|
// slots, err := db.getModuleSlots(params.Modules)
|
|
// if err == nil {
|
|
// moduleSlots = slots
|
|
// }
|
|
// for _, moduleID := range params.Modules {
|
|
// filteredModules[moduleID] = true
|
|
// // Add filtered modules immediately with 100%
|
|
// Stats := Stats{
|
|
// Count: total,
|
|
// Percentage: 100.0,
|
|
// }
|
|
// slot, _ := moduleSlots[moduleID]
|
|
// switch slot {
|
|
// case "low":
|
|
// stats.LowSlotModules[int32(moduleID)] = Stats
|
|
// case "mid":
|
|
// stats.MidSlotModules[int32(moduleID)] = Stats
|
|
// case "high":
|
|
// stats.HighSlotModules[int32(moduleID)] = Stats
|
|
// case "rig":
|
|
// stats.Rigs[int32(moduleID)] = Stats
|
|
// case "drone":
|
|
// stats.Drones[int32(moduleID)] = Stats
|
|
// default:
|
|
// stats.HighSlotModules[int32(moduleID)] = Stats
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// // Add all modules from query results (filtered ones already added with 100%)
|
|
// for itemTypeID, count := range itemCounts {
|
|
// if filteredModules[itemTypeID] {
|
|
// continue
|
|
// }
|
|
// percentage := float64(count) / float64(total) * 100.0
|
|
// Stats := Stats{
|
|
// Count: int64(count),
|
|
// Percentage: percentage,
|
|
// }
|
|
|
|
// flag := itemFlags[itemTypeID]
|
|
// switch {
|
|
// case flag >= 11 && flag <= 18:
|
|
// stats.LowSlotModules[int32(itemTypeID)] = Stats
|
|
// case flag >= 19 && flag <= 26:
|
|
// stats.MidSlotModules[int32(itemTypeID)] = Stats
|
|
// case flag >= 27 && flag <= 34:
|
|
// stats.HighSlotModules[int32(itemTypeID)] = Stats
|
|
// case flag >= 92 && flag <= 99:
|
|
// stats.Rigs[int32(itemTypeID)] = Stats
|
|
// case flag == 87:
|
|
// stats.Drones[int32(itemTypeID)] = Stats
|
|
// }
|
|
// }
|
|
|
|
// return nil
|
|
return nil
|
|
}
|
|
|
|
func (db *DBWrapper) CacheSet(key string, data []byte) error {
|
|
flog := logger.Default.WithPrefix("CacheSet").WithPrefix(key)
|
|
flog.Dump("data", data)
|
|
|
|
err := db.CacheClean()
|
|
if err != nil {
|
|
flog.Debug("Failed to clean cache: %v", err)
|
|
return err
|
|
}
|
|
|
|
cacheEntry := CacheEntry{
|
|
Key: key,
|
|
Data: data,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
flog.Debug("Creating cache entry")
|
|
flog.Dump("cacheEntry", cacheEntry)
|
|
return db.db.Create(&cacheEntry).Error
|
|
}
|
|
|
|
var ErrCacheMiss = gorm.ErrRecordNotFound
|
|
|
|
func (db *DBWrapper) CacheGet(key string) ([]byte, error) {
|
|
flog := logger.Default.WithPrefix("CacheGet").WithPrefix(key)
|
|
flog.Dump("key", key)
|
|
|
|
err := db.CacheClean()
|
|
if err != nil {
|
|
flog.Debug("Failed to clean cache: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
cacheEntry := CacheEntry{Key: key}
|
|
res := db.db.Model(&CacheEntry{}).
|
|
Where(cacheEntry).
|
|
First(&cacheEntry)
|
|
flog.Debug("Found cache entry")
|
|
flog.Dump("cacheEntry", cacheEntry)
|
|
flog.Dump("res", res)
|
|
return cacheEntry.Data, res.Error
|
|
}
|
|
|
|
func (db *DBWrapper) CacheClean() error {
|
|
flog := logger.Default.WithPrefix("CacheClean")
|
|
threshold := time.Now().Add(-72 * time.Hour)
|
|
flog.Dump("threshold", threshold)
|
|
return db.db.
|
|
Where("created_at < ?", threshold).
|
|
Delete(&CacheEntry{}).Error
|
|
}
|
|
|
|
func (db *DBWrapper) GetModuleSlots(moduleIDs []int64) (map[int64]ModuleSlot, error) {
|
|
if len(moduleIDs) == 0 {
|
|
return make(map[int64]ModuleSlot), nil
|
|
}
|
|
|
|
var effects []models.DgmTypeEffect
|
|
qres := db.db.Model(&models.DgmTypeEffect{}).
|
|
Select("typeID, effectID").
|
|
Where("typeID IN ? AND effectID IN (11, 12, 13, 2663)", moduleIDs).
|
|
Find(&effects)
|
|
if qres.Error != nil {
|
|
return nil, qres.Error
|
|
}
|
|
|
|
result := make(map[int64]ModuleSlot)
|
|
for _, e := range effects {
|
|
var slot ModuleSlot
|
|
switch e.EffectID {
|
|
case 11:
|
|
slot = ModuleSlotLow
|
|
case 12:
|
|
slot = ModuleSlotHigh
|
|
case 13:
|
|
slot = ModuleSlotMid
|
|
case 2663:
|
|
slot = ModuleSlotRig
|
|
}
|
|
result[int64(e.TypeID)] = slot
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func limitKillmails(killmailIDs []int64, limit int) []int64 {
|
|
if limit <= 0 || len(killmailIDs) <= limit {
|
|
return killmailIDs
|
|
}
|
|
return killmailIDs[:limit]
|
|
}
|
|
|
|
func (db *DBWrapper) calculateModuleStats(killmailIDs []int64, stats *FitStatistics, flog *cylogger.Logger) error {
|
|
if len(killmailIDs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// Create placeholders for killmail IDs
|
|
placeholders := make([]string, len(killmailIDs))
|
|
args := make([]interface{}, len(killmailIDs))
|
|
for i, id := range killmailIDs {
|
|
placeholders[i] = "?"
|
|
args = append(args, id)
|
|
}
|
|
|
|
// Query module statistics - count distinct killmails per (item_type_id, slot) combination
|
|
query := fmt.Sprintf(`
|
|
SELECT
|
|
fm.item_type_id,
|
|
fm.slot,
|
|
COUNT(DISTINCT fm.killmail_id) as count
|
|
FROM modules fm
|
|
WHERE fm.killmail_id IN (%s)
|
|
GROUP BY fm.item_type_id, fm.slot
|
|
ORDER BY count DESC`, strings.Join(placeholders, ","))
|
|
|
|
rows, err := db.ch.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to query module stats: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
totalKillmails := float64(stats.TotalKillmails)
|
|
|
|
for rows.Next() {
|
|
var itemTypeID int32
|
|
var slot string
|
|
var count int64
|
|
|
|
if err := rows.Scan(&itemTypeID, &slot, &count); err != nil {
|
|
return fmt.Errorf("failed to scan module row: %w", err)
|
|
}
|
|
|
|
percentage := float64(count) / totalKillmails * 100.0
|
|
stat := Stats{Count: count, Percentage: percentage}
|
|
|
|
// Map slot to the appropriate map
|
|
switch slot {
|
|
case "Low":
|
|
stats.LowSlotModules[itemTypeID] = stat
|
|
case "Mid":
|
|
stats.MidSlotModules[itemTypeID] = stat
|
|
case "High":
|
|
stats.HighSlotModules[itemTypeID] = stat
|
|
case "Rig":
|
|
stats.Rigs[itemTypeID] = stat
|
|
case "Drone":
|
|
stats.Drones[itemTypeID] = stat
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|