-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.ts
executable file
·154 lines (125 loc) · 5.12 KB
/
datasource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// import defaults from 'lodash/defaults';
import {
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
MutableDataFrame,
FieldType,
} from '@grafana/data';
import { getBackendSrv, FetchResponse } from "@grafana/runtime"
import { MyQuery, MyDataSourceOptions } from './types';
import { Observable, merge } from 'rxjs';
import { QuandlDataset } from './QuandlApiTypes';
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
instanceUrl?: string;
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.instanceUrl = instanceSettings.url;
}
// TODO: Add default query
// query doesn't need to be async anymore because we are using Observables
query(options: DataQueryRequest<MyQuery>): Observable<DataQueryResponse> {
const observableResponses: Array<Observable<DataQueryResponse>> = options.targets.map((query) => {
// Build query parameters and path
const apiParams: Map<string, any> = new Map<string,any>();
// range params
const { range } = options;
const from = range!.from.format("YYYY-MM-DD");
const to = range!.to.format("YYYY-MM-DD");
apiParams.set("start_date", from);
apiParams.set("end_date", to);
// Advanced Params
if(query.setAdvanced) { // By only applying them when setAdvanced is true, we only apply them when they are visible. Prevents confusion.
if(query.limit) {apiParams.set("limit", query.limit);}
if(query.column_index) {apiParams.set("column_index", query.column_index);}
if(query.order) {apiParams.set("order", query.order);}
if(query.collapse) {apiParams.set("collapse", query.collapse);}
if(query.transform) {apiParams.set("transform", query.transform)}
}
const apiPath = `/api/v3/datasets/${query.database_code}/${query.dataset_code}/data.json`;
// Create a new observable to return
const queryObservable = new Observable<DataQueryResponse>((subscriber) => {
// The doRequest function returns an observable. We subscribe to it, format the data
// and emit our own next with the formatted data.
let response: Observable<FetchResponse> = this.doRequest(apiPath, Object.fromEntries(apiParams));
let respSubscriber = response.subscribe({
next(r) {
//console.log(`Response for query ${query.refId}`);
//console.log(r);
if(r.status !== 200) {
subscriber.error(`Unexpected HTTP Response from API: ${r.status} - ${r.statusText}`);
return;
}
// Start Parsing the Response
let df = new MutableDataFrame({
refId: query.refId,
fields: [],
})
let dataset_data: QuandlDataset = r.data.dataset_data as QuandlDataset;
for(const f of dataset_data.column_names) {
// The time series data set always has a date and then number fields.
// With tables we'll probably have to infer data types or just use xml because the xml format shows types. .
if(f === "Date") {
df.addField({name: f, type: FieldType.time})
} else {
df.addField({ name: f, type: FieldType.number})
}
}
for(const r of dataset_data.data) {
df.appendRow(r);
}
// Alert the subscriber that we have new formatted data.
// Not sure why I have to put it in an object with the array, but it seems to work.
subscriber.next( {data: [df] } );
},
error(err) {
console.log(err);
subscriber.error(`API returned error: ${err.status} - ${err.statusText}`);
},
complete() {
// Once we are done reading the response, we can call it complete here too.
subscriber.complete();
respSubscriber.unsubscribe()
}
});
});
return queryObservable;
});
// The query function only returns one observable. we use merge to combine them all?
return merge(...observableResponses);
}
async testDatasource() {
// Implement a health check for your data source.
let response = this.doRequest("/api/v3/datasets/FED/FA087005086_A/data.json");
let respSubscriber = response.subscribe({
next(x) {
if(x.status === 200) {
return {
status: "success",
message: "Success"
}
}
else { throw Error(`Error connecting to Quandl API: ${x.status}`)}
},
error(err) {
console.log(err);
},
complete() {
respSubscriber.unsubscribe();
}
});
return {
status: 'success',
message: 'Success',
};
}
doRequest(path: string, apiParams: Record<string, any> | undefined = undefined): Observable<FetchResponse> {
const result = getBackendSrv().fetch({
method: "GET",
url: this.instanceUrl + "/quandlApi" + path,
params: apiParams
});
return result;
}
}