Fixes now() time parsing when requesting annotations

pull/10616/head
Brandon Farmer 2018-04-18 11:29:14 -07:00
parent 60c286f3c4
commit 3ba680adbe
3 changed files with 64 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import TemplateControlBar from 'src/dashboards/components/TemplateControlBar'
import {errorThrown as errorThrownAction} from 'shared/actions/errors'
import {notify as notifyAction} from 'shared/actions/notifications'
import idNormalizer, {TYPE_ID} from 'src/normalizers/id'
import {millisecondTimeRange} from 'src/dashboards/utils/time'
import * as dashboardActionCreators from 'src/dashboards/actions'
import * as annotationActions from 'shared/actions/annotations'
@ -81,7 +82,7 @@ class DashboardPage extends Component {
autoRefresh,
} = this.props
const annotationRange = this.millisecondTimeRange(timeRange)
const annotationRange = millisecondTimeRange(timeRange)
getAnnotationsAsync(source.links.annotations, annotationRange)
if (autoRefresh) {
@ -114,7 +115,7 @@ class DashboardPage extends Component {
const {source, getAnnotationsAsync, timeRange} = this.props
if (this.props.autoRefresh !== nextProps.autoRefresh) {
clearInterval(this.intervalID)
const annotationRange = this.millisecondTimeRange(timeRange)
const annotationRange = millisecondTimeRange(timeRange)
if (nextProps.autoRefresh) {
this.intervalID = setInterval(() => {
getAnnotationsAsync(source.links.annotations, annotationRange)
@ -183,26 +184,10 @@ class DashboardPage extends Component {
format: FORMAT_INFLUXQL,
})
const annotationRange = this.millisecondTimeRange(timeRange)
const annotationRange = millisecondTimeRange(timeRange)
getAnnotationsAsync(source.links.annotations, annotationRange)
}
millisecondTimeRange({seconds, lower, upper}) {
// Is this a relative time range?
if (seconds) {
return {
since: Date.now() - seconds * 1000,
until: null,
}
}
// No, this is an absolute (custom) time range
return {
since: Date.parse(lower),
until: Date.parse(upper),
}
}
handleUpdatePosition = cells => {
const {dashboardActions, dashboard, meRole, isUsingAuth} = this.props
const newDashboard = {...dashboard, cells}

View File

@ -0,0 +1,30 @@
interface InputTimeRange {
seconds?: number
lower?: string
upper?: string
}
interface OutputTimeRange {
since: number
until: number | null
}
export const millisecondTimeRange = ({
seconds,
lower,
upper,
}: InputTimeRange): OutputTimeRange => {
// Is this a relative time range?
if (seconds) {
return {since: Date.now() - seconds * 1000, until: null}
}
const since = Date.parse(lower)
let until
if (upper === 'now()') {
until = Date.now()
} else {
until = Date.parse(upper)
}
return {since, until}
}

View File

@ -0,0 +1,30 @@
import moment from 'moment'
import * as time from 'src/dashboards/utils/time'
describe('dashboards.utils.time', () => {
describe('millisecondTimeRange', () => {
it('when upper is now() returns valid dates', () => {
const expectedNow = moment()
.subtract()
.seconds(1)
.unix()
const timeRange = {upper: 'now()', lower: moment().format()}
const result = time.millisecondTimeRange(timeRange)
expect(result.since).toBeGreaterThanOrEqual(expectedNow)
expect(result.until).toBeGreaterThanOrEqual(expectedNow)
})
it('when seconds is present returns valid dates', () => {
const timeRange = {seconds: 10}
const expectedSince = moment()
.subtract()
.seconds(10)
.unix()
const result = time.millisecondTimeRange(timeRange)
expect(result.since).toBeGreaterThanOrEqual(expectedSince)
expect(result.until).toBe(null)
})
})
})