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

Commit f29d957

Browse files
committed
add comment for more readability
1 parent c6af7a8 commit f29d957

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/datasource.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,73 @@ import { getBackendSrv } from '@grafana/runtime';
1515
import { MyQuery, MyDataSourceOptions, defaultQuery } from './types';
1616

1717
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
18-
baseUrl: string;
18+
baseUrl: string; // base url of the api
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 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;
28+
// fetch graph fields from api
29+
const responseGraphFields = await this.doRequest('/api/graph/fields', `query=${query.queryText}`);
30+
// fetch graph data from api
31+
const responseGraphData = await this.doRequest('/api/graph/data', `query=${query.queryText}`);
32+
// extract fields of the nodes and edges in the graph fields object
33+
const nodeFieldsResponse = responseGraphFields.data.nodes_fields;
34+
const edgeFieldsResponse = responseGraphFields.data.edges_fields;
35+
// Define an interface for types of the FrameField
3236
interface FrameFieldType {
3337
name: string;
3438
type: any;
3539
config?: any;
3640
}
41+
// This function gets the fields of the api and transforms them to what grafana dataframe prefers
3742
function fieldAssignator(FieldsResponse: any): FrameFieldType[] {
3843
var outputFields: FrameFieldType[] = [];
3944
FieldsResponse.forEach((field: any) => {
45+
// fieldType can be either number of string
4046
var fieldType = field['type'] === 'number' ? FieldType.number : FieldType.string;
47+
// add 'name' and 'type' items to the output object
4148
var outputField: FrameFieldType = { name: field['field_name'], type: fieldType };
49+
// add color for 'arc__*' items(only apperas for the nodes)
4250
if ('color' in field) {
4351
outputField.config = { color: { fixedColor: field['color'], mode: FieldColorModeId.Fixed } };
4452
}
53+
// add disPlayName for 'detail__*' items
4554
if ('displayName' in field) {
4655
outputField.config = { displayName: field['displayName'] };
4756
}
4857
outputFields.push(outputField);
4958
});
5059
return outputFields;
5160
}
61+
// Extract node fields
5262
const nodeFields: FrameFieldType[] = fieldAssignator(nodeFieldsResponse);
63+
// Create nodes dataframe
5364
const nodeFrame = new MutableDataFrame({
5465
name: 'Nodes',
5566
refId: query.refId,
5667
fields: nodeFields,
5768
});
69+
// Extract edge fields
5870
const edgeFields: FrameFieldType[] = fieldAssignator(edgeFieldsResponse);
71+
// Create Edges dataframe
5972
const edgeFrame = new MutableDataFrame({
6073
name: 'Edges',
6174
refId: query.refId,
6275
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-
// ],
6976
});
70-
const nodes = response.data.nodes;
71-
const edges = response.data.edges;
77+
// Extract graph data of the related api response
78+
const nodes = responseGraphData.data.nodes;
79+
const edges = responseGraphData.data.edges;
80+
// add nodes to the node dataframe
7281
nodes.forEach((node: any) => {
7382
nodeFrame.add(node);
7483
});
84+
// add edges to the edges dataframe
7585
edges.forEach((edge: any) => {
7686
edgeFrame.add(edge);
7787
});
@@ -95,7 +105,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
95105
async testDatasource() {
96106
const defaultErrorMessage = 'Cannot connect to API';
97107
try {
98-
const response = await this.doRequest('/health');
108+
const response = await this.doRequest('/api/health');
99109
if (response.status === 200) {
100110
return {
101111
status: 'success',

0 commit comments

Comments
 (0)