|
| 1 | +package data |
| 2 | + |
| 3 | +import ( |
| 4 | + "MineTracker/database" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var allowedTimeRanges = map[string]string{ |
| 13 | + "1h": "10s", |
| 14 | + "6h": "1m", |
| 15 | + "12h": "2m", |
| 16 | + "24h": "4m", |
| 17 | + "7d": "30m", |
| 18 | + "30d": "2h", |
| 19 | + "1y": "1d", |
| 20 | +} |
| 21 | + |
| 22 | +// ValidateTimeRange returns the canonical time range and the exact aggregation step. |
| 23 | +func ValidateTimeRange(timeRange string) (string, string, error) { |
| 24 | + normalized := strings.TrimSpace(timeRange) |
| 25 | + step, ok := allowedTimeRanges[normalized] |
| 26 | + if !ok { |
| 27 | + return "", "", fmt.Errorf("unsupported time range: %s", timeRange) |
| 28 | + } |
| 29 | + return normalized, step, nil |
| 30 | +} |
| 31 | + |
| 32 | +// QueryHistoricalDataPoints queries the historical series for a validated time range. |
| 33 | +// The returned step is the exact aggregation step used in the query. |
| 34 | +func QueryHistoricalDataPoints(ip string, timeRange string) ([]ServerDataPoint, string, error) { |
| 35 | + canonicalRange, step, err := ValidateTimeRange(timeRange) |
| 36 | + if err != nil { |
| 37 | + return nil, "", err |
| 38 | + } |
| 39 | + |
| 40 | + query, _, resolvedStep, err := BuildInfluxQueryFromParams(QueryParams{ |
| 41 | + Start: "-" + canonicalRange, |
| 42 | + Step: step, |
| 43 | + ServerFilter: ip, |
| 44 | + MaxDataPoints: 5000, |
| 45 | + MinDataPoints: 1, |
| 46 | + UseAdaptive: false, |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + return nil, "", err |
| 50 | + } |
| 51 | + |
| 52 | + queryApi := database.InfluxClient.QueryAPI(os.Getenv("INFLUXDB_ORG")) |
| 53 | + result, err := queryApi.Query(context.Background(), query) |
| 54 | + if err != nil { |
| 55 | + return nil, "", fmt.Errorf("query execution failed: %w", err) |
| 56 | + } |
| 57 | + defer func() { _ = result.Close() }() |
| 58 | + |
| 59 | + points := make([]ServerDataPoint, 0, 512) |
| 60 | + for result.Next() { |
| 61 | + record := result.Record() |
| 62 | + if record == nil { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + playerCount, ok := recordValueToInt(record.Value()) |
| 67 | + if !ok { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + point := ServerDataPoint{ |
| 72 | + Timestamp: record.Time().Unix(), |
| 73 | + PlayerCount: playerCount, |
| 74 | + Ip: record.ValueByKey("ip").(string), |
| 75 | + Name: record.ValueByKey("name").(string), |
| 76 | + } |
| 77 | + if ip == "" || point.Ip == ip { |
| 78 | + points = append(points, point) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if result.Err() != nil { |
| 83 | + return nil, "", fmt.Errorf("result error: %w", result.Err()) |
| 84 | + } |
| 85 | + |
| 86 | + sort.Slice(points, func(i, j int) bool { |
| 87 | + if points[i].Timestamp != points[j].Timestamp { |
| 88 | + return points[i].Timestamp < points[j].Timestamp |
| 89 | + } |
| 90 | + if points[i].PlayerCount != points[j].PlayerCount { |
| 91 | + return points[i].PlayerCount < points[j].PlayerCount |
| 92 | + } |
| 93 | + if points[i].Ip != points[j].Ip { |
| 94 | + return points[i].Ip < points[j].Ip |
| 95 | + } |
| 96 | + return points[i].Name < points[j].Name |
| 97 | + }) |
| 98 | + |
| 99 | + return points, resolvedStep, nil |
| 100 | +} |
0 commit comments