Skip to content

Commit

Permalink
feat(agg-window): Add window periods for common time ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ebb-tide committed Jan 14, 2020
1 parent 96ec359 commit 1d87c2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ui/src/shared/constants/timeRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const pastHourTimeRange: SelectableDurationTimeRange = {
label: 'Past 1h',
duration: '1h',
type: 'selectable-duration',
windowPeriod: 10000, // 10s
}

export const pastThirtyDaysTimeRange: SelectableDurationTimeRange = {
Expand All @@ -20,6 +21,7 @@ export const pastThirtyDaysTimeRange: SelectableDurationTimeRange = {
label: 'Past 30d',
duration: '30d',
type: 'selectable-duration',
windowPeriod: 3600000, // 1h
}

export const pastFifteenMinTimeRange: SelectableDurationTimeRange = {
Expand All @@ -29,6 +31,7 @@ export const pastFifteenMinTimeRange: SelectableDurationTimeRange = {
label: 'Past 15m',
duration: '15m',
type: 'selectable-duration',
windowPeriod: 10000, // 10s
}

export const CUSTOM_TIME_RANGE: {label: string; type: 'custom'} = {
Expand All @@ -44,6 +47,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 5m',
duration: '5m',
type: 'selectable-duration',
windowPeriod: 10000, // 10s
},
pastFifteenMinTimeRange,
pastHourTimeRange,
Expand All @@ -54,6 +58,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 6h',
duration: '6h',
type: 'selectable-duration',
windowPeriod: 60000, // 1m
},
{
seconds: 43200,
Expand All @@ -62,6 +67,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 12h',
duration: '12h',
type: 'selectable-duration',
windowPeriod: 120000, // 2m
},
{
seconds: 86400,
Expand All @@ -70,6 +76,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 24h',
duration: '24h',
type: 'selectable-duration',
windowPeriod: 300000, // 5m
},
{
seconds: 172800,
Expand All @@ -78,6 +85,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 2d',
duration: '2d',
type: 'selectable-duration',
windowPeriod: 600000, // 10m
},
{
seconds: 604800,
Expand All @@ -86,6 +94,7 @@ export const SELECTABLE_TIME_RANGES: SelectableDurationTimeRange[] = [
label: 'Past 7d',
duration: '7d',
type: 'selectable-duration',
windowPeriod: 1800000, // 30 min
},
pastThirtyDaysTimeRange,
]
Expand Down
1 change: 1 addition & 0 deletions ui/src/types/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface SelectableDurationTimeRange {
label: string
duration: string
type: 'selectable-duration'
windowPeriod: number
}

export interface DurationTimeRange {
Expand Down
11 changes: 10 additions & 1 deletion ui/src/variables/utils/getWindowVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {WINDOW_PERIOD} from 'src/variables/constants'

// Types
import {VariableAssignment, Package} from 'src/types/ast'
import {SELECTABLE_TIME_RANGES} from 'src/shared/constants/timeRanges'

const DESIRED_POINTS_PER_GRAPH = 360
const FALLBACK_WINDOW_PERIOD = 15000
Expand Down Expand Up @@ -63,7 +64,15 @@ export const getWindowPeriod = (
files: [ast, buildVarsOption(variables)],
}

const queryDuration = getMinDurationFromAST(substitutedAST)
const queryDuration = getMinDurationFromAST(substitutedAST) // in ms

const foundDuration = SELECTABLE_TIME_RANGES.find(
tr => tr.seconds * 1000 == queryDuration
)

if (foundDuration) {
return foundDuration.windowPeriod
}

return Math.round(queryDuration / DESIRED_POINTS_PER_GRAPH)
} catch (error) {
Expand Down

0 comments on commit 1d87c2d

Please sign in to comment.