fix: use new telemetry endpoint

This commit is contained in:
NGPixel 2020-04-16 20:38:28 -04:00
parent 14beb949d9
commit 71be4660bf
2 changed files with 122 additions and 58 deletions

View File

@ -1,9 +1,9 @@
const _ = require('lodash') const _ = require('lodash')
const request = require('request-promise') const { createApolloFetch } = require('apollo-fetch')
const bugsnag = require('@bugsnag/node') const bugsnag = require('@bugsnag/node')
const { v4: uuid } = require('uuid') const { v4: uuid } = require('uuid')
const qs = require('querystring')
const os = require('os') const os = require('os')
const fs = require('fs-extra')
/* global WIKI */ /* global WIKI */
@ -30,67 +30,127 @@ module.exports = {
if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true && WIKI.config.offline !== true) { if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true && WIKI.config.offline !== true) {
this.enabled = true this.enabled = true
this.sendOSInfo() this.sendInstanceEvent('STARTUP')
} }
}, },
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
}
])
},
sendError(err) { sendError(err) {
this.client.notify(err) this.client.notify(err)
}, },
sendEvent(eventCategory, eventAction, eventLabel) { sendEvent(eventCategory, eventAction, eventLabel) {
this.sendBatchEvents([{ // TODO
eventCategory,
eventAction,
eventLabel
}])
}, },
sendBatchEvents(events) { async sendInstanceEvent(eventType) {
if (!this.enabled || WIKI.IS_DEBUG) { return false } try {
request({ const apollo = createApolloFetch({
method: 'POST', uri: WIKI.config.graphEndpoint
url: WIKI.data.telemetry.GA_REMOTE, })
headers: {
'Content-type': 'text/plain' // Platform detection
}, let platform = 'LINUX'
body: events.map(ev => { let isDockerized = false
return qs.stringify({ let osname = `${os.type()} ${os.release()}`
v: 1, // API version switch (os.platform()) {
tid: WIKI.data.telemetry.GA_ID, // Tracking ID case 'win32':
aip: 1, // Anonymize IP platform = 'WINDOWS'
ds: 'server', // Data source break
cid: WIKI.telemetry.cid, // Client ID case 'darwin':
t: 'event', // Hit Type platform = 'MACOS'
ec: ev.eventCategory, // Event Category break
ea: ev.eventAction, // Event Action default:
el: ev.eventLabel // Event Label platform = 'LINUX'
}) isDockerized = await fs.pathExists('/.dockerenv')
}).join('\n') if (isDockerized) {
}).then(resp => { osname = 'Docker'
if (resp.status !== 200) { }
WIKI.logger.warn('Unable to send analytics telemetry request.') break
} }
}, err => {
WIKI.logger.warn('Unable to send analytics telemetry request.') // DB Version detection
}) let dbVersion = 'Unknown'
switch (WIKI.config.db.type) {
case 'mariadb':
case 'mysql':
const resultMYSQL = await WIKI.models.knex.raw('SELECT VERSION() as version;')
dbVersion = _.get(resultMYSQL, '[0][0].version', 'Unknown')
break
case 'mssql':
const resultMSSQL = await WIKI.models.knex.raw('SELECT @@VERSION as version;')
dbVersion = _.get(resultMSSQL, '[0].version', 'Unknown')
break
case 'postgres':
dbVersion = _.get(WIKI.models, 'knex.client.version', 'Unknown')
break
case 'sqlite':
dbVersion = _.get(WIKI.models, 'knex.client.driver.VERSION', 'Unknown')
break
}
let arch = os.arch().toUpperCase()
if (['ARM', 'ARM64', 'X32', 'X64'].indexOf(arch) < 0) {
arch = 'OTHER'
}
// Send Event
const respStrings = await apollo({
query: `mutation (
$version: String!
$platform: TelemetryPlatform!
$os: String!
$architecture: TelemetryArchitecture!
$dbType: TelemetryDBType!
$dbVersion: String!
$nodeVersion: String!
$cpuCores: Int!
$ramMBytes: Int!,
$clientId: String!,
$event: TelemetryInstanceEvent!
) {
telemetry {
instance(
version: $version
platform: $platform
os: $os
architecture: $architecture
dbType: $dbType
dbVersion: $dbVersion
nodeVersion: $nodeVersion
cpuCores: $cpuCores
ramMBytes: $ramMBytes
clientId: $clientId
event: $event
) {
responseResult {
succeeded
errorCode
slug
message
}
}
}
}`,
variables: {
version: WIKI.version,
platform,
os: osname,
architecture: arch,
dbType: WIKI.config.db.type.toUpperCase(),
dbVersion,
nodeVersion: process.version.substr(1),
cpuCores: os.cpus().length,
ramMBytes: Math.round(os.totalmem() / 1024 / 1024),
clientId: WIKI.config.telemetry.clientId,
event: eventType
}
})
const telemetryResponse = _.get(respStrings, 'data.telemetry.instance.responseResult', { succeeded: false, message: 'Unexpected Error' })
if (!telemetryResponse.succeeded) {
WIKI.logger.warn('Failed to send instance telemetry: ' + telemetryResponse.message)
} else {
WIKI.logger.info('Telemetry is active: [ OK ]')
}
} catch (err) {
WIKI.logger.warn(err)
}
}, },
generateClientId() { generateClientId() {
_.set(WIKI.config, 'telemetry.clientId', uuid()) _.set(WIKI.config, 'telemetry.clientId', uuid())

View File

@ -134,7 +134,7 @@ module.exports = () => {
// Init Telemetry // Init Telemetry
WIKI.kernel.initTelemetry() WIKI.kernel.initTelemetry()
WIKI.telemetry.sendEvent('setup', 'install-start') // WIKI.telemetry.sendEvent('setup', 'install-start')
// Basic checks // Basic checks
if (!semver.satisfies(process.version, '>=10.12')) { if (!semver.satisfies(process.version, '>=10.12')) {
@ -269,7 +269,7 @@ module.exports = () => {
await WIKI.models.searchEngines.refreshSearchEnginesFromDisk() await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
await WIKI.models.searchEngines.query().patch({ isEnabled: true }).where('key', 'db') await WIKI.models.searchEngines.query().patch({ isEnabled: true }).where('key', 'db')
WIKI.telemetry.sendEvent('setup', 'install-loadedmodules') // WIKI.telemetry.sendEvent('setup', 'install-loadedmodules')
// Load storage targets // Load storage targets
await WIKI.models.storage.refreshTargetsFromDisk() await WIKI.models.storage.refreshTargetsFromDisk()
@ -326,13 +326,17 @@ module.exports = () => {
}) })
WIKI.logger.info('Setup is complete!') WIKI.logger.info('Setup is complete!')
WIKI.telemetry.sendEvent('setup', 'install-completed') // WIKI.telemetry.sendEvent('setup', 'install-completed')
res.json({ res.json({
ok: true, ok: true,
redirectPath: '/', redirectPath: '/',
redirectPort: WIKI.config.port redirectPort: WIKI.config.port
}).end() }).end()
if (WIKI.config.telemetry.isEnabled) {
await WIKI.telemetry.sendInstanceEvent('INSTALL')
}
WIKI.config.setup = false WIKI.config.setup = false
WIKI.logger.info('Stopping Setup...') WIKI.logger.info('Stopping Setup...')