Delete assets/js/dashboard/stats/graph/intervals.ts with huggingface_hub
Browse files
assets/js/dashboard/stats/graph/intervals.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
| 1 |
-
import { Dayjs } from 'dayjs'
|
| 2 |
-
import {
|
| 3 |
-
ComparisonMode,
|
| 4 |
-
DashboardPeriod,
|
| 5 |
-
DashboardTimeSettings
|
| 6 |
-
} from '../../dashboard-time-periods'
|
| 7 |
-
import { now, parseUTCDate } from '../../util/date'
|
| 8 |
-
|
| 9 |
-
export enum Interval {
|
| 10 |
-
minute = 'minute',
|
| 11 |
-
hour = 'hour',
|
| 12 |
-
day = 'day',
|
| 13 |
-
week = 'week',
|
| 14 |
-
month = 'month'
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
export type GetIntervalProps = Omit<DashboardTimeSettings, 'date'>
|
| 18 |
-
|
| 19 |
-
type DayjsRange = { from: Dayjs; to: Dayjs }
|
| 20 |
-
|
| 21 |
-
type FixedPeriod = Exclude<DashboardPeriod, 'custom' | 'all'>
|
| 22 |
-
|
| 23 |
-
const VALID_INTERVALS_BY_FIXED_PERIOD: Record<FixedPeriod, Interval[]> = {
|
| 24 |
-
realtime: [Interval.minute],
|
| 25 |
-
realtime_30m: [Interval.minute],
|
| 26 |
-
day: [Interval.minute, Interval.hour],
|
| 27 |
-
'24h': [Interval.minute, Interval.hour],
|
| 28 |
-
'7d': [Interval.hour, Interval.day],
|
| 29 |
-
'28d': [Interval.day, Interval.week],
|
| 30 |
-
'30d': [Interval.day, Interval.week],
|
| 31 |
-
'91d': [Interval.day, Interval.week, Interval.month],
|
| 32 |
-
month: [Interval.day, Interval.week],
|
| 33 |
-
'6mo': [Interval.day, Interval.week, Interval.month],
|
| 34 |
-
'12mo': [Interval.day, Interval.week, Interval.month],
|
| 35 |
-
year: [Interval.day, Interval.week, Interval.month]
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
const INTERVAL_COARSENESS: Record<Interval, number> = {
|
| 39 |
-
[Interval.minute]: 0,
|
| 40 |
-
[Interval.hour]: 1,
|
| 41 |
-
[Interval.day]: 2,
|
| 42 |
-
[Interval.week]: 3,
|
| 43 |
-
[Interval.month]: 4
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
export function extractIntervalFromDimensions(dimensions: string[]): Interval {
|
| 47 |
-
return dimensions[0].split(':')[1] as Interval
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
/**
|
| 51 |
-
* Returns the intervals available for the current dashboard state.
|
| 52 |
-
*
|
| 53 |
-
* When a custom comparison period is active, the valid intervals for both the
|
| 54 |
-
* main period and the comparison period are computed independently and the
|
| 55 |
-
* coarser set is returned — ensuring the chosen interval is granular enough to
|
| 56 |
-
* be meaningful for whichever date range is longer. For all other comparison
|
| 57 |
-
* modes the valid intervals are determined solely by the main period.
|
| 58 |
-
*/
|
| 59 |
-
export function validIntervals({
|
| 60 |
-
siteTimezoneOffset,
|
| 61 |
-
siteStatsBegin,
|
| 62 |
-
period,
|
| 63 |
-
to,
|
| 64 |
-
from,
|
| 65 |
-
comparison,
|
| 66 |
-
compare_to,
|
| 67 |
-
compare_from
|
| 68 |
-
}: GetIntervalProps): Interval[] {
|
| 69 |
-
const mainIntervals = validIntervalsForMainPeriod(
|
| 70 |
-
siteTimezoneOffset,
|
| 71 |
-
siteStatsBegin,
|
| 72 |
-
period,
|
| 73 |
-
from,
|
| 74 |
-
to
|
| 75 |
-
)
|
| 76 |
-
const comparisonIntervals = validIntervalsForCustomComparison(
|
| 77 |
-
comparison,
|
| 78 |
-
compare_from,
|
| 79 |
-
compare_to
|
| 80 |
-
)
|
| 81 |
-
return comparisonIntervals
|
| 82 |
-
? coarser(mainIntervals, comparisonIntervals)
|
| 83 |
-
: mainIntervals
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
/**
|
| 87 |
-
* Returns the default interval for the current dashboard state.
|
| 88 |
-
*
|
| 89 |
-
* The default is always derived from the main period. The only exception is
|
| 90 |
-
* when a custom comparison period is active and that period does not support
|
| 91 |
-
* the main-period default — in that case the default falls back to whatever is
|
| 92 |
-
* appropriate for the comparison date range.
|
| 93 |
-
*/
|
| 94 |
-
export function getDefaultInterval({
|
| 95 |
-
siteTimezoneOffset,
|
| 96 |
-
siteStatsBegin,
|
| 97 |
-
period,
|
| 98 |
-
to,
|
| 99 |
-
from,
|
| 100 |
-
comparison,
|
| 101 |
-
compare_to,
|
| 102 |
-
compare_from
|
| 103 |
-
}: GetIntervalProps): Interval {
|
| 104 |
-
const defaultForMain = defaultForMainPeriod(
|
| 105 |
-
siteTimezoneOffset,
|
| 106 |
-
siteStatsBegin,
|
| 107 |
-
period,
|
| 108 |
-
from,
|
| 109 |
-
to
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
const validComparisonIntervals = validIntervalsForCustomComparison(
|
| 113 |
-
comparison,
|
| 114 |
-
compare_from,
|
| 115 |
-
compare_to
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
if (
|
| 119 |
-
!validComparisonIntervals ||
|
| 120 |
-
validComparisonIntervals.includes(defaultForMain)
|
| 121 |
-
) {
|
| 122 |
-
return defaultForMain
|
| 123 |
-
} else {
|
| 124 |
-
return defaultForCustomPeriod({ from: compare_from!, to: compare_to! })
|
| 125 |
-
}
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
function max_coarseness(intervals: Interval[]): number {
|
| 129 |
-
return Math.max(...intervals.map((i) => INTERVAL_COARSENESS[i]))
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
function coarser(a: Interval[], b: Interval[]): Interval[] {
|
| 133 |
-
return max_coarseness(a) >= max_coarseness(b) ? a : b
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
function validIntervalsForMainPeriod(
|
| 137 |
-
siteTimezoneOffset: DashboardTimeSettings['siteTimezoneOffset'],
|
| 138 |
-
siteStatsBegin: DashboardTimeSettings['siteStatsBegin'],
|
| 139 |
-
period: DashboardPeriod,
|
| 140 |
-
from: Dayjs | null,
|
| 141 |
-
to: Dayjs | null
|
| 142 |
-
): Interval[] {
|
| 143 |
-
if (period === DashboardPeriod.custom && from && to) {
|
| 144 |
-
return validIntervalsForCustomPeriod({ from, to })
|
| 145 |
-
}
|
| 146 |
-
if (period === 'all') {
|
| 147 |
-
return validIntervalsForAllTimePeriod(siteTimezoneOffset, siteStatsBegin)
|
| 148 |
-
}
|
| 149 |
-
return VALID_INTERVALS_BY_FIXED_PERIOD[period as FixedPeriod]
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
function validIntervalsForCustomComparison(
|
| 153 |
-
comparison: ComparisonMode | null,
|
| 154 |
-
compare_from: Dayjs | null,
|
| 155 |
-
compare_to: Dayjs | null
|
| 156 |
-
): Interval[] | null {
|
| 157 |
-
if (comparison === ComparisonMode.custom && compare_from && compare_to) {
|
| 158 |
-
return validIntervalsForCustomPeriod({ from: compare_from, to: compare_to })
|
| 159 |
-
}
|
| 160 |
-
return null
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
function defaultForMainPeriod(
|
| 164 |
-
siteTimezoneOffset: DashboardTimeSettings['siteTimezoneOffset'],
|
| 165 |
-
siteStatsBegin: DashboardTimeSettings['siteStatsBegin'],
|
| 166 |
-
period: DashboardPeriod,
|
| 167 |
-
from: Dayjs | null,
|
| 168 |
-
to: Dayjs | null
|
| 169 |
-
): Interval {
|
| 170 |
-
if (period === DashboardPeriod.custom && from && to) {
|
| 171 |
-
return defaultForCustomPeriod({ from, to })
|
| 172 |
-
}
|
| 173 |
-
if (period === 'all') {
|
| 174 |
-
return validIntervalsForAllTimePeriod(
|
| 175 |
-
siteTimezoneOffset,
|
| 176 |
-
siteStatsBegin
|
| 177 |
-
).includes(Interval.day)
|
| 178 |
-
? Interval.day
|
| 179 |
-
: Interval.month
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
switch (period) {
|
| 183 |
-
case 'day':
|
| 184 |
-
return Interval.hour
|
| 185 |
-
case '24h':
|
| 186 |
-
return Interval.hour
|
| 187 |
-
case '7d':
|
| 188 |
-
return Interval.day
|
| 189 |
-
case '6mo':
|
| 190 |
-
return Interval.month
|
| 191 |
-
case '12mo':
|
| 192 |
-
return Interval.month
|
| 193 |
-
case 'year':
|
| 194 |
-
return Interval.month
|
| 195 |
-
default:
|
| 196 |
-
return VALID_INTERVALS_BY_FIXED_PERIOD[period as FixedPeriod][0]
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
function validIntervalsForCustomPeriod({ to, from }: DayjsRange): Interval[] {
|
| 201 |
-
if (to.diff(from, 'days') < 1) {
|
| 202 |
-
return [Interval.minute, Interval.hour]
|
| 203 |
-
}
|
| 204 |
-
if (to.diff(from, 'days') < 7) {
|
| 205 |
-
return [Interval.hour, Interval.day]
|
| 206 |
-
}
|
| 207 |
-
if (to.diff(from, 'months') < 1) {
|
| 208 |
-
return [Interval.day, Interval.week]
|
| 209 |
-
}
|
| 210 |
-
if (to.diff(from, 'months') < 12) {
|
| 211 |
-
return [Interval.day, Interval.week, Interval.month]
|
| 212 |
-
}
|
| 213 |
-
return [Interval.week, Interval.month]
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
function validIntervalsForAllTimePeriod(
|
| 217 |
-
siteTimezoneOffset: DashboardTimeSettings['siteTimezoneOffset'],
|
| 218 |
-
siteStatsBegin: DashboardTimeSettings['siteStatsBegin']
|
| 219 |
-
): Interval[] {
|
| 220 |
-
const to = now(siteTimezoneOffset)
|
| 221 |
-
const from = siteStatsBegin
|
| 222 |
-
? parseUTCDate(siteStatsBegin).utcOffset(siteTimezoneOffset / 60, true)
|
| 223 |
-
: to
|
| 224 |
-
|
| 225 |
-
return validIntervalsForCustomPeriod({ from, to })
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
function defaultForCustomPeriod({ from, to }: DayjsRange): Interval {
|
| 229 |
-
if (to.diff(from, 'days') < 1) {
|
| 230 |
-
return Interval.hour
|
| 231 |
-
} else if (to.diff(from, 'days') < 30) {
|
| 232 |
-
return Interval.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