-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: BetaHuhn <schiller@mxis.ch>
- Loading branch information
Showing
7 changed files
with
145 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ src/config.json | |
report.json | ||
report.xml | ||
.env | ||
test.html | ||
report.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const ejs = require('ejs') | ||
const mjml = require('mjml') | ||
const path = require('path') | ||
|
||
module.exports = async function(data, endpoint, to) { | ||
// Prepare list items | ||
const listItems = [ 'pages', 'referrers', 'languages', 'browsers', 'devices', 'sizes', 'systems' ] | ||
const domains = data.domains.map((domain) => { | ||
// Filter domains | ||
const result = listItems | ||
.filter((item) => domain[item] === undefined || domain[item].length) | ||
.map((item) => { | ||
return { | ||
title: item.charAt(0).toUpperCase() + item.slice(1), | ||
data: domain[item] | ||
} | ||
}) | ||
|
||
// Split domains into chunks | ||
const chunked = [] | ||
for (let i = 0; i < result.length; i += 2) { | ||
chunked.push(result.slice(i, i + 2)) | ||
} | ||
|
||
return { | ||
id: domain.id, | ||
title: domain.title, | ||
viewsInRange: domain.viewsInRange, | ||
viewsDay: domain.viewsDay, | ||
viewsMonth: domain.viewsMonth, | ||
viewsYear: domain.viewsYear, | ||
viewsAvg: domain.viewsAvg, | ||
durationAvg: domain.durationAvg, | ||
rows: chunked | ||
} | ||
}) | ||
|
||
const events = [] | ||
if (data.events !== undefined && data.events.length > 0) { | ||
const filteredEvents = data.events.filter((item) => item.data !== undefined && item.data.length > 0) | ||
|
||
// Split events into chunks | ||
if (filteredEvents.length > 0) { | ||
for (let i = 0; i < filteredEvents.length; i += 2) { | ||
events.push(filteredEvents.slice(i, i + 2)) | ||
} | ||
} | ||
} | ||
|
||
// Render HTML with data | ||
const html = await ejs.renderFile( | ||
path.join(__dirname, `../../templates/email.ejs`), | ||
{ | ||
domains, | ||
events, | ||
viewsInRange: data.viewsInRange, | ||
viewsYear: data.viewsYear, | ||
viewsAvg: data.viewsAvg, | ||
durationAvg: data.durationAvg, | ||
names: data.names, | ||
namesShort: data.namesShort, | ||
generatedAt: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''), | ||
range: data.range, | ||
endpoint, | ||
to | ||
} | ||
) | ||
|
||
// Generate final html | ||
const htmlOutput = mjml(html, { validationLevel: 'skip' }) | ||
|
||
if (htmlOutput.errors.length > 0) { | ||
console.log(htmlOutput.errors) | ||
throw new Error(JSON.stringify(htmlOutput.errors)) | ||
} | ||
|
||
return htmlOutput.html | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const fs = require('fs') | ||
|
||
const render = require('./helpers/renderHtml') | ||
|
||
const report = async function(data, config, output) { | ||
// eslint-disable-next-line no-async-promise-executor | ||
return new Promise(async (resolve, reject) => { | ||
const html = await render(data, config.ackee.server, 'html') | ||
|
||
fs.writeFile(output, html, (err) => { | ||
if (err) reject(err) | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const email = require('./email') | ||
const json = require('./json') | ||
const rss = require('./rss') | ||
const html = require('./html') | ||
|
||
module.exports.email = email | ||
module.exports.json = json | ||
module.exports.rss = rss | ||
module.exports.rss = rss | ||
module.exports.html = html |