@grafana/scenes 5.18.1 β 5.18.3
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +24 -0
- package/dist/esm/core/SceneTimeRange.js +13 -8
- package/dist/esm/core/SceneTimeRange.js.map +1 -1
- package/dist/esm/utils/date.js +15 -0
- package/dist/esm/utils/date.js.map +1 -0
- package/dist/esm/utils/parseUrlParam.js +5 -0
- package/dist/esm/utils/parseUrlParam.js.map +1 -1
- package/dist/esm/variables/interpolation/formatRegistry.js +5 -1
- package/dist/esm/variables/interpolation/formatRegistry.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +32 -8
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
# v5.18.3 (Tue Oct 08 2024)
|
2
|
+
|
3
|
+
#### π Bug Fix
|
4
|
+
|
5
|
+
- Add checks for valid dates before saving calculating values [#914](https://github.com/grafana/scenes/pull/914) ([@javiruiz01](https://github.com/javiruiz01))
|
6
|
+
|
7
|
+
#### Authors: 1
|
8
|
+
|
9
|
+
- Javier Ruiz ([@javiruiz01](https://github.com/javiruiz01))
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
# v5.18.2 (Mon Oct 07 2024)
|
14
|
+
|
15
|
+
#### π Bug Fix
|
16
|
+
|
17
|
+
- Interpolation: Fixes queryparam variable format when used with adhoc filter variable [#931](https://github.com/grafana/scenes/pull/931) ([@torkelo](https://github.com/torkelo))
|
18
|
+
|
19
|
+
#### Authors: 1
|
20
|
+
|
21
|
+
- Torkel Γdegaard ([@torkelo](https://github.com/torkelo))
|
22
|
+
|
23
|
+
---
|
24
|
+
|
1
25
|
# v5.18.0 (Fri Oct 04 2024)
|
2
26
|
|
3
27
|
#### π Enhancement
|
@@ -5,6 +5,7 @@ import { getClosest } from './sceneGraph/utils.js';
|
|
5
5
|
import { parseUrlParam } from '../utils/parseUrlParam.js';
|
6
6
|
import { evaluateTimeRange } from '../utils/evaluateTimeRange.js';
|
7
7
|
import { RefreshEvent, locationService } from '@grafana/runtime';
|
8
|
+
import { isValid } from '../utils/date.js';
|
8
9
|
|
9
10
|
var __defProp = Object.defineProperty;
|
10
11
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
@@ -24,9 +25,9 @@ var __spreadValues = (a, b) => {
|
|
24
25
|
};
|
25
26
|
class SceneTimeRange extends SceneObjectBase {
|
26
27
|
constructor(state = {}) {
|
27
|
-
var _a
|
28
|
-
const from =
|
29
|
-
const to =
|
28
|
+
var _a;
|
29
|
+
const from = state.from && isValid(state.from) ? state.from : "now-6h";
|
30
|
+
const to = state.to && isValid(state.to) ? state.to : "now";
|
30
31
|
const timeZone = state.timeZone;
|
31
32
|
const value = evaluateTimeRange(
|
32
33
|
from,
|
@@ -35,7 +36,7 @@ class SceneTimeRange extends SceneObjectBase {
|
|
35
36
|
state.fiscalYearStartMonth,
|
36
37
|
state.UNSAFE_nowDelay
|
37
38
|
);
|
38
|
-
const refreshOnActivate = (
|
39
|
+
const refreshOnActivate = (_a = state.refreshOnActivate) != null ? _a : { percent: 10 };
|
39
40
|
super(__spreadValues({ from, to, timeZone, value, refreshOnActivate }, state));
|
40
41
|
this._urlSync = new SceneObjectUrlSyncConfig(this, { keys: ["from", "to", "timezone", "time", "time.window"] });
|
41
42
|
this.onTimeRangeChange = (timeRange) => {
|
@@ -186,16 +187,20 @@ class SceneTimeRange extends SceneObjectBase {
|
|
186
187
|
const time = Array.isArray(values.time) ? values.time[0] : values.time;
|
187
188
|
const timeWindow = Array.isArray(values["time.window"]) ? values["time.window"][0] : values["time.window"];
|
188
189
|
const timeRange = getTimeWindow(time, timeWindow);
|
189
|
-
from
|
190
|
-
|
190
|
+
if (timeRange.from && isValid(timeRange.from)) {
|
191
|
+
from = timeRange.from;
|
192
|
+
}
|
193
|
+
if (timeRange.to && isValid(timeRange.to)) {
|
194
|
+
to = timeRange.to;
|
195
|
+
}
|
191
196
|
}
|
192
197
|
if (!from && !to) {
|
193
198
|
return;
|
194
199
|
}
|
195
|
-
if (from) {
|
200
|
+
if (from && isValid(from)) {
|
196
201
|
update.from = from;
|
197
202
|
}
|
198
|
-
if (to) {
|
203
|
+
if (to && isValid(to)) {
|
199
204
|
update.to = to;
|
200
205
|
}
|
201
206
|
if (typeof values.timezone === "string") {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"SceneTimeRange.js","sources":["../../../src/core/SceneTimeRange.tsx"],"sourcesContent":["import { getTimeZone, rangeUtil, TimeRange, toUtc } from '@grafana/data';\nimport { TimeZone } from '@grafana/schema';\n\nimport { SceneObjectUrlSyncConfig } from '../services/SceneObjectUrlSyncConfig';\n\nimport { SceneObjectBase } from './SceneObjectBase';\nimport { SceneTimeRangeLike, SceneTimeRangeState, SceneObjectUrlValues } from './types';\nimport { getClosest } from './sceneGraph/utils';\nimport { parseUrlParam } from '../utils/parseUrlParam';\nimport { evaluateTimeRange } from '../utils/evaluateTimeRange';\nimport { locationService, RefreshEvent } from '@grafana/runtime';\n\nexport class SceneTimeRange extends SceneObjectBase<SceneTimeRangeState> implements SceneTimeRangeLike {\n protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['from', 'to', 'timezone', 'time', 'time.window'] });\n\n public constructor(state: Partial<SceneTimeRangeState> = {}) {\n const from = state.from ?? 'now-6h';\n const to = state.to ?? 'now';\n const timeZone = state.timeZone;\n const value = evaluateTimeRange(\n from,\n to,\n timeZone || getTimeZone(),\n state.fiscalYearStartMonth,\n state.UNSAFE_nowDelay\n );\n const refreshOnActivate = state.refreshOnActivate ?? { percent: 10 };\n super({ from, to, timeZone, value, refreshOnActivate, ...state });\n\n this.addActivationHandler(this._onActivate.bind(this));\n }\n\n private _onActivate() {\n // When SceneTimeRange has no time zone provided, find closest source of time zone and subscribe to it\n if (!this.state.timeZone) {\n const timeZoneSource = this.getTimeZoneSource();\n if (timeZoneSource !== this) {\n this._subs.add(\n timeZoneSource.subscribeToState((n, p) => {\n if (n.timeZone !== undefined && n.timeZone !== p.timeZone) {\n this.setState({\n value: evaluateTimeRange(\n this.state.from,\n this.state.to,\n timeZoneSource.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n ),\n });\n }\n })\n );\n }\n }\n\n if (rangeUtil.isRelativeTimeRange(this.state.value.raw)) {\n this.refreshIfStale();\n }\n }\n\n private refreshIfStale() {\n let ms;\n if (this.state?.refreshOnActivate?.percent !== undefined) {\n ms = this.calculatePercentOfInterval(this.state.refreshOnActivate.percent);\n }\n if (this.state?.refreshOnActivate?.afterMs !== undefined) {\n ms = Math.min(this.state.refreshOnActivate.afterMs, ms ?? Infinity);\n }\n if (ms !== undefined) {\n this.refreshRange(ms);\n }\n }\n\n /**\n * Will traverse up the scene graph to find the closest SceneTimeRangeLike with time zone set\n */\n private getTimeZoneSource() {\n if (!this.parent || !this.parent.parent) {\n return this;\n }\n // Find the closest source of time zone\n const source = getClosest<SceneTimeRangeLike>(this.parent.parent, (o) => {\n if (o.state.$timeRange && o.state.$timeRange.state.timeZone) {\n return o.state.$timeRange;\n }\n return undefined;\n });\n\n if (!source) {\n return this;\n }\n\n return source;\n }\n\n /**\n * Refreshes time range if it is older than the invalidation interval\n * @param refreshAfterMs invalidation interval (milliseconds)\n * @private\n */\n private refreshRange(refreshAfterMs: number) {\n const value = evaluateTimeRange(\n this.state.from,\n this.state.to,\n this.state.timeZone ?? getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n const diff = value.to.diff(this.state.value.to, 'milliseconds');\n if (diff >= refreshAfterMs) {\n this.setState({\n value,\n });\n }\n }\n\n private calculatePercentOfInterval(percent: number): number {\n const intervalMs = this.state.value.to.diff(this.state.value.from, 'milliseconds');\n return Math.ceil(intervalMs / percent);\n }\n\n public getTimeZone(): TimeZone {\n // Return local time zone if provided\n if (this.state.timeZone) {\n return this.state.timeZone;\n }\n\n // Resolve higher level time zone source\n const timeZoneSource = this.getTimeZoneSource();\n if (timeZoneSource !== this) {\n return timeZoneSource.state.timeZone!;\n }\n\n // Return default time zone\n return getTimeZone();\n }\n\n public onTimeRangeChange = (timeRange: TimeRange) => {\n const update: Partial<SceneTimeRangeState> = {};\n\n if (typeof timeRange.raw.from === 'string') {\n update.from = timeRange.raw.from;\n } else {\n update.from = timeRange.raw.from.toISOString();\n }\n\n if (typeof timeRange.raw.to === 'string') {\n update.to = timeRange.raw.to;\n } else {\n update.to = timeRange.raw.to.toISOString();\n }\n\n update.value = evaluateTimeRange(\n update.from,\n update.to,\n this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n // Only update if time range actually changed\n if (update.from !== this.state.from || update.to !== this.state.to) {\n this._urlSync.performBrowserHistoryAction(() => {\n this.setState(update);\n });\n }\n };\n\n public onTimeZoneChange = (timeZone: TimeZone) => {\n this._urlSync.performBrowserHistoryAction(() => {\n this.setState({ timeZone });\n });\n };\n\n public onRefresh = () => {\n this.setState({\n value: evaluateTimeRange(\n this.state.from,\n this.state.to,\n this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n ),\n });\n\n this.publishEvent(new RefreshEvent(), true);\n };\n\n public getUrlState() {\n const params = locationService.getSearchObject();\n const urlValues: SceneObjectUrlValues = { from: this.state.from, to: this.state.to };\n\n if (this.state.timeZone) {\n urlValues.timezone = this.state.timeZone;\n }\n\n // Clear time and time.window once they are converted to from and to\n if (params.time && params['time.window']) {\n urlValues.time = null;\n urlValues['time.window'] = null;\n }\n\n return urlValues;\n }\n\n public updateFromUrl(values: SceneObjectUrlValues) {\n const update: Partial<SceneTimeRangeState> = {};\n\n let from = parseUrlParam(values.from);\n let to = parseUrlParam(values.to);\n\n if (values.time && values['time.window']) {\n const time = Array.isArray(values.time) ? values.time[0] : values.time;\n const timeWindow = Array.isArray(values['time.window']) ? values['time.window'][0] : values['time.window'];\n const timeRange = getTimeWindow(time, timeWindow);\n from = timeRange.from;\n to = timeRange.to;\n }\n\n if (!from && !to) {\n return;\n }\n\n if (from) {\n update.from = from;\n }\n\n if (to) {\n update.to = to;\n }\n\n if (typeof values.timezone === 'string') {\n update.timeZone = values.timezone !== '' ? values.timezone : undefined;\n }\n\n update.value = evaluateTimeRange(\n update.from ?? this.state.from,\n update.to ?? this.state.to,\n update.timeZone ?? this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n this.setState(update);\n }\n}\n\n/**\n * Calculates the duration of the time range from time-time.window/2 to time+time.window/2. Both be specified in ms. For example ?time=1500000000000&time.window=10000 results in a 10-second time range from 1499999995000 to 1500000005000`.\n * @param time - time in ms\n * @param timeWindow - time window in ms or interval string\n */\nfunction getTimeWindow(time: string, timeWindow: string) {\n // Parse the time, assuming it could be an ISO string or a number in milliseconds\n const valueTime = isNaN(Date.parse(time)) ? parseInt(time, 10) : Date.parse(time);\n\n let timeWindowMs;\n\n if (timeWindow.match(/^\\d+$/) && parseInt(timeWindow, 10)) {\n // when time window is specified in ms\n timeWindowMs = parseInt(timeWindow, 10);\n } else {\n timeWindowMs = rangeUtil.intervalToMs(timeWindow);\n }\n\n return {\n from: toUtc(valueTime - timeWindowMs / 2).toISOString(),\n to: toUtc(valueTime + timeWindowMs / 2).toISOString(),\n };\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAYO,MAAM,uBAAuB,eAAmE,CAAA;AAAA,EAG9F,WAAA,CAAY,KAAsC,GAAA,EAAI,EAAA;AAf/D,IAAA,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,CAAA;AAgBI,IAAM,MAAA,IAAA,GAAA,CAAO,EAAM,GAAA,KAAA,CAAA,IAAA,KAAN,IAAc,GAAA,EAAA,GAAA,QAAA,CAAA;AAC3B,IAAM,MAAA,EAAA,GAAA,CAAK,EAAM,GAAA,KAAA,CAAA,EAAA,KAAN,IAAY,GAAA,EAAA,GAAA,KAAA,CAAA;AACvB,IAAA,MAAM,WAAW,KAAM,CAA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AM,CAAA,OAAO,KAAK,QAAS,CAAA,UAAA,EAAY,EAAE,CAAG,EAAA;AAEzD,IAAe,YAAA,GAAA,QAAA,CAAS,YAAY,EAAE,CAAA,CAAA;AAAA,GACjC,MAAA;AACL,IAAe,YAAA,GAAA,SAAA,CAAU,aAAa,UAAU,CAAA,CAAA;AAAA,GAClD;AAEA,EAAO,OAAA;AAAA,IACL,MAAM,KAAM,CAAA,SAAA,GAAY,YAAe,GAAA,CAAC,EAAE,WAAY,EAAA;AAAA,IACtD,IAAI,KAAM,CAAA,SAAA,GAAY,YAAe,GAAA,CAAC,EAAE,WAAY,EAAA;AAAA,GACtD,CAAA;AACF;;;;"}
|
1
|
+
{"version":3,"file":"SceneTimeRange.js","sources":["../../../src/core/SceneTimeRange.tsx"],"sourcesContent":["import { getTimeZone, rangeUtil, TimeRange, toUtc } from '@grafana/data';\nimport { TimeZone } from '@grafana/schema';\n\nimport { SceneObjectUrlSyncConfig } from '../services/SceneObjectUrlSyncConfig';\n\nimport { SceneObjectBase } from './SceneObjectBase';\nimport { SceneTimeRangeLike, SceneTimeRangeState, SceneObjectUrlValues } from './types';\nimport { getClosest } from './sceneGraph/utils';\nimport { parseUrlParam } from '../utils/parseUrlParam';\nimport { evaluateTimeRange } from '../utils/evaluateTimeRange';\nimport { locationService, RefreshEvent } from '@grafana/runtime';\nimport { isValid } from '../utils/date';\n\nexport class SceneTimeRange extends SceneObjectBase<SceneTimeRangeState> implements SceneTimeRangeLike {\n protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['from', 'to', 'timezone', 'time', 'time.window'] });\n\n public constructor(state: Partial<SceneTimeRangeState> = {}) {\n const from = state.from && isValid(state.from) ? state.from : 'now-6h';\n const to = state.to && isValid(state.to) ? state.to : 'now';\n\n const timeZone = state.timeZone;\n const value = evaluateTimeRange(\n from,\n to,\n timeZone || getTimeZone(),\n state.fiscalYearStartMonth,\n state.UNSAFE_nowDelay\n );\n const refreshOnActivate = state.refreshOnActivate ?? { percent: 10 };\n super({ from, to, timeZone, value, refreshOnActivate, ...state });\n\n this.addActivationHandler(this._onActivate.bind(this));\n }\n\n private _onActivate() {\n // When SceneTimeRange has no time zone provided, find closest source of time zone and subscribe to it\n if (!this.state.timeZone) {\n const timeZoneSource = this.getTimeZoneSource();\n if (timeZoneSource !== this) {\n this._subs.add(\n timeZoneSource.subscribeToState((n, p) => {\n if (n.timeZone !== undefined && n.timeZone !== p.timeZone) {\n this.setState({\n value: evaluateTimeRange(\n this.state.from,\n this.state.to,\n timeZoneSource.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n ),\n });\n }\n })\n );\n }\n }\n\n if (rangeUtil.isRelativeTimeRange(this.state.value.raw)) {\n this.refreshIfStale();\n }\n }\n\n private refreshIfStale() {\n let ms;\n if (this.state?.refreshOnActivate?.percent !== undefined) {\n ms = this.calculatePercentOfInterval(this.state.refreshOnActivate.percent);\n }\n if (this.state?.refreshOnActivate?.afterMs !== undefined) {\n ms = Math.min(this.state.refreshOnActivate.afterMs, ms ?? Infinity);\n }\n if (ms !== undefined) {\n this.refreshRange(ms);\n }\n }\n\n /**\n * Will traverse up the scene graph to find the closest SceneTimeRangeLike with time zone set\n */\n private getTimeZoneSource() {\n if (!this.parent || !this.parent.parent) {\n return this;\n }\n // Find the closest source of time zone\n const source = getClosest<SceneTimeRangeLike>(this.parent.parent, (o) => {\n if (o.state.$timeRange && o.state.$timeRange.state.timeZone) {\n return o.state.$timeRange;\n }\n return undefined;\n });\n\n if (!source) {\n return this;\n }\n\n return source;\n }\n\n /**\n * Refreshes time range if it is older than the invalidation interval\n * @param refreshAfterMs invalidation interval (milliseconds)\n * @private\n */\n private refreshRange(refreshAfterMs: number) {\n const value = evaluateTimeRange(\n this.state.from,\n this.state.to,\n this.state.timeZone ?? getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n const diff = value.to.diff(this.state.value.to, 'milliseconds');\n if (diff >= refreshAfterMs) {\n this.setState({\n value,\n });\n }\n }\n\n private calculatePercentOfInterval(percent: number): number {\n const intervalMs = this.state.value.to.diff(this.state.value.from, 'milliseconds');\n return Math.ceil(intervalMs / percent);\n }\n\n public getTimeZone(): TimeZone {\n // Return local time zone if provided\n if (this.state.timeZone) {\n return this.state.timeZone;\n }\n\n // Resolve higher level time zone source\n const timeZoneSource = this.getTimeZoneSource();\n if (timeZoneSource !== this) {\n return timeZoneSource.state.timeZone!;\n }\n\n // Return default time zone\n return getTimeZone();\n }\n\n public onTimeRangeChange = (timeRange: TimeRange) => {\n const update: Partial<SceneTimeRangeState> = {};\n\n if (typeof timeRange.raw.from === 'string') {\n update.from = timeRange.raw.from;\n } else {\n update.from = timeRange.raw.from.toISOString();\n }\n\n if (typeof timeRange.raw.to === 'string') {\n update.to = timeRange.raw.to;\n } else {\n update.to = timeRange.raw.to.toISOString();\n }\n\n update.value = evaluateTimeRange(\n update.from,\n update.to,\n this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n // Only update if time range actually changed\n if (update.from !== this.state.from || update.to !== this.state.to) {\n this._urlSync.performBrowserHistoryAction(() => {\n this.setState(update);\n });\n }\n };\n\n public onTimeZoneChange = (timeZone: TimeZone) => {\n this._urlSync.performBrowserHistoryAction(() => {\n this.setState({ timeZone });\n });\n };\n\n public onRefresh = () => {\n this.setState({\n value: evaluateTimeRange(\n this.state.from,\n this.state.to,\n this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n ),\n });\n\n this.publishEvent(new RefreshEvent(), true);\n };\n\n public getUrlState() {\n const params = locationService.getSearchObject();\n const urlValues: SceneObjectUrlValues = { from: this.state.from, to: this.state.to };\n\n if (this.state.timeZone) {\n urlValues.timezone = this.state.timeZone;\n }\n\n // Clear time and time.window once they are converted to from and to\n if (params.time && params['time.window']) {\n urlValues.time = null;\n urlValues['time.window'] = null;\n }\n\n return urlValues;\n }\n\n public updateFromUrl(values: SceneObjectUrlValues) {\n const update: Partial<SceneTimeRangeState> = {};\n\n let from = parseUrlParam(values.from);\n let to = parseUrlParam(values.to);\n\n if (values.time && values['time.window']) {\n const time = Array.isArray(values.time) ? values.time[0] : values.time;\n const timeWindow = Array.isArray(values['time.window']) ? values['time.window'][0] : values['time.window'];\n const timeRange = getTimeWindow(time, timeWindow);\n if (timeRange.from && isValid(timeRange.from)) {\n from = timeRange.from;\n }\n\n if (timeRange.to && isValid(timeRange.to)) {\n to = timeRange.to;\n }\n }\n\n if (!from && !to) {\n return;\n }\n\n if (from && isValid(from)) {\n update.from = from;\n }\n\n if (to && isValid(to)) {\n update.to = to;\n }\n\n if (typeof values.timezone === 'string') {\n update.timeZone = values.timezone !== '' ? values.timezone : undefined;\n }\n\n update.value = evaluateTimeRange(\n update.from ?? this.state.from,\n update.to ?? this.state.to,\n update.timeZone ?? this.getTimeZone(),\n this.state.fiscalYearStartMonth,\n this.state.UNSAFE_nowDelay\n );\n\n this.setState(update);\n }\n}\n\n/**\n * Calculates the duration of the time range from time-time.window/2 to time+time.window/2. Both be specified in ms. For example ?time=1500000000000&time.window=10000 results in a 10-second time range from 1499999995000 to 1500000005000`.\n * @param time - time in ms\n * @param timeWindow - time window in ms or interval string\n */\nfunction getTimeWindow(time: string, timeWindow: string) {\n // Parse the time, assuming it could be an ISO string or a number in milliseconds\n const valueTime = isNaN(Date.parse(time)) ? parseInt(time, 10) : Date.parse(time);\n\n let timeWindowMs;\n\n if (timeWindow.match(/^\\d+$/) && parseInt(timeWindow, 10)) {\n // when time window is specified in ms\n timeWindowMs = parseInt(timeWindow, 10);\n } else {\n timeWindowMs = rangeUtil.intervalToMs(timeWindow);\n }\n\n return {\n from: toUtc(valueTime - timeWindowMs / 2).toISOString(),\n to: toUtc(valueTime + timeWindowMs / 2).toISOString(),\n };\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAaO,MAAM,uBAAuB,eAAmE,CAAA;AAAA,EAG9F,WAAA,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KAAP,IAAa,GAAA,EAAA,GAAA,IAAA,CAAK,KAAM,CAAA,EAAA;AAAA,MAAA,CACxB,EAAO,GAAA,MAAA,CAAA,QAAA,KAAP,IAAmB,GAAA,EAAA,GAAA,IAAA,CAAK,WAAY,EAAA;AAAA,MACpC,KAAK,KAAM,CAAA,oBAAA;AAAA,MACX,KAAK,KAAM,CAAA,eAAA;AAAA,KACb,CAAA;AAEA,IAAA,IAAA,CAAK,SAAS,MAAM,CAAA,CAAA;AAAA,GACtB;AACF,CAAA;AAOA,SAAS,aAAA,CAAc,MAAc,UAAoB,EAAA;AAEvD,EAAA,MAAM,SAAY,GAAA,KAAA,CAAM,IAAK,CAAA,KAAA,CAAM,IAAI,CAAC,CAAI,GAAA,QAAA,CAAS,IAAM,EAAA,EAAE,CAAI,GAAA,IAAA,CAAK,MAAM,IAAI,CAAA,CAAA;AAEhF,EAAI,IAAA,YAAA,CAAA;AAEJ,EAAA,IAAI,WAAW,KAAM,CAAA,OAAO,KAAK,QAAS,CAAA,UAAA,EAAY,EAAE,CAAG,EAAA;AAEzD,IAAe,YAAA,GAAA,QAAA,CAAS,YAAY,EAAE,CAAA,CAAA;AAAA,GACjC,MAAA;AACL,IAAe,YAAA,GAAA,SAAA,CAAU,aAAa,UAAU,CAAA,CAAA;AAAA,GAClD;AAEA,EAAO,OAAA;AAAA,IACL,MAAM,KAAM,CAAA,SAAA,GAAY,YAAe,GAAA,CAAC,EAAE,WAAY,EAAA;AAAA,IACtD,IAAI,KAAM,CAAA,SAAA,GAAY,YAAe,GAAA,CAAC,EAAE,WAAY,EAAA;AAAA,GACtD,CAAA;AACF;;;;"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { isDateTime, dateMath, dateTimeParse } from '@grafana/data';
|
2
|
+
|
3
|
+
function isValid(value, roundUp, timeZone) {
|
4
|
+
if (isDateTime(value)) {
|
5
|
+
return value.isValid();
|
6
|
+
}
|
7
|
+
if (dateMath.isMathString(value)) {
|
8
|
+
return dateMath.isValid(value);
|
9
|
+
}
|
10
|
+
const parsed = dateTimeParse(value, { roundUp, timeZone });
|
11
|
+
return parsed.isValid();
|
12
|
+
}
|
13
|
+
|
14
|
+
export { isValid };
|
15
|
+
//# sourceMappingURL=date.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"date.js","sources":["../../../src/utils/date.ts"],"sourcesContent":["import { isDateTime, dateMath, dateTimeParse } from '@grafana/data';\nimport { TimeZone } from '@grafana/schema';\n\nexport function isValid(value: string, roundUp?: boolean, timeZone?: TimeZone): boolean {\n if (isDateTime(value)) {\n return value.isValid();\n }\n\n if (dateMath.isMathString(value)) {\n return dateMath.isValid(value);\n }\n\n const parsed = dateTimeParse(value, { roundUp, timeZone });\n return parsed.isValid();\n}\n"],"names":[],"mappings":";;AAGgB,SAAA,OAAA,CAAQ,KAAe,EAAA,OAAA,EAAmB,QAA8B,EAAA;AACtF,EAAI,IAAA,UAAA,CAAW,KAAK,CAAG,EAAA;AACrB,IAAA,OAAO,MAAM,OAAQ,EAAA,CAAA;AAAA,GACvB;AAEA,EAAI,IAAA,QAAA,CAAS,YAAa,CAAA,KAAK,CAAG,EAAA;AAChC,IAAO,OAAA,QAAA,CAAS,QAAQ,KAAK,CAAA,CAAA;AAAA,GAC/B;AAEA,EAAA,MAAM,SAAS,aAAc,CAAA,KAAA,EAAO,EAAE,OAAA,EAAS,UAAU,CAAA,CAAA;AACzD,EAAA,OAAO,OAAO,OAAQ,EAAA,CAAA;AACxB;;;;"}
|
@@ -21,6 +21,11 @@ function parseUrlParam(value) {
|
|
21
21
|
if (utcValue.isValid()) {
|
22
22
|
return utcValue.toISOString();
|
23
23
|
}
|
24
|
+
} else if (value.length === 19) {
|
25
|
+
const utcValue = toUtc(value, "YYYY-MM-DD HH:mm:ss");
|
26
|
+
if (utcValue.isValid()) {
|
27
|
+
return utcValue.toISOString();
|
28
|
+
}
|
24
29
|
} else if (value.length === 24) {
|
25
30
|
const utcValue = toUtc(value);
|
26
31
|
return utcValue.toISOString();
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"parseUrlParam.js","sources":["../../../src/utils/parseUrlParam.ts"],"sourcesContent":["import { toUtc } from '@grafana/data';\nimport { SceneObjectUrlValue } from '../core/types';\n\nconst INTERVAL_STRING_REGEX = /^\\d+[yYmMsSwWhHdD]$/;\n\nexport function parseUrlParam(value: SceneObjectUrlValue): string | null {\n if (typeof value !== 'string') {\n return null;\n }\n\n if (value.indexOf('now') !== -1) {\n return value;\n }\n\n if (INTERVAL_STRING_REGEX.test(value)) {\n return value;\n }\n\n if (value.length === 8) {\n const utcValue = toUtc(value, 'YYYYMMDD');\n if (utcValue.isValid()) {\n return utcValue.toISOString();\n }\n } else if (value.length === 15) {\n const utcValue = toUtc(value, 'YYYYMMDDTHHmmss');\n if (utcValue.isValid()) {\n return utcValue.toISOString();\n }\n } else if (value.length === 24) {\n const utcValue = toUtc(value);\n return utcValue.toISOString();\n }\n\n const epoch = parseInt(value, 10);\n if (!isNaN(epoch)) {\n return toUtc(epoch).toISOString();\n }\n\n return null;\n}\n"],"names":[],"mappings":";;AAGA,MAAM,qBAAwB,GAAA,qBAAA,CAAA;AAEvB,SAAS,cAAc,KAA2C,EAAA;AACvE,EAAI,IAAA,OAAO,UAAU,QAAU,EAAA;AAC7B,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AAEA,EAAA,IAAI,KAAM,CAAA,OAAA,CAAQ,KAAK,CAAA,KAAM,CAAI,CAAA,EAAA;AAC/B,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAI,IAAA,qBAAA,CAAsB,IAAK,CAAA,KAAK,CAAG,EAAA;AACrC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAI,IAAA,KAAA,CAAM,WAAW,CAAG,EAAA;AACtB,IAAM,MAAA,QAAA,GAAW,KAAM,CAAA,KAAA,EAAO,UAAU,CAAA,CAAA;AACxC,IAAI,IAAA,QAAA,CAAS,SAAW,EAAA;AACtB,MAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,KAC9B;AAAA,GACF,MAAA,IAAW,KAAM,CAAA,MAAA,KAAW,EAAI,EAAA;AAC9B,IAAM,MAAA,QAAA,GAAW,KAAM,CAAA,KAAA,EAAO,iBAAiB,CAAA,CAAA;AAC/C,IAAI,IAAA,QAAA,CAAS,SAAW,EAAA;AACtB,MAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,KAC9B;AAAA,GACF,MAAA,IAAW,KAAM,CAAA,MAAA,KAAW,EAAI,EAAA;AAC9B,IAAM,MAAA,QAAA,GAAW,MAAM,KAAK,CAAA,CAAA;AAC5B,IAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,GAC9B;AAEA,EAAM,MAAA,KAAA,GAAQ,QAAS,CAAA,KAAA,EAAO,EAAE,CAAA,CAAA;AAChC,EAAI,IAAA,CAAC,KAAM,CAAA,KAAK,CAAG,EAAA;AACjB,IAAO,OAAA,KAAA,CAAM,KAAK,CAAA,CAAE,WAAY,EAAA,CAAA;AAAA,GAClC;AAEA,EAAO,OAAA,IAAA,CAAA;AACT;;;;"}
|
1
|
+
{"version":3,"file":"parseUrlParam.js","sources":["../../../src/utils/parseUrlParam.ts"],"sourcesContent":["import { toUtc } from '@grafana/data';\nimport { SceneObjectUrlValue } from '../core/types';\n\nconst INTERVAL_STRING_REGEX = /^\\d+[yYmMsSwWhHdD]$/;\n\nexport function parseUrlParam(value: SceneObjectUrlValue): string | null {\n if (typeof value !== 'string') {\n return null;\n }\n\n if (value.indexOf('now') !== -1) {\n return value;\n }\n\n if (INTERVAL_STRING_REGEX.test(value)) {\n return value;\n }\n\n if (value.length === 8) {\n const utcValue = toUtc(value, 'YYYYMMDD');\n if (utcValue.isValid()) {\n return utcValue.toISOString();\n }\n } else if (value.length === 15) {\n const utcValue = toUtc(value, 'YYYYMMDDTHHmmss');\n if (utcValue.isValid()) {\n return utcValue.toISOString();\n }\n } else if (value.length === 19) {\n const utcValue = toUtc(value, 'YYYY-MM-DD HH:mm:ss');\n if (utcValue.isValid()) {\n return utcValue.toISOString();\n }\n } else if (value.length === 24) {\n const utcValue = toUtc(value);\n return utcValue.toISOString();\n }\n\n const epoch = parseInt(value, 10);\n if (!isNaN(epoch)) {\n return toUtc(epoch).toISOString();\n }\n\n return null;\n}\n"],"names":[],"mappings":";;AAGA,MAAM,qBAAwB,GAAA,qBAAA,CAAA;AAEvB,SAAS,cAAc,KAA2C,EAAA;AACvE,EAAI,IAAA,OAAO,UAAU,QAAU,EAAA;AAC7B,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AAEA,EAAA,IAAI,KAAM,CAAA,OAAA,CAAQ,KAAK,CAAA,KAAM,CAAI,CAAA,EAAA;AAC/B,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAI,IAAA,qBAAA,CAAsB,IAAK,CAAA,KAAK,CAAG,EAAA;AACrC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAI,IAAA,KAAA,CAAM,WAAW,CAAG,EAAA;AACtB,IAAM,MAAA,QAAA,GAAW,KAAM,CAAA,KAAA,EAAO,UAAU,CAAA,CAAA;AACxC,IAAI,IAAA,QAAA,CAAS,SAAW,EAAA;AACtB,MAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,KAC9B;AAAA,GACF,MAAA,IAAW,KAAM,CAAA,MAAA,KAAW,EAAI,EAAA;AAC9B,IAAM,MAAA,QAAA,GAAW,KAAM,CAAA,KAAA,EAAO,iBAAiB,CAAA,CAAA;AAC/C,IAAI,IAAA,QAAA,CAAS,SAAW,EAAA;AACtB,MAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,KAC9B;AAAA,GACF,MAAA,IAAW,KAAM,CAAA,MAAA,KAAW,EAAI,EAAA;AAC9B,IAAM,MAAA,QAAA,GAAW,KAAM,CAAA,KAAA,EAAO,qBAAqB,CAAA,CAAA;AACnD,IAAI,IAAA,QAAA,CAAS,SAAW,EAAA;AACtB,MAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,KAC9B;AAAA,GACF,MAAA,IAAW,KAAM,CAAA,MAAA,KAAW,EAAI,EAAA;AAC9B,IAAM,MAAA,QAAA,GAAW,MAAM,KAAK,CAAA,CAAA;AAC5B,IAAA,OAAO,SAAS,WAAY,EAAA,CAAA;AAAA,GAC9B;AAEA,EAAM,MAAA,KAAA,GAAQ,QAAS,CAAA,KAAA,EAAO,EAAE,CAAA,CAAA;AAChC,EAAI,IAAA,CAAC,KAAM,CAAA,KAAK,CAAG,EAAA;AACjB,IAAO,OAAA,KAAA,CAAM,KAAK,CAAA,CAAE,WAAY,EAAA,CAAA;AAAA,GAClC;AAEA,EAAO,OAAA,IAAA,CAAA;AACT;;;;"}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { map, isArray, replace } from 'lodash';
|
2
|
-
import { Registry, escapeRegex, textUtil, dateTime } from '@grafana/data';
|
2
|
+
import { Registry, escapeRegex, textUtil, dateTime, urlUtil } from '@grafana/data';
|
3
3
|
import { VariableFormatID } from '@grafana/schema';
|
4
4
|
|
5
5
|
const formatRegistry = new Registry(() => {
|
@@ -230,6 +230,10 @@ const formatRegistry = new Registry(() => {
|
|
230
230
|
name: "Query parameter",
|
231
231
|
description: "Format variables as URL parameters. Example in multi-variable scenario A + B + C => var-foo=A&var-foo=B&var-foo=C.",
|
232
232
|
formatter: (value, _args, variable) => {
|
233
|
+
if (variable.urlSync) {
|
234
|
+
const urlParam = variable.urlSync.getUrlState();
|
235
|
+
return urlUtil.toUrlParams(urlParam);
|
236
|
+
}
|
233
237
|
if (Array.isArray(value)) {
|
234
238
|
return value.map((v) => formatQueryParameter(variable.state.name, v)).join("&");
|
235
239
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"formatRegistry.js","sources":["../../../../src/variables/interpolation/formatRegistry.ts"],"sourcesContent":["import { isArray, map, replace } from 'lodash';\n\nimport { dateTime, Registry, RegistryItem, textUtil, escapeRegex } from '@grafana/data';\nimport { VariableType, VariableFormatID } from '@grafana/schema';\n\nimport { VariableValue, VariableValueSingle } from '../types';\nimport { ALL_VARIABLE_VALUE } from '../constants';\n\nexport interface FormatRegistryItem extends RegistryItem {\n formatter(value: VariableValue, args: string[], variable: FormatVariable): string;\n}\n\n/**\n * Slimmed down version of the SceneVariable interface so that it only contains what the formatters actually use.\n * This is useful as we have some implementations of this interface that does not need to be full scene objects.\n * For example ScopedVarsVariable and LegacyVariableWrapper.\n */\nexport interface FormatVariable {\n state: {\n name: string;\n type: VariableType | string;\n isMulti?: boolean;\n includeAll?: boolean;\n };\n\n getValue(fieldPath?: string): VariableValue | undefined | null;\n getValueText?(fieldPath?: string): string;\n}\n\nexport const formatRegistry = new Registry<FormatRegistryItem>(() => {\n const formats: FormatRegistryItem[] = [\n {\n id: VariableFormatID.Lucene,\n name: 'Lucene',\n description: 'Values are lucene escaped and multi-valued variables generate an OR expression',\n formatter: (value) => {\n if (typeof value === 'string') {\n return luceneEscape(value);\n }\n\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return '__empty__';\n }\n const quotedValues = map(value, (val: string) => {\n return '\"' + luceneEscape(val) + '\"';\n });\n return '(' + quotedValues.join(' OR ') + ')';\n } else {\n return luceneEscape(`${value}`);\n }\n },\n },\n {\n id: VariableFormatID.Raw,\n name: 'raw',\n description: 'Keep value as is',\n formatter: (value) => String(value),\n },\n {\n id: VariableFormatID.Regex,\n name: 'Regex',\n description: 'Values are regex escaped and multi-valued variables generate a (<value>|<value>) expression',\n formatter: (value) => {\n if (typeof value === 'string') {\n return escapeRegex(value);\n }\n\n if (Array.isArray(value)) {\n const escapedValues = value.map((item) => {\n if (typeof item === 'string') {\n return escapeRegex(item);\n } else {\n return escapeRegex(String(item));\n }\n });\n\n if (escapedValues.length === 1) {\n return escapedValues[0];\n }\n\n return '(' + escapedValues.join('|') + ')';\n }\n\n return escapeRegex(`${value}`);\n },\n },\n {\n id: VariableFormatID.Pipe,\n name: 'Pipe',\n description: 'Values are separated by | character',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (Array.isArray(value)) {\n return value.join('|');\n }\n\n return `${value}`;\n },\n },\n {\n id: VariableFormatID.Distributed,\n name: 'Distributed',\n description: 'Multiple values are formatted like variable=value',\n formatter: (value, args, variable) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (Array.isArray(value)) {\n value = map(value, (val: string, index: number) => {\n if (index !== 0) {\n return variable.state.name + '=' + val;\n } else {\n return val;\n }\n });\n\n return value.join(',');\n }\n\n return `${value}`;\n },\n },\n {\n id: VariableFormatID.CSV,\n name: 'Csv',\n description: 'Comma-separated values',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (isArray(value)) {\n return value.join(',');\n }\n\n return String(value);\n },\n },\n {\n id: VariableFormatID.HTML,\n name: 'HTML',\n description: 'HTML escaping of values',\n formatter: (value) => {\n if (typeof value === 'string') {\n return textUtil.escapeHtml(value);\n }\n\n if (isArray(value)) {\n return textUtil.escapeHtml(value.join(', '));\n }\n\n return textUtil.escapeHtml(String(value));\n },\n },\n {\n id: VariableFormatID.JSON,\n name: 'JSON',\n description: 'JSON stringify value',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n return JSON.stringify(value);\n },\n },\n {\n id: VariableFormatID.PercentEncode,\n name: 'Percent encode',\n description: 'Useful for URL escaping values',\n formatter: (value) => {\n // like glob, but url escaped\n if (isArray(value)) {\n return encodeURIComponentStrict('{' + value.join(',') + '}');\n }\n\n return encodeURIComponentStrict(value);\n },\n },\n {\n id: VariableFormatID.SingleQuote,\n name: 'Single quote',\n description: 'Single quoted values',\n formatter: (value) => {\n // escape single quotes with backslash\n const regExp = new RegExp(`'`, 'g');\n\n if (isArray(value)) {\n return map(value, (v: string) => `'${replace(v, regExp, `\\\\'`)}'`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `'${replace(strVal, regExp, `\\\\'`)}'`;\n },\n },\n {\n id: VariableFormatID.DoubleQuote,\n name: 'Double quote',\n description: 'Double quoted values',\n formatter: (value) => {\n // escape double quotes with backslash\n const regExp = new RegExp('\"', 'g');\n if (isArray(value)) {\n return map(value, (v: string) => `\"${replace(v, regExp, '\\\\\"')}\"`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `\"${replace(strVal, regExp, '\\\\\"')}\"`;\n },\n },\n {\n id: VariableFormatID.SQLString,\n name: 'SQL string',\n description: 'SQL string quoting and commas for use in IN statements and other scenarios',\n formatter: sqlStringFormatter,\n },\n {\n id: VariableFormatID.Date,\n name: 'Date',\n description: 'Format date in different ways',\n formatter: (value, args) => {\n let nrValue = NaN;\n\n if (typeof value === 'number') {\n nrValue = value;\n } else if (typeof value === 'string') {\n nrValue = parseInt(value, 10);\n }\n\n if (isNaN(nrValue)) {\n return 'NaN';\n }\n\n const arg = args[0] ?? 'iso';\n switch (arg) {\n case 'ms':\n return String(value);\n case 'seconds':\n return `${Math.round(nrValue! / 1000)}`;\n case 'iso':\n return dateTime(nrValue).toISOString();\n default:\n if ((args || []).length > 1) {\n return dateTime(nrValue).format(args.join(':'));\n }\n return dateTime(nrValue).format(arg);\n }\n },\n },\n {\n id: VariableFormatID.Glob,\n name: 'Glob',\n description: 'Format multi-valued variables using glob syntax, example {value1,value2}',\n formatter: (value) => {\n if (isArray(value) && value.length > 1) {\n return '{' + value.join(',') + '}';\n }\n return String(value);\n },\n },\n {\n id: VariableFormatID.Text,\n name: 'Text',\n description: 'Format variables in their text representation. Example in multi-variable scenario A + B + C.',\n formatter: (value, _args, variable) => {\n if (variable.getValueText) {\n return variable.getValueText();\n }\n\n return String(value);\n },\n },\n {\n id: VariableFormatID.QueryParam,\n name: 'Query parameter',\n description:\n 'Format variables as URL parameters. Example in multi-variable scenario A + B + C => var-foo=A&var-foo=B&var-foo=C.',\n formatter: (value, _args, variable) => {\n if (Array.isArray(value)) {\n return value.map((v) => formatQueryParameter(variable.state.name, v)).join('&');\n }\n return formatQueryParameter(variable.state.name, value);\n },\n },\n {\n id: VariableFormatID.UriEncode,\n name: 'Percent encode as URI',\n description: 'Useful for URL escaping values, taking into URI syntax characters',\n formatter: (value: VariableValue) => {\n if (isArray(value)) {\n return encodeURIStrict('{' + value.join(',') + '}');\n }\n\n return encodeURIStrict(value);\n },\n },\n ];\n\n return formats;\n});\n\nfunction luceneEscape(value: string) {\n if (isNaN(+value) === false) {\n return value;\n }\n\n return value.replace(/([\\!\\*\\+\\-\\=<>\\s\\&\\|\\(\\)\\[\\]\\{\\}\\^\\~\\?\\:\\\\/\"])/g, '\\\\$1');\n}\n\n/**\n * encode string according to RFC 3986; in contrast to encodeURIComponent()\n * also the sub-delims \"!\", \"'\", \"(\", \")\" and \"*\" are encoded;\n * unicode handling uses UTF-8 as in ECMA-262.\n */\nfunction encodeURIComponentStrict(str: VariableValueSingle) {\n if (typeof str === 'object') {\n str = String(str);\n }\n\n return replaceSpecialCharactersToASCII(encodeURIComponent(str));\n}\n\nconst encodeURIStrict = (str: VariableValueSingle): string => replaceSpecialCharactersToASCII(encodeURI(String(str)));\n\nconst replaceSpecialCharactersToASCII = (value: string): string =>\n value.replace(/[!'()*]/g, (c) => {\n return '%' + c.charCodeAt(0).toString(16).toUpperCase();\n });\n\nfunction formatQueryParameter(name: string, value: VariableValueSingle): string {\n return `var-${name}=${encodeURIComponentStrict(value)}`;\n}\n\nexport function isAllValue(value: VariableValueSingle) {\n return value === ALL_VARIABLE_VALUE || (Array.isArray(value) && value[0] === ALL_VARIABLE_VALUE);\n}\n\nconst SQL_ESCAPE_MAP: Record<string, string> = {\n \"'\": \"''\",\n '\"': '\\\\\"',\n};\n\nfunction sqlStringFormatter(value: VariableValue) {\n // escape single quotes by pairing them\n const regExp = new RegExp(`'|\"`, 'g');\n\n if (isArray(value)) {\n return map(value, (v: string) => `'${replace(v, regExp, (match) => SQL_ESCAPE_MAP[match] ?? '')}'`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `'${replace(strVal, regExp, (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AA,EAAA,GAAA,cAAA,CAAe,WAAf,IAAyB,GAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAE,CAAA,CAAA,CAAA,CAAA,CAAA;AAC3E;;;;"}
|
1
|
+
{"version":3,"file":"formatRegistry.js","sources":["../../../../src/variables/interpolation/formatRegistry.ts"],"sourcesContent":["import { isArray, map, replace } from 'lodash';\n\nimport { dateTime, Registry, RegistryItem, textUtil, escapeRegex, urlUtil } from '@grafana/data';\nimport { VariableType, VariableFormatID } from '@grafana/schema';\n\nimport { VariableValue, VariableValueSingle } from '../types';\nimport { ALL_VARIABLE_VALUE } from '../constants';\nimport { SceneObjectUrlSyncHandler } from '../../core/types';\n\nexport interface FormatRegistryItem extends RegistryItem {\n formatter(value: VariableValue, args: string[], variable: FormatVariable): string;\n}\n\n/**\n * Slimmed down version of the SceneVariable interface so that it only contains what the formatters actually use.\n * This is useful as we have some implementations of this interface that does not need to be full scene objects.\n * For example ScopedVarsVariable and LegacyVariableWrapper.\n */\nexport interface FormatVariable {\n state: {\n name: string;\n type: VariableType | string;\n isMulti?: boolean;\n includeAll?: boolean;\n };\n\n getValue(fieldPath?: string): VariableValue | undefined | null;\n getValueText?(fieldPath?: string): string;\n urlSync?: SceneObjectUrlSyncHandler;\n}\n\nexport const formatRegistry = new Registry<FormatRegistryItem>(() => {\n const formats: FormatRegistryItem[] = [\n {\n id: VariableFormatID.Lucene,\n name: 'Lucene',\n description: 'Values are lucene escaped and multi-valued variables generate an OR expression',\n formatter: (value) => {\n if (typeof value === 'string') {\n return luceneEscape(value);\n }\n\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return '__empty__';\n }\n const quotedValues = map(value, (val: string) => {\n return '\"' + luceneEscape(val) + '\"';\n });\n return '(' + quotedValues.join(' OR ') + ')';\n } else {\n return luceneEscape(`${value}`);\n }\n },\n },\n {\n id: VariableFormatID.Raw,\n name: 'raw',\n description: 'Keep value as is',\n formatter: (value) => String(value),\n },\n {\n id: VariableFormatID.Regex,\n name: 'Regex',\n description: 'Values are regex escaped and multi-valued variables generate a (<value>|<value>) expression',\n formatter: (value) => {\n if (typeof value === 'string') {\n return escapeRegex(value);\n }\n\n if (Array.isArray(value)) {\n const escapedValues = value.map((item) => {\n if (typeof item === 'string') {\n return escapeRegex(item);\n } else {\n return escapeRegex(String(item));\n }\n });\n\n if (escapedValues.length === 1) {\n return escapedValues[0];\n }\n\n return '(' + escapedValues.join('|') + ')';\n }\n\n return escapeRegex(`${value}`);\n },\n },\n {\n id: VariableFormatID.Pipe,\n name: 'Pipe',\n description: 'Values are separated by | character',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (Array.isArray(value)) {\n return value.join('|');\n }\n\n return `${value}`;\n },\n },\n {\n id: VariableFormatID.Distributed,\n name: 'Distributed',\n description: 'Multiple values are formatted like variable=value',\n formatter: (value, args, variable) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (Array.isArray(value)) {\n value = map(value, (val: string, index: number) => {\n if (index !== 0) {\n return variable.state.name + '=' + val;\n } else {\n return val;\n }\n });\n\n return value.join(',');\n }\n\n return `${value}`;\n },\n },\n {\n id: VariableFormatID.CSV,\n name: 'Csv',\n description: 'Comma-separated values',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n\n if (isArray(value)) {\n return value.join(',');\n }\n\n return String(value);\n },\n },\n {\n id: VariableFormatID.HTML,\n name: 'HTML',\n description: 'HTML escaping of values',\n formatter: (value) => {\n if (typeof value === 'string') {\n return textUtil.escapeHtml(value);\n }\n\n if (isArray(value)) {\n return textUtil.escapeHtml(value.join(', '));\n }\n\n return textUtil.escapeHtml(String(value));\n },\n },\n {\n id: VariableFormatID.JSON,\n name: 'JSON',\n description: 'JSON stringify value',\n formatter: (value) => {\n if (typeof value === 'string') {\n return value;\n }\n return JSON.stringify(value);\n },\n },\n {\n id: VariableFormatID.PercentEncode,\n name: 'Percent encode',\n description: 'Useful for URL escaping values',\n formatter: (value) => {\n // like glob, but url escaped\n if (isArray(value)) {\n return encodeURIComponentStrict('{' + value.join(',') + '}');\n }\n\n return encodeURIComponentStrict(value);\n },\n },\n {\n id: VariableFormatID.SingleQuote,\n name: 'Single quote',\n description: 'Single quoted values',\n formatter: (value) => {\n // escape single quotes with backslash\n const regExp = new RegExp(`'`, 'g');\n\n if (isArray(value)) {\n return map(value, (v: string) => `'${replace(v, regExp, `\\\\'`)}'`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `'${replace(strVal, regExp, `\\\\'`)}'`;\n },\n },\n {\n id: VariableFormatID.DoubleQuote,\n name: 'Double quote',\n description: 'Double quoted values',\n formatter: (value) => {\n // escape double quotes with backslash\n const regExp = new RegExp('\"', 'g');\n if (isArray(value)) {\n return map(value, (v: string) => `\"${replace(v, regExp, '\\\\\"')}\"`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `\"${replace(strVal, regExp, '\\\\\"')}\"`;\n },\n },\n {\n id: VariableFormatID.SQLString,\n name: 'SQL string',\n description: 'SQL string quoting and commas for use in IN statements and other scenarios',\n formatter: sqlStringFormatter,\n },\n {\n id: VariableFormatID.Date,\n name: 'Date',\n description: 'Format date in different ways',\n formatter: (value, args) => {\n let nrValue = NaN;\n\n if (typeof value === 'number') {\n nrValue = value;\n } else if (typeof value === 'string') {\n nrValue = parseInt(value, 10);\n }\n\n if (isNaN(nrValue)) {\n return 'NaN';\n }\n\n const arg = args[0] ?? 'iso';\n switch (arg) {\n case 'ms':\n return String(value);\n case 'seconds':\n return `${Math.round(nrValue! / 1000)}`;\n case 'iso':\n return dateTime(nrValue).toISOString();\n default:\n if ((args || []).length > 1) {\n return dateTime(nrValue).format(args.join(':'));\n }\n return dateTime(nrValue).format(arg);\n }\n },\n },\n {\n id: VariableFormatID.Glob,\n name: 'Glob',\n description: 'Format multi-valued variables using glob syntax, example {value1,value2}',\n formatter: (value) => {\n if (isArray(value) && value.length > 1) {\n return '{' + value.join(',') + '}';\n }\n return String(value);\n },\n },\n {\n id: VariableFormatID.Text,\n name: 'Text',\n description: 'Format variables in their text representation. Example in multi-variable scenario A + B + C.',\n formatter: (value, _args, variable) => {\n if (variable.getValueText) {\n return variable.getValueText();\n }\n\n return String(value);\n },\n },\n {\n id: VariableFormatID.QueryParam,\n name: 'Query parameter',\n description:\n 'Format variables as URL parameters. Example in multi-variable scenario A + B + C => var-foo=A&var-foo=B&var-foo=C.',\n formatter: (value, _args, variable) => {\n if (variable.urlSync) {\n const urlParam = variable.urlSync.getUrlState();\n return urlUtil.toUrlParams(urlParam);\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => formatQueryParameter(variable.state.name, v)).join('&');\n }\n\n return formatQueryParameter(variable.state.name, value);\n },\n },\n {\n id: VariableFormatID.UriEncode,\n name: 'Percent encode as URI',\n description: 'Useful for URL escaping values, taking into URI syntax characters',\n formatter: (value: VariableValue) => {\n if (isArray(value)) {\n return encodeURIStrict('{' + value.join(',') + '}');\n }\n\n return encodeURIStrict(value);\n },\n },\n ];\n\n return formats;\n});\n\nfunction luceneEscape(value: string) {\n if (isNaN(+value) === false) {\n return value;\n }\n\n return value.replace(/([\\!\\*\\+\\-\\=<>\\s\\&\\|\\(\\)\\[\\]\\{\\}\\^\\~\\?\\:\\\\/\"])/g, '\\\\$1');\n}\n\n/**\n * encode string according to RFC 3986; in contrast to encodeURIComponent()\n * also the sub-delims \"!\", \"'\", \"(\", \")\" and \"*\" are encoded;\n * unicode handling uses UTF-8 as in ECMA-262.\n */\nfunction encodeURIComponentStrict(str: VariableValueSingle) {\n if (typeof str === 'object') {\n str = String(str);\n }\n\n return replaceSpecialCharactersToASCII(encodeURIComponent(str));\n}\n\nconst encodeURIStrict = (str: VariableValueSingle): string => replaceSpecialCharactersToASCII(encodeURI(String(str)));\n\nconst replaceSpecialCharactersToASCII = (value: string): string =>\n value.replace(/[!'()*]/g, (c) => {\n return '%' + c.charCodeAt(0).toString(16).toUpperCase();\n });\n\nfunction formatQueryParameter(name: string, value: VariableValueSingle): string {\n return `var-${name}=${encodeURIComponentStrict(value)}`;\n}\n\nexport function isAllValue(value: VariableValueSingle) {\n return value === ALL_VARIABLE_VALUE || (Array.isArray(value) && value[0] === ALL_VARIABLE_VALUE);\n}\n\nconst SQL_ESCAPE_MAP: Record<string, string> = {\n \"'\": \"''\",\n '\"': '\\\\\"',\n};\n\nfunction sqlStringFormatter(value: VariableValue) {\n // escape single quotes by pairing them\n const regExp = new RegExp(`'|\"`, 'g');\n\n if (isArray(value)) {\n return map(value, (v: string) => `'${replace(v, regExp, (match) => SQL_ESCAPE_MAP[match] ?? '')}'`).join(',');\n }\n\n let strVal = typeof value === 'string' ? value : String(value);\n return `'${replace(strVal, regExp, (match) => SQL_ESCAPE_MAP[match] ?? '')}'`;\n}\n"],"names":[],"mappings":";;;;AA+Ba,MAAA,cAAA,GAAiB,IAAI,QAAA,CAA6B,MAAM;AACnE,EAAA,MAAM,OAAgC,GAAA;AAAA,IACpC;AAAA,MACE,IAAI,gBAAiB,CAAA,MAAA;AAAA,MACrB,IAAM,EAAA,QAAA;AAAA,MACN,WAAa,EAAA,gFAAA;AAAA,MACb,SAAA,EAAW,CAAC,KAAU,KAAA;AACpB,QAAI,IAAA,OAAO,UAAU,QAAU,EAAA;AAC7B,UAAA,OAAO,aAAa,KAAK,CAAA,CAAA;AAAA,SAC3B;AAEA,QAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,KAAK,CAAG,EAAA;AACxB,UAAI,IAAA,KAAA,CAAM,WAAW,CAAG,EAAA;AACtB,YAAO,OAAA,WAAA,CAAA;AAAA,WACT;AACA,UAAA,MAAM,YAAe,GAAA,GAAA,CAAI,KAAO,EAAA,CAAC,GAAgB,KAAA;AAC/C,YAAO,OAAA,GAAA,GAAM,YAAa,CAAA,GAAG,CAAI,GAAA,GAAA,CAAA;AAAA,WAClC,CAAA,CAAA;AACD,UAAA,OAAO,GAAM,GAAA,YAAA,CAAa,IAAK,CAAA,MAAM,CAAI,GAAA,GAAA,CAAA;AAAA,SACpC,MAAA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AQ,KAAK,CAAG,EAAA;AAClB,IAAO,OAAA,GAAA,CAAI,OAAO,CAAC,CAAA,KAAc,IAAI,OAAQ,CAAA,CAAA,EAAG,MAAQ,EAAA,CAAC,KAAO,KAAA;AAvWpE,MAAA,IAAA,EAAA,CAAA;AAuWuE,MAAA,OAAA,CAAA,EAAA,GAAA,cAAA,CAAe,WAAf,IAAyB,GAAA,EAAA,GAAA,EAAA,CAAA;AAAA,KAAE,CAAA,CAAA,CAAA,CAAI,CAAE,CAAA,IAAA,CAAK,GAAG,CAAA,CAAA;AAAA,GAC9G;AAEA,EAAA,IAAI,SAAS,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,OAAO,KAAK,CAAA,CAAA;AAC7D,EAAA,OAAO,CAAI,CAAA,EAAA,OAAA,CAAQ,MAAQ,EAAA,MAAA,EAAQ,CAAC,KAAO,KAAA;AA3W7C,IAAA,IAAA,EAAA,CAAA;AA2WgD,IAAA,OAAA,CAAA,EAAA,GAAA,cAAA,CAAe,WAAf,IAAyB,GAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAE,CAAA,CAAA,CAAA,CAAA,CAAA;AAC3E;;;;"}
|
package/dist/index.d.ts
CHANGED
@@ -505,6 +505,7 @@ interface FormatVariable {
|
|
505
505
|
};
|
506
506
|
getValue(fieldPath?: string): VariableValue | undefined | null;
|
507
507
|
getValueText?(fieldPath?: string): string;
|
508
|
+
urlSync?: SceneObjectUrlSyncHandler;
|
508
509
|
}
|
509
510
|
declare const formatRegistry: Registry<FormatRegistryItem>;
|
510
511
|
|
package/dist/index.js
CHANGED
@@ -692,6 +692,11 @@ function parseUrlParam(value) {
|
|
692
692
|
if (utcValue.isValid()) {
|
693
693
|
return utcValue.toISOString();
|
694
694
|
}
|
695
|
+
} else if (value.length === 19) {
|
696
|
+
const utcValue = data.toUtc(value, "YYYY-MM-DD HH:mm:ss");
|
697
|
+
if (utcValue.isValid()) {
|
698
|
+
return utcValue.toISOString();
|
699
|
+
}
|
695
700
|
} else if (value.length === 24) {
|
696
701
|
const utcValue = data.toUtc(value);
|
697
702
|
return utcValue.toISOString();
|
@@ -715,6 +720,17 @@ function evaluateTimeRange(from, to, timeZone, fiscalYearStartMonth, delay) {
|
|
715
720
|
};
|
716
721
|
}
|
717
722
|
|
723
|
+
function isValid$1(value, roundUp, timeZone) {
|
724
|
+
if (data.isDateTime(value)) {
|
725
|
+
return value.isValid();
|
726
|
+
}
|
727
|
+
if (data.dateMath.isMathString(value)) {
|
728
|
+
return data.dateMath.isValid(value);
|
729
|
+
}
|
730
|
+
const parsed = data.dateTimeParse(value, { roundUp, timeZone });
|
731
|
+
return parsed.isValid();
|
732
|
+
}
|
733
|
+
|
718
734
|
var __defProp$I = Object.defineProperty;
|
719
735
|
var __getOwnPropSymbols$I = Object.getOwnPropertySymbols;
|
720
736
|
var __hasOwnProp$I = Object.prototype.hasOwnProperty;
|
@@ -733,9 +749,9 @@ var __spreadValues$I = (a, b) => {
|
|
733
749
|
};
|
734
750
|
class SceneTimeRange extends SceneObjectBase {
|
735
751
|
constructor(state = {}) {
|
736
|
-
var _a
|
737
|
-
const from =
|
738
|
-
const to =
|
752
|
+
var _a;
|
753
|
+
const from = state.from && isValid$1(state.from) ? state.from : "now-6h";
|
754
|
+
const to = state.to && isValid$1(state.to) ? state.to : "now";
|
739
755
|
const timeZone = state.timeZone;
|
740
756
|
const value = evaluateTimeRange(
|
741
757
|
from,
|
@@ -744,7 +760,7 @@ class SceneTimeRange extends SceneObjectBase {
|
|
744
760
|
state.fiscalYearStartMonth,
|
745
761
|
state.UNSAFE_nowDelay
|
746
762
|
);
|
747
|
-
const refreshOnActivate = (
|
763
|
+
const refreshOnActivate = (_a = state.refreshOnActivate) != null ? _a : { percent: 10 };
|
748
764
|
super(__spreadValues$I({ from, to, timeZone, value, refreshOnActivate }, state));
|
749
765
|
this._urlSync = new SceneObjectUrlSyncConfig(this, { keys: ["from", "to", "timezone", "time", "time.window"] });
|
750
766
|
this.onTimeRangeChange = (timeRange) => {
|
@@ -895,16 +911,20 @@ class SceneTimeRange extends SceneObjectBase {
|
|
895
911
|
const time = Array.isArray(values.time) ? values.time[0] : values.time;
|
896
912
|
const timeWindow = Array.isArray(values["time.window"]) ? values["time.window"][0] : values["time.window"];
|
897
913
|
const timeRange = getTimeWindow(time, timeWindow);
|
898
|
-
from
|
899
|
-
|
914
|
+
if (timeRange.from && isValid$1(timeRange.from)) {
|
915
|
+
from = timeRange.from;
|
916
|
+
}
|
917
|
+
if (timeRange.to && isValid$1(timeRange.to)) {
|
918
|
+
to = timeRange.to;
|
919
|
+
}
|
900
920
|
}
|
901
921
|
if (!from && !to) {
|
902
922
|
return;
|
903
923
|
}
|
904
|
-
if (from) {
|
924
|
+
if (from && isValid$1(from)) {
|
905
925
|
update.from = from;
|
906
926
|
}
|
907
|
-
if (to) {
|
927
|
+
if (to && isValid$1(to)) {
|
908
928
|
update.to = to;
|
909
929
|
}
|
910
930
|
if (typeof values.timezone === "string") {
|
@@ -1241,6 +1261,10 @@ const formatRegistry = new data.Registry(() => {
|
|
1241
1261
|
name: "Query parameter",
|
1242
1262
|
description: "Format variables as URL parameters. Example in multi-variable scenario A + B + C => var-foo=A&var-foo=B&var-foo=C.",
|
1243
1263
|
formatter: (value, _args, variable) => {
|
1264
|
+
if (variable.urlSync) {
|
1265
|
+
const urlParam = variable.urlSync.getUrlState();
|
1266
|
+
return data.urlUtil.toUrlParams(urlParam);
|
1267
|
+
}
|
1244
1268
|
if (Array.isArray(value)) {
|
1245
1269
|
return value.map((v) => formatQueryParameter(variable.state.name, v)).join("&");
|
1246
1270
|
}
|