Skip to content

Commit 8b4c314

Browse files
refactor: Splits out variable logic
1 parent 27ec484 commit 8b4c314

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/datasource.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
1212

1313
import { HaystackQuery, OpsQuery, HaystackDataSourceOptions, HaystackVariableQuery, QueryType } from './types';
1414
import { firstValueFrom } from 'rxjs';
15+
import { isRef, parseRef } from 'haystack';
1516

1617
export const queryTypes: QueryType[] = [
1718
{ label: 'Eval', value: 'eval', apiRequirements: ['eval'], description: 'Evaluate an Axon expression' },
@@ -118,32 +119,10 @@ export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDat
118119
}
119120

120121
let variableValues = column.values.map((value, index) => {
121-
let variableValue = value;
122-
switch (column.type) {
123-
case FieldType.string:
124-
if (value.startsWith('@')) {
125-
// Detect ref using @ prefix, and adjust value to just the Ref
126-
const spaceIndex = value.indexOf(' ');
127-
if (spaceIndex > -1) {
128-
// Display name exists
129-
variableValue = value.substring(0, spaceIndex);
130-
}
131-
}
132-
}
122+
let variableValue = variableValueFromCell(value, column.type);
133123

134124
let displayValue = displayColumn.values[index];
135-
let variableText = displayValue;
136-
switch (displayColumn.type) {
137-
case FieldType.string:
138-
if (displayValue.startsWith('@')) {
139-
// Detect ref using @ prefix, and adjust value to just the Ref
140-
const spaceIndex = displayValue.indexOf(' ');
141-
if (spaceIndex > -1) {
142-
// Display name exists
143-
variableText = displayValue.substring(spaceIndex + 2, displayValue.length - 1);
144-
}
145-
}
146-
}
125+
let variableText = variableTextFromCell(displayValue, displayColumn.type);
147126

148127
return { text: variableText, value: variableValue };
149128
});
@@ -170,3 +149,24 @@ export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDat
170149
};
171150
}
172151
}
152+
153+
function variableValueFromCell(value: string, columnType: FieldType): string {
154+
switch (columnType) {
155+
case FieldType.string:
156+
if (isRef(value)) {
157+
return parseRef(value).id;
158+
}
159+
}
160+
return value;
161+
}
162+
163+
function variableTextFromCell(value: string, columnType: FieldType): string {
164+
switch (columnType) {
165+
case FieldType.string:
166+
if (isRef(value)) {
167+
let ref = parseRef(value);
168+
return ref.dis ?? ref.id;
169+
}
170+
}
171+
return value;
172+
}

src/haystack.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Detect refs using @ prefix
2+
export function isRef(value: string): boolean {
3+
return value.startsWith('@');
4+
}
5+
6+
// Converts a string into ref components by detecting the first space. Assumes format `@id "dis"`
7+
export function parseRef(value: string): { id: string; dis: string | null } {
8+
let id = value;
9+
let dis: string | null = null;
10+
const spaceIndex = value.indexOf(' ');
11+
12+
if (spaceIndex > -1) {
13+
// Display name exists
14+
id = value.substring(0, spaceIndex);
15+
// Cut off leading and trailing quotes
16+
dis = value.substring(spaceIndex + 2, value.length - 1);
17+
}
18+
return { id: id, dis: dis };
19+
}

0 commit comments

Comments
 (0)