diff --git a/packages/code-du-travail-frontend/pages/stats.tsx b/packages/code-du-travail-frontend/pages/stats.tsx index f9f2c1cfda..85067765ef 100644 --- a/packages/code-du-travail-frontend/pages/stats.tsx +++ b/packages/code-du-travail-frontend/pages/stats.tsx @@ -7,12 +7,11 @@ import { Section, Wrapper, } from "@socialgouv/cdtn-ui"; -import { max, startOfDay, subMonths } from "date-fns"; import React from "react"; import styled from "styled-components"; import Metas from "../src/common/Metas"; -import { REVALIDATE_TIME, SITE_URL } from "../src/config"; +import { REVALIDATE_TIME, REVALIDATE_TIME_DAY, SITE_URL } from "../src/config"; import { Layout } from "../src/layout/Layout"; import { captureException } from "@sentry/nextjs"; import { getStatsService } from "../src/api"; @@ -29,9 +28,6 @@ type Props = { }; const Stats = ({ data }: Props): JSX.Element => { - const launchDate = new Date(Date.UTC(2020, 0, 1)); - const startDate = max([subMonths(startOfDay(new Date()), 6), launchDate]); - return ( { const body: any = getDocsCountQuery(); + const refYear = 2020; + const now = new Date(); + const currentYear = now.getFullYear(); + const numberLoop = currentYear - refYear + 1; const response = await elasticsearchClient.search({ body, @@ -24,33 +37,73 @@ export const getStatsService = async () => { nbDocuments += doc_count; } - const URLS = [ - `${PIWIK_URL}/?module=API&method=VisitsSummary.getVisits&idSite=${PIWIK_SITE_ID}&format=JSON&period=range&date=2020-01-01,today`, - `${PIWIK_URL}/?module=API&method=Actions.get&idSite=${PIWIK_SITE_ID}&format=JSON&period=range&date=2020-01-01,today`, - ]; + const generateUrlVisit = (index: number) => { + return `${PIWIK_URL}/?module=API&method=VisitsSummary.getVisits&idSite=${PIWIK_SITE_ID}&format=JSON&period=year&date=${ + refYear + index + }-01-01`; + }; + + const generateUrlAction = (index: number) => { + return `${PIWIK_URL}/?module=API&method=Actions.get&idSite=${PIWIK_SITE_ID}&format=JSON&period=year&date=${ + refYear + index + }-01-01`; + }; - const promises = URLS.map((url) => - fetch(url) + const visitsPromises: Promise[] = Array.from( + Array(numberLoop).keys() + ).map((index) => { + const url = generateUrlVisit(index); + return fetch(url) .then(async (data: Response) => data.json()) - .catch((e: Error) => { - console.error(e); - return null; - }) + .catch(() => { + throw new NotFoundError({ + cause: null, + message: "No visit data and info data", + name: "STATS_NOT_FOUND", + }); + }); + }); + + const nbVisitDatas = await Promise.all(visitsPromises); + const nbVisitData = nbVisitDatas?.reduce( + (obj, item) => { + return { + nbVisits: obj.nbVisits + (item?.value ?? 0), + }; + }, + { nbVisits: 0 } + ); + + const actionsPromises: Promise[] = Array.from( + Array(numberLoop).keys() + ).map((index) => { + const url = generateUrlAction(index); + return fetch(url) + .then(async (data: Response) => data.json()) + .catch(() => { + throw new NotFoundError({ + cause: null, + message: "No visit data and info data", + name: "STATS_NOT_FOUND", + }); + }); + }); + + const infoDatas = await Promise.all(actionsPromises); + const infoData = infoDatas?.reduce( + (obj, item) => { + return { + nbPageViews: obj.nbPageViews + (item?.nb_pageviews ?? 0), + nbSearches: obj.nbSearches + (item?.nb_searches ?? 0), + }; + }, + { nbPageViews: 0, nbSearches: 0 } ); - const [nbVisitData, infoData] = await Promise.all(promises); - - if (!nbVisitData && !infoData) { - throw new NotFoundError({ - cause: null, - message: "No visit data and info data", - name: "STATS_NOT_FOUND", - }); - } return { nbDocuments, - nbPageViews: infoData.nb_pageviews, - nbSearches: infoData.nb_searches, - nbVisits: nbVisitData.value, + nbPageViews: infoData.nbPageViews, + nbSearches: infoData.nbSearches, + nbVisits: nbVisitData.nbVisits, }; }; diff --git a/packages/code-du-travail-frontend/src/config.ts b/packages/code-du-travail-frontend/src/config.ts index e7c06b5e7c..40a63ea229 100644 --- a/packages/code-du-travail-frontend/src/config.ts +++ b/packages/code-du-travail-frontend/src/config.ts @@ -20,3 +20,4 @@ export const IS_PROD = export const ENTERPRISE_API_URL = "https://api.recherche-entreprises.fabrique.social.gouv.fr/api/v1"; export const REVALIDATE_TIME = 1800; // 30 minutes +export const REVALIDATE_TIME_DAY = 86400; // 1 day