Init sender and middleware, ping

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-25 20:02:29 +01:00
parent 6e3de95656
commit 6f2103a402
6 changed files with 104 additions and 54 deletions

View File

@ -145,9 +145,7 @@ module.exports = {
}
// First, check if their is at least one admin
const admins = await strapi
.query('administrator', 'admin')
.find({ _limit: 1 });
const admins = await strapi.query('administrator', 'admin').find({ _limit: 1 });
if (admins.length > 0) {
return ctx.badRequest(
@ -159,9 +157,7 @@ module.exports = {
);
}
params.password = await strapi.admin.services.auth.hashPassword(
params.password
);
params.password = await strapi.admin.services.auth.hashPassword(params.password);
const admin = await strapi.query('administrator', 'admin').findOne({
email: params.email,
@ -184,7 +180,7 @@ module.exports = {
const jwt = strapi.admin.services.auth.createJwtToken(admin);
strapi.emit('didCreateFirstAdmin');
await strapi.telemetry.send('didCreateFirstAdmin');
ctx.send({
jwt,
@ -300,9 +296,7 @@ module.exports = {
}
// Find the admin thanks to his email.
const admin = await strapi
.query('administrator', 'admin')
.findOne({ email });
const admin = await strapi.query('administrator', 'admin').findOne({ email });
// admin not found.
if (!admin) {
@ -355,9 +349,7 @@ module.exports = {
}
// Update the admin.
await strapi
.query('administrator', 'admin')
.update({ id: admin.id }, { resetPasswordToken });
await strapi.query('administrator', 'admin').update({ id: admin.id }, { resetPasswordToken });
ctx.send({ ok: true });
},

View File

@ -111,7 +111,7 @@ module.exports = {
ctx.body = await contentManagerService.create(ctx.request.body, { model });
}
strapi.emit('didCreateFirstContentTypeEntry', { model });
await strapi.telemetry.send('didCreateFirstContentTypeEntry', { model });
} catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [

View File

@ -72,9 +72,9 @@ module.exports = {
});
if (_.isEmpty(strapi.api)) {
strapi.emit('didCreateFirstContentType');
await strapi.telemetry.send('didCreateFirstContentType');
} else {
strapi.emit('didCreateContentType');
await strapi.telemetry.send('didCreateContentType');
}
setImmediate(() => strapi.reload());
@ -82,7 +82,7 @@ module.exports = {
ctx.send({ data: { uid: component.uid } }, 201);
} catch (error) {
strapi.log.error(error);
strapi.emit('didNotCreateContentType', error);
await strapi.telemetry.send('didNotCreateContentType', { error: error.message });
ctx.send({ error: error.message }, 400);
}
},

View File

@ -30,7 +30,7 @@ module.exports = function createComponentBuilder() {
/**
* create a component in the tmpComponent map
*/
createComponent(infos) {
async createComponent(infos) {
const uid = this.createComponentUID(infos);
if (this.components.has(uid)) {
@ -62,9 +62,9 @@ module.exports = function createComponentBuilder() {
.setAttributes(this.convertAttributes(infos.attributes));
if (this.components.size === 0) {
strapi.emit('didCreateFirstComponent');
strapi.telemetry.send('didCreateFirstComponent');
} else {
strapi.emit('didCreateComponent');
strapi.telemetry.send('didCreateComponent');
}
this.components.set(uid, handler);

View File

@ -3,44 +3,102 @@
* Strapi telemetry package.
* You can learn more at https://strapi.io/documentation/3.0.0-beta.x/global-strapi/usage-information.html#commitment-to-our-users-data-collection
*/
const fetch = require('node-fetch');
const os = require('os');
const isDocker = require('is-docker');
const { machineIdSync } = require('node-machine-id');
const fetch = require('node-fetch');
const ciEnv = require('ci-info');
const scheduleJob = require('node-schedule');
const sendEvent = async (event, payload) => {
try {
const res = await fetch('https://analytics.strapi.io/track', {
method: 'POST',
body: JSON.stringify({
event,
...payload,
}),
timeout: 1000,
headers: { 'Content-Type': 'application/json' },
});
const createTelemetryInstance = strapi => {
const uuid = strapi.config.uuid;
const deviceId = machineIdSync();
return res.ok;
} catch (err) {
return false;
}
};
const isDisabled = !uuid;
const createTelemetryInstance = () => {
return {
middleware: ctx => {},
track(event) {
return sendEvent(event, {
uuid: process.env.STRAPI_UUID,
deviceId: process.env.DEVICE_ID,
properties: {
os: os.type(),
os_platform: os.platform(),
os_release: os.release(),
node_version: process.version,
version: process.env.STRAPI_VERSION,
docker: process.env.DOCKER,
},
const anonymous_metadata = {
environment: strapi.config.environment,
os: os.type(),
osPlatform: os.platform(),
osRelease: os.release(),
nodeVersion: process.version,
docker: process.env.DOCKER || isDocker(),
isCI: ciEnv.isCI,
version: strapi.config.info.strapi,
strapiVersion: strapi.config.info.strapi,
};
const sendEvent = async (event, payload) => {
// do not send anything when user has disabled analytics
if (isDisabled) return true;
try {
const res = await fetch('https://analytics.strapi.io/track', {
method: 'POST',
body: JSON.stringify({
event,
uuid,
deviceId,
properties: {
...payload,
...anonymous_metadata,
},
}),
timeout: 1000,
headers: { 'Content-Type': 'application/json' },
});
return res.ok;
} catch (err) {
return false;
}
};
const _state = {
currentDay: null,
counter: 0,
};
return {
initPing() {
if (isDisabled) {
return;
}
scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
},
middleware: async (ctx, next) => {
if (isDisabled) {
return next();
}
const { url, method } = ctx.request;
if (!url.includes('.') && ['GET', 'PUT', 'POST', 'DELETE'].includes(method)) {
const dayOfMonth = new Date().getDate();
if (dayOfMonth !== _state.currentDay) {
_state.currentDay = dayOfMonth;
_state.counter = 0;
}
// Send max. 1000 events per day.
if (_state.counter < 1000) {
await sendEvent('didReceiveRequest', { url: ctx.request.url });
// Increase counter.
_state.counter++;
}
}
await next();
},
async send(event, properties) {
if (isDisabled) {
return true;
}
await sendEvent(event, properties);
},
};
};

View File

@ -220,7 +220,7 @@ class Strapi extends EventEmitter {
}
// Emit started event.
this.emit('server:started');
await this.telemetry.send('didStartServer');
if (cb && typeof cb === 'function') {
cb();