Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit c6af7a8

Browse files
committed
fetch fields of the nodes and edges from api
1 parent 72ab470 commit c6af7a8

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

src/datasource.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,54 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
1818
baseUrl: string;
1919
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
2020
super(instanceSettings);
21-
21+
2222
this.baseUrl = instanceSettings.jsonData.baseUrl || '';
2323
}
2424

2525
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
2626
const promises = options.targets.map(async target => {
2727
const query = defaults(target, defaultQuery);
28-
const response = await this.doRequest('/api/fetchgraph', `query=${query.queryText}`);
28+
const responseFields = await this.doRequest('/api/graph/fields', `query=${query.queryText}`);
29+
const response = await this.doRequest('/api/graph/data', `query=${query.queryText}`);
30+
const nodeFieldsResponse = responseFields.data.nodes_fields;
31+
const edgeFieldsResponse = responseFields.data.edges_fields;
32+
interface FrameFieldType {
33+
name: string;
34+
type: any;
35+
config?: any;
36+
}
37+
function fieldAssignator(FieldsResponse: any): FrameFieldType[] {
38+
var outputFields: FrameFieldType[] = [];
39+
FieldsResponse.forEach((field: any) => {
40+
var fieldType = field['type'] === 'number' ? FieldType.number : FieldType.string;
41+
var outputField: FrameFieldType = { name: field['field_name'], type: fieldType };
42+
if ('color' in field) {
43+
outputField.config = { color: { fixedColor: field['color'], mode: FieldColorModeId.Fixed } };
44+
}
45+
if ('displayName' in field) {
46+
outputField.config = { displayName: field['displayName'] };
47+
}
48+
outputFields.push(outputField);
49+
});
50+
return outputFields;
51+
}
52+
const nodeFields: FrameFieldType[] = fieldAssignator(nodeFieldsResponse);
2953
const nodeFrame = new MutableDataFrame({
3054
name: 'Nodes',
3155
refId: query.refId,
32-
fields: [
33-
{ name: 'id', type: FieldType.string },
34-
{ name: 'title', type: FieldType.string },
35-
{ name: 'subTitle', type: FieldType.string },
36-
{ name: 'detail__role', type: FieldType.string },
37-
{
38-
name: 'arc__failed',
39-
type: FieldType.number,
40-
config: { color: { fixedColor: 'red', mode: FieldColorModeId.Fixed } },
41-
},
42-
{
43-
name: 'arc__passed',
44-
type: FieldType.number,
45-
config: { color: { fixedColor: 'green', mode: FieldColorModeId.Fixed } },
46-
},
47-
],
56+
fields: nodeFields,
4857
});
49-
58+
const edgeFields: FrameFieldType[] = fieldAssignator(edgeFieldsResponse);
5059
const edgeFrame = new MutableDataFrame({
5160
name: 'Edges',
5261
refId: query.refId,
53-
fields: [
54-
{ name: 'id', type: FieldType.string },
55-
{ name: 'source', type: FieldType.string },
56-
{ name: 'target', type: FieldType.string },
57-
{ name: 'mainStat', type: FieldType.string },
58-
// { name: 'secondaryStat', type: FieldType.number },
59-
],
62+
fields: edgeFields,
63+
// [] { name: 'id', type: FieldType.string },
64+
// { name: 'source', type: FieldType.string },
65+
// { name: 'target', type: FieldType.string },
66+
// { name: 'mainStat', type: FieldType.string },
67+
// // { name: 'secondaryStat', type: FieldType.number },
68+
// ],
6069
});
6170
const nodes = response.data.nodes;
6271
const edges = response.data.edges;

0 commit comments

Comments
 (0)