Skip to content

Commit

Permalink
Merge pull request #72 from andymchugh/linkvars
Browse files Browse the repository at this point in the history
link variables
  • Loading branch information
andymchugh authored Jun 10, 2024
2 parents 978f49f + ff70215 commit 2f34f29
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 117 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 1.x.0

Link Variables
---------------
This adds the ability to define link url substitutions to simplify oft-repeated base urls
or argument sets. New config terms in siteConfig and panelConfig:
- linkVariables

Zoom Pan Pinch Wheel Activation
-------------------------------
This adds the ability to require an activation key such as 'Alt' to be pressed in order to enter
Expand Down
2 changes: 1 addition & 1 deletion provisioning/dashboardData/links.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions provisioning/dashboardData/links.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ cellIdPreamble: "cell-"
gradientMode: "hue"
tagConfig:
condensed: false
cells:

linkVariables:
urlPanelWebsiteBase: "https://github.com/andymchugh/andrewbmchugh-flow-panel"
urlPanelWebsiteSurname: "mchugh"

cells:
grafana:
link:
url: "https://grafana.com/"
Expand Down Expand Up @@ -69,4 +73,19 @@ cells:
link:
url: "\\\\< > bad data"
params: null

websiteSiteVariables:
link:
url: "https://github.com/andy${urlSiteWebsiteSurname}/andrewb${urlSiteWebsiteSurname}-flow-panel"
params: null
websitePanelVariables:
link:
url: "${urlPanelWebsiteBase}/tree/main/src/README.md"
params: null
relativeSiteVar:
link:
url: "/d/c3c52b34-4c6f-4f25-b2b9-b4c8cb31bbe1/flow-${animationSiteVar}?orgId=1"
params: null
badLinkSiteVar:
link:
url: "\\\\< > bad link ${animationSiteVar} variable data"
params: null
6 changes: 6 additions & 0 deletions provisioning/dashboardData/linksSite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---

linkVariables:
urlSiteWebsiteBase: "https://github.com/andymchugh/andrewbmchugh-flow-panel"
urlSiteWebsiteSurname: "mchugh"
animationSiteVar: "animation"
180 changes: 90 additions & 90 deletions provisioning/dashboards/links.json

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/components/Config.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VariableInterpolation, getTemplateSrv } from "@grafana/runtime";
import { createUrl } from "./Utils";

export type DatapointMode = 'last' | 'lastNotNull';
export type ColorGradientMode = 'none' | 'hue';
Expand All @@ -24,6 +23,7 @@ export type Threshold = {
export type Link = {
url: string;
params: LinkUrlParams;
initialized: boolean | undefined;
};

export type Background = {
Expand Down Expand Up @@ -132,6 +132,7 @@ export type ZoomPanPinch = {
export type SiteConfig = {
zoomPanPinch: ZoomPanPinch;
variableThresholdScalars: Map<string, VariableThresholdScalars[]>;
linkVariables: Map<string, string>;
links: Map<string, Link>;
colors: Map<string, string>;
thresholds: Map<string, Threshold[]>;
Expand All @@ -141,6 +142,7 @@ export type SiteConfig = {
export type PanelConfig = {
test: TestConfig;
zoomPanPinch: ZoomPanPinch;
linkVariables: Map<string, string>;
background: Background;
animationsPresent: boolean;
variableThresholdScalars: Map<string, VariableThresholdScalars[]>;
Expand Down Expand Up @@ -182,6 +184,7 @@ export function panelConfigFactory(config: any) {
return {
test: config.test || {},
zoomPanPinch: config.zoomPanPinch || {},
linkVariables: new Map<string, string>(Object.entries(config.linkVariables || {})),
background: config.background || {},
variableThresholdScalars: new Map<string, VariableThresholdScalars[]>(Object.entries(config.variableThresholdScalars || {})),
gradientMode: config.gradientMode || 'none',
Expand All @@ -198,6 +201,7 @@ export function siteConfigFactory(config: any) {
config = config || {};
return {
zoomPanPinch: config.zoomPanPinch || {},
linkVariables: new Map<string, string>(Object.entries(config.linkVariables || {})),
links: new Map<string, Link>(Object.entries(config.links || {})),
colors: new Map<string, string>(Object.entries(config.colors || {})),
variableThresholdScalars: new Map<string, VariableThresholdScalars[]>(Object.entries(config.variableThresholdScalars || {})),
Expand Down Expand Up @@ -246,6 +250,7 @@ function panelConfigDereference(siteConfig: SiteConfig, panelConfig: PanelConfig
}

}

panelConfig.cells.forEach((cell) => {
colorBlend(cell, 'labelColor', cell.labelColorCompound);
colorBlend(cell, 'strokeColor', cell.strokeColorCompound);
Expand All @@ -254,9 +259,6 @@ function panelConfigDereference(siteConfig: SiteConfig, panelConfig: PanelConfig
if (!cell.link && cell.linkRef) {
cell.link = siteConfig.links.get(cell.linkRef);
}
if (cell.link) {
cell.link = createUrl(cell.link);
}

if (cell.label) {
if (typeof cell.label.decimalPoints === 'undefined') {
Expand Down Expand Up @@ -313,6 +315,9 @@ function panelConfigDereference(siteConfig: SiteConfig, panelConfig: PanelConfig
}
});

// Blend linkVariables
panelConfig.linkVariables = new Map([...siteConfig.linkVariables, ...panelConfig.linkVariables]);

// Blend ZoomPanPinch settings
panelConfig.zoomPanPinch = {...siteConfig.zoomPanPinch, ...panelConfig.zoomPanPinch};
}
Expand Down
23 changes: 7 additions & 16 deletions src/components/FlowPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { svgInit, svgUpdate, SvgHolder, SvgElementAttribs } from 'components/Svg
import { seriesExtend, seriesInterpolate , seriesTransform } from 'components/TimeSeries';
import { TimeSliderFactory } from 'components/TimeSlider';
import { displayColorsInner, displayDataInner, displayMappingsInner, displaySvgInner } from 'components/DebuggingEditor';
import { appendUrlParams, colorLookup, getInstrumenter } from 'components/Utils';
import { colorLookup, constructUrl, getInstrumenter } from 'components/Utils';
import { addHook, sanitize } from 'dompurify';
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch';

Expand Down Expand Up @@ -74,31 +74,22 @@ const getStyles = () => {
};
};

function clickHandlerFactory(elementAttribs: Map<string, SvgElementAttribs>) {
function clickHandlerFactory(elementAttribs: Map<string, SvgElementAttribs>, linkVariables: Map<string, string>) {
return (event: React.MouseEvent<HTMLElement, MouseEvent>) => {
if (event.target) {
const element = event.target as HTMLElement;
const link = elementAttribs.get(element.id)?.link;
if (link) {
let url = link.url;
if (link.params === 'time') {
const urlParams = new URLSearchParams(window.location.search);
const from = urlParams.get('from');
const to = urlParams.get('to');
const phrase = from && to ? `?from=${from}&to=${to}` :
from ? `?from=${from}` :
to ? `?to=${to}` : '';
url = appendUrlParams(url, phrase);
const url = constructUrl(link, linkVariables);
if (url) {
window.open(getTemplateSrv().replace(url));
}
else if (link.params === 'all') {
url = appendUrlParams(url, window.location.search);
}
window.open(getTemplateSrv().replace(url));
}
}
}
}


export const FlowPanel: React.FC<Props> = ({ options, data, width, height, timeZone }) => {
//---------------------------------------------------------------------------
// State for 'load -> init -> update' startup phasing
Expand Down Expand Up @@ -171,7 +162,7 @@ export const FlowPanel: React.FC<Props> = ({ options, data, width, height, timeZ
doc: svgDoc,
attribs: svgAttribs,
};
clickHandlerRef.current = clickHandlerFactory(svgAttribs.elementAttribs);
clickHandlerRef.current = clickHandlerFactory(svgAttribs.elementAttribs, panelConfig.linkVariables);

setHighlighterSelection(undefined);
setInitialized(true);
Expand Down
36 changes: 32 additions & 4 deletions src/components/Utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,45 @@ export function isUrl(str: string) {
}
}

export function createUrl(link: Link) {
export function createUrl(url: string) {
try {
link.url = new URL(link.url, document.baseURI).href;
return link;
url = new URL(url, document.baseURI).href;
return url;
}
catch(err) {
console.log('invalid url link', link.url, 'error:', err);
console.log('invalid url', url, 'error:', err);
return undefined;
}
}

export function constructUrl(link: Link, linkVariables: Map<string, string>) {
if (!link.initialized) {
link.initialized = true;
linkVariables.forEach((value: string, key: string) => {
const token = '\$\{'.concat(key, '\}');
link.url = link.url.split(token).join(value);
});
link.url = createUrl(link.url) || "";
}
if (link.url.length) {
let url = link.url;
if (link.params === 'time') {
const urlParams = new URLSearchParams(window.location.search);
const from = urlParams.get('from');
const to = urlParams.get('to');
const phrase = from && to ? `?from=${from}&to=${to}` :
from ? `?from=${from}` :
to ? `?to=${to}` : '';
url = appendUrlParams(url, phrase);
}
else if (link.params === 'all') {
url = appendUrlParams(url, window.location.search);
}
return url;
}
return undefined;
}

//-----------------------------------------------------------------------------
// RegExp
//-------
Expand Down
9 changes: 9 additions & 0 deletions yaml_defs/panelConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test:
# Version 1.12.0 onwards: When set test data values are extended with a string-data timeseries.
testDataStringData: false

#------------------------------------------------------------------------------
# Version 1.14.0 onwards: The zoomPanPinch yaml provides customization of the zoom-pan-pinch
# behavior. It can be defined in either panelConfig or siteConfig.
zoomPanPinch:
Expand All @@ -40,6 +41,14 @@ zoomPanPinch:
# requiring a combo such as ALT+Scroll this clash can be avoided.
wheelActivationKeys: ["Alt"]

#------------------------------------------------------------------------------
# Version 1.14.0 onwards: Link variables will be substituted in urls prior to passing through
# grafana variable substitution. Variables should be defined in the same format as grafana variables.
# i.e. ${urlBasePlugin}. These variables can be defined in siteConfig or panelConfig.
linkVariables:
urlBasePlugin: "https://github.com/andymchugh/andrewbmchugh-flow-panel/blob/main/"
urlBaseGrafana: "https://app.diagrams.net/"

#------------------------------------------------------------------------------
# Grafana Variable Scalars

Expand Down
7 changes: 7 additions & 0 deletions yaml_defs/siteConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ variableThresholdScalars:
environment:
- {variableValuePattern: "dev", thresholdScalar: 0.2, cellIdPatternScope: ["inbox_depth"]}

# Version 1.14.0 onwards: Link variables will be substituted in urls prior to passing through
# grafana variable substitution. Variables should be defined in the same format as grafana variables.
# i.e. ${urlBasePlugin}. These variables can be defined in siteConfig or panelConfig.
linkVariables:
urlBasePlugin: "https://github.com/andymchugh/andrewbmchugh-flow-panel/blob/main/"
urlBaseGrafana: "https://app.diagrams.net/"

# These are referenced from panelConfig via the 'linkRef' field.
# The fields inside a link are explained in the panelConfig documentation.
# i.e. In panelConfig you see: linkRef: "drawio"
Expand Down

0 comments on commit 2f34f29

Please sign in to comment.