feat(rtk-query): display median of timings

Other changes:

* improved shouldExpandNode logic of QueryPreviewActions

* tweaked mean logic
This commit is contained in:
FaberVitale 2021-06-28 11:59:51 +02:00
parent 15d8c72c9b
commit 323a5d954c
4 changed files with 59 additions and 12 deletions

View File

@ -32,22 +32,35 @@ export class QueryPreviewActions extends PureComponent<QueryPreviewActionsProps>
return output; return output;
}); });
isLastActionNode = (keyPath: (string | number)[], layer: number): boolean => {
if (layer >= 1) {
const len = this.props.actionsOfQuery.length;
const actionKey = keyPath[keyPath.length - 1];
if (typeof actionKey === 'string') {
const index = Number(actionKey.split(keySep)[0]);
return len > 0 && len - index < 2;
}
}
return false;
};
shouldExpandNode = ( shouldExpandNode = (
keyPath: (string | number)[], keyPath: (string | number)[],
value: unknown, value: unknown,
layer: number layer: number
): boolean => { ): boolean => {
if (layer === 1) { if (layer === 1) {
const len = this.props.actionsOfQuery.length; return this.isLastActionNode(keyPath, layer);
const lastKey = keyPath[keyPath.length - 1]; }
if (typeof lastKey === 'string') { if (layer === 2) {
const index = Number(lastKey.split(keySep)[0]); return (
this.isLastActionNode(keyPath, layer) &&
return len - index < 2; (keyPath[0] === 'meta' || keyPath[0] === 'error')
} );
return false;
} }
return layer <= 1; return layer <= 1;

View File

@ -117,6 +117,7 @@ export interface QueryTimings {
readonly slowest: { key: string; duration: string } | null; readonly slowest: { key: string; duration: string } | null;
readonly fastest: { key: string; duration: string } | null; readonly fastest: { key: string; duration: string } | null;
readonly average: string; readonly average: string;
readonly median: string;
} }
export interface ApiTimings { export interface ApiTimings {

View File

@ -20,7 +20,7 @@ import { missingTagId } from '../monitor-config';
import { Comparator } from './comparators'; import { Comparator } from './comparators';
import { emptyArray } from './object'; import { emptyArray } from './object';
import { formatMs } from './formatters'; import { formatMs } from './formatters';
import { mean } from './statistics'; import * as statistics from './statistics';
const rtkqueryApiStateKeys: ReadonlyArray<keyof RtkQueryApiState> = [ const rtkqueryApiStateKeys: ReadonlyArray<keyof RtkQueryApiState> = [
'queries', 'queries',
@ -283,7 +283,14 @@ function computeQueryApiTimings(
} }
const average = const average =
pendingDurations.length > 0 ? formatMs(mean(pendingDurations)) : '-'; pendingDurations.length > 0
? formatMs(statistics.mean(pendingDurations))
: '-';
const median =
pendingDurations.length > 0
? formatMs(statistics.median(pendingDurations))
: '-';
return { return {
latestFetch, latestFetch,
@ -291,6 +298,7 @@ function computeQueryApiTimings(
slowest, slowest,
fastest, fastest,
average, average,
median,
} as QueryTimings; } as QueryTimings;
} }

View File

@ -39,5 +39,30 @@ export function mean(nums: number[]): number {
return NaN; return NaN;
} }
return sum(nums) / nums.length; return +(sum(nums) / nums.length).toFixed(6);
}
/**
* Returns median value of a numeric sequence.
* @param nums
* @returns
*/
export function median(nums: number[]): number {
const len = nums.length;
if (len === 0) {
return NaN;
}
if (len === 1) {
return nums[0];
}
const sorted = nums.slice().sort();
if (len % 2 === 1) {
return sorted[(len + 1) / 2 - 1];
}
return +(0.5 * (sorted[len / 2 - 1] + sorted[len / 2])).toFixed(6);
} }