Download wp-calypso/client/dashboard/app/queries/site-stats.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/dashboard/app/queries/site-stats.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/dashboard/app/queries/site-stats.ts
-
curl -L -o site-stats.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/dashboard/app/queries/site-stats.ts
1.64 kB
| import { EngagementStatsDataPoint, fetchSiteEngagementStats } from '../../data/site-stats'; | |
| export const siteEngagementStatsQuery = ( siteId: number ) => ( { | |
| queryKey: [ 'site', siteId, 'engagement-stats' ], | |
| queryFn: () => fetchSiteEngagementStats( siteId ), | |
| select: ( { data: stats }: { data: Array< [ string, number, number, number, number ] > } ) => { | |
| // We need to normalize the returned data. We ask for 14 days of data (quantity:14) | |
| // and we get a response with an array of data like: `[ '2025-04-13', 1, 3, 0, 0 ]`. | |
| // Each number in the response is referring to the order of the field provided in `stat_fields`. | |
| // The returned array is sorted with ascending date order, so we need to use the last 7 entries | |
| // for our current data and the first 7 entries for the previous data. | |
| // Noting that we can't use `unit:'week'` because the API has a specific behavior for start/end of weeks. | |
| const calculateStats = ( data: Array< [ string, number, number, number, number ] > ) => | |
| data.reduce( | |
| ( accumulator: EngagementStatsDataPoint, [ , visitors, views, likes, comments ] ) => { | |
| accumulator.visitors += Number( visitors ); | |
| accumulator.views += Number( views ); | |
| accumulator.likes += Number( likes ); | |
| accumulator.comments += Number( comments ); | |
| return accumulator; | |
| }, | |
| { visitors: 0, views: 0, likes: 0, comments: 0 } | |
| ); | |
| const dataLength = stats.length; | |
| const currentData = calculateStats( stats.slice( Math.max( 0, dataLength - 7 ) ) ); | |
| const previousData = calculateStats( stats.slice( 0, Math.max( 0, dataLength - 7 ) ) ); | |
| return { previousData, currentData }; | |
| }, | |
| } ); | |