2
0

analytics.js 3.2 KB

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