-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(assets): split asset utilization plugin code from asset calibrati…
…on plugin code (#65) Co-authored-by: Alex Kerezsi <[email protected]> Co-authored-by: Alex Kerezsi <[email protected]>
- Loading branch information
1 parent
31f515a
commit 2c99c56
Showing
21 changed files
with
494 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
* @mure @cameronwaterman | ||
|
||
/src/datasources/asset-calibration @CiprianAnton @kkerezsi | ||
/src/datasources/asset @CiprianAnton @kkerezsi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/datasources/asset-calibration/AssetCalibrationDataSource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { | ||
DataFrameDTO, | ||
DataQueryRequest, | ||
DataSourceInstanceSettings, | ||
FieldDTO, | ||
TestDataSourceResponse, | ||
} from '@grafana/data'; | ||
import { BackendSrv, getBackendSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime'; | ||
import { DataSourceBase } from 'core/DataSourceBase'; | ||
import { | ||
AssetCalibrationForecastGroupByType, | ||
AssetCalibrationForecastKey, | ||
AssetCalibrationQuery, | ||
AssetModel, | ||
AssetsResponse, | ||
CalibrationForecastResponse, | ||
} from './types'; | ||
import { SystemMetadata } from "../system/types"; | ||
import { defaultOrderBy, defaultProjection } from "../system/constants"; | ||
|
||
export class AssetCalibrationDataSource extends DataSourceBase<AssetCalibrationQuery> { | ||
constructor( | ||
readonly instanceSettings: DataSourceInstanceSettings, | ||
readonly backendSrv: BackendSrv = getBackendSrv(), | ||
readonly templateSrv: TemplateSrv = getTemplateSrv() | ||
) { | ||
super(instanceSettings, backendSrv, templateSrv); | ||
} | ||
|
||
defaultQuery = { | ||
groupBy: [], | ||
}; | ||
|
||
baseUrl = this.instanceSettings.url + '/niapm/v1'; | ||
|
||
async runQuery(query: AssetCalibrationQuery, options: DataQueryRequest): Promise<DataFrameDTO> { | ||
return await this.processCalibrationForecastQuery(query as AssetCalibrationQuery, options); | ||
} | ||
async processCalibrationForecastQuery(query: AssetCalibrationQuery, options: DataQueryRequest) { | ||
const result: DataFrameDTO = { refId: query.refId, fields: [] }; | ||
const from = options.range!.from.toISOString(); | ||
const to = options.range!.to.toISOString(); | ||
|
||
const calibrationForecastResponse: CalibrationForecastResponse = await this.queryCalibrationForecast(query.groupBy, from, to); | ||
|
||
result.fields = calibrationForecastResponse.calibrationForecast.columns || []; | ||
result.fields = result.fields.map(field => this.formatField(field, query)); | ||
|
||
return result; | ||
} | ||
|
||
formatField(field: FieldDTO, query: AssetCalibrationQuery): FieldDTO { | ||
if (!field.values) { | ||
return field; | ||
} | ||
|
||
if (field.name === AssetCalibrationForecastKey.Time) { | ||
field.values = this.formatTimeField(field.values, query); | ||
field.name = 'Formatted Time'; | ||
return field; | ||
} | ||
|
||
return field; | ||
} | ||
|
||
formatTimeField(values: string[], query: AssetCalibrationQuery): string[] { | ||
const timeGrouping = query.groupBy.find(item => | ||
[AssetCalibrationForecastGroupByType.Day, | ||
AssetCalibrationForecastGroupByType.Week, | ||
AssetCalibrationForecastGroupByType.Month].includes(item as AssetCalibrationForecastGroupByType) | ||
) as AssetCalibrationForecastGroupByType | undefined; | ||
|
||
const formatFunctionMap = { | ||
[AssetCalibrationForecastGroupByType.Day]: this.formatDateForDay, | ||
[AssetCalibrationForecastGroupByType.Week]: this.formatDateForWeek, | ||
[AssetCalibrationForecastGroupByType.Month]: this.formatDateForMonth, | ||
}; | ||
|
||
const formatFunction = formatFunctionMap[timeGrouping!] || ((v: string) => v); | ||
values = values.map(formatFunction); | ||
return values; | ||
} | ||
|
||
formatDateForDay(date: string): string { | ||
return new Date(date).toISOString().split('T')[0]; | ||
} | ||
|
||
formatDateForWeek(date: string): string { | ||
const startDate = new Date(date); | ||
const endDate = new Date(startDate); | ||
endDate.setDate(startDate.getDate() + 6); | ||
return `${startDate.toISOString().split('T')[0]} : ${endDate.toISOString().split('T')[0]}`; | ||
} | ||
|
||
formatDateForMonth(date: string): string { | ||
return new Date(date).toLocaleDateString('en-US', { year: 'numeric', month: 'long' }); | ||
} | ||
|
||
shouldRunQuery(_: AssetCalibrationQuery): boolean { | ||
return true; | ||
} | ||
|
||
async queryAssets(filter = '', take = -1): Promise<AssetModel[]> { | ||
let data = { filter, take }; | ||
try { | ||
let response = await this.post<AssetsResponse>(this.baseUrl + '/query-assets', data); | ||
return response.assets; | ||
} catch (error) { | ||
throw new Error(`An error occurred while querying assets: ${error}`); | ||
} | ||
} | ||
|
||
async queryCalibrationForecast(groupBy: string[], startTime: string, endTime: string, filter = ''): Promise<CalibrationForecastResponse> { | ||
let data = { groupBy, startTime, endTime, filter }; | ||
try { | ||
let response = await this.post<CalibrationForecastResponse>(this.baseUrl + '/assets/calibration-forecast', data); | ||
return response; | ||
} catch (error) { | ||
throw new Error(`An error occurred while querying assets calibration forecast: ${error}`); | ||
} | ||
} | ||
|
||
async querySystems(filter = '', projection = defaultProjection): Promise<SystemMetadata[]> { | ||
try { | ||
let response = await this.getSystems({ | ||
filter: filter, | ||
projection: `new(${projection.join()})`, | ||
orderBy: defaultOrderBy, | ||
}) | ||
|
||
return response.data; | ||
} catch (error) { | ||
throw new Error(`An error occurred while querying systems: ${error}`); | ||
} | ||
} | ||
|
||
async testDatasource(): Promise<TestDataSourceResponse> { | ||
await this.get(this.baseUrl + '/assets?take=1'); | ||
return { status: 'success', message: 'Data source connected and authentication successful!' }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# SystemLink Asset Calibration data source | ||
|
||
This is a plugin for the Asset service calibration related functionalities. It allows you to: | ||
|
||
- Visualize asset calibration forecast |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 9 additions & 27 deletions
36
...etQueryCalibrationForecastEditor.test.tsx → ...ents/AssetQueryCalibrationEditor.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { DataSourcePlugin } from '@grafana/data'; | ||
import { AssetCalibrationDataSource } from './AssetCalibrationDataSource'; | ||
import { HttpConfigEditor } from 'core/components/HttpConfigEditor'; | ||
import { AssetCalibrationQueryEditor } from './components/AssetCalibrationQueryEditor'; | ||
|
||
export const plugin = new DataSourcePlugin(AssetCalibrationDataSource) | ||
.setConfigEditor(HttpConfigEditor) | ||
.setQueryEditor(AssetCalibrationQueryEditor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"type": "datasource", | ||
"name": "SystemLink Asset Calibration", | ||
"id": "ni-slassetcalibration-datasource", | ||
"metrics": true, | ||
"info": { | ||
"author": { | ||
"name": "NI" | ||
}, | ||
"logos": { | ||
"small": "img/logo-ni.svg", | ||
"large": "img/logo-ni.svg" | ||
} | ||
} | ||
} |
Oops, something went wrong.