Skip to content

Commit 87f62c1

Browse files
author
mbrill-nt
committed
Template Variables are now resolved
1 parent 022a674 commit 87f62c1

File tree

10 files changed

+77
-41
lines changed

10 files changed

+77
-41
lines changed

src/module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { PanelPlugin } from '@grafana/data';
22
import { PanelSettings } from './types';
33

4-
import { ServiceDependencyGraphPanelController } from './panel/ServiceDependencyGraphPanelController';
4+
import { PanelController } from './panel/PanelController';
55
import { optionsBuilder } from './options/options';
66

7-
export const plugin = new PanelPlugin<PanelSettings>(ServiceDependencyGraphPanelController).setPanelOptions(
8-
optionsBuilder
9-
);
7+
export const plugin = new PanelPlugin<PanelSettings>(PanelController).setPanelOptions(optionsBuilder);

src/options/options.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PanelOptionsEditorBuilder } from '@grafana/data';
22
import { PanelSettings } from '../types';
33
import { TypeaheadTextField } from './TypeAheadTextfield/TypeaheadTextfield';
4-
import { IconMapping } from './iconMapping/iconMapping';
4+
import { IconMapping } from './iconMapping/IconMapping';
55
import { DummyDataSwitch } from './dummyDataSwitch/DummyDataSwitch';
66
import { DefaultSettings } from './DefaultSettings';
77

@@ -14,7 +14,7 @@ export const optionsBuilder = (builder: PanelOptionsEditorBuilder<PanelSettings>
1414
path: 'dataMapping.aggregationType',
1515
id: 'aggregationType',
1616
editor: TypeaheadTextField,
17-
name: 'Aggregation Type',
17+
name: 'Component Column',
1818
category: ['Connection Mapping'],
1919
defaultValue: DefaultSettings.dataMapping.aggregationType,
2020
})

src/panel/ServiceDependencyGraphPanelController.tsx renamed to src/panel/PanelController.tsx

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import '../css/novatec-service-dependency-graph-panel.css';
1616
import GraphGenerator from 'processing/graph_generator';
1717
import PreProcessor from 'processing/pre_processor';
1818
import data from '../dummy_data_frame';
19+
import { getTemplateSrv } from '@grafana/runtime';
1920

2021
interface Props extends PanelProps<PanelSettings> {}
2122

@@ -36,7 +37,7 @@ interface PanelState {
3637
options: PanelSettings;
3738
}
3839

39-
export class ServiceDependencyGraphPanelController extends PureComponent<Props, PanelState> {
40+
export class PanelController extends PureComponent<Props, PanelState> {
4041
cy: cytoscape.Core | undefined;
4142

4243
ref: LegacyRef<HTMLDivElement>;
@@ -57,10 +58,45 @@ export class ServiceDependencyGraphPanelController extends PureComponent<Props,
5758
this.preProcessor = new PreProcessor(this);
5859
}
5960

60-
getSettings(): PanelSettings {
61+
getSettings(resolveVariables: boolean): PanelSettings {
62+
if (resolveVariables) {
63+
return this.resolveVariables(this.props.options);
64+
}
6165
return this.props.options;
6266
}
6367

68+
resolveVariables(element: any) {
69+
if (element instanceof Object) {
70+
const newObject: any = {};
71+
for (const key of Object.keys(element)) {
72+
newObject[key] = this.resolveVariables(element[key]);
73+
}
74+
return newObject;
75+
}
76+
77+
if (element instanceof String || typeof element === 'string') {
78+
return getTemplateSrv().replace(element.toString());
79+
}
80+
return element;
81+
}
82+
83+
resolveTemplateVars(input: any, copy: boolean) {
84+
var value = input;
85+
if (copy) {
86+
value = _.cloneDeep(value);
87+
}
88+
89+
if (typeof value === 'string' || value instanceof String) {
90+
value = getTemplateSrv().replace(value.toString());
91+
}
92+
if (value instanceof Object) {
93+
for (const key of Object.keys(value)) {
94+
value[key] = this.resolveTemplateVars(value[key], false);
95+
}
96+
}
97+
return value;
98+
}
99+
64100
componentDidUpdate() {
65101
this.processData();
66102
}
@@ -86,7 +122,7 @@ export class ServiceDependencyGraphPanelController extends PureComponent<Props,
86122

87123
processData() {
88124
var inputData: DataFrame[] = this.props.data.series;
89-
if (this.getSettings().dataMapping.showDummyData) {
125+
if (this.getSettings(true).dataMapping.showDummyData) {
90126
inputData = data;
91127
}
92128
this.processQueryData(inputData);
@@ -186,7 +222,8 @@ export class ServiceDependencyGraphPanelController extends PureComponent<Props,
186222
</div>
187223
</div>
188224
);
225+
} else {
226+
return <div>{error}</div>;
189227
}
190-
return <div>{error}</div>;
191228
}
192229
}

src/panel/canvas/graph_canvas.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default class CanvasDrawer {
124124

125125
_getImageAsset(assetName: string, resolveName = true) {
126126
if (!_.has(this.imageAssets, assetName)) {
127-
const assetUrl = assetUtils.getTypeSymbol(assetName, this.controller.getSettings().icons, resolveName);
127+
const assetUrl = assetUtils.getTypeSymbol(assetName, this.controller.getSettings(true).icons, resolveName);
128128
this._loadImage(assetUrl, assetName);
129129
}
130130

@@ -182,7 +182,7 @@ export default class CanvasDrawer {
182182
return false;
183183
}
184184

185-
if (!this.controller.getSettings().animate && elapsedTime < 1000) {
185+
if (!this.controller.getSettings(true).animate && elapsedTime < 1000) {
186186
return true;
187187
}
188188
return false;
@@ -227,7 +227,7 @@ export default class CanvasDrawer {
227227
// static element rendering
228228
// cyCanvas.resetTransform(ctx);
229229
cyCanvas.clear(ctx);
230-
if (this.controller.getSettings().showDebugInformation) {
230+
if (this.controller.getSettings(true).showDebugInformation) {
231231
this._drawDebugInformation();
232232
}
233233

@@ -275,7 +275,7 @@ export default class CanvasDrawer {
275275
this._drawEdgeParticles(ctx, edge, sourcePoint, targetPoint, now);
276276
}
277277

278-
const { showConnectionStats } = this.controller.getSettings();
278+
const { showConnectionStats } = this.controller.getSettings(true);
279279
if (showConnectionStats && cy.zoom() > 1) {
280280
for (const edge of edges) {
281281
this._drawEdgeLabel(ctx, edge);
@@ -322,7 +322,7 @@ export default class CanvasDrawer {
322322
}
323323

324324
_drawEdgeLabel(ctx: CanvasRenderingContext2D, edge: cytoscape.EdgeSingular) {
325-
const { timeFormat } = this.controller.getSettings();
325+
const { timeFormat } = this.controller.getSettings(true);
326326

327327
const midpoint = edge.midpoint();
328328
const xMid = midpoint.x;
@@ -413,7 +413,7 @@ export default class CanvasDrawer {
413413
index--;
414414
}
415415

416-
const dangerColor = this.controller.getSettings().style.dangerColor;
416+
const dangerColor = this.controller.getSettings(true).style.dangerColor;
417417
ctx.fillStyle = dangerColor;
418418
ctx.fill();
419419
}
@@ -508,7 +508,7 @@ export default class CanvasDrawer {
508508
this._drawDonut(ctx, node, 15, 5, 0.5, [errorPct, unknownPct, healthyPct]);
509509

510510
// drawing the baseline status
511-
const { showBaselines } = this.controller.getSettings();
511+
const { showBaselines } = this.controller.getSettings(true);
512512
if (showBaselines && responseTime >= 0 && threshold >= 0) {
513513
const thresholdViolation = threshold < responseTime;
514514

@@ -527,7 +527,7 @@ export default class CanvasDrawer {
527527

528528
_drawServiceIcon(ctx: CanvasRenderingContext2D, node: cytoscape.NodeSingular) {
529529
const nodeId: string = node.id();
530-
const iconMappings = this.controller.getSettings().icons;
530+
const iconMappings = this.controller.getSettings(true).icons;
531531

532532
const mapping = _.find(iconMappings, ({ pattern }) => {
533533
try {
@@ -550,7 +550,7 @@ export default class CanvasDrawer {
550550
}
551551

552552
_drawNodeStatistics(ctx: CanvasRenderingContext2D, node: cytoscape.NodeSingular) {
553-
const { timeFormat } = this.controller.getSettings();
553+
const { timeFormat } = this.controller.getSettings(true);
554554
const lines: string[] = [];
555555

556556
const metrics: IntGraphMetrics = node.data('metrics');
@@ -614,7 +614,7 @@ export default class CanvasDrawer {
614614
ctx.closePath();
615615

616616
ctx.setLineDash([10, 2]);
617-
if (violation && this.controller.getSettings().animate) {
617+
if (violation && this.controller.getSettings(true).animate) {
618618
ctx.lineDashOffset = this.dashAnimationOffset;
619619
} else {
620620
ctx.lineDashOffset = 0;
@@ -673,7 +673,7 @@ export default class CanvasDrawer {
673673
const xPos = pos.x - labelWidth / 2;
674674
const yPos = pos.y + node.height() * 0.8;
675675

676-
const { showBaselines } = this.controller.getSettings();
676+
const { showBaselines } = this.controller.getSettings(true);
677677
const metrics: IntGraphMetrics = node.data('metrics');
678678
const responseTime = _.defaultTo(metrics.response_time, -1);
679679
const threshold = _.defaultTo(metrics.threshold, -1);
@@ -719,7 +719,7 @@ export default class CanvasDrawer {
719719
ctx.fillStyle = 'white';
720720
ctx.fill();
721721

722-
const { healthyColor, dangerColor, noDataColor } = this.controller.getSettings().style;
722+
const { healthyColor, dangerColor, noDataColor } = this.controller.getSettings(true).style;
723723
const colors = [dangerColor, noDataColor, healthyColor];
724724
for (let i = 0; i < percentages.length; i++) {
725725
let arc = this._drawArc(ctx, currentArc, cX, cY, radius, percentages[i], colors[i]);

src/panel/canvas/particle_engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class ParticleEngine {
99

1010
minSpawnPropability = 0.004;
1111

12-
spawnInterval: any;
12+
spawnInterval: NodeJS.Timeout;
1313

1414
animating: boolean;
1515

src/panel/serviceDependencyGraph/ServiceDependencyGraph.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import CanvasDrawer from 'panel/canvas/graph_canvas';
22
import cytoscape, { EdgeCollection, EdgeSingular, ElementDefinition, NodeSingular } from 'cytoscape';
33
import React from 'react';
44
import { PureComponent } from 'react';
5-
import { ServiceDependencyGraphPanelController } from '../ServiceDependencyGraphPanelController';
5+
import { PanelController } from '../PanelController';
66
import cyCanvas from 'cytoscape-canvas';
77
import cola from 'cytoscape-cola';
88
import layoutOptions from '../layout_options';
@@ -23,7 +23,7 @@ import './ServiceDependencyGraph.css';
2323
interface PanelState {
2424
zoom: number | undefined;
2525
animate: boolean | undefined;
26-
controller: ServiceDependencyGraphPanelController;
26+
controller: PanelController;
2727
cy?: cytoscape.Core | undefined;
2828
graphCanvas?: CanvasDrawer | undefined;
2929
animateButtonClass?: string;
@@ -223,8 +223,8 @@ export class ServiceDependencyGraph extends PureComponent<PanelState, PanelState
223223
}
224224
}
225225

226-
getSettings() {
227-
return this.props.settings;
226+
getSettings(resolveVariables: boolean): PanelSettings {
227+
return this.state.controller.getSettings(resolveVariables);
228228
}
229229

230230
toggleAnimation() {
@@ -371,7 +371,7 @@ export class ServiceDependencyGraph extends PureComponent<PanelState, PanelState
371371
}
372372

373373
generateDrillDownLink() {
374-
const { drillDownLink } = this.getSettings();
374+
const { drillDownLink } = this.getSettings(false);
375375
if (drillDownLink !== undefined) {
376376
const link = drillDownLink.replace('{}', this.selectionId);
377377
this.resolvedDrillDownLink = this.templateSrv.replace(link);

src/panel/statistics/NodeStatistics.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ function getStatisticsTable(noDataText: string, nodeList: TableContent[]) {
3232
})}
3333
/>
3434
);
35+
} else {
36+
return <div className="no-data--selection">{noDataText}</div>;
3537
}
36-
return <div className="no-data--selection">{noDataText}</div>;
3738
}
3839

3940
export const NodeStatistics: React.FC<NodeStatisticsProps> = ({ nodeList, noDataText, title }) => {

src/processing/graph_generator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import { isPresent } from './utils/Utils';
3-
import { ServiceDependencyGraphPanelController } from '../panel/ServiceDependencyGraphPanelController';
3+
import { PanelController } from '../panel/PanelController';
44
import {
55
GraphDataElement,
66
IntGraph,
@@ -12,9 +12,9 @@ import {
1212
} from '../types';
1313

1414
class GraphGenerator {
15-
controller: ServiceDependencyGraphPanelController;
15+
controller: PanelController;
1616

17-
constructor(controller: ServiceDependencyGraphPanelController) {
17+
constructor(controller: PanelController) {
1818
this.controller = controller;
1919
}
2020

@@ -23,7 +23,7 @@ class GraphGenerator {
2323
return undefined;
2424
}
2525

26-
const sumMetrics = !this.controller.getSettings().sumTimings;
26+
const sumMetrics = !this.controller.getSettings(true).sumTimings;
2727

2828
var nodeName = dataElements[0].target;
2929
if (nodeName === '' || nodeName === undefined || nodeName === null) {
@@ -186,7 +186,7 @@ class GraphGenerator {
186186
metrics.error_rate = error_rate_out;
187187
}
188188
if (!_.isUndefined(response_time_out)) {
189-
const { sumTimings } = this.controller.getSettings();
189+
const { sumTimings } = this.controller.getSettings(true);
190190

191191
if (!sumTimings && metrics.rate) {
192192
metrics.response_time = response_time_out / metrics.rate;
@@ -209,7 +209,7 @@ class GraphGenerator {
209209
}
210210

211211
_filterData(graph: IntGraph): IntGraph {
212-
const { filterEmptyConnections: filterData } = this.controller.getSettings();
212+
const { filterEmptyConnections: filterData } = this.controller.getSettings(true);
213213

214214
if (filterData) {
215215
const filteredGraph: IntGraph = {

src/processing/pre_processor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { DataFrame } from '@grafana/data';
22
import _ from 'lodash';
3-
import { ServiceDependencyGraphPanelController } from '../panel/ServiceDependencyGraphPanelController';
3+
import { PanelController } from '../panel/PanelController';
44
import { GraphDataElement, GraphDataType, CurrentData } from '../types';
55

66
class PreProcessor {
7-
controller: ServiceDependencyGraphPanelController;
7+
controller: PanelController;
88

9-
constructor(controller: ServiceDependencyGraphPanelController) {
9+
constructor(controller: PanelController) {
1010
this.controller = controller;
1111
}
1212

@@ -17,7 +17,7 @@ class PreProcessor {
1717
targetColumn,
1818
extOrigin: externalSource,
1919
extTarget: externalTarget,
20-
} = this.controller.getSettings().dataMapping;
20+
} = this.controller.getSettings(true).dataMapping;
2121

2222
const result = _.map(data, dataObject => {
2323
var source = _.has(dataObject, sourceColumn) && dataObject[sourceColumn] !== '';
@@ -158,7 +158,7 @@ class PreProcessor {
158158
requestRateColumn,
159159
requestRateOutgoingColumn,
160160
baselineRtUpper,
161-
} = this.controller.getSettings().dataMapping;
161+
} = this.controller.getSettings(true).dataMapping;
162162

163163
for (const inputData of inputDataSets) {
164164
const { fields } = inputData;

src/processing/utils/Utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export function isPresent<T>(t: T | undefined | null | void): t is T {
88

99
export default {
1010
getConfig: function(graph: ServiceDependencyGraph, configName: keyof DataMapping) {
11-
return graph.getSettings().dataMapping[configName];
11+
return graph.getSettings(true).dataMapping[configName];
1212
},
1313
};

0 commit comments

Comments
 (0)