This commit is contained in:
Para Dox
2025-05-26 14:58:54 +07:00
parent 07871d8efc
commit 0ab7a45d64

View File

@@ -50,7 +50,7 @@ type WebSocketStats struct {
// CUDataPoint represents a historical CU data point with timestamp // CUDataPoint represents a historical CU data point with timestamp
type CUDataPoint struct { type CUDataPoint struct {
Timestamp time.Time Timestamp time.Time // End time of the interval
CU int CU int
} }
@@ -295,33 +295,36 @@ func (sc *StatsCollector) printSummary() {
actualCU, needsExtrapolation := sc.calculateCUForTimeWindow(window.duration) actualCU, needsExtrapolation := sc.calculateCUForTimeWindow(window.duration)
if needsExtrapolation { if needsExtrapolation {
// Calculate actual data duration for extrapolation using the same logic as calculateCUForTimeWindow // Calculate actual data duration for extrapolation
now := time.Now() now := time.Now()
cutoff := now.Add(-window.duration) cutoff := now.Add(-window.duration)
var earliestTimestamp time.Time var oldestDataStartTime time.Time
hasData := false hasData := false
// Check current interval // Check current interval
if sc.intervalStartTime.After(cutoff) { if sc.intervalStartTime.After(cutoff) {
earliestTimestamp = sc.intervalStartTime oldestDataStartTime = sc.intervalStartTime
hasData = true hasData = true
} }
// Check historical data for earliest timestamp // Check historical data
for i := len(sc.historicalCU) - 1; i >= 0; i-- { for i := len(sc.historicalCU) - 1; i >= 0; i-- {
point := sc.historicalCU[i] point := sc.historicalCU[i]
intervalStart := point.Timestamp.Add(-sc.summaryInterval)
if point.Timestamp.Before(cutoff) { if point.Timestamp.Before(cutoff) {
break break
} }
if !hasData || point.Timestamp.Before(earliestTimestamp) {
earliestTimestamp = point.Timestamp if !hasData || intervalStart.Before(oldestDataStartTime) {
oldestDataStartTime = intervalStart
} }
hasData = true hasData = true
} }
var actualDuration time.Duration var actualDuration time.Duration
if hasData { if hasData {
actualDuration = now.Sub(earliestTimestamp) actualDuration = now.Sub(oldestDataStartTime)
} }
extrapolatedCU := sc.extrapolateCU(actualCU, actualDuration, window.duration) extrapolatedCU := sc.extrapolateCU(actualCU, actualDuration, window.duration)
@@ -432,7 +435,7 @@ func (sc *StatsCollector) printSummary() {
// Store current interval's CU data in historical data before resetting // Store current interval's CU data in historical data before resetting
if sc.totalCU > 0 { if sc.totalCU > 0 {
sc.historicalCU = append(sc.historicalCU, CUDataPoint{ sc.historicalCU = append(sc.historicalCU, CUDataPoint{
Timestamp: time.Now(), Timestamp: time.Now(), // Store the end time of the interval
CU: sc.totalCU, CU: sc.totalCU,
}) })
} }
@@ -488,27 +491,34 @@ func (sc *StatsCollector) calculateCUForTimeWindow(window time.Duration) (int, b
cutoff := now.Add(-window) cutoff := now.Add(-window)
totalCU := 0 totalCU := 0
var earliestTimestamp time.Time var oldestDataStartTime time.Time
hasData := false hasData := false
// First add the current interval's CU if it's within the window // Add current interval's CU if it started within the window
if sc.intervalStartTime.After(cutoff) { if sc.intervalStartTime.After(cutoff) {
totalCU += sc.totalCU totalCU += sc.totalCU
earliestTimestamp = sc.intervalStartTime oldestDataStartTime = sc.intervalStartTime
hasData = true hasData = true
} }
// Add historical CU data within the window // Add historical CU data within the window
// Historical timestamps represent the END of intervals
for i := len(sc.historicalCU) - 1; i >= 0; i-- { for i := len(sc.historicalCU) - 1; i >= 0; i-- {
point := sc.historicalCU[i] point := sc.historicalCU[i]
// Calculate the start time of this historical interval
intervalStart := point.Timestamp.Add(-sc.summaryInterval)
// Skip if the interval ended before our cutoff
if point.Timestamp.Before(cutoff) { if point.Timestamp.Before(cutoff) {
break // Data is too old break
} }
// Include this interval's CU
totalCU += point.CU totalCU += point.CU
// Track the earliest timestamp within the window // Track the oldest data start time
if !hasData || point.Timestamp.Before(earliestTimestamp) { if !hasData || intervalStart.Before(oldestDataStartTime) {
earliestTimestamp = point.Timestamp oldestDataStartTime = intervalStart
} }
hasData = true hasData = true
} }
@@ -516,10 +526,10 @@ func (sc *StatsCollector) calculateCUForTimeWindow(window time.Duration) (int, b
// Calculate actual data span // Calculate actual data span
var actualDataDuration time.Duration var actualDataDuration time.Duration
if hasData { if hasData {
actualDataDuration = now.Sub(earliestTimestamp) actualDataDuration = now.Sub(oldestDataStartTime)
} }
// Check if we need extrapolation - only if data span is less than requested window // We need extrapolation if we don't have enough historical data to cover the window
needsExtrapolation := hasData && actualDataDuration < window needsExtrapolation := hasData && actualDataDuration < window
return totalCU, needsExtrapolation return totalCU, needsExtrapolation