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.

144 lines
4.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const { createApolloFetch } = require('apollo-fetch')
  3. const { v4: uuid } = require('uuid')
  4. const os = require('os')
  5. const fs = require('fs-extra')
  6. /* global WIKI */
  7. module.exports = {
  8. enabled: false,
  9. init() {
  10. WIKI.telemetry = this
  11. if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true && WIKI.config.offline !== true) {
  12. this.enabled = true
  13. this.sendInstanceEvent('STARTUP')
  14. }
  15. },
  16. sendError(err) {
  17. // TODO
  18. },
  19. sendEvent(eventCategory, eventAction, eventLabel) {
  20. // TODO
  21. },
  22. async sendInstanceEvent(eventType) {
  23. if (WIKI.devMode || !this.enabled) { return }
  24. try {
  25. const apollo = createApolloFetch({
  26. uri: WIKI.config.graphEndpoint
  27. })
  28. // Platform detection
  29. let platform = 'LINUX'
  30. let isDockerized = false
  31. let osname = `${os.type()} ${os.release()}`
  32. switch (os.platform()) {
  33. case 'win32':
  34. platform = 'WINDOWS'
  35. break
  36. case 'darwin':
  37. platform = 'MACOS'
  38. break
  39. default:
  40. platform = 'LINUX'
  41. isDockerized = await fs.pathExists('/.dockerenv')
  42. if (isDockerized) {
  43. osname = 'Docker'
  44. }
  45. break
  46. }
  47. // DB Version detection
  48. let dbVersion = 'Unknown'
  49. switch (WIKI.config.db.type) {
  50. case 'mariadb':
  51. case 'mysql':
  52. const resultMYSQL = await WIKI.models.knex.raw('SELECT VERSION() as version;')
  53. dbVersion = _.get(resultMYSQL, '[0][0].version', 'Unknown')
  54. break
  55. case 'mssql':
  56. const resultMSSQL = await WIKI.models.knex.raw('SELECT @@VERSION as version;')
  57. dbVersion = _.get(resultMSSQL, '[0].version', 'Unknown')
  58. break
  59. case 'postgres':
  60. dbVersion = _.get(WIKI.models, 'knex.client.version', 'Unknown')
  61. break
  62. case 'sqlite':
  63. dbVersion = _.get(WIKI.models, 'knex.client.driver.VERSION', 'Unknown')
  64. break
  65. }
  66. let arch = os.arch().toUpperCase()
  67. if (['ARM', 'ARM64', 'X32', 'X64'].indexOf(arch) < 0) {
  68. arch = 'OTHER'
  69. }
  70. // Send Event
  71. const respStrings = await apollo({
  72. query: `mutation (
  73. $version: String!
  74. $platform: TelemetryPlatform!
  75. $os: String!
  76. $architecture: TelemetryArchitecture!
  77. $dbType: TelemetryDBType!
  78. $dbVersion: String!
  79. $nodeVersion: String!
  80. $cpuCores: Int!
  81. $ramMBytes: Int!,
  82. $clientId: String!,
  83. $event: TelemetryInstanceEvent!
  84. ) {
  85. telemetry {
  86. instance(
  87. version: $version
  88. platform: $platform
  89. os: $os
  90. architecture: $architecture
  91. dbType: $dbType
  92. dbVersion: $dbVersion
  93. nodeVersion: $nodeVersion
  94. cpuCores: $cpuCores
  95. ramMBytes: $ramMBytes
  96. clientId: $clientId
  97. event: $event
  98. ) {
  99. responseResult {
  100. succeeded
  101. errorCode
  102. slug
  103. message
  104. }
  105. }
  106. }
  107. }`,
  108. variables: {
  109. version: WIKI.version,
  110. platform,
  111. os: osname,
  112. architecture: arch,
  113. dbType: WIKI.config.db.type.toUpperCase(),
  114. dbVersion,
  115. nodeVersion: process.version.substr(1),
  116. cpuCores: os.cpus().length,
  117. ramMBytes: Math.round(os.totalmem() / 1024 / 1024),
  118. clientId: WIKI.config.telemetry.clientId,
  119. event: eventType
  120. }
  121. })
  122. const telemetryResponse = _.get(respStrings, 'data.telemetry.instance.responseResult', { succeeded: false, message: 'Unexpected Error' })
  123. if (!telemetryResponse.succeeded) {
  124. WIKI.logger.warn('Failed to send instance telemetry: ' + telemetryResponse.message)
  125. } else {
  126. WIKI.logger.info('Telemetry is active: [ OK ]')
  127. }
  128. } catch (err) {
  129. WIKI.logger.warn(err)
  130. }
  131. },
  132. generateClientId() {
  133. _.set(WIKI.config, 'telemetry.clientId', uuid())
  134. return WIKI.config.telemetry.clientId
  135. }
  136. }