New project structure
Beta of extract.js for extracting english locale
This commit is contained in:
parent
e18d2b2f84
commit
2edbd67205
247 changed files with 6405 additions and 4237 deletions
22
server/workers/reports/config/default.yaml
Normal file
22
server/workers/reports/config/default.yaml
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Process title visible in monitoring logs and process listing
|
||||
title: mailtrain
|
||||
|
||||
# Default language to use
|
||||
language: en
|
||||
|
||||
log:
|
||||
# silly|verbose|info|http|warn|error|silent
|
||||
level: verbose
|
||||
|
||||
mysql:
|
||||
host: localhost
|
||||
user: mailtrain
|
||||
password: mailtrain
|
||||
database: mailtrain
|
||||
# Some installations, eg. MAMP can use a different port (8889)
|
||||
# MAMP users should also turn on Allow network access to MySQL otherwise MySQL might not be accessible
|
||||
port: 3306
|
||||
charset: utf8mb4
|
||||
# The timezone configured on the MySQL server. This can be 'local', 'Z', or an offset in the form +HH:MM or -HH:MM
|
||||
timezone: local
|
||||
|
6
server/workers/reports/config/test.toml
Normal file
6
server/workers/reports/config/test.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[www]
|
||||
port=3000
|
||||
[mysql]
|
||||
user="mailtrain_test"
|
||||
password="bahquaiphoor"
|
||||
database="mailtrain_test"
|
95
server/workers/reports/report-processor.js
Normal file
95
server/workers/reports/report-processor.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
'use strict';
|
||||
|
||||
const reports = require('../../models/reports');
|
||||
const reportTemplates = require('../../models/report-templates');
|
||||
const lists = require('../../models/lists');
|
||||
const subscriptions = require('../../models/subscriptions');
|
||||
const campaigns = require('../../models/campaigns');
|
||||
const handlebars = require('handlebars');
|
||||
const handlebarsHelpers = require('../../lib/handlebars-helpers');
|
||||
const _ = require('../../lib/translate')._;
|
||||
const hbs = require('hbs');
|
||||
const vm = require('vm');
|
||||
const log = require('../../lib/log');
|
||||
const fs = require('fs');
|
||||
const knex = require('../../lib/knex');
|
||||
const contextHelpers = require('../../lib/context-helpers');
|
||||
|
||||
|
||||
handlebarsHelpers.registerHelpers(handlebars);
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const context = contextHelpers.getAdminContext();
|
||||
|
||||
const userFieldGetters = {
|
||||
'campaign': campaigns.getById,
|
||||
'list': id => lists.getById(context, id)
|
||||
};
|
||||
|
||||
const reportId = Number(process.argv[2]);
|
||||
|
||||
const report = await reports.getByIdWithTemplate(context, reportId);
|
||||
|
||||
const inputs = {};
|
||||
|
||||
for (const spec of report.user_fields) {
|
||||
const getter = userFieldGetters[spec.type];
|
||||
if (!getter) {
|
||||
throw new Error('Unknown user field type "' + spec.type + '".');
|
||||
}
|
||||
|
||||
const entities = [];
|
||||
for (const id of report.params[spec.id]) {
|
||||
entities.push(await getter(id));
|
||||
}
|
||||
|
||||
if (spec.minOccurences == 1 && spec.maxOccurences == 1) {
|
||||
inputs[spec.id] = entities[0];
|
||||
} else {
|
||||
inputs[spec.id] = entities;
|
||||
}
|
||||
}
|
||||
|
||||
const campaignsProxy = {
|
||||
getResults: (campaign, select, extra) => reports.getCampaignResults(context, campaign, select, extra),
|
||||
getById: campaignId => campaigns.getById(context, campaignId)
|
||||
};
|
||||
|
||||
const subscriptionsProxy = {
|
||||
list: listId => subscriptions.list(context, listId)
|
||||
};
|
||||
|
||||
const sandbox = {
|
||||
console,
|
||||
campaigns: campaignsProxy,
|
||||
subscriptions: subscriptionsProxy,
|
||||
knex,
|
||||
process,
|
||||
inputs,
|
||||
|
||||
render: data => {
|
||||
const hbsTmpl = handlebars.compile(report.hbs);
|
||||
const reportText = hbsTmpl(data);
|
||||
|
||||
process.stdout.write(reportText);
|
||||
}
|
||||
};
|
||||
|
||||
const js =
|
||||
'(async function() {' +
|
||||
report.js +
|
||||
'})().then(() => process.exit(0)).catch(err => { console.error(err); process.exit(1); })';
|
||||
|
||||
const script = new vm.Script(js);
|
||||
|
||||
script.runInNewContext(sandbox, {displayErrors: true, timeout: 120000});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue