analytics.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const Model = require('objection').Model
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Analytics model
  10. */
  11. module.exports = class Analytics extends Model {
  12. static get tableName() { return 'analytics' }
  13. static get jsonSchema () {
  14. return {
  15. type: 'object',
  16. required: ['module', 'isEnabled'],
  17. properties: {
  18. id: { type: 'string' },
  19. module: { type: 'string' },
  20. isEnabled: { type: 'boolean', default: false }
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['config']
  26. }
  27. static async getProviders(isEnabled) {
  28. const providers = await WIKI.models.analytics.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
  29. return _.sortBy(providers, ['module'])
  30. }
  31. static async refreshProvidersFromDisk() {
  32. try {
  33. // -> Fetch definitions from disk
  34. const analyticsDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/analytics'))
  35. WIKI.data.analytics = []
  36. for (const dir of analyticsDirs) {
  37. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/analytics', dir, 'definition.yml'), 'utf8')
  38. const defParsed = yaml.load(def)
  39. defParsed.key = dir
  40. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  41. WIKI.data.analytics.push(defParsed)
  42. WIKI.logger.debug(`Loaded analytics module definition ${dir}: [ OK ]`)
  43. }
  44. WIKI.logger.info(`Loaded ${WIKI.data.analytics.length} analytics module definitions: [ OK ]`)
  45. } catch (err) {
  46. WIKI.logger.error(`Failed to scan or load analytics providers: [ FAILED ]`)
  47. WIKI.logger.error(err)
  48. }
  49. }
  50. static async getCode ({ cache = false } = {}) {
  51. if (cache) {
  52. const analyticsCached = await WIKI.cache.get('analytics')
  53. if (analyticsCached) {
  54. return analyticsCached
  55. }
  56. }
  57. try {
  58. const analyticsCode = {
  59. head: '',
  60. bodyStart: '',
  61. bodyEnd: ''
  62. }
  63. const providers = await WIKI.models.analytics.getProviders(true)
  64. for (let provider of providers) {
  65. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/analytics', provider.key, 'code.yml'), 'utf8')
  66. let code = yaml.safeLoad(def)
  67. code.head = _.defaultTo(code.head, '')
  68. code.bodyStart = _.defaultTo(code.bodyStart, '')
  69. code.bodyEnd = _.defaultTo(code.bodyEnd, '')
  70. _.forOwn(provider.config, (value, key) => {
  71. code.head = _.replace(code.head, new RegExp(`{{${key}}}`, 'g'), value)
  72. code.bodyStart = _.replace(code.bodyStart, `{{${key}}}`, value)
  73. code.bodyEnd = _.replace(code.bodyEnd, `{{${key}}}`, value)
  74. })
  75. analyticsCode.head += code.head
  76. analyticsCode.bodyStart += code.bodyStart
  77. analyticsCode.bodyEnd += code.bodyEnd
  78. }
  79. await WIKI.cache.set('analytics', analyticsCode, 300)
  80. return analyticsCode
  81. } catch (err) {
  82. WIKI.logger.warn('Error while getting analytics code: ', err)
  83. return {
  84. head: '',
  85. bodyStart: '',
  86. bodyEnd: ''
  87. }
  88. }
  89. }
  90. }