mirror of https://github.com/grafana/grafana
Query History: Split data and view models (#44922)
* Remove unused properties * Fix unit tests * Fix unit tests * Split data models * Simplify updating items in rich history * Update tests * Fix starring an item and add a unit test * Move the converter to a separate file and add unit tests * Convert a private function to an inline function * Add more docs and clean up the code * Update public/app/core/history/localStorageConverter.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/core/utils/richHistory.test.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Use template literals over explicit casting * Split updateRichHistory to three separate functions Co-authored-by: Giordano Ricci <me@giordanoricci.com>pull/45233/head
parent
374681b546
commit
e7605ad974
@ -0,0 +1,60 @@ |
||||
import { backendSrv } from '../services/backend_srv'; |
||||
import { fromDTO, toDTO } from './localStorageConverter'; |
||||
import { RichHistoryQuery } from '../../types'; |
||||
import { RichHistoryLocalStorageDTO } from './RichHistoryLocalStorage'; |
||||
|
||||
jest.mock('@grafana/runtime', () => ({ |
||||
...(jest.requireActual('@grafana/runtime') as unknown as object), |
||||
getBackendSrv: () => backendSrv, |
||||
getDataSourceSrv: () => { |
||||
return { |
||||
getList: () => { |
||||
return [{ uid: 'uid', name: 'dev-test' }]; |
||||
}, |
||||
}; |
||||
}, |
||||
})); |
||||
|
||||
const validRichHistory: RichHistoryQuery = { |
||||
comment: 'comment', |
||||
createdAt: 1, |
||||
datasourceName: 'dev-test', |
||||
datasourceUid: 'uid', |
||||
id: '1', |
||||
queries: [{ refId: 'A' }], |
||||
starred: true, |
||||
}; |
||||
|
||||
const validDTO: RichHistoryLocalStorageDTO = { |
||||
comment: 'comment', |
||||
datasourceName: 'dev-test', |
||||
queries: [{ refId: 'A' }], |
||||
starred: true, |
||||
ts: 1, |
||||
}; |
||||
|
||||
describe('LocalStorage converted', () => { |
||||
it('converts RichHistoryQuery to local storage DTO', () => { |
||||
expect(toDTO(validRichHistory)).toMatchObject(validDTO); |
||||
}); |
||||
|
||||
it('throws an error when data source for RichHistory does not exist to avoid saving invalid items', () => { |
||||
const invalidRichHistory = { ...validRichHistory, datasourceUid: 'invalid' }; |
||||
expect(() => { |
||||
toDTO(invalidRichHistory); |
||||
}).toThrow(); |
||||
}); |
||||
|
||||
it('converts DTO to RichHistoryQuery', () => { |
||||
expect(fromDTO(validDTO)).toMatchObject(validRichHistory); |
||||
}); |
||||
|
||||
it('uses empty uid when datasource does not exist for a DTO to fail gracefully for queries from removed datasources', () => { |
||||
const invalidDto = { ...validDTO, datasourceName: 'removed' }; |
||||
expect(fromDTO(invalidDto)).toMatchObject({ |
||||
...validRichHistory, |
||||
datasourceName: 'removed', |
||||
datasourceUid: '', |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,41 @@ |
||||
import { find } from 'lodash'; |
||||
import { DataSourceInstanceSettings } from '@grafana/data'; |
||||
import { getDataSourceSrv } from '@grafana/runtime'; |
||||
import { RichHistoryLocalStorageDTO } from './RichHistoryLocalStorage'; |
||||
import { RichHistoryQuery } from '../../types'; |
||||
|
||||
export const fromDTO = (dto: RichHistoryLocalStorageDTO): RichHistoryQuery => { |
||||
const datasource = find( |
||||
getDataSourceSrv().getList(), |
||||
(settings: DataSourceInstanceSettings) => settings.name === dto.datasourceName |
||||
); |
||||
|
||||
return { |
||||
id: dto.ts.toString(), |
||||
createdAt: dto.ts, |
||||
datasourceName: dto.datasourceName, |
||||
datasourceUid: datasource?.uid || '', // will be show on the list as coming from a removed data source
|
||||
starred: dto.starred, |
||||
comment: dto.comment, |
||||
queries: dto.queries, |
||||
}; |
||||
}; |
||||
|
||||
export const toDTO = (richHistoryQuery: RichHistoryQuery): RichHistoryLocalStorageDTO => { |
||||
const datasource = find( |
||||
getDataSourceSrv().getList(), |
||||
(settings: DataSourceInstanceSettings) => settings.uid === richHistoryQuery.datasourceUid |
||||
); |
||||
|
||||
if (!datasource) { |
||||
throw new Error('Datasource not found.'); |
||||
} |
||||
|
||||
return { |
||||
ts: richHistoryQuery.createdAt, |
||||
datasourceName: richHistoryQuery.datasourceName, |
||||
starred: richHistoryQuery.starred, |
||||
comment: richHistoryQuery.comment, |
||||
queries: richHistoryQuery.queries, |
||||
}; |
||||
}; |
Loading…
Reference in new issue