Skip to content

Commit

Permalink
feat: 5954 fix page stats (#5971)
Browse files Browse the repository at this point in the history
* feat: divide into batch

* chore: test api prod

* feat: api with range year

* chore: clean piwik raw values

* feat: page stats cache time + piwik env

* fix: TU

* fix: TU

* chore: clean

* chore: review

* chore: review

---------

Co-authored-by: victor <[email protected]>
  • Loading branch information
Viczei and victor authored Jun 20, 2024
1 parent 0b33db4 commit c5c2798
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
8 changes: 2 additions & 6 deletions packages/code-du-travail-frontend/pages/stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 (
<Layout>
<Metas
Expand Down Expand Up @@ -94,7 +90,7 @@ export async function getStaticProps() {
} else {
data = await getStatsService();
}
return { props: { data }, revalidate: REVALIDATE_TIME };
return { props: { data }, revalidate: REVALIDATE_TIME_DAY };
} catch (e) {
console.error(e);
captureException(e);
Expand Down
97 changes: 75 additions & 22 deletions packages/code-du-travail-frontend/src/api/modules/stats/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ import {
} from "../../utils";
import { getDocsCountQuery } from "./queries";

type ActionStat = {
nb_pageviews: number;
nb_searches: number;
};

type VisitStat = {
value: number;
};

export const getStatsService = async () => {
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<any>({
body,
Expand All @@ -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<VisitStat>[] = 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<ActionStat | undefined>[] = 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,
};
};
1 change: 1 addition & 0 deletions packages/code-du-travail-frontend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c5c2798

Please sign in to comment.