Skip to content

Commit

Permalink
Merge pull request #60 from bitmovin/feat/AN-3229_implement-test-data…
Browse files Browse the repository at this point in the history
…source-method

feat/AN-3229 implements the testDatasource method
  • Loading branch information
MGJamJam authored Apr 5, 2024
2 parents 2ce7b6f + 5cf7556 commit 8d12bb9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
10 changes: 9 additions & 1 deletion bitmovin-analytics-datasource/src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent } from 'react';
import React, { ChangeEvent, useEffect } from 'react';
import { DataSourceHttpSettings, FieldSet, InlineField, InlineSwitch, Input } from '@grafana/ui';
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
import { MyDataSourceOptions } from '../types';
Expand All @@ -8,6 +8,14 @@ interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions>
export function ConfigEditor(props: Props) {
const { onOptionsChange, options } = props;

// sets the instanceSettings.url to the default bitmovin api url if it is not already set when opening the ConfigEditor
useEffect(() => {
if (options.url === '' || options.url == null) {
onOptionsChange({ ...options, url: 'https://api.bitmovin.com/v1' });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const onAdAnalyticsChange = (event: ChangeEvent<HTMLInputElement>) => {
const jsonData = {
...options.jsonData,
Expand Down
52 changes: 47 additions & 5 deletions bitmovin-analytics-datasource/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
MutableDataFrame,
FieldType,
} from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime';
import { catchError, lastValueFrom, map, of, Observable } from 'rxjs';

import { MyQuery, MyDataSourceOptions } from './types';

export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
baseUrl: string;
apiKey: string;
tenantOrgId?: string;
adAnalytics?: boolean;
Expand All @@ -20,6 +23,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
this.apiKey = instanceSettings.jsonData.apiKey;
this.tenantOrgId = instanceSettings.jsonData.tenantOrgId;
this.adAnalytics = instanceSettings.jsonData.adAnalytics;
this.baseUrl = instanceSettings.url!;
}

async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
Expand All @@ -41,11 +45,49 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
return { data };
}

async testDatasource() {
// Implement a health check for your data source.
return {
status: 'success',
message: 'Success',
async request(url: string, method: string) {
const options = {
url: this.baseUrl + url,
headers: { 'X-Api-Key': this.apiKey },
method: method,
};
return getBackendSrv().fetch(options);
}

async testDatasource() {
return lastValueFrom(
this.request('/analytics/licenses', 'GET').pipe(
map(() => {
return {
status: 'success',
message: 'Data source successfully setup and connected.',
};
}),
catchError((err) => {
let message = 'Bitmovin: ';
if (err.status) message += err.status + ' ';
if (err.statusText) {
message += err.statusText;
} else {
message += 'Can not connect to Bitmovin API';
}

let errorMessage = err.data?.message || err.data?.data?.message;

//additional errorDetails like requestId and timestamp if requestId is set
let errorDetails;
if (err.data?.requestId) {
errorDetails = 'Timestamp: ' + new Date().toISOString();
errorDetails += err.data?.requestId ? '\nRequestId: ' + err.data?.requestId : '';
}

return of({
status: 'error',
message: message,
details: { message: errorMessage, verboseMessage: errorDetails },
});
})
)
);
}
}

0 comments on commit 8d12bb9

Please sign in to comment.