Skip to content

Commit

Permalink
Fix NaN for variables (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-vl authored Jun 26, 2021
1 parent 0b791b6 commit 744c899
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
1 change: 0 additions & 1 deletion provisioning/datasources/redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ datasources:
type: redis-datasource
access: proxy
orgId: 1
isDefault: true
version: 1
url: redis://host.docker.internal:6379
jsonData:
Expand Down
33 changes: 33 additions & 0 deletions src/data-source/data-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,39 @@ describe('DataSource', () => {
});
});

it('Should call query method with numbers', (done) => {
const querySpyMethod = jest.spyOn(dataSource, 'query').mockImplementation(
() =>
new Observable((subscriber) => {
subscriber.next({
data: [
{
fields: [
{
name: 'get',
values: {
toArray() {
return new Float64Array([21, 31]);
},
},
},
],
length: 1,
},
],
});
subscriber.complete();
})
);

dataSource.metricFindQuery &&
dataSource.metricFindQuery('123', { variable: { datasource: '123' } }).then((result: MetricFindValue[]) => {
expect(querySpyMethod).toHaveBeenCalled();
expect(result).toEqual([{ text: 21 }, { text: 31 }]);
done();
});
});

afterAll(() => {
superQueryMock.mockReset();
setTemplateSrv(null as any);
Expand Down
15 changes: 9 additions & 6 deletions src/data-source/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ export class DataSource extends DataSourceWithBackend<RedisQuery, RedisDataSourc
* Run Query
*/
return this.query({
targets: [{ datasource: options.variable.datasource, query: query }],
targets: [{ refId: 'A', datasource: options.variable.datasource, query: query }],
} as DataQueryRequest<RedisQuery>)
.pipe(
switchMap$((response) => response.data),
switchMap$((data: DataFrame) => data.fields),
map$((field) =>
field.values.toArray().map((value) => {
return { text: value };
})
)
map$((field) => {
const values: MetricFindValue[] = [];
field.values.toArray().forEach((value) => {
values.push({ text: value });
});

return values;
})
)
.toPromise();
}
Expand Down

0 comments on commit 744c899

Please sign in to comment.