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:
Ryan McKinley
2019-08-16 02:10:32 -07:00
committed by Torkel Ödegaard
parent 6335509a23
commit 5a41e8b119
16 changed files with 23 additions and 21 deletions

View File

@@ -1,3 +1,5 @@
export type KeyValue<T = any> = { [s: string]: T };
export enum LoadingState {
NotStarted = 'NotStarted',
Loading = 'Loading',

View 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();
});

View 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;
}
};

View File

@@ -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';

View File

@@ -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;
}