wikijs-fork/server/core/telemetry.js

145 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

const _ = require('lodash')
2020-04-17 00:38:28 +00:00
const { createApolloFetch } = require('apollo-fetch')
const { v4: uuid } = require('uuid')
2019-02-02 06:17:09 +00:00
const os = require('os')
2020-04-17 00:38:28 +00:00
const fs = require('fs-extra')
2017-10-23 01:58:07 +00:00
/* global WIKI */
2017-10-23 01:58:07 +00:00
module.exports = {
enabled: false,
init() {
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
2020-04-17 00:38:28 +00:00
this.sendInstanceEvent('STARTUP')
2017-10-23 01:58:07 +00:00
}
2019-02-02 06:17:09 +00:00
},
2017-10-23 01:58:07 +00:00
sendError(err) {
2020-10-03 21:30:56 +00:00
// TODO
2017-10-23 01:58:07 +00:00
},
sendEvent(eventCategory, eventAction, eventLabel) {
2020-04-17 00:38:28 +00:00
// TODO
2019-02-02 06:17:09 +00:00
},
2020-04-17 00:38:28 +00:00
async sendInstanceEvent(eventType) {
2020-10-03 21:30:56 +00:00
if (WIKI.devMode || !this.enabled) { return }
2020-04-17 21:14:02 +00:00
2020-04-17 00:38:28 +00:00
try {
const apollo = createApolloFetch({
uri: WIKI.config.graphEndpoint
})
// Platform detection
let platform = 'LINUX'
let isDockerized = false
let osname = `${os.type()} ${os.release()}`
switch (os.platform()) {
case 'win32':
platform = 'WINDOWS'
break
case 'darwin':
platform = 'MACOS'
break
default:
platform = 'LINUX'
isDockerized = await fs.pathExists('/.dockerenv')
if (isDockerized) {
osname = 'Docker'
}
break
2017-10-23 01:58:07 +00:00
}
2020-04-17 00:38:28 +00:00
// 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)
}
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
}
}