Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat: retrieve-specific-actions-from-matomo (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberthou authored Dec 11, 2020
1 parent 0f4e92c commit 805826f
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 47 deletions.
9 changes: 7 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { flatten } from "lodash/fp";
import packageJson from "../package.json";
import { corsOrigins, port } from "./config";
import { getGitHubData } from "./github/github-service";
import { getMatomoData } from "./matomo/matomo-service";
import { matomoConfig } from "./matomo-config";
import { getMultiSiteMatomoData } from "./matomo/matomo-service";
import { getYoutubeData } from "./youtube/youtube-service";

const app = express();
Expand All @@ -25,7 +26,11 @@ app.get("/healthz", (req, res) => {
});

app.get("/statistics", (req, res) => {
void Promise.all([getMatomoData(), getYoutubeData(), getGitHubData()])
void Promise.all([
getMultiSiteMatomoData(matomoConfig),
getYoutubeData(),
getGitHubData(),
])
.then(flatten)
.then((data) => res.json(data));
});
Expand Down
6 changes: 5 additions & 1 deletion src/github/github-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export const getGitHubData = async (): Promise<ArchifiltreCountStatistic[]> =>
})
.then(({ data }: { data: GithubDataItem[] }) => data)
.then(filterWikiItem)
.then(convertGitHubDataToApiData);
.then(convertGitHubDataToApiData)
.catch((err) => {
console.log(err);
return [];
});
28 changes: 28 additions & 0 deletions src/matomo-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { MatomoSiteConfig } from "./matomo/matomo-types";

export const matomoConfig: MatomoSiteConfig[] = [
{
events: [
"FileTreeDrop",
"CSV Export",
"CSV with hashes Export",
"Tree CSV Export",
"METS Export",
"Excel Export",
"RESIP Export",
"Audit report export",
],
idSite: 9,
},
{
actions: [
{
categoryId: 1,
},
{
categoryId: 3,
},
],
idSite: 20,
},
];
33 changes: 16 additions & 17 deletions src/matomo/matomo-service.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import axios from "axios";
import { flatten } from "lodash/fp";

import type { ArchifiltreCountStatistic } from "../api-types";
import { matomoToken, matomoUrl } from "../config";
import type { MatomoEventCategory } from "./matomo-types";
import type { MatomoEventCategory, MatomoSiteConfig } from "./matomo-types";
import {
getBulkRequestParamsFromLabels,
getBulkRequestParamsFromConfig,
sanitizeMatomoData,
} from "./matomo-utils";

const labels = [
"FileTreeDrop",
"CSV Export",
"CSV with hashes Export",
"Tree CSV Export",
"METS Export",
"Excel Export",
"RESIP Export",
"Audit report export",
];

const getBulkMatomoData = async (): Promise<MatomoEventCategory[][]> =>
const getBulkMatomoData = async (
config: MatomoSiteConfig
): Promise<MatomoEventCategory[][]> =>
axios
.get(matomoUrl, {
params: {
Expand All @@ -28,10 +20,17 @@ const getBulkMatomoData = async (): Promise<MatomoEventCategory[][]> =>
module: "API",
// eslint-disable-next-line @typescript-eslint/naming-convention
token_auth: matomoToken,
...getBulkRequestParamsFromLabels(labels),
...getBulkRequestParamsFromConfig(config),
},
})
.then(({ data }: { data: MatomoEventCategory[][] }) => data);

export const getMatomoData = async (): Promise<ArchifiltreCountStatistic[]> =>
getBulkMatomoData().then(sanitizeMatomoData);
export const getMatomoData = async (
config: MatomoSiteConfig
): Promise<ArchifiltreCountStatistic[]> =>
getBulkMatomoData(config).then(sanitizeMatomoData);

export const getMultiSiteMatomoData = async (
configs: MatomoSiteConfig[]
): Promise<ArchifiltreCountStatistic[]> =>
Promise.all(configs.map(getMatomoData)).then(flatten);
19 changes: 19 additions & 0 deletions src/matomo/matomo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,22 @@ export type MatomoEventCategory = {
// eslint-disable-next-line @typescript-eslint/naming-convention
nb_events: number;
};

export type MatomoEventConfigObject = {
label: string;
value?: string;
};

export type MatomoActionConfigObject = {
categoryId: number;
};

export type MatomoRequestConfig = MatomoEventConfig | MatomoActionConfigObject;

export type MatomoEventConfig = string | MatomoEventConfigObject;

export type MatomoSiteConfig = {
idSite: number;
events?: MatomoEventConfig[];
actions?: MatomoActionConfigObject[];
};
51 changes: 42 additions & 9 deletions src/matomo/matomo-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { MatomoEventCategory } from "./matomo-types";
import { mapValues } from "lodash/fp";
import querystring from "querystring";

import type { MatomoEventCategory, MatomoSiteConfig } from "./matomo-types";
import {
getBulkRequestParamsFromLabels,
getBulkRequestParamsFromConfig,
sanitizeMatomoData,
} from "./matomo-utils";

Expand All @@ -10,14 +13,44 @@ type TestData = {
} & MatomoEventCategory;

describe("matomoUtils", () => {
describe("getBulkRequestParamsFromLabels", () => {
describe("getBulkRequestParamsFromConfig", () => {
it("should format the queryParams for matomo", () => {
const labels = ["label1", "label2"];
expect(getBulkRequestParamsFromLabels(labels)).toEqual({
"urls[0]":
"method=Events.getCategory&idSite=9&date=2019-04-17,today&period=range&label=label1",
"urls[1]":
"method=Events.getCategory&idSite=9&date=2019-04-17,today&period=range&label=label2",
const events = ["label1", "label2"];
const idSite = 9;
const actions = [
{
categoryId: 1,
},
];
const siteConfig: MatomoSiteConfig = {
actions,
events,
idSite,
};
expect(
mapValues(querystring.parse)(getBulkRequestParamsFromConfig(siteConfig))
).toEqual({
"urls[0]": {
date: "2019-04-17,today",
idSite: "9",
label: "label1",
method: "Events.getCategory",
period: "range",
},
"urls[1]": {
date: "2019-04-17,today",
idSite: "9",
label: "label2",
method: "Events.getCategory",
period: "range",
},
"urls[2]": {
date: "2019-04-17,today",
idSite: "9",
idSubtable: "1",
method: "Events.getActionFromCategoryId",
period: "range",
},
});
});
});
Expand Down
91 changes: 73 additions & 18 deletions src/matomo/matomo-utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,85 @@
import { compose, flatten, map, mapKeys, pick } from "lodash/fp";
import * as querystring from "querystring";

import type { ArchifiltreCountStatistic } from "../api-types";
import type { MatomoEventCategory } from "./matomo-types";
import type {
MatomoActionConfigObject,
MatomoEventCategory,
MatomoEventConfig,
MatomoEventConfigObject,
MatomoSiteConfig,
} from "./matomo-types";

type CreateMatomoMethodParams = {
method: string;
label: string;
type CreateMatomoEventCategoryMethodParams = {
config: MatomoEventConfig;
idSite: number;
};

const createMatomoMethod = ({ method, label }: CreateMatomoMethodParams) =>
`method=${method}&idSite=9&date=2019-04-17,today&period=range&label=${label}`;
const sanitizeMatomoEventConfig = (
config: MatomoEventConfig
): MatomoEventConfigObject =>
typeof config === "string"
? {
label: config,
}
: config;

const createMatomoRequestBaseParams = (
idSite: number
): Record<string, string | number> => ({
date: "2019-04-17,today",
idSite,
period: "range",
});

const createMatomoEventCategoryMethod = ({
config,
idSite,
}: CreateMatomoEventCategoryMethodParams) => {
const { label } = sanitizeMatomoEventConfig(config);
return querystring.stringify({
...createMatomoRequestBaseParams(idSite),
label,
method: "Events.getCategory",
});
};

type CreateMatomoEventActionMethodParams = {
config: MatomoActionConfigObject;
idSite: number;
};

const createMatomoEventActionMethod = ({
config,
idSite,
}: CreateMatomoEventActionMethodParams): string =>
querystring.stringify({
...createMatomoRequestBaseParams(idSite),
idSubtable: config.categoryId,
method: "Events.getActionFromCategoryId",
});

type RequestParams = Record<string, string>;

export const getBulkRequestParamsFromLabels = (
labels: string[]
): RequestParams =>
labels
.map((label) => createMatomoMethod({ label, method: "Events.getCategory" }))
.reduce(
(urlParams, urlParam, index) => ({
...urlParams,
[`urls[${index}]`]: urlParam,
}),
{}
);
export const getBulkRequestParamsFromConfig = ({
events = [],
actions = [],
idSite,
}: MatomoSiteConfig): RequestParams =>
[
...events.map((config) =>
createMatomoEventCategoryMethod({ config, idSite })
),
...actions.map((config) =>
createMatomoEventActionMethod({ config, idSite })
),
].reduce(
(urlParams, urlParam, index) => ({
...urlParams,
[`urls[${index}]`]: urlParam,
}),
{}
);

const keysMap: Record<string, string> = {
label: "label",
Expand Down

0 comments on commit 805826f

Please sign in to comment.