You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const Promise = require('bluebird')
  3. const getos = Promise.promisify(require('getos'))
  4. const os = require('os')
  5. const filesize = require('filesize')
  6. const path = require('path')
  7. const fs = require('fs-extra')
  8. const moment = require('moment')
  9. const graphHelper = require('../../helpers/graph')
  10. /* global WIKI */
  11. const dbTypes = {
  12. mysql: 'MySQL',
  13. mariadb: 'MariaDB',
  14. postgres: 'PostgreSQL',
  15. sqlite: 'SQLite',
  16. mssql: 'MS SQL Server'
  17. }
  18. module.exports = {
  19. Query: {
  20. async system() { return {} }
  21. },
  22. Mutation: {
  23. async system() { return {} }
  24. },
  25. SystemQuery: {
  26. flags() {
  27. return _.transform(WIKI.config.flags, (result, value, key) => {
  28. result.push({ key, value })
  29. }, [])
  30. },
  31. async info() { return {} }
  32. },
  33. SystemMutation: {
  34. async updateFlags(obj, args, context) {
  35. WIKI.config.flags = _.transform(args.flags, (result, row) => {
  36. _.set(result, row.key, row.value)
  37. }, {})
  38. await WIKI.configSvc.applyFlags()
  39. await WIKI.configSvc.saveToDb(['flags'])
  40. return {
  41. responseResult: graphHelper.generateSuccess('System Flags applied successfully')
  42. }
  43. }
  44. },
  45. SystemInfo: {
  46. configFile() {
  47. return path.join(process.cwd(), 'config.yml')
  48. },
  49. currentVersion() {
  50. return WIKI.version
  51. },
  52. dbType() {
  53. return _.get(dbTypes, WIKI.config.db.type, 'Unknown DB')
  54. },
  55. async dbVersion() {
  56. let version = 'Unknown Version'
  57. switch (WIKI.config.db.type) {
  58. case 'mariadb':
  59. case 'mysql':
  60. const resultMYSQL = await WIKI.models.knex.raw('SELECT VERSION() as version;')
  61. version = _.get(resultMYSQL, '[0][0].version', 'Unknown Version')
  62. break
  63. case 'mssql':
  64. const resultMSSQL = await WIKI.models.knex.raw('SELECT @@VERSION as version;')
  65. version = _.get(resultMSSQL, '[0].version', 'Unknown Version')
  66. break
  67. case 'postgres':
  68. version = _.get(WIKI.models, 'knex.client.version', 'Unknown Version')
  69. break
  70. case 'sqlite':
  71. version = _.get(WIKI.models, 'knex.client.driver.VERSION', 'Unknown Version')
  72. break
  73. }
  74. return version
  75. },
  76. dbHost() {
  77. if (WIKI.config.db.type === 'sqlite') {
  78. return WIKI.config.db.storage
  79. } else {
  80. return WIKI.config.db.host
  81. }
  82. },
  83. latestVersion() {
  84. return WIKI.system.updates.version
  85. },
  86. latestVersionReleaseDate() {
  87. return moment.utc(WIKI.system.updates.releaseDate)
  88. },
  89. async operatingSystem() {
  90. let osLabel = `${os.type()} (${os.platform()}) ${os.release()} ${os.arch()}`
  91. if (os.platform() === 'linux') {
  92. const osInfo = await getos()
  93. osLabel = `${os.type()} - ${osInfo.dist} (${osInfo.codename || os.platform()}) ${osInfo.release || os.release()} ${os.arch()}`
  94. }
  95. return osLabel
  96. },
  97. async platform () {
  98. const isDockerized = await fs.pathExists('/.dockerenv')
  99. if (isDockerized) {
  100. return 'docker'
  101. }
  102. return os.platform()
  103. },
  104. hostname() {
  105. return os.hostname()
  106. },
  107. cpuCores() {
  108. return os.cpus().length
  109. },
  110. ramTotal() {
  111. return filesize(os.totalmem())
  112. },
  113. workingDirectory() {
  114. return process.cwd()
  115. },
  116. nodeVersion() {
  117. return process.version.substr(1)
  118. },
  119. async groupsTotal() {
  120. const total = await WIKI.models.groups.query().count('* as total').first().pluck('total')
  121. return _.toSafeInteger(total)
  122. },
  123. async pagesTotal() {
  124. const total = await WIKI.models.pages.query().count('* as total').first().pluck('total')
  125. return _.toSafeInteger(total)
  126. },
  127. async usersTotal() {
  128. const total = await WIKI.models.users.query().count('* as total').first().pluck('total')
  129. return _.toSafeInteger(total)
  130. }
  131. }
  132. }