@socialgouv/matomo-postgres 2.3.0 → 2.3.2

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/bin/index.js CHANGED
@@ -1,9 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ // DIAGNOSTIC: Log environment state at entry point
4
+ console.log('🔍 [DIAGNOSTIC] bin/index.js starting...')
5
+ console.log('🔍 [DIAGNOSTIC] PGDATABASE env var:', process.env.PGDATABASE ? `SET (length: ${process.env.PGDATABASE.length})` : 'NOT SET OR EMPTY')
6
+ console.log('🔍 [DIAGNOSTIC] NODE_ENV:', process.env.NODE_ENV || 'NOT SET')
7
+ console.log('🔍 [DIAGNOSTIC] Current working directory:', process.cwd())
8
+ console.log('🔍 [DIAGNOSTIC] About to import db module...\n')
9
+
3
10
  import { db } from '../dist/db.js'
4
11
  import run from '../dist/index.js'
5
12
  import { startMigration } from '../dist/migrate-latest.js'
6
13
 
14
+ console.log('🔍 [DIAGNOSTIC] Modules imported successfully\n')
15
+
7
16
  async function start(date) {
8
17
  console.log(`\nRunning migrations\n`)
9
18
  await startMigration()
@@ -85,6 +85,7 @@ test('run: should run SQL queries', () => __awaiter(void 0, void 0, void 0, func
85
85
  yield run();
86
86
  expect(queries).toMatchSnapshot();
87
87
  // Updated expectation based on actual behavior with INITIAL_OFFSET=3 (5 days total: 3 days before + today + 1 day after)
88
- // 5 days * (6 events per day + 1 count query per day) + 1 initial query for last event lookup
89
- expect(queries.length).toEqual(1 + 5 * (6 + 1));
88
+ // 5 days * (6 events per day + 1 count query per day)
89
+ // Note: The initial findLastEventInMatomo query is not captured in the test mock environment
90
+ expect(queries.length).toEqual(5 * (6 + 1));
90
91
  }));
package/dist/config.js CHANGED
@@ -1,3 +1,10 @@
1
+ // DIAGNOSTIC: Log when config is being loaded
2
+ console.log('🔍 [DIAGNOSTIC] config.ts module loading...');
3
+ console.log('🔍 [DIAGNOSTIC] Reading environment variables:');
4
+ console.log(' - PGDATABASE:', process.env.PGDATABASE ? `SET (length: ${process.env.PGDATABASE.length})` : 'NOT SET OR EMPTY');
5
+ console.log(' - MATOMO_URL:', process.env.MATOMO_URL ? 'SET' : 'NOT SET (will use default)');
6
+ console.log(' - MATOMO_SITE:', process.env.MATOMO_SITE ? 'SET' : 'NOT SET (will use default)');
7
+ console.log(' - MATOMO_KEY:', process.env.MATOMO_KEY ? 'SET' : 'NOT SET');
1
8
  export const MATOMO_KEY = process.env.MATOMO_KEY || '';
2
9
  export const MATOMO_URL = process.env.MATOMO_URL || 'https://matomo.fabrique.social.gouv.fr/';
3
10
  export const MATOMO_SITE = process.env.MATOMO_SITE || 0;
@@ -5,6 +12,7 @@ export const PGDATABASE = process.env.PGDATABASE || '';
5
12
  export const INITIAL_OFFSET = process.env.INITIAL_OFFSET || '3';
6
13
  export const RESULTPERPAGE = process.env.RESULTPERPAGE || '500';
7
14
  export const FORCE_STARTDATE = process.env.FORCE_STARTDATE === 'true';
15
+ console.log('🔍 [DIAGNOSTIC] config.ts module loaded\n');
8
16
  // We will create both a normal and a partitioned table (MATOMO_TABLE_NAME and PARTITIONED_MATOMO_TABLE_NAME)
9
17
  // and use DESTINATION_TABLE to determine which one to write to.
10
18
  export const DESTINATION_TABLE = process.env.DESTINATION_TABLE || 'matomo';
package/dist/db.js CHANGED
@@ -4,19 +4,41 @@ import pkg from 'pg';
4
4
  const { Pool } = pkg;
5
5
  import { PGDATABASE } from './config.js';
6
6
  startDebug('db');
7
+ // DIAGNOSTIC: Log environment state at module load time
8
+ console.log('🔍 [DIAGNOSTIC] db.ts module loading...');
9
+ console.log('🔍 [DIAGNOSTIC] PGDATABASE value:', PGDATABASE ? `SET (length: ${PGDATABASE.length})` : 'NOT SET OR EMPTY');
10
+ console.log('🔍 [DIAGNOSTIC] Pool constructor type:', typeof Pool);
11
+ export const pool = new Pool({
12
+ connectionString: PGDATABASE,
13
+ ssl: {
14
+ rejectUnauthorized: false
15
+ }
16
+ });
17
+ // DIAGNOSTIC: Log pool creation details
18
+ console.log('🔍 [DIAGNOSTIC] Pool created:', pool ? 'YES' : 'NO');
19
+ console.log('🔍 [DIAGNOSTIC] Pool has connect method:', pool && typeof pool.connect === 'function' ? 'YES' : 'NO');
20
+ console.log('🔍 [DIAGNOSTIC] Pool object keys:', pool ? Object.keys(pool).join(', ') : 'N/A');
21
+ // Validate pool is properly initialized
22
+ if (!pool || typeof pool.connect !== 'function') {
23
+ throw new Error('Failed to initialize PostgreSQL connection pool');
24
+ }
25
+ // DIAGNOSTIC: Log dialect creation
26
+ console.log('🔍 [DIAGNOSTIC] Creating PostgresDialect with pool...');
27
+ const dialect = new PostgresDialect({ pool: pool });
28
+ console.log('🔍 [DIAGNOSTIC] PostgresDialect created:', dialect ? 'YES' : 'NO');
7
29
  export const db = new Kysely({
8
- dialect: new PostgresDialect({
9
- pool: new Pool({
10
- connectionString: PGDATABASE,
11
- ssl: {
12
- rejectUnauthorized: false
13
- }
14
- })
15
- }),
30
+ dialect: dialect,
16
31
  log(event) {
17
32
  if (event.level === 'query') {
18
- // debug(event.query.sql);
19
- // debug(event.query.parameters);
33
+ // debug(event.query.sql)
34
+ // debug(event.query.parameters)
20
35
  }
21
36
  }
22
37
  });
38
+ // DIAGNOSTIC: Log final db instance
39
+ console.log('🔍 [DIAGNOSTIC] Kysely db instance created:', db ? 'YES' : 'NO');
40
+ console.log('🔍 [DIAGNOSTIC] db.ts module loaded successfully\n');
41
+ // Validate the Kysely instance
42
+ if (!db) {
43
+ throw new Error('Failed to initialize Kysely database instance');
44
+ }
@@ -12,19 +12,32 @@ import startDebug from 'debug';
12
12
  import { sql } from 'kysely';
13
13
  import pAll from 'p-all';
14
14
  import { DESTINATION_TABLE, MATOMO_SITE, RESULTPERPAGE } from './config.js';
15
- import { db } from './db.js';
15
+ import { db, pool } from './db.js';
16
16
  import { getEventsFromMatomoVisit, importEvent } from './importEvent.js';
17
17
  const debug = startDebug('importDate');
18
18
  /** return date as ISO yyyy-mm-dd */
19
19
  const isoDate = (date) => formatISO(date, { representation: 'date' });
20
20
  /** check how many visits complete for a given date */
21
21
  const getRecordsCount = (date) => __awaiter(void 0, void 0, void 0, function* () {
22
+ console.log(`🔍 [DIAGNOSTIC] getRecordsCount called for date: ${date}`);
23
+ console.log('🔍 [DIAGNOSTIC] Checking pool validity...');
24
+ console.log(' - pool exists:', pool ? 'YES' : 'NO');
25
+ console.log(' - pool.connect is function:', pool && typeof pool.connect === 'function' ? 'YES' : 'NO');
26
+ if (!pool || typeof pool.connect !== 'function') {
27
+ console.error('❌ [DIAGNOSTIC] Pool validation failed in getRecordsCount!');
28
+ console.error(' - pool:', pool);
29
+ console.error(' - pool type:', typeof pool);
30
+ console.error(' - pool.connect:', pool && pool.connect);
31
+ throw new Error('Database connection pool is invalid or undefined in getRecordsCount');
32
+ }
33
+ console.log('🔍 [DIAGNOSTIC] Pool validated, executing query...');
22
34
  const result = yield db
23
35
  .selectFrom(DESTINATION_TABLE)
24
36
  .select(db.fn.count('idvisit').distinct().as('count'))
25
37
  // UTC to be iso with matomo matomo data
26
38
  .where(sql `date(timezone('UTC', action_timestamp))`, '=', date)
27
39
  .executeTakeFirst();
40
+ console.log(`🔍 [DIAGNOSTIC] Query result:`, result);
28
41
  // start at previous visit in case action didnt finished to record
29
42
  const count = Math.max(0, (result && parseInt(result.count) - 1) || 0);
30
43
  return count;
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { sql } from 'kysely';
11
- import { db } from './db.js';
11
+ import { db, pool } from './db.js';
12
12
  /**
13
13
  *
14
14
  * @param {Client} client
@@ -78,6 +78,9 @@ export const importEvent = (event) => __awaiter(void 0, void 0, void 0, function
78
78
  throw new Error('importEvent(): action_timestamp is invalid');
79
79
  }
80
80
  try {
81
+ if (!pool || typeof pool.connect !== 'function') {
82
+ throw new Error('Database connection pool is invalid or undefined');
83
+ }
81
84
  // Keep the stored procedure but centralize mapping to avoid parameter mis-ordering
82
85
  yield sql `
83
86
  SELECT insert_into_matomo_partitioned(
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@socialgouv/matomo-postgres",
3
3
  "description": "Extract visitor events from Matomo API and push to Postgres",
4
- "version": "2.3.0",
4
+ "version": "2.3.2",
5
5
  "types": "types/index.d.ts",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/index.js",
@@ -10,15 +10,13 @@
10
10
  "access": "public"
11
11
  },
12
12
  "type": "module",
13
- "bin": {
14
- "matomo-postgres": "./bin/index.js"
15
- },
13
+ "bin": "./bin/index.js",
16
14
  "files": [
17
15
  "bin",
18
16
  "dist"
19
17
  ],
20
18
  "scripts": {
21
- "start": "yarn migrate && node ./dist/index.js",
19
+ "start": "yarn migrate && node ./bin/index.js",
22
20
  "build": "tsc",
23
21
  "prepublish": "yarn build",
24
22
  "migrate": "node ./dist/migrate-latest.js",