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.

99 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const axios = require('axios')
  3. const bugsnag = require('@bugsnag/node')
  4. const uuid = require('uuid/v4')
  5. const qs = require('querystring')
  6. const os = require('os')
  7. /* global WIKI */
  8. module.exports = {
  9. client: null,
  10. enabled: false,
  11. init() {
  12. this.client = bugsnag({
  13. apiKey: WIKI.data.telemetry.BUGSNAG_ID,
  14. appType: 'server',
  15. appVersion: WIKI.version,
  16. autoNotify: false,
  17. collectUserIp: false,
  18. hostname: _.get(WIKI.config, 'telemetry.clientId', uuid()),
  19. notifyReleaseStages: ['production'],
  20. releaseStage: WIKI.IS_DEBUG ? 'development' : 'production',
  21. projectRoot: WIKI.ROOTPATH,
  22. logger: null,
  23. beforeSend: (report) => {
  24. if (!WIKI.telemetry.enabled) { return false }
  25. }
  26. })
  27. WIKI.telemetry = this
  28. if (_.get(WIKI.config, 'telemetry.isEnabled', false) === true) {
  29. this.enabled = true
  30. this.sendOSInfo()
  31. }
  32. },
  33. sendOSInfo() {
  34. this.sendBatchEvents([
  35. {
  36. eventCategory: 'node-version',
  37. eventAction: process.version
  38. },
  39. {
  40. eventCategory: 'os-platform',
  41. eventAction: os.platform()
  42. },
  43. {
  44. eventCategory: 'cpu-cores',
  45. eventAction: os.cpus().length
  46. },
  47. {
  48. eventCategory: 'db-type',
  49. eventAction: WIKI.config.db.type
  50. }
  51. ])
  52. },
  53. sendError(err) {
  54. this.client.notify(err)
  55. },
  56. sendEvent(eventCategory, eventAction, eventLabel) {
  57. this.sendBatchEvents([{
  58. eventCategory,
  59. eventAction,
  60. eventLabel
  61. }])
  62. },
  63. sendBatchEvents(events) {
  64. if (!this.enabled || WIKI.IS_DEBUG) { return false }
  65. axios({
  66. method: 'post',
  67. url: WIKI.data.telemetry.GA_REMOTE,
  68. headers: {
  69. 'Content-type': 'text/plain'
  70. },
  71. data: events.map(ev => {
  72. return qs.stringify({
  73. v: 1, // API version
  74. tid: WIKI.data.telemetry.GA_ID, // Tracking ID
  75. aip: 1, // Anonymize IP
  76. ds: 'server', // Data source
  77. cid: WIKI.telemetry.cid, // Client ID
  78. t: 'event', // Hit Type
  79. ec: ev.eventCategory, // Event Category
  80. ea: ev.eventAction, // Event Action
  81. el: ev.eventLabel // Event Label
  82. })
  83. }).join('\n')
  84. }).then(resp => {
  85. if (resp.status !== 200) {
  86. WIKI.logger.warn('Unable to send analytics telemetry request.')
  87. }
  88. }, err => {
  89. WIKI.logger.warn('Unable to send analytics telemetry request.')
  90. })
  91. },
  92. generateClientId() {
  93. _.set(WIKI.config, 'telemetry.clientId', uuid())
  94. return WIKI.config.telemetry.clientId
  95. }
  96. }