@socialgouv/matomo-postgres 2.0.1 → 2.0.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/dist/PiwikClient.js +117 -0
- package/dist/__tests__/run.test.js +1 -1
- package/dist/index.js +2 -2
- package/package.json +2 -3
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const http = __importStar(require("http"));
|
|
27
|
+
const https = __importStar(require("https"));
|
|
28
|
+
const url = __importStar(require("url"));
|
|
29
|
+
const querystring = __importStar(require("querystring"));
|
|
30
|
+
class PiwikClient {
|
|
31
|
+
constructor(baseURL, token) {
|
|
32
|
+
const parsedUrl = url.parse(baseURL, true);
|
|
33
|
+
this.settings = {
|
|
34
|
+
apihost: parsedUrl.hostname || '',
|
|
35
|
+
apipath: parsedUrl.pathname || '',
|
|
36
|
+
};
|
|
37
|
+
// Determine protocol and set http module
|
|
38
|
+
switch (parsedUrl.protocol) {
|
|
39
|
+
case 'http:':
|
|
40
|
+
this.http = http;
|
|
41
|
+
this.settings.apiport = parsedUrl.port ? parseInt(parsedUrl.port, 10) : 80;
|
|
42
|
+
break;
|
|
43
|
+
case 'https:':
|
|
44
|
+
this.http = https;
|
|
45
|
+
this.settings.apiport = parsedUrl.port ? parseInt(parsedUrl.port, 10) : 443;
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
this.http = http;
|
|
49
|
+
this.settings.apiport = 80;
|
|
50
|
+
}
|
|
51
|
+
// Set token from URL query or constructor parameter
|
|
52
|
+
if (parsedUrl.query && parsedUrl.query.token_auth) {
|
|
53
|
+
this.settings.token = parsedUrl.query.token_auth;
|
|
54
|
+
}
|
|
55
|
+
if (token) {
|
|
56
|
+
this.settings.token = token;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
api(vars, cb) {
|
|
60
|
+
if (typeof vars !== 'object') {
|
|
61
|
+
vars = {};
|
|
62
|
+
}
|
|
63
|
+
// Set default values
|
|
64
|
+
vars.module = 'API';
|
|
65
|
+
vars.format = 'JSON';
|
|
66
|
+
// Set token if not provided in vars
|
|
67
|
+
if (vars.token_auth == null) {
|
|
68
|
+
vars.token_auth = this.settings.token;
|
|
69
|
+
}
|
|
70
|
+
// Extract token_auth for POST body
|
|
71
|
+
const token_auth = vars.token_auth;
|
|
72
|
+
const postData = querystring.stringify({ token_auth });
|
|
73
|
+
// Remove token_auth from URL query params
|
|
74
|
+
const queryVars = Object.assign({}, vars);
|
|
75
|
+
delete queryVars.token_auth;
|
|
76
|
+
// Prepare request options
|
|
77
|
+
const options = {
|
|
78
|
+
host: this.settings.apihost,
|
|
79
|
+
port: this.settings.apiport,
|
|
80
|
+
path: this.settings.apipath + '?' + querystring.stringify(queryVars),
|
|
81
|
+
method: 'POST',
|
|
82
|
+
headers: {
|
|
83
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
// Make HTTP POST request
|
|
87
|
+
const req = this.http.request(options, (response) => {
|
|
88
|
+
let data = '';
|
|
89
|
+
// Collect data chunks
|
|
90
|
+
response.on('data', (chunk) => {
|
|
91
|
+
data += chunk;
|
|
92
|
+
});
|
|
93
|
+
// Process complete response
|
|
94
|
+
response.on('end', () => {
|
|
95
|
+
try {
|
|
96
|
+
const resObj = JSON.parse(data);
|
|
97
|
+
if (resObj.result === 'error') {
|
|
98
|
+
return cb(new Error(resObj.message), null);
|
|
99
|
+
}
|
|
100
|
+
return cb(null, resObj);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
return cb(error instanceof Error ? error : new Error(String(error)), null);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
// Handle request errors
|
|
108
|
+
req.on('error', (error) => {
|
|
109
|
+
cb(error, null);
|
|
110
|
+
});
|
|
111
|
+
// Write POST data and end request
|
|
112
|
+
req.write(postData);
|
|
113
|
+
req.end();
|
|
114
|
+
return req;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.default = PiwikClient;
|
|
@@ -49,7 +49,7 @@ afterEach(() => {
|
|
|
49
49
|
jest.clearAllMocks();
|
|
50
50
|
});
|
|
51
51
|
let piwikApiCalls = [];
|
|
52
|
-
jest.mock("
|
|
52
|
+
jest.mock("../PiwikClient", () => {
|
|
53
53
|
const matomoVisits = [
|
|
54
54
|
Object.assign(Object.assign({}, visit_json_1.default), { idVisit: 123 }),
|
|
55
55
|
Object.assign(Object.assign({}, visit_json_1.default), { idVisit: 124 }),
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ const kysely_1 = require("kysely");
|
|
|
16
16
|
const p_all_1 = __importDefault(require("p-all"));
|
|
17
17
|
const debug_1 = __importDefault(require("debug"));
|
|
18
18
|
const eachDayOfInterval_1 = __importDefault(require("date-fns/eachDayOfInterval"));
|
|
19
|
-
const
|
|
19
|
+
const PiwikClient_1 = __importDefault(require("./PiwikClient"));
|
|
20
20
|
const db_1 = require("./db");
|
|
21
21
|
const config_1 = require("./config");
|
|
22
22
|
const importDate_1 = require("./importDate");
|
|
@@ -24,7 +24,7 @@ const debug = (0, debug_1.default)("index");
|
|
|
24
24
|
function run(date) {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
26
|
debug("run, date=" + date);
|
|
27
|
-
const piwik = new
|
|
27
|
+
const piwik = new PiwikClient_1.default(config_1.MATOMO_URL, config_1.MATOMO_KEY);
|
|
28
28
|
// priority:
|
|
29
29
|
// - optional parameter date
|
|
30
30
|
// - last event in the table
|
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.0.
|
|
4
|
+
"version": "2.0.2",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"main": "dist/index.js",
|
|
@@ -32,8 +32,7 @@
|
|
|
32
32
|
"dotenv": "^16.0.3",
|
|
33
33
|
"kysely": "^0.23.4",
|
|
34
34
|
"p-all": "^3",
|
|
35
|
-
"pg": "^8.9.0"
|
|
36
|
-
"piwik-client": "^0.2.2"
|
|
35
|
+
"pg": "^8.9.0"
|
|
37
36
|
},
|
|
38
37
|
"devDependencies": {
|
|
39
38
|
"@types/debug": "^4.1.7",
|