Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 3x 1x | /**
* External dependencies
*/
import {
STATS_CHART_BUCKET_PERIODS,
defaultPeriodForInterval,
getDaysBetweenInclusive,
isEdgeBotRequestsRangeTooLong,
useStatsEdgeBotRequests,
type ReportParams,
type StatsChartBucketPeriod,
} from '@jetpack-premium-analytics/data';
import { resolveBucketStamp } from '@jetpack-premium-analytics/datetime';
import { __ } from '@wordpress/i18n';
import { useMemo } from 'react';
import type { MetricTab } from '@jetpack-premium-analytics/widgets-toolkit';
/** A window this short is drawn by the hour, since two daily points make no chart. */
const HOURLY_MAX_DAYS = 2;
type CompanyMetric = {
key: 'crawl' | 'blocked' | 'total';
label: string;
read: ( point: { ai: number; blocked: number } ) => number;
};
// Scoped to a company, the template counts its served requests as `ai`; `human` and `other` are 0.
function companyMetrics(): CompanyMetric[] {
return [
{
key: 'crawl',
label: __( 'Crawl requests', 'jetpack-premium-analytics-pkg' ),
read: point => point.ai,
},
{
key: 'blocked',
label: __( 'Blocked', 'jetpack-premium-analytics-pkg' ),
read: point => point.blocked,
},
{
key: 'total',
label: __( 'Total attempts', 'jetpack-premium-analytics-pkg' ),
read: point => point.ai + point.blocked,
},
];
}
/**
* One AI company's requests in the page's window: served, blocked and both, each with its points over time.
*
* @param reportParams - The page range, scoped to the company by `company`; without it nothing is requested.
* @return The metrics, the bucket size drawn and the request's state.
*/
export default function useCompanyRequests( reportParams: ReportParams ) {
const period: 'hour' | StatsChartBucketPeriod =
getDaysBetweenInclusive( reportParams.from, reportParams.to ) <= HOURLY_MAX_DAYS
? 'hour'
: defaultPeriodForInterval( reportParams.interval, STATS_CHART_BUCKET_PERIODS );
const params = useMemo( () => ( { ...reportParams, period } ), [ reportParams, period ] );
const isRangeTooLong = isEdgeBotRequestsRangeTooLong( params );
// Without the company scope the template counts the whole site, which this widget would name one company's.
const hasCompany = !! reportParams.company;
const { primary, timezone, isLoading, isFetching, isError, error, refetch } =
useStatsEdgeBotRequests( params, { enabled: hasCompany } );
const report = primary.data;
const metrics = useMemo< MetricTab[] >(
() =>
companyMetrics().map( ( { key, label, read } ) => ( {
key,
label,
value: report ? read( report.summary ) : null,
current: ( report?.data ?? [] ).flatMap( point => {
const date = resolveBucketStamp( point.date_start, timezone );
const endDate = resolveBucketStamp( point.date_end, timezone );
return date ? [ { date, endDate, value: read( point ) } ] : [];
} ),
} ) ),
[ report, timezone ]
);
// Placeholder rows from the previous window outlive a failed refetch.
const showError = isError && ! report?.data.length;
const hasRequests = metrics.some( metric => ( metric.value ?? 0 ) > 0 );
return {
metrics,
period,
isLoading,
isFetching,
isError: showError,
isEmpty: isRangeTooLong || ( ! isLoading && ! hasRequests ),
hasCompany,
isRangeTooLong,
error: showError ? error : null,
refetch,
};
}
|