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;
});
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 = (
keyPath: (string | number)[],
value: unknown,
layer: number
): boolean => {
if (layer === 1) {
const len = this.props.actionsOfQuery.length;
const lastKey = keyPath[keyPath.length - 1];
return this.isLastActionNode(keyPath, layer);
}
if (typeof lastKey === 'string') {
const index = Number(lastKey.split(keySep)[0]);
return len - index < 2;
}
return false;
if (layer === 2) {
return (
this.isLastActionNode(keyPath, layer) &&
(keyPath[0] === 'meta' || keyPath[0] === 'error')
);
}
return layer <= 1;

View File

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

View File

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

View File

@ -39,5 +39,30 @@ export function mean(nums: number[]): number {
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);
}