mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactor: move KeyValue and deprecation warning to @grafana/data (#18582)
* move KeyValue and deprecation warning * move KeyValue and deprecation warning * rename displayProcessor file
This commit is contained in:
committed by
Torkel Ödegaard
parent
6335509a23
commit
5a41e8b119
@@ -1,3 +1,5 @@
|
||||
export type KeyValue<T = any> = { [s: string]: T };
|
||||
|
||||
export enum LoadingState {
|
||||
NotStarted = 'NotStarted',
|
||||
Loading = 'Loading',
|
||||
|
||||
34
packages/grafana-data/src/utils/deprecationWarning.test.ts
Normal file
34
packages/grafana-data/src/utils/deprecationWarning.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { deprecationWarning } from './deprecationWarning';
|
||||
|
||||
test('It should not output deprecation warnings too often', () => {
|
||||
let dateNowValue = 10000000;
|
||||
|
||||
const spyConsoleWarn = jest.spyOn(console, 'warn').mockImplementation();
|
||||
const spyDateNow = jest.spyOn(global.Date, 'now').mockImplementation(() => dateNowValue);
|
||||
// Make sure the mock works
|
||||
expect(Date.now()).toEqual(dateNowValue);
|
||||
expect(console.warn).toHaveBeenCalledTimes(0);
|
||||
|
||||
// Call the deprecation many times
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
expect(console.warn).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Increment the time by 1min
|
||||
dateNowValue += 60000;
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
deprecationWarning('file', 'oldName', 'newName');
|
||||
expect(console.warn).toHaveBeenCalledTimes(2);
|
||||
|
||||
deprecationWarning('file2', 'oldName', 'newName');
|
||||
deprecationWarning('file2', 'oldName', 'newName');
|
||||
deprecationWarning('file2', 'oldName', 'newName');
|
||||
expect(console.warn).toHaveBeenCalledTimes(3);
|
||||
|
||||
// or restoreMocks automatically?
|
||||
spyConsoleWarn.mockRestore();
|
||||
spyDateNow.mockRestore();
|
||||
});
|
||||
17
packages/grafana-data/src/utils/deprecationWarning.ts
Normal file
17
packages/grafana-data/src/utils/deprecationWarning.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { KeyValue } from '../types';
|
||||
|
||||
// Avoid writing the warning message more than once every 10s
|
||||
const history: KeyValue<number> = {};
|
||||
|
||||
export const deprecationWarning = (file: string, oldName: string, newName?: string) => {
|
||||
let message = `[Deprecation warning] ${file}: ${oldName} is deprecated`;
|
||||
if (newName) {
|
||||
message += `. Use ${newName} instead`;
|
||||
}
|
||||
const now = Date.now();
|
||||
const last = history[message];
|
||||
if (!last || now - last > 10000) {
|
||||
console.warn(message);
|
||||
history[message] = now;
|
||||
}
|
||||
};
|
||||
@@ -2,6 +2,7 @@ export * from './string';
|
||||
export * from './registry';
|
||||
export * from './markdown';
|
||||
export * from './processDataFrame';
|
||||
export * from './deprecationWarning';
|
||||
export * from './csv';
|
||||
export * from './fieldReducer';
|
||||
export * from './logs';
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
import { isDateTime } from './moment_wrapper';
|
||||
import { ArrayVector, SortedVector } from './vector';
|
||||
import { DataFrameHelper } from './dataFrameHelper';
|
||||
import { deprecationWarning } from './deprecationWarning';
|
||||
|
||||
function convertTableToDataFrame(table: TableData): DataFrame {
|
||||
const fields = table.columns.map(c => {
|
||||
@@ -224,7 +225,7 @@ export const toDataFrame = (data: any): DataFrame => {
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
v.appendRow(rows[i]);
|
||||
}
|
||||
// TODO: deprection warning
|
||||
deprecationWarning('DataFrame', '.rows', 'columnar format');
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user