mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: type improvements (#80464)
* type improvements * some more fixes * add TODOs to remove type assertions
This commit is contained in:
@@ -263,12 +263,11 @@ const validKeys = ['refId', 'key', 'context', 'datasource'];
|
||||
export function hasNonEmptyQuery<TQuery extends DataQuery>(queries: TQuery[]): boolean {
|
||||
return (
|
||||
queries &&
|
||||
queries.some((query: any) => {
|
||||
const keys = Object.keys(query)
|
||||
.filter((key) => validKeys.indexOf(key) === -1)
|
||||
.map((k) => query[k])
|
||||
.filter((v) => v);
|
||||
return keys.length > 0;
|
||||
queries.some((query) => {
|
||||
const entries = Object.entries(query)
|
||||
.filter(([key, _]) => validKeys.indexOf(key) === -1)
|
||||
.filter(([_, value]) => value);
|
||||
return entries.length > 0;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -103,23 +103,29 @@ export async function parseResponseBody<T>(
|
||||
if (responseType) {
|
||||
switch (responseType) {
|
||||
case 'arraybuffer':
|
||||
return response.arrayBuffer() as any;
|
||||
// this specifically returns a Promise<ArrayBuffer>
|
||||
// TODO refactor this function to remove the type assertions
|
||||
return response.arrayBuffer() as Promise<T>;
|
||||
|
||||
case 'blob':
|
||||
return response.blob() as any;
|
||||
// this specifically returns a Promise<Blob>
|
||||
// TODO refactor this function to remove the type assertions
|
||||
return response.blob() as Promise<T>;
|
||||
|
||||
case 'json':
|
||||
// An empty string is not a valid JSON.
|
||||
// Sometimes (unfortunately) our APIs declare their Content-Type as JSON, however they return an empty body.
|
||||
if (response.headers.get('Content-Length') === '0') {
|
||||
console.warn(`${response.url} returned an invalid JSON`);
|
||||
return {} as unknown as T;
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
|
||||
case 'text':
|
||||
return response.text() as any;
|
||||
// this specifically returns a Promise<string>
|
||||
// TODO refactor this function to remove the type assertions
|
||||
return response.text() as Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +133,7 @@ export async function parseResponseBody<T>(
|
||||
try {
|
||||
return JSON.parse(textData); // majority of the requests this will be something that can be parsed
|
||||
} catch {}
|
||||
return textData as any;
|
||||
return textData as T;
|
||||
}
|
||||
|
||||
function serializeParams(data: Record<string, any>): string {
|
||||
|
||||
@@ -30,127 +30,6 @@ export function getScaledDecimals(decimals: number, tickSize: number) {
|
||||
return decimals - Math.floor(Math.log(tickSize) / Math.LN10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate tick size based on min and max values, number of ticks and precision.
|
||||
* Implementation from Flot.
|
||||
* @param min Axis minimum
|
||||
* @param max Axis maximum
|
||||
* @param noTicks Number of ticks
|
||||
* @param tickDecimals Tick decimal precision
|
||||
*/
|
||||
export function getFlotTickSize(min: number, max: number, noTicks: number, tickDecimals: number) {
|
||||
const delta = (max - min) / noTicks;
|
||||
let dec = -Math.floor(Math.log(delta) / Math.LN10);
|
||||
const maxDec = tickDecimals;
|
||||
|
||||
const magn = Math.pow(10, -dec);
|
||||
const norm = delta / magn; // norm is between 1.0 and 10.0
|
||||
let size;
|
||||
|
||||
if (norm < 1.5) {
|
||||
size = 1;
|
||||
} else if (norm < 3) {
|
||||
size = 2;
|
||||
// special case for 2.5, requires an extra decimal
|
||||
if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
|
||||
size = 2.5;
|
||||
++dec;
|
||||
}
|
||||
} else if (norm < 7.5) {
|
||||
size = 5;
|
||||
} else {
|
||||
size = 10;
|
||||
}
|
||||
|
||||
size *= magn;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate axis range (min and max).
|
||||
* Implementation from Flot.
|
||||
*/
|
||||
export function getFlotRange(panelMin: any, panelMax: any, datamin: number, datamax: number) {
|
||||
const autoscaleMargin = 0.02;
|
||||
|
||||
let min = +(panelMin != null ? panelMin : datamin);
|
||||
let max = +(panelMax != null ? panelMax : datamax);
|
||||
const delta = max - min;
|
||||
|
||||
if (delta === 0.0) {
|
||||
// Grafana fix: wide Y min and max using increased wideFactor
|
||||
// when all series values are the same
|
||||
const wideFactor = 0.25;
|
||||
const widen = Math.abs(max === 0 ? 1 : max * wideFactor);
|
||||
|
||||
if (panelMin === null) {
|
||||
min -= widen;
|
||||
}
|
||||
// always widen max if we couldn't widen min to ensure we
|
||||
// don't fall into min == max which doesn't work
|
||||
if (panelMax == null || panelMin != null) {
|
||||
max += widen;
|
||||
}
|
||||
} else {
|
||||
// consider autoscaling
|
||||
const margin = autoscaleMargin;
|
||||
if (margin != null) {
|
||||
if (panelMin == null) {
|
||||
min -= delta * margin;
|
||||
// make sure we don't go below zero if all values
|
||||
// are positive
|
||||
if (min < 0 && datamin != null && datamin >= 0) {
|
||||
min = 0;
|
||||
}
|
||||
}
|
||||
if (panelMax == null) {
|
||||
max += delta * margin;
|
||||
if (max > 0 && datamax != null && datamax <= 0) {
|
||||
max = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return { min, max };
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate tick decimals.
|
||||
* Implementation from Flot.
|
||||
*/
|
||||
export function getFlotTickDecimals(datamin: number, datamax: number, axis: { min: any; max: any }, height: number) {
|
||||
const { min, max } = getFlotRange(axis.min, axis.max, datamin, datamax);
|
||||
const noTicks = 0.3 * Math.sqrt(height);
|
||||
const delta = (max - min) / noTicks;
|
||||
const dec = -Math.floor(Math.log(delta) / Math.LN10);
|
||||
|
||||
const magn = Math.pow(10, -dec);
|
||||
// norm is between 1.0 and 10.0
|
||||
const norm = delta / magn;
|
||||
let size;
|
||||
|
||||
if (norm < 1.5) {
|
||||
size = 1;
|
||||
} else if (norm < 3) {
|
||||
size = 2;
|
||||
// special case for 2.5, requires an extra decimal
|
||||
if (norm > 2.25) {
|
||||
size = 2.5;
|
||||
}
|
||||
} else if (norm < 7.5) {
|
||||
size = 5;
|
||||
} else {
|
||||
size = 10;
|
||||
}
|
||||
size *= magn;
|
||||
|
||||
const tickDecimals = Math.max(0, -Math.floor(Math.log(delta) / Math.LN10) + 1);
|
||||
// grafana addition
|
||||
const scaledDecimals = tickDecimals - Math.floor(Math.log(size) / Math.LN10);
|
||||
return { tickDecimals, scaledDecimals };
|
||||
}
|
||||
|
||||
/**
|
||||
* Format timestamp similar to Grafana graph panel.
|
||||
* @param ticks Number of ticks
|
||||
|
||||
Reference in New Issue
Block a user