Commit d6de40cb by Torkel Ödegaard

Added missing strict type checking options to grafana/ui and fixed type errors

parent 0256a4dd
...@@ -22,7 +22,7 @@ export class Graph extends PureComponent<GraphProps> { ...@@ -22,7 +22,7 @@ export class Graph extends PureComponent<GraphProps> {
showBars: false, showBars: false,
}; };
element: HTMLElement | null; element: HTMLElement | null = null;
componentDidUpdate() { componentDidUpdate() {
this.draw(); this.draw();
......
...@@ -6,7 +6,7 @@ let themeMock: ((name?: string) => GrafanaTheme) | null; ...@@ -6,7 +6,7 @@ let themeMock: ((name?: string) => GrafanaTheme) | null;
export let getTheme = (name?: string) => (themeMock && themeMock(name)) || (name === 'light' ? lightTheme : darkTheme); export let getTheme = (name?: string) => (themeMock && themeMock(name)) || (name === 'light' ? lightTheme : darkTheme);
export const mockTheme = (mock: (name: string) => GrafanaTheme) => { export const mockTheme = (mock: (name?: string) => GrafanaTheme) => {
themeMock = mock; themeMock = mock;
return () => { return () => {
themeMock = null; themeMock = null;
......
import { toFixed } from './valueFormats'; import { toFixed, DecimalCount } from './valueFormats';
export function toPercent(size: number, decimals: number) { export function toPercent(size: number, decimals: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
return toFixed(size, decimals) + '%'; return toFixed(size, decimals) + '%';
} }
export function toPercentUnit(size: number, decimals: number) { export function toPercentUnit(size: number, decimals: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
return toFixed(100 * size, decimals) + '%'; return toFixed(100 * size, decimals) + '%';
} }
export function toHex0x(value: number, decimals: number) { export function toHex0x(value: number, decimals: DecimalCount) {
if (value == null) { if (value == null) {
return ''; return '';
} }
...@@ -25,7 +25,7 @@ export function toHex0x(value: number, decimals: number) { ...@@ -25,7 +25,7 @@ export function toHex0x(value: number, decimals: number) {
return '0x' + hexString; return '0x' + hexString;
} }
export function toHex(value: number, decimals: number) { export function toHex(value: number, decimals: DecimalCount) {
if (value == null) { if (value == null) {
return ''; return '';
} }
...@@ -34,9 +34,9 @@ export function toHex(value: number, decimals: number) { ...@@ -34,9 +34,9 @@ export function toHex(value: number, decimals: number) {
.toUpperCase(); .toUpperCase();
} }
export function sci(value: number, decimals: number) { export function sci(value: number, decimals: DecimalCount) {
if (value == null) { if (value == null) {
return ''; return '';
} }
return value.toExponential(decimals); return value.toExponential(decimals as number);
} }
import { toFixed, toFixedScaled } from './valueFormats'; import { toFixed, toFixedScaled, DecimalCount } from './valueFormats';
import moment from 'moment'; import moment from 'moment';
interface IntervalsInSeconds { interface IntervalsInSeconds {
...@@ -27,7 +27,7 @@ const INTERVALS_IN_SECONDS: IntervalsInSeconds = { ...@@ -27,7 +27,7 @@ const INTERVALS_IN_SECONDS: IntervalsInSeconds = {
[Interval.Millisecond]: 0.001, [Interval.Millisecond]: 0.001,
}; };
export function toNanoSeconds(size: number, decimals: number, scaledDecimals: number) { export function toNanoSeconds(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -45,7 +45,7 @@ export function toNanoSeconds(size: number, decimals: number, scaledDecimals: nu ...@@ -45,7 +45,7 @@ export function toNanoSeconds(size: number, decimals: number, scaledDecimals: nu
} }
} }
export function toMicroSeconds(size: number, decimals: number, scaledDecimals: number) { export function toMicroSeconds(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -59,7 +59,7 @@ export function toMicroSeconds(size: number, decimals: number, scaledDecimals: n ...@@ -59,7 +59,7 @@ export function toMicroSeconds(size: number, decimals: number, scaledDecimals: n
} }
} }
export function toMilliSeconds(size: number, decimals: number, scaledDecimals: number) { export function toMilliSeconds(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -83,22 +83,29 @@ export function toMilliSeconds(size: number, decimals: number, scaledDecimals: n ...@@ -83,22 +83,29 @@ export function toMilliSeconds(size: number, decimals: number, scaledDecimals: n
return toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, ' year'); return toFixedScaled(size / 31536000000, decimals, scaledDecimals, 10, ' year');
} }
export function toSeconds(size: number, decimals: number, scaledDecimals: number) { export function trySubstract(value1: DecimalCount, value2: DecimalCount): DecimalCount {
if (value1 !== null && value1 !== undefined && value2 !== null && value2 !== undefined) {
return value1 - value2;
}
return undefined;
}
export function toSeconds(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
// Less than 1 µs, divide in ns // Less than 1 µs, divide in ns
if (Math.abs(size) < 0.000001) { if (Math.abs(size) < 0.000001) {
return toFixedScaled(size * 1e9, decimals, scaledDecimals - decimals, -9, ' ns'); return toFixedScaled(size * 1e9, decimals, trySubstract(scaledDecimals, decimals), -9, ' ns');
} }
// Less than 1 ms, divide in µs // Less than 1 ms, divide in µs
if (Math.abs(size) < 0.001) { if (Math.abs(size) < 0.001) {
return toFixedScaled(size * 1e6, decimals, scaledDecimals - decimals, -6, ' µs'); return toFixedScaled(size * 1e6, decimals, trySubstract(scaledDecimals, decimals), -6, ' µs');
} }
// Less than 1 second, divide in ms // Less than 1 second, divide in ms
if (Math.abs(size) < 1) { if (Math.abs(size) < 1) {
return toFixedScaled(size * 1e3, decimals, scaledDecimals - decimals, -3, ' ms'); return toFixedScaled(size * 1e3, decimals, trySubstract(scaledDecimals, decimals), -3, ' ms');
} }
if (Math.abs(size) < 60) { if (Math.abs(size) < 60) {
...@@ -120,7 +127,7 @@ export function toSeconds(size: number, decimals: number, scaledDecimals: number ...@@ -120,7 +127,7 @@ export function toSeconds(size: number, decimals: number, scaledDecimals: number
return toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, ' year'); return toFixedScaled(size / 3.15569e7, decimals, scaledDecimals, 7, ' year');
} }
export function toMinutes(size: number, decimals: number, scaledDecimals: number) { export function toMinutes(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -138,7 +145,7 @@ export function toMinutes(size: number, decimals: number, scaledDecimals: number ...@@ -138,7 +145,7 @@ export function toMinutes(size: number, decimals: number, scaledDecimals: number
} }
} }
export function toHours(size: number, decimals: number, scaledDecimals: number) { export function toHours(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -154,7 +161,7 @@ export function toHours(size: number, decimals: number, scaledDecimals: number) ...@@ -154,7 +161,7 @@ export function toHours(size: number, decimals: number, scaledDecimals: number)
} }
} }
export function toDays(size: number, decimals: number, scaledDecimals: number) { export function toDays(size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -168,13 +175,15 @@ export function toDays(size: number, decimals: number, scaledDecimals: number) { ...@@ -168,13 +175,15 @@ export function toDays(size: number, decimals: number, scaledDecimals: number) {
} }
} }
export function toDuration(size: number, decimals: number, timeScale: Interval): string { export function toDuration(size: number, decimals: DecimalCount, timeScale: Interval): string {
if (size === null) { if (size === null) {
return ''; return '';
} }
if (size === 0) { if (size === 0) {
return '0 ' + timeScale + 's'; return '0 ' + timeScale + 's';
} }
if (size < 0) { if (size < 0) {
return toDuration(-size, decimals, timeScale) + ' ago'; return toDuration(-size, decimals, timeScale) + ' ago';
} }
...@@ -189,14 +198,22 @@ export function toDuration(size: number, decimals: number, timeScale: Interval): ...@@ -189,14 +198,22 @@ export function toDuration(size: number, decimals: number, timeScale: Interval):
{ long: Interval.Second }, { long: Interval.Second },
{ long: Interval.Millisecond }, { long: Interval.Millisecond },
]; ];
// convert $size to milliseconds // convert $size to milliseconds
// intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors // intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
size *= INTERVALS_IN_SECONDS[timeScale] * 1000; size *= INTERVALS_IN_SECONDS[timeScale] * 1000;
const strings = []; const strings = [];
// after first value >= 1 print only $decimals more // after first value >= 1 print only $decimals more
let decrementDecimals = false; let decrementDecimals = false;
for (let i = 0; i < units.length && decimals >= 0; i++) { let decimalsCount = 0;
if (decimals !== null || decimals !== undefined) {
decimalsCount = decimals as number;
}
for (let i = 0; i < units.length && decimalsCount >= 0; i++) {
const interval = INTERVALS_IN_SECONDS[units[i].long] * 1000; const interval = INTERVALS_IN_SECONDS[units[i].long] * 1000;
const value = size / interval; const value = size / interval;
if (value >= 1 || decrementDecimals) { if (value >= 1 || decrementDecimals) {
...@@ -205,14 +222,14 @@ export function toDuration(size: number, decimals: number, timeScale: Interval): ...@@ -205,14 +222,14 @@ export function toDuration(size: number, decimals: number, timeScale: Interval):
const unit = units[i].long + (floor !== 1 ? 's' : ''); const unit = units[i].long + (floor !== 1 ? 's' : '');
strings.push(floor + ' ' + unit); strings.push(floor + ' ' + unit);
size = size % interval; size = size % interval;
decimals--; decimalsCount--;
} }
} }
return strings.join(', '); return strings.join(', ');
} }
export function toClock(size: number, decimals?: number) { export function toClock(size: number, decimals?: DecimalCount) {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -257,11 +274,11 @@ export function toClock(size: number, decimals?: number) { ...@@ -257,11 +274,11 @@ export function toClock(size: number, decimals?: number) {
return format ? `${hours}:${moment.utc(size).format(format)}` : hours; return format ? `${hours}:${moment.utc(size).format(format)}` : hours;
} }
export function toDurationInMilliseconds(size: number, decimals: number) { export function toDurationInMilliseconds(size: number, decimals: DecimalCount) {
return toDuration(size, decimals, Interval.Millisecond); return toDuration(size, decimals, Interval.Millisecond);
} }
export function toDurationInSeconds(size: number, decimals: number) { export function toDurationInSeconds(size: number, decimals: DecimalCount) {
return toDuration(size, decimals, Interval.Second); return toDuration(size, decimals, Interval.Second);
} }
...@@ -276,19 +293,19 @@ export function toDurationInHoursMinutesSeconds(size: number) { ...@@ -276,19 +293,19 @@ export function toDurationInHoursMinutesSeconds(size: number) {
return strings.join(':'); return strings.join(':');
} }
export function toTimeTicks(size: number, decimals: number, scaledDecimals: number) { export function toTimeTicks(size: number, decimals: DecimalCount, scaledDecimals: DecimalCount) {
return toSeconds(size, decimals, scaledDecimals); return toSeconds(size, decimals, scaledDecimals);
} }
export function toClockMilliseconds(size: number, decimals: number) { export function toClockMilliseconds(size: number, decimals: DecimalCount) {
return toClock(size, decimals); return toClock(size, decimals);
} }
export function toClockSeconds(size: number, decimals: number) { export function toClockSeconds(size: number, decimals: DecimalCount) {
return toClock(size * 1000, decimals); return toClock(size * 1000, decimals);
} }
export function dateTimeAsIso(value: number, decimals: number, scaledDecimals: number, isUtc: boolean) { export function dateTimeAsIso(value: number, decimals: DecimalCount, scaledDecimals: DecimalCount, isUtc?: boolean) {
const time = isUtc ? moment.utc(value) : moment(value); const time = isUtc ? moment.utc(value) : moment(value);
if (moment().isSame(value, 'day')) { if (moment().isSame(value, 'day')) {
...@@ -297,7 +314,7 @@ export function dateTimeAsIso(value: number, decimals: number, scaledDecimals: n ...@@ -297,7 +314,7 @@ export function dateTimeAsIso(value: number, decimals: number, scaledDecimals: n
return time.format('YYYY-MM-DD HH:mm:ss'); return time.format('YYYY-MM-DD HH:mm:ss');
} }
export function dateTimeAsUS(value: number, decimals: number, scaledDecimals: number, isUtc: boolean) { export function dateTimeAsUS(value: number, decimals: DecimalCount, scaledDecimals: DecimalCount, isUtc?: boolean) {
const time = isUtc ? moment.utc(value) : moment(value); const time = isUtc ? moment.utc(value) : moment(value);
if (moment().isSame(value, 'day')) { if (moment().isSame(value, 'day')) {
...@@ -306,7 +323,7 @@ export function dateTimeAsUS(value: number, decimals: number, scaledDecimals: nu ...@@ -306,7 +323,7 @@ export function dateTimeAsUS(value: number, decimals: number, scaledDecimals: nu
return time.format('MM/DD/YYYY h:mm:ss a'); return time.format('MM/DD/YYYY h:mm:ss a');
} }
export function dateTimeFromNow(value: number, decimals: number, scaledDecimals: number, isUtc: boolean) { export function dateTimeFromNow(value: number, decimals: DecimalCount, scaledDecimals: DecimalCount, isUtc?: boolean) {
const time = isUtc ? moment.utc(value) : moment(value); const time = isUtc ? moment.utc(value) : moment(value);
return time.fromNow(); return time.fromNow();
} }
import { scaledUnits } from './valueFormats'; import { scaledUnits, DecimalCount } from './valueFormats';
export function currency(symbol: string) { export function currency(symbol: string) {
const units = ['', 'K', 'M', 'B', 'T']; const units = ['', 'K', 'M', 'B', 'T'];
const scaler = scaledUnits(1000, units); const scaler = scaledUnits(1000, units);
return (size: number, decimals: number, scaledDecimals: number) => { return (size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) => {
if (size === null) { if (size === null) {
return ''; return '';
} }
......
import { getCategories } from './categories'; import { getCategories } from './categories';
type ValueFormatter = (value: number, decimals?: number, scaledDecimals?: number, isUtc?: boolean) => string; export type DecimalCount = number | null | undefined;
interface ValueFormat { export type ValueFormatter = (
value: number,
decimals?: DecimalCount,
scaledDecimals?: DecimalCount,
isUtc?: boolean
) => string;
export interface ValueFormat {
name: string; name: string;
id: string; id: string;
fn: ValueFormatter; fn: ValueFormatter;
...@@ -22,7 +29,7 @@ let categories: ValueFormatCategory[] = []; ...@@ -22,7 +29,7 @@ let categories: ValueFormatCategory[] = [];
const index: ValueFormatterIndex = {}; const index: ValueFormatterIndex = {};
let hasBuiltIndex = false; let hasBuiltIndex = false;
export function toFixed(value: number, decimals?: number): string { export function toFixed(value: number, decimals?: DecimalCount): string {
if (value === null) { if (value === null) {
return ''; return '';
} }
...@@ -50,20 +57,24 @@ export function toFixed(value: number, decimals?: number): string { ...@@ -50,20 +57,24 @@ export function toFixed(value: number, decimals?: number): string {
export function toFixedScaled( export function toFixedScaled(
value: number, value: number,
decimals: number, decimals?: DecimalCount,
scaledDecimals: number, scaledDecimals?: DecimalCount,
additionalDecimals: number, additionalDecimals?: DecimalCount,
ext: string ext?: string
) { ) {
if (scaledDecimals === null) { if (scaledDecimals) {
return toFixed(value, decimals) + ext; if (additionalDecimals) {
} else {
return toFixed(value, scaledDecimals + additionalDecimals) + ext; return toFixed(value, scaledDecimals + additionalDecimals) + ext;
} else {
return toFixed(value, scaledDecimals) + ext;
}
} }
return toFixed(value, decimals) + ext;
} }
export function toFixedUnit(unit: string) { export function toFixedUnit(unit: string): ValueFormatter {
return (size: number, decimals: number) => { return (size: number, decimals?: DecimalCount) => {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -75,7 +86,7 @@ export function toFixedUnit(unit: string) { ...@@ -75,7 +86,7 @@ export function toFixedUnit(unit: string) {
// numeric factor. Repeatedly scales the value down by the factor until it is // numeric factor. Repeatedly scales the value down by the factor until it is
// less than the factor in magnitude, or the end of the array is reached. // less than the factor in magnitude, or the end of the array is reached.
export function scaledUnits(factor: number, extArray: string[]) { export function scaledUnits(factor: number, extArray: string[]) {
return (size: number, decimals: number, scaledDecimals: number) => { return (size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) => {
if (size === null) { if (size === null) {
return ''; return '';
} }
...@@ -92,7 +103,7 @@ export function scaledUnits(factor: number, extArray: string[]) { ...@@ -92,7 +103,7 @@ export function scaledUnits(factor: number, extArray: string[]) {
} }
} }
if (steps > 0 && scaledDecimals !== null) { if (steps > 0 && scaledDecimals !== null && scaledDecimals !== undefined) {
decimals = scaledDecimals + 3 * steps; decimals = scaledDecimals + 3 * steps;
} }
...@@ -100,17 +111,17 @@ export function scaledUnits(factor: number, extArray: string[]) { ...@@ -100,17 +111,17 @@ export function scaledUnits(factor: number, extArray: string[]) {
}; };
} }
export function locale(value: number, decimals: number) { export function locale(value: number, decimals: DecimalCount) {
if (value == null) { if (value == null) {
return ''; return '';
} }
return value.toLocaleString(undefined, { maximumFractionDigits: decimals }); return value.toLocaleString(undefined, { maximumFractionDigits: decimals as number });
} }
export function simpleCountUnit(symbol: string) { export function simpleCountUnit(symbol: string) {
const units = ['', 'K', 'M', 'B', 'T']; const units = ['', 'K', 'M', 'B', 'T'];
const scaler = scaledUnits(1000, units); const scaler = scaledUnits(1000, units);
return (size: number, decimals: number, scaledDecimals: number) => { return (size: number, decimals?: DecimalCount, scaledDecimals?: DecimalCount) => {
if (size === null) { if (size === null) {
return ''; return '';
} }
......
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"include": [ "include": ["src/**/*.ts", "src/**/*.tsx"],
"src/**/*.ts", "exclude": ["dist", "node_modules"],
"src/**/*.tsx"
],
"exclude": [
"dist",
"node_modules"
],
"compilerOptions": { "compilerOptions": {
"rootDirs": [".", "stories"], "rootDirs": [".", "stories"],
"module": "esnext", "module": "esnext",
"outDir": "dist", "outDir": "dist",
"declaration": true, "declaration": true,
"strict": true,
"alwaysStrict": true,
"noImplicitAny": true, "noImplicitAny": true,
"strictNullChecks": true, "strictNullChecks": true,
"typeRoots": ["./node_modules/@types", "types"], "typeRoots": ["./node_modules/@types", "types"],
"skipLibCheck": true // Temp workaround for Duplicate identifier tsc errors "skipLibCheck": true // Temp workaround for Duplicate identifier tsc errors
}, }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment