Fixes in reports. Reports seem to work now
This commit is contained in:
parent
0be4af5f6c
commit
5a16d789a0
30 changed files with 716 additions and 338 deletions
27
server/services/workers/reports/config/default.yaml
Normal file
27
server/services/workers/reports/config/default.yaml
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Process title visible in monitoring logs and process listing
|
||||
title: mailtrain
|
||||
|
||||
log:
|
||||
# silly|verbose|info|http|warn|error|silent
|
||||
level: info
|
||||
|
||||
# Default language to use
|
||||
defaultLanguage: en-US
|
||||
|
||||
# Enabled languages
|
||||
enabledLanguages:
|
||||
- en-US
|
||||
- fk-FK
|
||||
|
||||
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
|
||||
# If the MySQL server runs on the same server as Mailtrain, use 'local'
|
||||
timezone: local
|
113
server/services/workers/reports/report-processor.js
Normal file
113
server/services/workers/reports/report-processor.js
Normal file
|
@ -0,0 +1,113 @@
|
|||
'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 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');
|
||||
|
||||
const csvStringify = require('csv-stringify');
|
||||
const stream = require('stream');
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const context = contextHelpers.getAdminContext();
|
||||
|
||||
const userFieldGetters = {
|
||||
'campaign': id => campaigns.getById(context, id, false, campaigns.Content.ALL),
|
||||
'list': id => lists.getById(context, id)
|
||||
};
|
||||
|
||||
const reportId = Number(process.argv[2]);
|
||||
|
||||
const report = await reports.getByIdWithTemplate(context, reportId, false);
|
||||
|
||||
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 = {
|
||||
getCampaignOpenStatistics: reports.getCampaignOpenStatistics,
|
||||
getCampaignClickStatistics: reports.getCampaignClickStatistics,
|
||||
getCampaignLinkClickStatistics: reports.getCampaignLinkClickStatistics,
|
||||
getCampaignOpenStatisticsStream: reports.getCampaignOpenStatisticsStream,
|
||||
getCampaignClickStatisticsStream: reports.getCampaignClickStatisticsStream,
|
||||
getCampaignLinkClickStatisticsStream: reports.getCampaignLinkClickStatisticsStream,
|
||||
getById: campaignId => campaigns.getById(context, campaignId, false, campaigns.Content.ALL)
|
||||
};
|
||||
|
||||
const subscriptionsProxy = {
|
||||
list: (listId, grouped, offset, limit) => subscriptions.list(context, listId, grouped, offset, limit)
|
||||
};
|
||||
|
||||
const sandbox = {
|
||||
console,
|
||||
campaigns: campaignsProxy,
|
||||
subscriptions: subscriptionsProxy,
|
||||
stream,
|
||||
knex,
|
||||
process,
|
||||
inputs,
|
||||
|
||||
renderCsvFromStream: async (readable, opts) => {
|
||||
const stringifier = csvStringify(opts);
|
||||
|
||||
const finished = new Promise((success, fail) => {
|
||||
stringifier.on('finish', () => success())
|
||||
stringifier.on('error', (err) => fail(err))
|
||||
});
|
||||
|
||||
stringifier.pipe(process.stdout);
|
||||
readable.pipe(stringifier);
|
||||
|
||||
await finished;
|
||||
},
|
||||
|
||||
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