-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Rework variables handling (#262)
This commit changes the variables handling, like it was discussed in issue #250. This means that each plugin can now export a "variables" function to load variables in a dashboard. The only exception is the "core" plugin, where the variables are still handled within the Dashboards component. This change allows us, to introduced variables for more plugins, like a list of resource groups via the Azure plugin or a list of field values via the klogs plugin. We also adjusted the naming and location of some interfaces for this change. So that the interfaces for the CRDs are now prefixed with the name of the CRD (e.g. "IPlaceholder" becomes "IDashboardPlaceholder"). This was necessary to reduce conflicts in names and to avoid cycle imports. The documentation for the variables options which can be used in a dashboard, should be placed on the corresponding plugin page and a link should be added to the "Variable Plugin Options" section in the dashboards documentation. Closes #250.
- Loading branch information
1 parent
53665a6
commit a9d15fb
Showing
23 changed files
with
198 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { IDashboardVariableValues } from '../crds/dashboard'; | ||
import { IPluginTimes } from '../context/PluginsContext'; | ||
|
||
// IVariables is a map of variable names with the current value. This interface should only be used by the interpolate | ||
// function, to convert a given array of variables to the format, which is required by the function. | ||
interface IVariables { | ||
[key: string]: string; | ||
} | ||
|
||
// interpolate is used to replace the variables in a given string with the current value for this variable. Before we | ||
// can replace the variables in a string we have to convert the array of variables to a map of variable names and there | ||
// value. | ||
// The default interpolator/delimiter is "{%" and "%}", so that it doesn't conflict with the delimiter used for the | ||
// placeholder. We can not use the same, because the are replaced at different points in our app logic. | ||
// See: https://stackoverflow.com/a/57598892/4104109 | ||
export const interpolate = ( | ||
str: string, | ||
variables: IDashboardVariableValues[], | ||
times: IPluginTimes, | ||
interpolator: string[] = ['{%', '%}'], | ||
): string => { | ||
const vars: IVariables = {}; | ||
|
||
for (const variable of variables) { | ||
vars[variable.name] = variable.value; | ||
} | ||
|
||
vars['__timeStart'] = `${times.timeStart}`; | ||
vars['__timeEnd'] = `${times.timeEnd}`; | ||
|
||
return str | ||
.split(interpolator[0]) | ||
.map((s1, i) => { | ||
if (i === 0) { | ||
return s1; | ||
} | ||
|
||
const s2 = s1.split(interpolator[1]); | ||
if (s1 === s2[0]) { | ||
return interpolator[0] + s2[0]; | ||
} | ||
|
||
if (s2.length > 1) { | ||
s2[0] = | ||
s2[0] && vars.hasOwnProperty(s2[0].trim().substring(1)) | ||
? vars[s2[0].trim().substring(1)] | ||
: interpolator.join(` ${s2[0]} `); | ||
} | ||
|
||
return s2.join(''); | ||
}) | ||
.join(''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
plugins/dashboards/src/components/dashboards/DashboardToolbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
plugins/dashboards/src/components/dashboards/DashboardToolbarVariable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.