Commit e1acc772 by Ryan McKinley Committed by GitHub

@grafana/data: use timeZone parameter rather than isUtc (#21276)

parent c1b70787
...@@ -246,7 +246,7 @@ describe('Format value', () => { ...@@ -246,7 +246,7 @@ describe('Format value', () => {
describe('Date display options', () => { describe('Date display options', () => {
it('should format UTC dates', () => { it('should format UTC dates', () => {
const processor = getDisplayProcessor({ const processor = getDisplayProcessor({
isUtc: true, timeZone: 'utc',
field: { field: {
type: FieldType.time, type: FieldType.time,
config: { config: {
...@@ -259,7 +259,7 @@ describe('Date display options', () => { ...@@ -259,7 +259,7 @@ describe('Date display options', () => {
it('should pick configured time format', () => { it('should pick configured time format', () => {
const processor = getDisplayProcessor({ const processor = getDisplayProcessor({
isUtc: true, timeZone: 'utc',
field: { field: {
type: FieldType.time, type: FieldType.time,
config: { config: {
...@@ -272,7 +272,7 @@ describe('Date display options', () => { ...@@ -272,7 +272,7 @@ describe('Date display options', () => {
it('respect the configured date format', () => { it('respect the configured date format', () => {
const processor = getDisplayProcessor({ const processor = getDisplayProcessor({
isUtc: true, timeZone: 'utc',
field: { field: {
type: FieldType.time, type: FieldType.time,
config: { config: {
......
...@@ -8,14 +8,14 @@ import { DisplayProcessor, DisplayValue, DecimalCount, DecimalInfo } from '../ty ...@@ -8,14 +8,14 @@ import { DisplayProcessor, DisplayValue, DecimalCount, DecimalInfo } from '../ty
import { getValueFormat } from '../valueFormats/valueFormats'; import { getValueFormat } from '../valueFormats/valueFormats';
import { getMappedValue } from '../utils/valueMappings'; import { getMappedValue } from '../utils/valueMappings';
import { DEFAULT_DATE_TIME_FORMAT } from '../datetime'; import { DEFAULT_DATE_TIME_FORMAT } from '../datetime';
import { KeyValue } from '../types'; import { KeyValue, TimeZone } from '../types';
import { getScaleCalculator } from './scale'; import { getScaleCalculator } from './scale';
interface DisplayProcessorOptions { interface DisplayProcessorOptions {
field: Partial<Field>; field: Partial<Field>;
// Context // Context
isUtc?: boolean; timeZone?: TimeZone;
theme?: GrafanaTheme; // Will pick 'dark' if not defined theme?: GrafanaTheme; // Will pick 'dark' if not defined
} }
...@@ -73,7 +73,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP ...@@ -73,7 +73,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
if (!isNaN(numeric)) { if (!isNaN(numeric)) {
if (shouldFormat && !_.isBoolean(value)) { if (shouldFormat && !_.isBoolean(value)) {
const { decimals, scaledDecimals } = getDecimalsForValue(value, config.decimals); const { decimals, scaledDecimals } = getDecimalsForValue(value, config.decimals);
const v = formatFunc(numeric, decimals, scaledDecimals, options.isUtc); const v = formatFunc(numeric, decimals, scaledDecimals, options.timeZone);
text = v.text; text = v.text;
suffix = v.suffix; suffix = v.suffix;
prefix = v.prefix; prefix = v.prefix;
......
...@@ -19,81 +19,81 @@ describe('date time formats', () => { ...@@ -19,81 +19,81 @@ describe('date time formats', () => {
it('should format as iso date', () => { it('should format as iso date', () => {
const expected = browserTime.format('YYYY-MM-DD HH:mm:ss'); const expected = browserTime.format('YYYY-MM-DD HH:mm:ss');
const actual = dateTimeAsIso(epoch, 0, 0, false); const actual = dateTimeAsIso(epoch, 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as iso date (in UTC)', () => { it('should format as iso date (in UTC)', () => {
const expected = utcTime.format('YYYY-MM-DD HH:mm:ss'); const expected = utcTime.format('YYYY-MM-DD HH:mm:ss');
const actual = dateTimeAsIso(epoch, 0, 0, true); const actual = dateTimeAsIso(epoch, 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as iso date and skip date when today', () => { it('should format as iso date and skip date when today', () => {
const now = dateTime(); const now = dateTime();
const expected = now.format('HH:mm:ss'); const expected = now.format('HH:mm:ss');
const actual = dateTimeAsIso(now.valueOf(), 0, 0, false); const actual = dateTimeAsIso(now.valueOf(), 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as iso date (in UTC) and skip date when today', () => { it('should format as iso date (in UTC) and skip date when today', () => {
const now = toUtc(); const now = toUtc();
const expected = now.format('HH:mm:ss'); const expected = now.format('HH:mm:ss');
const actual = dateTimeAsIso(now.valueOf(), 0, 0, true); const actual = dateTimeAsIso(now.valueOf(), 0, 0, 'utc');
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as US date', () => { it('should format as US date', () => {
const expected = browserTime.format('MM/DD/YYYY h:mm:ss a'); const expected = browserTime.format('MM/DD/YYYY h:mm:ss a');
const actual = dateTimeAsUS(epoch, 0, 0, false); const actual = dateTimeAsUS(epoch, 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as US date (in UTC)', () => { it('should format as US date (in UTC)', () => {
const expected = utcTime.format('MM/DD/YYYY h:mm:ss a'); const expected = utcTime.format('MM/DD/YYYY h:mm:ss a');
const actual = dateTimeAsUS(epoch, 0, 0, true); const actual = dateTimeAsUS(epoch, 0, 0, 'utc');
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as US date and skip date when today', () => { it('should format as US date and skip date when today', () => {
const now = dateTime(); const now = dateTime();
const expected = now.format('h:mm:ss a'); const expected = now.format('h:mm:ss a');
const actual = dateTimeAsUS(now.valueOf(), 0, 0, false); const actual = dateTimeAsUS(now.valueOf(), 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as US date (in UTC) and skip date when today', () => { it('should format as US date (in UTC) and skip date when today', () => {
const now = toUtc(); const now = toUtc();
const expected = now.format('h:mm:ss a'); const expected = now.format('h:mm:ss a');
const actual = dateTimeAsUS(now.valueOf(), 0, 0, true); const actual = dateTimeAsUS(now.valueOf(), 0, 0, 'utc');
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as from now with days', () => { it('should format as from now with days', () => {
const daysAgo = dateTime().add(-7, 'd'); const daysAgo = dateTime().add(-7, 'd');
const expected = '7 days ago'; const expected = '7 days ago';
const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, false); const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as from now with days (in UTC)', () => { it('should format as from now with days (in UTC)', () => {
const daysAgo = toUtc().add(-7, 'd'); const daysAgo = toUtc().add(-7, 'd');
const expected = '7 days ago'; const expected = '7 days ago';
const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, true); const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, 'utc');
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as from now with minutes', () => { it('should format as from now with minutes', () => {
const daysAgo = dateTime().add(-2, 'm'); const daysAgo = dateTime().add(-2, 'm');
const expected = '2 minutes ago'; const expected = '2 minutes ago';
const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, false); const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0);
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
it('should format as from now with minutes (in UTC)', () => { it('should format as from now with minutes (in UTC)', () => {
const daysAgo = toUtc().add(-2, 'm'); const daysAgo = toUtc().add(-2, 'm');
const expected = '2 minutes ago'; const expected = '2 minutes ago';
const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, true); const actual = dateTimeFromNow(daysAgo.valueOf(), 0, 0, 'utc');
expect(actual.text).toBe(expected); expect(actual.text).toBe(expected);
}); });
}); });
......
...@@ -2,6 +2,7 @@ import { toDuration as duration, toUtc, dateTime } from '../datetime/moment_wrap ...@@ -2,6 +2,7 @@ import { toDuration as duration, toUtc, dateTime } from '../datetime/moment_wrap
import { toFixed, toFixedScaled, FormattedValue, ValueFormatter } from './valueFormats'; import { toFixed, toFixedScaled, FormattedValue, ValueFormatter } from './valueFormats';
import { DecimalCount } from '../types/displayValue'; import { DecimalCount } from '../types/displayValue';
import { TimeZone } from '../types';
interface IntervalsInSeconds { interface IntervalsInSeconds {
[interval: string]: number; [interval: string]: number;
...@@ -324,7 +325,8 @@ export function toClockSeconds(size: number, decimals: DecimalCount): FormattedV ...@@ -324,7 +325,8 @@ export function toClockSeconds(size: number, decimals: DecimalCount): FormattedV
} }
export function toDateTimeValueFormatter(pattern: string, todayPattern?: string): ValueFormatter { export function toDateTimeValueFormatter(pattern: string, todayPattern?: string): ValueFormatter {
return (value: number, decimals: DecimalCount, scaledDecimals: DecimalCount, isUtc?: boolean): FormattedValue => { return (value: number, decimals: DecimalCount, scaledDecimals: DecimalCount, timeZone?: TimeZone): FormattedValue => {
const isUtc = timeZone === 'utc';
const time = isUtc ? toUtc(value) : dateTime(value); const time = isUtc ? toUtc(value) : dateTime(value);
if (todayPattern) { if (todayPattern) {
if (dateTime().isSame(value, 'day')) { if (dateTime().isSame(value, 'day')) {
...@@ -342,8 +344,9 @@ export function dateTimeFromNow( ...@@ -342,8 +344,9 @@ export function dateTimeFromNow(
value: number, value: number,
decimals: DecimalCount, decimals: DecimalCount,
scaledDecimals: DecimalCount, scaledDecimals: DecimalCount,
isUtc?: boolean timeZone?: TimeZone
): FormattedValue { ): FormattedValue {
const isUtc = timeZone === 'utc';
const time = isUtc ? toUtc(value) : dateTime(value); const time = isUtc ? toUtc(value) : dateTime(value);
return { text: time.fromNow() }; return { text: time.fromNow() };
} }
...@@ -2,6 +2,7 @@ import { getCategories } from './categories'; ...@@ -2,6 +2,7 @@ import { getCategories } from './categories';
import { DecimalCount } from '../types/displayValue'; import { DecimalCount } from '../types/displayValue';
import { toDateTimeValueFormatter } from './dateTimeFormatters'; import { toDateTimeValueFormatter } from './dateTimeFormatters';
import { getOffsetFromSIPrefix, decimalSIPrefix, currency } from './symbolFormatters'; import { getOffsetFromSIPrefix, decimalSIPrefix, currency } from './symbolFormatters';
import { TimeZone } from '../types';
export interface FormattedValue { export interface FormattedValue {
text: string; text: string;
...@@ -17,7 +18,7 @@ export type ValueFormatter = ( ...@@ -17,7 +18,7 @@ export type ValueFormatter = (
value: number, value: number,
decimals?: DecimalCount, decimals?: DecimalCount,
scaledDecimals?: DecimalCount, scaledDecimals?: DecimalCount,
isUtc?: boolean // TODO: timezone?: string, timeZone?: TimeZone
) => FormattedValue; ) => FormattedValue;
export interface ValueFormat { export interface ValueFormat {
......
...@@ -26,8 +26,8 @@ export const mapOptionToTimeRange = (option: TimeOption, timeZone?: TimeZone): T ...@@ -26,8 +26,8 @@ export const mapOptionToTimeRange = (option: TimeOption, timeZone?: TimeZone): T
export const mapRangeToTimeOption = (range: TimeRange, timeZone?: TimeZone): TimeOption => { export const mapRangeToTimeOption = (range: TimeRange, timeZone?: TimeZone): TimeOption => {
const formattedFrom = stringToDateTime(range.from, false, timeZone).format(TIME_FORMAT); const formattedFrom = stringToDateTime(range.from, false, timeZone).format(TIME_FORMAT);
const formattedTo = stringToDateTime(range.to, true, timeZone).format(TIME_FORMAT); const formattedTo = stringToDateTime(range.to, true, timeZone).format(TIME_FORMAT);
const from = dateTimeToString(range.from, timeZone === 'utc'); const from = dateTimeToString(range.from, timeZone);
const to = dateTimeToString(range.to, timeZone === 'utc'); const to = dateTimeToString(range.to, timeZone);
return { return {
from, from,
...@@ -82,11 +82,12 @@ const stringToDateTime = (value: string | DateTime, roundUp?: boolean, timeZone? ...@@ -82,11 +82,12 @@ const stringToDateTime = (value: string | DateTime, roundUp?: boolean, timeZone?
return dateTimeForTimeZone(timeZone, value, TIME_FORMAT); return dateTimeForTimeZone(timeZone, value, TIME_FORMAT);
}; };
const dateTimeToString = (value: DateTime, isUtc: boolean): string => { const dateTimeToString = (value: DateTime, timeZone?: TimeZone): string => {
if (!isDateTime(value)) { if (!isDateTime(value)) {
return value; return value;
} }
const isUtc = timeZone === 'utc';
if (isUtc) { if (isUtc) {
return value.utc().format(TIME_FORMAT); return value.utc().format(TIME_FORMAT);
} }
......
...@@ -151,7 +151,7 @@ export function makeSeriesForLogs(rows: LogRowModel[], intervalMs: number, timeZ ...@@ -151,7 +151,7 @@ export function makeSeriesForLogs(rows: LogRowModel[], intervalMs: number, timeZ
const timeField = data.fields[1]; const timeField = data.fields[1];
timeField.display = getDisplayProcessor({ timeField.display = getDisplayProcessor({
field: timeField, field: timeField,
isUtc: timeZone === 'utc', timeZone,
}); });
const valueField = data.fields[0]; const valueField = data.fields[0];
......
...@@ -312,7 +312,7 @@ if (typeof Proxy !== 'undefined') { ...@@ -312,7 +312,7 @@ if (typeof Proxy !== 'undefined') {
if (formatter) { if (formatter) {
// Return the results as a simple string // Return the results as a simple string
return (value: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount, isUtc?: boolean) => { return (value: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount, isUtc?: boolean) => {
return formattedValueToString(formatter(value, decimals, scaledDecimals, isUtc)); return formattedValueToString(formatter(value, decimals, scaledDecimals, isUtc ? 'utc' : 'browser'));
}; };
} }
......
...@@ -18,11 +18,12 @@ import { TemplateSrv } from '../templating/template_srv'; ...@@ -18,11 +18,12 @@ import { TemplateSrv } from '../templating/template_srv';
import { getPanelLinksSupplier } from './panellinks/linkSuppliers'; import { getPanelLinksSupplier } from './panellinks/linkSuppliers';
import { AppEvent, PanelEvents, PanelPluginMeta, renderMarkdown } from '@grafana/data'; import { AppEvent, PanelEvents, PanelPluginMeta, renderMarkdown } from '@grafana/data';
import { getLocationSrv } from '@grafana/runtime'; import { getLocationSrv } from '@grafana/runtime';
import { DashboardModel } from '../dashboard/state';
export class PanelCtrl { export class PanelCtrl {
panel: any; panel: any;
error: any; error: any;
dashboard: any; dashboard: DashboardModel;
pluginName: string; pluginName: string;
pluginId: string; pluginId: string;
editorTabs: any; editorTabs: any;
......
...@@ -110,7 +110,7 @@ export const getGraphSeriesModel = ( ...@@ -110,7 +110,7 @@ export const getGraphSeriesModel = (
const useMsDateFormat = hasMsResolution(timeField); const useMsDateFormat = hasMsResolution(timeField);
timeField.display = getDisplayProcessor({ timeField.display = getDisplayProcessor({
isUtc: timeZone === 'utc', timeZone,
field: { field: {
...timeField, ...timeField,
type: timeField.type, type: timeField.type,
......
...@@ -254,7 +254,7 @@ class SingleStatCtrl extends MetricsPanelCtrl { ...@@ -254,7 +254,7 @@ class SingleStatCtrl extends MetricsPanelCtrl {
}, },
}, },
theme: config.theme, theme: config.theme,
isUtc: dashboard.isTimezoneUtc && dashboard.isTimezoneUtc(), timeZone: dashboard.getTimezone(),
}); });
const sparkline: any[] = []; const sparkline: any[] = [];
......
...@@ -2,6 +2,7 @@ import { SingleStatCtrl, ShowData } from '../module'; ...@@ -2,6 +2,7 @@ import { SingleStatCtrl, ShowData } from '../module';
import { dateTime, ReducerID } from '@grafana/data'; import { dateTime, ReducerID } from '@grafana/data';
import { LinkSrv } from 'app/features/panel/panellinks/link_srv'; import { LinkSrv } from 'app/features/panel/panellinks/link_srv';
import { LegacyResponseData } from '@grafana/data'; import { LegacyResponseData } from '@grafana/data';
import { DashboardModel } from 'app/features/dashboard/state';
interface TestContext { interface TestContext {
ctrl: SingleStatCtrl; ctrl: SingleStatCtrl;
...@@ -31,9 +32,9 @@ describe('SingleStatCtrl', () => { ...@@ -31,9 +32,9 @@ describe('SingleStatCtrl', () => {
emit: () => {}, emit: () => {},
}, },
}; };
SingleStatCtrl.prototype.dashboard = { SingleStatCtrl.prototype.dashboard = ({
isTimezoneUtc: jest.fn(() => true), getTimezone: jest.fn(() => 'utc'),
}; } as any) as DashboardModel;
SingleStatCtrl.prototype.events = { SingleStatCtrl.prototype.events = {
on: () => {}, on: () => {},
}; };
...@@ -112,7 +113,7 @@ describe('SingleStatCtrl', () => { ...@@ -112,7 +113,7 @@ describe('SingleStatCtrl', () => {
]; ];
ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.valueName = 'last_time';
ctx.ctrl.panel.format = 'dateTimeAsIso'; ctx.ctrl.panel.format = 'dateTimeAsIso';
ctx.ctrl.dashboard.isTimezoneUtc = () => false; ctx.ctrl.dashboard.getTimezone = () => 'browser';
}); });
it('Should use time instead of value', () => { it('Should use time instead of value', () => {
...@@ -137,7 +138,7 @@ describe('SingleStatCtrl', () => { ...@@ -137,7 +138,7 @@ describe('SingleStatCtrl', () => {
]; ];
ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.valueName = 'last_time';
ctx.ctrl.panel.format = 'dateTimeAsIso'; ctx.ctrl.panel.format = 'dateTimeAsIso';
ctx.ctrl.dashboard.isTimezoneUtc = () => true; ctx.ctrl.dashboard.getTimezone = () => 'utc';
}); });
it('should set value', () => { it('should set value', () => {
...@@ -158,7 +159,7 @@ describe('SingleStatCtrl', () => { ...@@ -158,7 +159,7 @@ describe('SingleStatCtrl', () => {
]; ];
ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.valueName = 'last_time';
ctx.ctrl.panel.format = 'dateTimeAsUS'; ctx.ctrl.panel.format = 'dateTimeAsUS';
ctx.ctrl.dashboard.isTimezoneUtc = () => false; ctx.ctrl.dashboard.getTimezone = () => 'browser';
}); });
it('Should use time instead of value', () => { it('Should use time instead of value', () => {
...@@ -183,7 +184,7 @@ describe('SingleStatCtrl', () => { ...@@ -183,7 +184,7 @@ describe('SingleStatCtrl', () => {
]; ];
ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.valueName = 'last_time';
ctx.ctrl.panel.format = 'dateTimeAsUS'; ctx.ctrl.panel.format = 'dateTimeAsUS';
ctx.ctrl.dashboard.isTimezoneUtc = () => true; ctx.ctrl.dashboard.getTimezone = () => 'utc';
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
......
...@@ -54,6 +54,9 @@ export function ControllerTestContext(this: any) { ...@@ -54,6 +54,9 @@ export function ControllerTestContext(this: any) {
self.panel = new PanelModel({ type: 'test' }); self.panel = new PanelModel({ type: 'test' });
self.dashboard = { meta: {} }; self.dashboard = { meta: {} };
self.isUtc = false; self.isUtc = false;
self.dashboard.getTimezone = () => {
return self.isUtc ? 'utc' : 'browser';
};
self.dashboard.isTimezoneUtc = () => { self.dashboard.isTimezoneUtc = () => {
return self.isUtc; return self.isUtc;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment