@socialgouv/matomo-postgres 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -3
- package/docker/Dockerfile +43 -0
- package/docker/initdb.sh +13 -0
- package/docker-compose.yml +9 -1
- package/initial.sql +73 -0
- package/package.json +1 -1
- package/src/__tests__/__snapshots__/index.test.js.snap +10 -5
- package/src/__tests__/index.test.js +1 -6
- package/src/createTable.js +11 -32
package/README.md
CHANGED
|
@@ -4,8 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
Extract matomo data from [`Live.getLastVisitsDetails`](https://developer.matomo.org/api-reference/reporting-api) API and push events and visits informations to Postgres.
|
|
6
6
|
|
|
7
|
+
Use [pg_partman](https://github.com/pgpartman/pg_partman) to partition data by month.
|
|
8
|
+
|
|
7
9
|
## Usage
|
|
8
10
|
|
|
11
|
+
Create the [initial table](./initial.sql) database table then run the following job with correct environment variables.
|
|
12
|
+
|
|
9
13
|
```sh
|
|
10
14
|
npx @socialgouv/matomo-postgres
|
|
11
15
|
```
|
|
@@ -30,11 +34,12 @@ docker-compose up
|
|
|
30
34
|
export MATOMO_URL=
|
|
31
35
|
export MATOMO_SITE=
|
|
32
36
|
export MATOMO_KEY=
|
|
33
|
-
export DESTINATION_TABLE=
|
|
34
|
-
export STARTDATE=
|
|
35
|
-
export OFFSET=
|
|
37
|
+
export DESTINATION_TABLE= # optional
|
|
38
|
+
export STARTDATE= # optional
|
|
39
|
+
export OFFSET= # optional
|
|
36
40
|
export PGDATABASE=postgres://postgres:postgres@127.0.0.1:5455/postgres
|
|
37
41
|
yarn start
|
|
38
42
|
```
|
|
39
43
|
|
|
40
44
|
Use `yarn test -u` to update the snapshots
|
|
45
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
FROM postgres:13-alpine
|
|
2
|
+
|
|
3
|
+
ENV PG_PARTMAN_VERSION v4.7.0
|
|
4
|
+
|
|
5
|
+
# Install pg_jobmon
|
|
6
|
+
RUN set -ex \
|
|
7
|
+
\
|
|
8
|
+
&& apk add --no-cache --virtual .fetch-deps \
|
|
9
|
+
ca-certificates \
|
|
10
|
+
openssl \
|
|
11
|
+
tar \
|
|
12
|
+
\
|
|
13
|
+
&& apk add --no-cache --virtual .build-deps \
|
|
14
|
+
autoconf \
|
|
15
|
+
automake \
|
|
16
|
+
g++ \
|
|
17
|
+
clang \
|
|
18
|
+
llvm \
|
|
19
|
+
libtool \
|
|
20
|
+
libxml2-dev \
|
|
21
|
+
make \
|
|
22
|
+
perl
|
|
23
|
+
# Install pg_partman
|
|
24
|
+
RUN set -ex \
|
|
25
|
+
&& wget -O pg_partman.tar.gz "https://github.com/pgpartman/pg_partman/archive/$PG_PARTMAN_VERSION.tar.gz" \
|
|
26
|
+
&& mkdir -p /usr/src/pg_partman \
|
|
27
|
+
&& tar \
|
|
28
|
+
--extract \
|
|
29
|
+
--file pg_partman.tar.gz \
|
|
30
|
+
--directory /usr/src/pg_partman \
|
|
31
|
+
--strip-components 1 \
|
|
32
|
+
&& rm pg_partman.tar.gz \
|
|
33
|
+
&& cd /usr/src/pg_partman \
|
|
34
|
+
&& make \
|
|
35
|
+
&& make install \
|
|
36
|
+
&& cd / \
|
|
37
|
+
&& rm -rf /usr/src/pg_partman \
|
|
38
|
+
&& apk del .fetch-deps .build-deps
|
|
39
|
+
|
|
40
|
+
# Copy the init script
|
|
41
|
+
# The Docker Postgres initd script will run anything
|
|
42
|
+
# in the directory /docker-entrypoint-initdb.d
|
|
43
|
+
COPY initdb.sh /docker-entrypoint-initdb.d/initdb.sh
|
package/docker/initdb.sh
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/bin/bash -e
|
|
2
|
+
|
|
3
|
+
echo "Creating partman extension"
|
|
4
|
+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
|
5
|
+
CREATE SCHEMA partman;
|
|
6
|
+
CREATE EXTENSION pg_partman SCHEMA partman;
|
|
7
|
+
EOSQL
|
|
8
|
+
|
|
9
|
+
echo "ADDING pg_partman_bgw TO postgresql.conf"
|
|
10
|
+
echo "shared_preload_libraries = 'pg_partman_bgw'" >> $PGDATA/postgresql.conf
|
|
11
|
+
echo "pg_partman_bgw.interval = 3600" >> $PGDATA/postgresql.conf
|
|
12
|
+
echo "pg_partman_bgw.role = '$POSTGRES_USER'" >> $PGDATA/postgresql.conf
|
|
13
|
+
echo "pg_partman_bgw.dbname = '$POSTGRES_DB'" >> $PGDATA/postgresql.conf
|
package/docker-compose.yml
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
version: "3.0"
|
|
2
2
|
services:
|
|
3
3
|
postgres:
|
|
4
|
-
|
|
4
|
+
build:
|
|
5
|
+
context: ./docker
|
|
6
|
+
dockerfile: ./Dockerfile
|
|
7
|
+
volumes:
|
|
8
|
+
- postgres_data:/var/lib/postgresql/data
|
|
5
9
|
environment:
|
|
6
10
|
POSTGRES_PASSWORD: postgres
|
|
7
11
|
POSTGRES_USERNAME: postgres
|
|
8
12
|
TZ: "Europe/Paris"
|
|
9
13
|
ports:
|
|
10
14
|
- 5455:5432
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
volumes:
|
|
18
|
+
postgres_data:
|
package/initial.sql
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
-- converting existing matomo table to partitioned witg pg_partman
|
|
2
|
+
-- usage : ON_ERROR_STOP=1 psql < partition.sql
|
|
3
|
+
|
|
4
|
+
--- pg_partman setup
|
|
5
|
+
|
|
6
|
+
CREATE SCHEMA IF NOT EXISTS partman;
|
|
7
|
+
CREATE EXTENSION IF NOT EXISTS pg_partman SCHEMA partman;
|
|
8
|
+
|
|
9
|
+
--- backup and recreate a new partionned matomo table
|
|
10
|
+
|
|
11
|
+
CREATE TABLE IF NOT EXISTS matomo_tmp as (select * from matomo);
|
|
12
|
+
|
|
13
|
+
ALTER TABLE IF EXISTS matomo RENAME TO matomo_backup;
|
|
14
|
+
|
|
15
|
+
CREATE TABLE IF NOT EXISTS matomo
|
|
16
|
+
(
|
|
17
|
+
idsite text,
|
|
18
|
+
idvisit text,
|
|
19
|
+
actions text,
|
|
20
|
+
country text,
|
|
21
|
+
region text,
|
|
22
|
+
city text,
|
|
23
|
+
operatingsystemname text,
|
|
24
|
+
devicemodel text,
|
|
25
|
+
devicebrand text,
|
|
26
|
+
visitduration text,
|
|
27
|
+
dayssincefirstvisit text,
|
|
28
|
+
visitortype text,
|
|
29
|
+
sitename text,
|
|
30
|
+
userid text,
|
|
31
|
+
serverdateprettyfirstaction date,
|
|
32
|
+
action_id text,
|
|
33
|
+
action_type text,
|
|
34
|
+
action_eventcategory text,
|
|
35
|
+
action_eventaction text,
|
|
36
|
+
action_eventname text,
|
|
37
|
+
action_eventvalue decimal,
|
|
38
|
+
action_timespent text,
|
|
39
|
+
action_timestamp timestamp with time zone DEFAULT now(),
|
|
40
|
+
usercustomproperties json,
|
|
41
|
+
usercustomdimensions json,
|
|
42
|
+
dimension1 text,
|
|
43
|
+
dimension2 text,
|
|
44
|
+
dimension3 text,
|
|
45
|
+
dimension4 text,
|
|
46
|
+
dimension5 text,
|
|
47
|
+
dimension6 text,
|
|
48
|
+
dimension7 text,
|
|
49
|
+
dimension8 text,
|
|
50
|
+
dimension9 text,
|
|
51
|
+
dimension10 text,
|
|
52
|
+
action_url text,
|
|
53
|
+
sitesearchkeyword text,
|
|
54
|
+
action_title text
|
|
55
|
+
) PARTITION BY RANGE (action_timestamp);
|
|
56
|
+
|
|
57
|
+
ALTER TABLE IF EXISTS matomo ADD CONSTRAINT unique_action_id UNIQUE (action_id, action_timestamp);
|
|
58
|
+
ALTER TABLE IF EXISTS matomo ALTER COLUMN action_eventvalue TYPE decimal USING action_eventvalue::decimal;
|
|
59
|
+
CREATE INDEX IF NOT EXISTS idx_action_timestamp_matomo ON matomo (action_timestamp);
|
|
60
|
+
CREATE INDEX IF NOT EXISTS idx_idvisit_matomo ON matomo(idvisit);
|
|
61
|
+
CREATE INDEX IF NOT EXISTS idx_action_eventcategory_matomo ON matomo(action_eventcategory);
|
|
62
|
+
CREATE INDEX IF NOT EXISTS idx_action_type_matomo ON matomo(action_type);
|
|
63
|
+
CREATE INDEX IF NOT EXISTS idx_action_eventaction_matomo ON matomo(action_eventaction);
|
|
64
|
+
|
|
65
|
+
SELECT partman.create_parent('public.matomo', 'action_timestamp', 'native', 'monthly');
|
|
66
|
+
|
|
67
|
+
-- Import des données depuis la table standard vers la table partitionnée
|
|
68
|
+
CALL partman.partition_data_proc('public.matomo', p_source_table := 'public.matomo_tmp', p_order:= 'DESC');
|
|
69
|
+
|
|
70
|
+
VACUUM ANALYZE public.matomo;
|
|
71
|
+
|
|
72
|
+
DROP TABLE if exists matomo_tmp;
|
|
73
|
+
|
package/package.json
CHANGED
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
exports[`run: should create table 1`] = `
|
|
4
4
|
Array [
|
|
5
|
-
"
|
|
6
|
-
|
|
5
|
+
"
|
|
6
|
+
|
|
7
|
+
CREATE SCHEMA IF NOT EXISTS partman;
|
|
8
|
+
CREATE EXTENSION IF NOT EXISTS pg_partman SCHEMA partman;
|
|
9
|
+
CREATE TABLE IF NOT EXISTS matomo
|
|
10
|
+
(
|
|
7
11
|
idsite text,
|
|
8
12
|
idvisit text,
|
|
9
13
|
actions text,
|
|
@@ -19,14 +23,14 @@ Array [
|
|
|
19
23
|
sitename text,
|
|
20
24
|
userid text,
|
|
21
25
|
serverdateprettyfirstaction date,
|
|
22
|
-
action_id text
|
|
26
|
+
action_id text,
|
|
23
27
|
action_type text,
|
|
24
28
|
action_eventcategory text,
|
|
25
29
|
action_eventaction text,
|
|
26
30
|
action_eventname text,
|
|
27
31
|
action_eventvalue decimal,
|
|
28
32
|
action_timespent text,
|
|
29
|
-
action_timestamp timestamp with time zone,
|
|
33
|
+
action_timestamp timestamp with time zone DEFAULT now(),
|
|
30
34
|
usercustomproperties json,
|
|
31
35
|
usercustomdimensions json,
|
|
32
36
|
dimension1 text,
|
|
@@ -42,7 +46,8 @@ Array [
|
|
|
42
46
|
action_url text,
|
|
43
47
|
sitesearchkeyword text,
|
|
44
48
|
action_title text
|
|
45
|
-
)
|
|
49
|
+
) PARTITION BY RANGE (action_timestamp);
|
|
50
|
+
",
|
|
46
51
|
Array [],
|
|
47
52
|
]
|
|
48
53
|
`;
|
|
@@ -12,7 +12,7 @@ const matomoVisit = require("./visit.json");
|
|
|
12
12
|
|
|
13
13
|
const run = require("../index");
|
|
14
14
|
|
|
15
|
-
const NB_REQUEST_TO_INIT_DB =
|
|
15
|
+
const NB_REQUEST_TO_INIT_DB = 1; // Number of query to init DB (createTable.js)
|
|
16
16
|
const TEST_DATE = new Date();
|
|
17
17
|
|
|
18
18
|
// @ts-ignore
|
|
@@ -86,11 +86,6 @@ test("run: should fetch the latest event date if no date provided", async () =>
|
|
|
86
86
|
|
|
87
87
|
// check db queries
|
|
88
88
|
expect(mock_pgQuery.mock.calls[NB_REQUEST_TO_INIT_DB][0]).toEqual(
|
|
89
|
-
// call 0 is create table
|
|
90
|
-
// call 1 is add column usercustomdimension
|
|
91
|
-
// call 2 is add column action_url
|
|
92
|
-
// ...
|
|
93
|
-
//
|
|
94
89
|
"select action_timestamp from matomo order by action_timestamp desc limit 1"
|
|
95
90
|
);
|
|
96
91
|
});
|
package/src/createTable.js
CHANGED
|
@@ -8,8 +8,12 @@ const { DESTINATION_TABLE } = require("./config");
|
|
|
8
8
|
*/
|
|
9
9
|
async function createTable(client) {
|
|
10
10
|
const table = client.escapeIdentifier(DESTINATION_TABLE);
|
|
11
|
-
const text = `
|
|
12
|
-
|
|
11
|
+
const text = `
|
|
12
|
+
|
|
13
|
+
CREATE SCHEMA IF NOT EXISTS partman;
|
|
14
|
+
CREATE EXTENSION IF NOT EXISTS pg_partman SCHEMA partman;
|
|
15
|
+
CREATE TABLE IF NOT EXISTS ${table}
|
|
16
|
+
(
|
|
13
17
|
idsite text,
|
|
14
18
|
idvisit text,
|
|
15
19
|
actions text,
|
|
@@ -25,14 +29,14 @@ async function createTable(client) {
|
|
|
25
29
|
sitename text,
|
|
26
30
|
userid text,
|
|
27
31
|
serverdateprettyfirstaction date,
|
|
28
|
-
action_id text
|
|
32
|
+
action_id text,
|
|
29
33
|
action_type text,
|
|
30
34
|
action_eventcategory text,
|
|
31
35
|
action_eventaction text,
|
|
32
36
|
action_eventname text,
|
|
33
37
|
action_eventvalue decimal,
|
|
34
38
|
action_timespent text,
|
|
35
|
-
action_timestamp timestamp with time zone,
|
|
39
|
+
action_timestamp timestamp with time zone DEFAULT now(),
|
|
36
40
|
usercustomproperties json,
|
|
37
41
|
usercustomdimensions json,
|
|
38
42
|
dimension1 text,
|
|
@@ -48,37 +52,12 @@ async function createTable(client) {
|
|
|
48
52
|
action_url text,
|
|
49
53
|
sitesearchkeyword text,
|
|
50
54
|
action_title text
|
|
51
|
-
)
|
|
55
|
+
) PARTITION BY RANGE (action_timestamp);
|
|
56
|
+
`;
|
|
52
57
|
|
|
53
58
|
await client.query(text, []);
|
|
54
59
|
|
|
55
|
-
const migrations = [
|
|
56
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "usercustomdimensions" json;`,
|
|
57
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "action_url" text;`,
|
|
58
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "sitesearchkeyword" text;`,
|
|
59
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "action_title" text;`,
|
|
60
|
-
`ALTER TABLE IF EXISTS ${table} ALTER COLUMN action_eventvalue TYPE decimal USING action_eventvalue::decimal;`,
|
|
61
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension1" text;`,
|
|
62
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension2" text;`,
|
|
63
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension3" text;`,
|
|
64
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension4" text;`,
|
|
65
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension5" text;`,
|
|
66
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension6" text;`,
|
|
67
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension7" text;`,
|
|
68
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension8" text;`,
|
|
69
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension9" text;`,
|
|
70
|
-
`ALTER TABLE IF EXISTS ${table} ADD COLUMN IF NOT EXISTS "dimension10" text;`,
|
|
71
|
-
`CREATE INDEX IF NOT EXISTS idx_action_timestamp ON ${table} (action_timestamp);`,
|
|
72
|
-
`CREATE INDEX IF NOT EXISTS idx_idvisit ON ${table}(idvisit);`,
|
|
73
|
-
`CREATE INDEX IF NOT EXISTS idx_action_eventcategory ON ${table}(action_eventcategory);`,
|
|
74
|
-
`CREATE INDEX IF NOT EXISTS idx_action_type ON ${table}(action_type);`,
|
|
75
|
-
`CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_action_eventaction ON ${table}(action_eventaction);`,
|
|
76
|
-
];
|
|
77
|
-
|
|
78
|
-
// --------------------------------------------- //
|
|
79
|
-
// If you add new query: Don't forget to update //
|
|
80
|
-
// const `NB_REQUEST_TO_INIT_DB` (index.test.js) //
|
|
81
|
-
// --------------------------------------------- //
|
|
60
|
+
const migrations = [];
|
|
82
61
|
|
|
83
62
|
for (const query of migrations) {
|
|
84
63
|
await client.query(query, []);
|