Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan
- [#217](https://github.com/kobsio/kobs/pull/217): [azure] Use resource groups to get a list of container instances.
- [#225](https://github.com/kobsio/kobs/pull/225): [core] :warning: _Breaking change:_ :warning: Change options handling accross all plugins and re-add `time` property.
- [#229](https://github.com/kobsio/kobs/pull/229): [opsgenie] Allow users to overwrite the selected time range in a dashboard for an Opsgenie panel via the new `interval` property.
- [#230](https://github.com/kobsio/kobs/pull/230): [dashboards] Add special variables `__timeStart` and `__timeEnd` for dashboards.

## [v0.7.0](https://github.com/kobsio/kobs/releases/tag/v0.7.0) (2021-11-19)

Expand Down
8 changes: 8 additions & 0 deletions docs/resources/dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ Variables can be used to select between different values in the dashboard. To us
| hide | boolean | Hide the variable in the UI. | No |
| plugin | [Variable Plugin](#variable-plugin) | The plugin, which should be used to get the values for the variable. | Yes |

!!! note
Dashboards are also supporting some special variables, which always can be used and must not be defined by a users. These variables are:

- `__timeStart`: The start time of the selected time range in seconds.
- `__timeEnd`: The end time of the selected time range in seconds.

These variables can then be used via `{{ .__timeStart }}` and `{{ .__timeEnd }}` in the dashboard.

### Variable Plugin

| Field | Type | Description | Required |
Expand Down
4 changes: 2 additions & 2 deletions plugins/dashboards/src/components/dashboards/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const Dashboard: React.FunctionComponent<IDashboardProps> = ({
const response = await fetch(`/api/plugins/prometheus/variable/${tmpVariables[i].plugin.name}`, {
body: JSON.stringify({
label: tmpVariables[i].plugin.options.label,
query: interpolate(tmpVariables[i].plugin.options.query, tmpVariables),
query: interpolate(tmpVariables[i].plugin.options.query, tmpVariables, times),
timeEnd: times.timeEnd,
timeStart: times.timeStart,
type: tmpVariables[i].plugin.options.type,
Expand Down Expand Up @@ -152,7 +152,7 @@ const Dashboard: React.FunctionComponent<IDashboardProps> = ({
// We do not use the dashboard.rows array directly to render the dashboard. Instead we are replacing all the variables
// in the dashboard first with users selected values. For that we have to convert the array to a string first so that
// we can replace the variables in the string and then we have to convert it back to an array,
const rows: IRow[] = data ? JSON.parse(interpolate(JSON.stringify(dashboard.rows), data)) : dashboard.rows;
const rows: IRow[] = JSON.parse(interpolate(JSON.stringify(dashboard.rows), data ? data : [], times));

if (isError) {
return (
Expand Down
6 changes: 5 additions & 1 deletion plugins/dashboards/src/utils/dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gridSpans } from '@patternfly/react-core';

import { IDashboard, IPlaceholders, IPluginDefaults, IReference } from '@kobsio/plugin-core';
import { IDashboard, IPlaceholders, IPluginDefaults, IPluginTimes, IReference } from '@kobsio/plugin-core';
import { IDashboardsOptions, IVariableValues } from './interfaces';

// toGridSpans is used to convert the provided col and row span value to the corresponding gridSpans value, so that it
Expand Down Expand Up @@ -46,6 +46,7 @@ interface IVariables {
export const interpolate = (
str: string,
variables: IVariableValues[],
times: IPluginTimes,
interpolator: string[] = ['{%', '%}'],
): string => {
const vars: IVariables = {};
Expand All @@ -54,6 +55,9 @@ export const interpolate = (
vars[variable.name] = variable.value;
}

vars['__timeStart'] = `${times.timeStart}`;
vars['__timeEnd'] = `${times.timeEnd}`;

return str
.split(interpolator[0])
.map((s1, i) => {
Expand Down