wikijs-fork/server/graph/resolvers/system.js

186 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-03-18 04:34:32 +00:00
const _ = require('lodash')
const Promise = require('bluebird')
const getos = Promise.promisify(require('getos'))
2018-03-18 04:34:32 +00:00
const os = require('os')
const filesize = require('filesize')
const path = require('path')
const fs = require('fs-extra')
2019-01-13 20:37:45 +00:00
const moment = require('moment')
2019-02-23 23:22:25 +00:00
const graphHelper = require('../../helpers/graph')
const request = require('request-promise')
2018-03-18 04:34:32 +00:00
/* global WIKI */
const dbTypes = {
2018-09-08 19:49:36 +00:00
mysql: 'MySQL',
mariadb: 'MariaDB',
postgres: 'PostgreSQL',
sqlite: 'SQLite',
mssql: 'MS SQL Server'
}
2018-03-18 04:34:32 +00:00
module.exports = {
Query: {
2019-09-22 20:32:39 +00:00
async system () { return {} }
2018-03-18 04:34:32 +00:00
},
2019-02-23 23:22:25 +00:00
Mutation: {
2019-09-22 20:32:39 +00:00
async system () { return {} }
2019-02-23 23:22:25 +00:00
},
2018-03-18 04:34:32 +00:00
SystemQuery: {
2019-09-22 20:32:39 +00:00
flags () {
2019-02-23 23:22:25 +00:00
return _.transform(WIKI.config.flags, (result, value, key) => {
result.push({ key, value })
}, [])
},
async info() { return {} }
},
2019-02-23 23:22:25 +00:00
SystemMutation: {
2019-09-22 20:32:39 +00:00
async updateFlags (obj, args, context) {
2019-02-23 23:22:25 +00:00
WIKI.config.flags = _.transform(args.flags, (result, row) => {
_.set(result, row.key, row.value)
}, {})
await WIKI.configSvc.applyFlags()
await WIKI.configSvc.saveToDb(['flags'])
return {
responseResult: graphHelper.generateSuccess('System Flags applied successfully')
}
2019-07-06 21:43:50 +00:00
},
2019-09-22 20:32:39 +00:00
async resetTelemetryClientId (obj, args, context) {
2019-07-06 21:43:50 +00:00
try {
WIKI.telemetry.generateClientId()
await WIKI.configSvc.saveToDb(['telemetry'])
return {
responseResult: graphHelper.generateSuccess('Telemetry state updated successfully')
}
} catch (err) {
2019-07-06 21:43:50 +00:00
return graphHelper.generateError(err)
}
},
2019-09-22 20:32:39 +00:00
async setTelemetry (obj, args, context) {
2019-07-06 21:43:50 +00:00
try {
_.set(WIKI.config, 'telemetry.isEnabled', args.enabled)
WIKI.telemetry.enabled = args.enabled
await WIKI.configSvc.saveToDb(['telemetry'])
return {
responseResult: graphHelper.generateSuccess('Telemetry Client ID has been reset successfully')
}
} catch (err) {
2019-07-06 21:43:50 +00:00
return graphHelper.generateError(err)
}
2019-09-22 20:32:39 +00:00
},
async performUpgrade (obj, args, context) {
try {
if (process.env.UPGRADE_COMPANION) {
await request({
method: 'POST',
uri: 'http://wiki-update-companion/upgrade'
})
return {
responseResult: graphHelper.generateSuccess('Upgrade has started.')
2019-09-22 20:32:39 +00:00
}
} else {
throw new Error('You must run the wiki-update-companion container and pass the UPGRADE_COMPANION env var in order to use this feature.')
2019-09-22 20:32:39 +00:00
}
} catch (err) {
return graphHelper.generateError(err)
}
2019-02-23 23:22:25 +00:00
}
},
SystemInfo: {
2019-09-22 20:32:39 +00:00
configFile () {
return path.join(process.cwd(), 'config.yml')
},
2019-09-22 20:32:39 +00:00
cpuCores () {
2019-07-06 21:43:50 +00:00
return os.cpus().length
},
2019-09-22 20:32:39 +00:00
currentVersion () {
return WIKI.version
},
2019-09-22 20:32:39 +00:00
dbType () {
return _.get(dbTypes, WIKI.config.db.type, 'Unknown DB')
},
2019-09-22 20:32:39 +00:00
async dbVersion () {
2018-09-08 19:49:36 +00:00
let version = 'Unknown Version'
switch (WIKI.config.db.type) {
case 'mariadb':
case 'mysql':
const resultMYSQL = await WIKI.models.knex.raw('SELECT VERSION() as version;')
version = _.get(resultMYSQL, '[0][0].version', 'Unknown Version')
break
case 'mssql':
const resultMSSQL = await WIKI.models.knex.raw('SELECT @@VERSION as version;')
version = _.get(resultMSSQL, '[0].version', 'Unknown Version')
break
case 'postgres':
version = _.get(WIKI.models, 'knex.client.version', 'Unknown Version')
break
case 'sqlite':
version = _.get(WIKI.models, 'knex.client.driver.VERSION', 'Unknown Version')
break
}
return version
},
2019-09-22 20:32:39 +00:00
dbHost () {
2018-09-08 19:49:36 +00:00
if (WIKI.config.db.type === 'sqlite') {
return WIKI.config.db.storage
} else {
return WIKI.config.db.host
}
},
2019-09-22 20:32:39 +00:00
hostname () {
2019-07-06 21:43:50 +00:00
return os.hostname()
},
2019-09-22 20:32:39 +00:00
latestVersion () {
2019-01-13 20:37:45 +00:00
return WIKI.system.updates.version
},
2019-09-22 20:32:39 +00:00
latestVersionReleaseDate () {
2019-01-13 20:37:45 +00:00
return moment.utc(WIKI.system.updates.releaseDate)
},
2019-09-22 20:32:39 +00:00
nodeVersion () {
2019-07-06 21:43:50 +00:00
return process.version.substr(1)
},
2019-09-22 20:32:39 +00:00
async operatingSystem () {
let osLabel = `${os.type()} (${os.platform()}) ${os.release()} ${os.arch()}`
if (os.platform() === 'linux') {
const osInfo = await getos()
osLabel = `${os.type()} - ${osInfo.dist} (${osInfo.codename || os.platform()}) ${osInfo.release || os.release()} ${os.arch()}`
}
return osLabel
},
async platform () {
const isDockerized = await fs.pathExists('/.dockerenv')
if (isDockerized) {
return 'docker'
}
return os.platform()
},
2019-09-22 20:32:39 +00:00
ramTotal () {
return filesize(os.totalmem())
},
2019-09-22 20:32:39 +00:00
telemetry () {
2019-07-06 21:43:50 +00:00
return WIKI.telemetry.enabled
},
2019-09-22 20:32:39 +00:00
telemetryClientId () {
2019-07-06 21:43:50 +00:00
return WIKI.config.telemetry.clientId
},
2019-09-22 20:32:39 +00:00
async upgradeCapable () {
return !_.isNil(process.env.UPGRADE_COMPANION)
2019-09-22 20:32:39 +00:00
},
workingDirectory () {
return process.cwd()
},
2019-09-22 20:32:39 +00:00
async groupsTotal () {
const total = await WIKI.models.groups.query().count('* as total').first().pluck('total')
return _.toSafeInteger(total)
},
2019-09-22 20:32:39 +00:00
async pagesTotal () {
const total = await WIKI.models.pages.query().count('* as total').first().pluck('total')
return _.toSafeInteger(total)
},
2019-09-22 20:32:39 +00:00
async usersTotal () {
const total = await WIKI.models.users.query().count('* as total').first().pluck('total')
return _.toSafeInteger(total)
2018-03-18 04:34:32 +00:00
}
}
2018-03-18 04:34:32 +00:00
}