Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: typescript and kysely #54

Merged
merged 24 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ jobs:
fetch-depth: 0
persist-credentials: false

- name: Installing
run: yarn --frozen-lockfile --perfer-offline --link-duplicates

- name: Build and test
run: |
yarn build
yarn test

- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
with:
Expand All @@ -31,3 +39,4 @@ jobs:
GIT_COMMITTER_EMAIL: ${{ secrets.SOCIALGROOVYBOT_EMAIL }}
GIT_COMMITTER_NAME: ${{ secrets.SOCIALGROOVYBOT_NAME }}
NPM_TOKEN: ${{ secrets.SOCIALGROOVYBOT_NPM_TOKEN }}

4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ jobs:
# run: yarn lint

- name: Unit tests
run: yarn test
run: |
yarn build
yarn test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.env
.env*
.env*
dist
1 change: 1 addition & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sonar.exclusions=**/src/__tests__/**,**/__snapshots__/**,**/src/migrations/**
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

Extract matomo data from [`Live.getLastVisitsDetails`](https://developer.matomo.org/api-reference/reporting-api) API and push events and visits informations to Postgres.

Use [pg_partman](https://github.com/pgpartman/pg_partman) to partition data by month.

## Usage

Create the [initial table](./initial.sql) database table then run the following job with correct environment variables.
Expand All @@ -16,16 +14,16 @@ npx @socialgouv/matomo-postgres

### Environment variables Deployment

| name | value |
| ----------------- | ---------------------------------------------- |
| MATOMO_KEY\* | matomo api token |
| MATOMO_SITE\* | matomo site id |
| MATOMO_URL\* | matomo url |
| PGDATABASE\* | Postgres connection string |
| DESTINATION_TABLE | `matomo` |
| STARTDATE | default to today() |
| RESULTPERPAGE | matomo pagination : `100` |
| OFFSET | default days to check in the past; default = 3 |
| name | value |
| ----------------- | -------------------------------------------------------- |
| MATOMO_KEY\* | matomo api token |
| MATOMO_SITE\* | matomo site id |
| MATOMO_URL\* | matomo url |
| PGDATABASE\* | Postgres connection string |
| DESTINATION_TABLE | `matomo` |
| STARTDATE | default to today() |
| RESULTPERPAGE | matomo pagination (defaults to 500) |
| INITIAL_OFFSET | How many days to fetch on initialisation (defaults to 3) |

## Dev

Expand All @@ -43,3 +41,6 @@ yarn start

Use `yarn test -u` to update the snapshots

## Database migrations

`yarn migrate` is run on each `yarn start` with Kysely migrations at [./src/migrations](./src/migrations/)
15 changes: 13 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#!/usr/bin/env node

const run = require("../src/index");
const { db } = require("../dist/db");

const { default: run } = require("../dist/index");
const { default: migrate } = require("../dist/migrate-latest");

async function start(date) {
console.log(`\nRunning migrations\n`);
await migrate();
console.log(`\nStarting import\n`);
await run(date);
db.destroy();
}

if (require.main === module) {
const date =
(process.argv[process.argv.length - 1].match(/^\d\d\d\d-\d\d-\d\d$/) && process.argv[process.argv.length - 1]) ||
"";
console.log(`\nRunning @socialgouv/matomo-postgres ${date}\n`);
run(date);
start(date);
}
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: "3.0"
services:
postgres:
build:
context: ./docker
dockerfile: ./Dockerfile
image: postgres:15
# build:
# context: ./docker
# dockerfile: ./Dockerfile
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
Expand Down
43 changes: 0 additions & 43 deletions docker/Dockerfile

This file was deleted.

13 changes: 0 additions & 13 deletions docker/initdb.sh

This file was deleted.

73 changes: 0 additions & 73 deletions initial.sql

This file was deleted.

16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/*.test.ts"],
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
};
10 changes: 0 additions & 10 deletions jsconfig.json

This file was deleted.

34 changes: 23 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,45 @@
"version": "0.0.1",
"types": "types/index.d.ts",
"license": "Apache-2.0",
"main": "src/index.js",
"main": "dist/index.js",
"preferGlobal": true,
"publishConfig": {
"access": "public"
},
"bin": {
"matomo-postgres": "./bin/index.js"
},
"files": [
"bin",
"dist"
],
"scripts": {
"start": "node ./src/index.js",
"start": "yarn migrate && node ./dist/index.js",
"build": "tsc",
"prepublish": "yarn build",
"migrate": "node ./dist/migrate-latest.js",
"test": "jest --verbose"
},
"prettier": {
"printWidth": 120
},
"dependencies": {
"date-fns": "^2.23.0",
"debug": "^4.3.3",
"dotenv": "^10.0.0",
"p-all": "^3.0.0",
"pg": "^8.7.1",
"date-fns": "^2.29.3",
"debug": "^4.3.4",
"dotenv": "^16.0.3",
"kysely": "^0.23.4",
"p-all": "^3",
"pg": "^8.9.0",
"piwik-client": "^0.2.2"
},
"devDependencies": {
"@types/jest": "^27.0.3",
"@types/pg": "^8.6.1",
"jest": "^27.1.1",
"typescript": "^4.4.4"
"@types/debug": "^4.1.7",
"@types/jest": "^29.4.0",
"@types/node": "^18.14.4",
"@types/pg": "^8.6.6",
"jest": "^29.4.3",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}
Loading