mirror of https://github.com/requarks/wiki
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.
103 lines
3.1 KiB
103 lines
3.1 KiB
2 years ago
|
import { Model } from 'objection'
|
||
|
import fs from 'node:fs/promises'
|
||
|
import path from 'node:path'
|
||
|
import { defaultTo, forOwn, isBoolean, replace, sortBy } from 'lodash-es'
|
||
|
import yaml from 'js-yaml'
|
||
|
import { parseModuleProps } from '../helpers/common.mjs'
|
||
6 years ago
|
|
||
|
/**
|
||
|
* Analytics model
|
||
|
*/
|
||
2 years ago
|
export class Analytics extends Model {
|
||
6 years ago
|
static get tableName() { return 'analytics' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
3 years ago
|
required: ['module', 'isEnabled'],
|
||
6 years ago
|
|
||
|
properties: {
|
||
3 years ago
|
id: { type: 'string' },
|
||
|
module: { type: 'string' },
|
||
|
isEnabled: { type: 'boolean', default: false }
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static get jsonAttributes() {
|
||
|
return ['config']
|
||
|
}
|
||
|
|
||
|
static async getProviders(isEnabled) {
|
||
2 years ago
|
const providers = await WIKI.db.analytics.query().where(isBoolean(isEnabled) ? { isEnabled } : {})
|
||
|
return sortBy(providers, ['module'])
|
||
6 years ago
|
}
|
||
|
|
||
|
static async refreshProvidersFromDisk() {
|
||
|
try {
|
||
|
// -> Fetch definitions from disk
|
||
|
const analyticsDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/analytics'))
|
||
3 years ago
|
WIKI.data.analytics = []
|
||
|
for (const dir of analyticsDirs) {
|
||
6 years ago
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/analytics', dir, 'definition.yml'), 'utf8')
|
||
3 years ago
|
const defParsed = yaml.load(def)
|
||
|
defParsed.key = dir
|
||
2 years ago
|
defParsed.props = parseModuleProps(defParsed.props)
|
||
3 years ago
|
WIKI.data.analytics.push(defParsed)
|
||
|
WIKI.logger.debug(`Loaded analytics module definition ${dir}: [ OK ]`)
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
WIKI.logger.info(`Loaded ${WIKI.data.analytics.length} analytics module definitions: [ OK ]`)
|
||
6 years ago
|
} catch (err) {
|
||
3 years ago
|
WIKI.logger.error(`Failed to scan or load analytics providers: [ FAILED ]`)
|
||
6 years ago
|
WIKI.logger.error(err)
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
static async getCode ({ cache = false } = {}) {
|
||
|
if (cache) {
|
||
|
const analyticsCached = await WIKI.cache.get('analytics')
|
||
|
if (analyticsCached) {
|
||
|
return analyticsCached
|
||
|
}
|
||
|
}
|
||
|
try {
|
||
|
const analyticsCode = {
|
||
|
head: '',
|
||
|
bodyStart: '',
|
||
|
bodyEnd: ''
|
||
|
}
|
||
2 years ago
|
const providers = await WIKI.db.analytics.getProviders(true)
|
||
6 years ago
|
|
||
|
for (let provider of providers) {
|
||
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/analytics', provider.key, 'code.yml'), 'utf8')
|
||
|
let code = yaml.safeLoad(def)
|
||
2 years ago
|
code.head = defaultTo(code.head, '')
|
||
|
code.bodyStart = defaultTo(code.bodyStart, '')
|
||
|
code.bodyEnd = defaultTo(code.bodyEnd, '')
|
||
6 years ago
|
|
||
2 years ago
|
forOwn(provider.config, (value, key) => {
|
||
|
code.head = replace(code.head, new RegExp(`{{${key}}}`, 'g'), value)
|
||
|
code.bodyStart = replace(code.bodyStart, `{{${key}}}`, value)
|
||
|
code.bodyEnd = replace(code.bodyEnd, `{{${key}}}`, value)
|
||
6 years ago
|
})
|
||
|
|
||
|
analyticsCode.head += code.head
|
||
|
analyticsCode.bodyStart += code.bodyStart
|
||
|
analyticsCode.bodyEnd += code.bodyEnd
|
||
|
}
|
||
|
|
||
|
await WIKI.cache.set('analytics', analyticsCode, 300)
|
||
|
|
||
|
return analyticsCode
|
||
|
} catch (err) {
|
||
|
WIKI.logger.warn('Error while getting analytics code: ', err)
|
||
|
return {
|
||
|
head: '',
|
||
|
bodyStart: '',
|
||
|
bodyEnd: ''
|
||
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|