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,4 +1,4 @@
import { deprecationWarning } from '../../utils/deprecationWarning';
import { deprecationWarning } from '@grafana/data';
import { ColorPickerProps } from './ColorPickerPopover';
export const warnAboutColorPickerPropsDeprecation = (componentName: string, props: ColorPickerProps) => {

View File

@@ -1,6 +1,6 @@
import { ComponentClass } from 'react';
import { NavModel } from '@grafana/data';
import { PluginMeta, PluginIncludeType, GrafanaPlugin, KeyValue } from './plugin';
import { NavModel, KeyValue } from '@grafana/data';
import { PluginMeta, PluginIncludeType, GrafanaPlugin } from './plugin';
export interface AppRootProps<T = KeyValue> {
meta: AppPluginMeta<T>;

View File

@@ -1,4 +1,5 @@
import { ComponentClass } from 'react';
import { KeyValue } from '@grafana/data';
export enum PluginState {
alpha = 'alpha', // Only included it `enable_alpha` is true
@@ -11,8 +12,6 @@ export enum PluginType {
app = 'app',
}
export type KeyValue<T = any> = { [s: string]: T };
export interface PluginMeta<T extends {} = KeyValue> {
id: string;
name: string;

View File

@@ -1,34 +0,0 @@
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

@@ -1,17 +0,0 @@
import { KeyValue } from '../types/index';
// 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

@@ -1,6 +1,6 @@
import { MappingType, ValueMapping, DisplayProcessor, DisplayValue } from '@grafana/data';
import { getDisplayProcessor, getColorFromThreshold, getDecimalsForValue } from './displayValue';
import { getDisplayProcessor, getColorFromThreshold, getDecimalsForValue } from './displayProcessor';
function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
processors.forEach(processor => {

View File

@@ -15,9 +15,9 @@ import { getValueFormat } from './valueFormats/valueFormats';
import { getColorFromHexRgbOrName } from './namedColorsPalette';
// Types
import { GrafanaTheme, GrafanaThemeType } from '../types';
import { GrafanaTheme, GrafanaThemeType } from '../types/index';
export interface DisplayValueOptions {
interface DisplayProcessorOptions {
field?: FieldConfig;
// Context
@@ -25,7 +25,7 @@ export interface DisplayValueOptions {
theme?: GrafanaTheme; // Will pick 'dark' if not defined
}
export function getDisplayProcessor(options?: DisplayValueOptions): DisplayProcessor {
export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayProcessor {
if (options && !_.isEmpty(options)) {
const field = options.field ? options.field : {};
const formatFunc = getValueFormat(field.unit || 'none');

View File

@@ -12,7 +12,7 @@ import toNumber from 'lodash/toNumber';
import toString from 'lodash/toString';
import { GrafanaTheme, InterpolateFunction, ScopedVars } from '../types/index';
import { getDisplayProcessor } from './displayValue';
import { getDisplayProcessor } from './displayProcessor';
import { getFlotPairs } from './flotPairs';
export interface FieldDisplayOptions {

View File

@@ -1,9 +1,8 @@
export * from './valueFormats/valueFormats';
export * from './colors';
export * from './namedColorsPalette';
export * from './displayValue';
export * from './displayProcessor';
export * from './fieldDisplay';
export * from './deprecationWarning';
export * from './validate';
export { getFlotPairs } from './flotPairs';
export * from './slate';