Skip to content

Commit

Permalink
feat: display system status counts (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Carson Moore <carson.moore@ni.com>
Co-authored-by: Cameron Waterman <cameron.waterman@ni.com>
  • Loading branch information
3 people authored Jul 26, 2023
1 parent 2b21199 commit 285b8b2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
2 changes: 1 addition & 1 deletion release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ module.exports = {
}],
'@semantic-release/git'
]
}
}
15 changes: 15 additions & 0 deletions src/core/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { enumToOptions } from "./utils";

test('enumToOptions', () => {
enum fakeStringEnum {
Label1 = 'Value1',
Label2 = 'Value2'
};

const result = enumToOptions(fakeStringEnum);

expect(result).toEqual([
{ label: 'Label1', value: 'Value1' },
{ label: 'Label2', value: 'Value2' }
]);
});
11 changes: 11 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SelectableValue } from "@grafana/data";

export function enumToOptions<T>(stringEnum: { [name: string]: T }): Array<SelectableValue<T>> {
const RESULT = [];

for (const [key, value] of Object.entries(stringEnum)) {
RESULT.push({ label: key, value: value });
}

return RESULT;
}
36 changes: 22 additions & 14 deletions src/datasources/system/SystemDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
DataSourceInstanceSettings,
MutableDataFrame,
FieldType,
CoreApp,
} from '@grafana/data';

import { TestingStatus, getBackendSrv } from '@grafana/runtime';

import { SystemQuery } from './types';
import { QueryType, SystemQuery, SystemSummary } from './types';

export class SystemDataSource extends DataSourceApi<SystemQuery> {
baseUrl: string;
Expand All @@ -19,24 +20,31 @@ export class SystemDataSource extends DataSourceApi<SystemQuery> {
}

async query(options: DataQueryRequest<SystemQuery>): Promise<DataQueryResponse> {
const { range } = options;
const from = range!.from.valueOf();
const to = range!.to.valueOf();

// Return a constant for each query.
const data = options.targets.map((target) => {
return new MutableDataFrame({
refId: target.refId,
fields: [
{ name: 'Time', values: [from, to], type: FieldType.time },
{ name: 'Value', values: [target.constant, target.constant], type: FieldType.number },
],
});
});
const data = await Promise.all(options.targets.map(async (target) => {
if (target.queryKind === QueryType.Summary) {
let summaryResponse = await getBackendSrv().get<SystemSummary>(this.baseUrl + '/get-systems-summary');
return new MutableDataFrame({
refId: target.refId,
fields: [
{ name: 'Connected', values: [summaryResponse.connectedCount], type: FieldType.number },
{ name: 'Disconnected', values: [summaryResponse.disconnectedCount], type: FieldType.number },
],
});
} else {
throw Error("Not implemented");
}
}));

return { data };
}

getDefaultQuery(_core: CoreApp): Partial<SystemQuery> {
return {
queryKind: QueryType.Summary,
};
}

async testDatasource(): Promise<TestingStatus> {
await getBackendSrv().get(this.baseUrl + '/get-systems-summary');
return { status: 'success', message: 'Data source connected and authentication successful!' };
Expand Down
38 changes: 18 additions & 20 deletions src/datasources/system/components/SystemQueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import React, { ChangeEvent } from 'react';
import { InlineField, Input } from '@grafana/ui';
import React from 'react';
import { InlineField, InlineFieldRow, RadioButtonGroup } from '@grafana/ui';
import { QueryEditorProps } from '@grafana/data';
import { SystemDataSource } from '../SystemDataSource';
import { SystemQuery } from '../types';
import { QueryType, SystemQuery } from '../types';
import { enumToOptions } from 'core/utils';

type Props = QueryEditorProps<SystemDataSource, SystemQuery>;

export function SystemQueryEditor({ query, onChange, onRunQuery }: Props) {
const onQueryTextChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange({ ...query, queryText: event.target.value });
};
// const QUERY_TYPES = [
// {label: "Metadata", value: QueryType.Metadata},
// {label: "Summary", value: QueryType.Summary}
// ]

const onConstantChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange({ ...query, constant: parseFloat(event.target.value) });
// executes the query
export function SystemQueryEditor({ query, onChange, onRunQuery }: Props) {
const onQueryTypeChange = (value: QueryType) => {
onChange({ ...query, queryKind: value })
onRunQuery();
};

const { queryText, constant } = query;
}

return (
<div className="gf-form">
<InlineField label="Constant">
<Input onChange={onConstantChange} value={constant} width={8} type="number" step="0.1" />
</InlineField>
<InlineField label="Query Text" labelWidth={16} tooltip="Not used yet">
<Input onChange={onQueryTextChange} value={queryText || ''} />
</InlineField>
<div>
<InlineFieldRow >
<InlineField label="Query type">
<RadioButtonGroup options={enumToOptions(QueryType)} onChange={onQueryTypeChange} value={query.queryKind} />
</InlineField>
</InlineFieldRow>
</div>
);
}
13 changes: 11 additions & 2 deletions src/datasources/system/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { DataQuery } from '@grafana/schema'

export enum QueryType {
Metadata = "Metadata",
Summary = "Summary"
}

export interface SystemQuery extends DataQuery {
queryText?: string;
constant: number;
queryKind: QueryType
}

export interface SystemSummary {
connectedCount: number,
disconnectedCount: number
}

0 comments on commit 285b8b2

Please sign in to comment.