wikijs-fork/server/core/telemetry.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

const _ = require('lodash')
2017-10-23 01:58:07 +00:00
const axios = require('axios')
2019-02-02 06:17:09 +00:00
const bugsnag = require('@bugsnag/node')
2017-10-23 01:58:07 +00:00
const uuid = require('uuid/v4')
2019-02-02 06:17:09 +00:00
const qs = require('querystring')
const os = require('os')
2017-10-23 01:58:07 +00:00
/* global WIKI */
2017-10-23 01:58:07 +00:00
module.exports = {
2019-02-02 06:17:09 +00:00
client: null,
2017-10-23 01:58:07 +00:00
enabled: false,
init() {
2019-02-02 06:17:09 +00:00
this.client = bugsnag({
apiKey: WIKI.data.telemetry.BUGSNAG_ID,
appType: 'server',
appVersion: WIKI.version,
2017-10-23 01:58:07 +00:00
autoNotify: false,
2019-02-02 06:17:09 +00:00
collectUserIp: false,
hostname: _.get(WIKI.config, 'telemetry.clientId', uuid()),
2017-10-23 01:58:07 +00:00
notifyReleaseStages: ['production'],
2019-02-02 06:17:09 +00:00
releaseStage: WIKI.IS_DEBUG ? 'development' : 'production',
projectRoot: WIKI.ROOTPATH,
2019-02-02 06:17:09 +00:00
logger: null,
beforeSend: (report) => {
if (!WIKI.telemetry.enabled) { return false }
}
2017-10-23 01:58:07 +00:00
})
2019-02-02 06:17:09 +00:00
WIKI.telemetry = this
2017-10-23 01:58:07 +00:00
2019-06-22 00:54:09 +00:00
if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true && WIKI.config.offline !== true) {
2017-10-23 01:58:07 +00:00
this.enabled = true
2019-02-02 06:17:09 +00:00
this.sendOSInfo()
2017-10-23 01:58:07 +00:00
}
2019-02-02 06:17:09 +00:00
},
sendOSInfo() {
this.sendBatchEvents([
{
eventCategory: 'node-version',
eventAction: process.version
},
{
eventCategory: 'os-platform',
eventAction: os.platform()
},
{
eventCategory: 'cpu-cores',
eventAction: os.cpus().length
},
{
eventCategory: 'db-type',
eventAction: WIKI.config.db.type
}
])
2017-10-23 01:58:07 +00:00
},
sendError(err) {
2019-02-02 06:17:09 +00:00
this.client.notify(err)
2017-10-23 01:58:07 +00:00
},
sendEvent(eventCategory, eventAction, eventLabel) {
2019-02-02 06:17:09 +00:00
this.sendBatchEvents([{
eventCategory,
eventAction,
eventLabel
}])
},
sendBatchEvents(events) {
if (!this.enabled || WIKI.IS_DEBUG) { return false }
2017-10-23 01:58:07 +00:00
axios({
method: 'post',
url: WIKI.data.telemetry.GA_REMOTE,
2017-10-23 01:58:07 +00:00
headers: {
2019-02-02 06:17:09 +00:00
'Content-type': 'text/plain'
2017-10-23 01:58:07 +00:00
},
2019-02-02 06:17:09 +00:00
data: events.map(ev => {
return qs.stringify({
v: 1, // API version
tid: WIKI.data.telemetry.GA_ID, // Tracking ID
aip: 1, // Anonymize IP
ds: 'server', // Data source
cid: WIKI.telemetry.cid, // Client ID
t: 'event', // Hit Type
ec: ev.eventCategory, // Event Category
ea: ev.eventAction, // Event Action
el: ev.eventLabel // Event Label
})
}).join('\n')
2017-10-23 01:58:07 +00:00
}).then(resp => {
if (resp.status !== 200) {
WIKI.logger.warn('Unable to send analytics telemetry request.')
2017-10-23 01:58:07 +00:00
}
}, err => {
WIKI.logger.warn('Unable to send analytics telemetry request.')
2017-10-23 01:58:07 +00:00
})
2019-02-02 06:17:09 +00:00
},
generateClientId() {
_.set(WIKI.config, 'telemetry.clientId', uuid())
return WIKI.config.telemetry.clientId
2017-10-23 01:58:07 +00:00
}
}