mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 04:33:11 +03:00
eslint, prettier fix
This commit is contained in:
parent
bdc4450b72
commit
2679fa20d5
10 changed files with 284 additions and 208 deletions
140
chart/chart.js
140
chart/chart.js
|
@ -13,13 +13,16 @@ const logger = require('../shared/logger/src/main.js');
|
|||
* @returns {Promise<Error|string>}
|
||||
*/
|
||||
async function gen_chart(from_currency, conv_currency, start_date, end_date) {
|
||||
const data = await pool.query('SELECT date, rate FROM currency WHERE ' +
|
||||
'(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2 ORDER BY date ', [
|
||||
from_currency.toUpperCase(),
|
||||
conv_currency.toUpperCase(),
|
||||
start_date,
|
||||
end_date
|
||||
]);
|
||||
const data = await pool.query(
|
||||
'SELECT date, rate FROM currency WHERE ' +
|
||||
'(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2 ORDER BY date ',
|
||||
[
|
||||
from_currency.toUpperCase(),
|
||||
conv_currency.toUpperCase(),
|
||||
start_date,
|
||||
end_date,
|
||||
],
|
||||
);
|
||||
|
||||
if (!data['rows'][0]) return 'Missing data';
|
||||
|
||||
|
@ -31,63 +34,71 @@ async function gen_chart(from_currency, conv_currency, start_date, end_date) {
|
|||
rate.push(data.rows[i].rate);
|
||||
}
|
||||
|
||||
const chart = ChartJSImage().chart({
|
||||
type: 'line',
|
||||
options: {
|
||||
title: {
|
||||
display: true,
|
||||
text: `${from_currency} / ${conv_currency}`,
|
||||
},
|
||||
scales: [
|
||||
{
|
||||
yAxes: [
|
||||
{
|
||||
ticks: {
|
||||
beginAtZero: false,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
data: {
|
||||
labels: date,
|
||||
datasets: [
|
||||
{
|
||||
borderColor: rate[0] < rate[rate.length-1] ? 'rgb(24, 218, 39)' : 'rgb(243, 85, 50)',
|
||||
backgroundColor: rate[0] < rate[rate.length-1] ? 'rgb(36, 175, 47)' : 'rgb(218, 56, 24)',
|
||||
data: rate,
|
||||
borderWidth: 2,
|
||||
const chart = ChartJSImage()
|
||||
.chart({
|
||||
type: 'line',
|
||||
options: {
|
||||
title: {
|
||||
display: true,
|
||||
text: `${from_currency} / ${conv_currency}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
scales: {
|
||||
xAxes: [
|
||||
{
|
||||
scaleLabel: {
|
||||
display: true,
|
||||
labelString: 'Day'
|
||||
},
|
||||
},
|
||||
],
|
||||
yAxes: [
|
||||
{
|
||||
stacked: false,
|
||||
scaleLabel: {
|
||||
display: true,
|
||||
labelString: 'Rate'
|
||||
scales: [
|
||||
{
|
||||
yAxes: [
|
||||
{
|
||||
ticks: {
|
||||
beginAtZero: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}).width(1000).height(1000);
|
||||
],
|
||||
},
|
||||
data: {
|
||||
labels: date,
|
||||
datasets: [
|
||||
{
|
||||
borderColor:
|
||||
rate[0] < rate[rate.length - 1]
|
||||
? 'rgb(24, 218, 39)'
|
||||
: 'rgb(243, 85, 50)',
|
||||
backgroundColor:
|
||||
rate[0] < rate[rate.length - 1]
|
||||
? 'rgb(36, 175, 47)'
|
||||
: 'rgb(218, 56, 24)',
|
||||
data: rate,
|
||||
borderWidth: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
scales: {
|
||||
xAxes: [
|
||||
{
|
||||
scaleLabel: {
|
||||
display: true,
|
||||
labelString: 'Day',
|
||||
},
|
||||
},
|
||||
],
|
||||
yAxes: [
|
||||
{
|
||||
stacked: false,
|
||||
scaleLabel: {
|
||||
display: true,
|
||||
labelString: 'Rate',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
.width(1000)
|
||||
.height(1000);
|
||||
|
||||
logger.debug(chart.toURL());
|
||||
|
||||
return chart.toURL();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saving a graph to a file
|
||||
* @param {String} url - URL (or Buffer) to the chart
|
||||
|
@ -95,18 +106,21 @@ async function gen_chart(from_currency, conv_currency, start_date, end_date) {
|
|||
*/
|
||||
function save_chart(url, filename) {
|
||||
if (!fs.existsSync('../charts')) fs.mkdirSync('../charts');
|
||||
if (!url.startsWith('https://')) throw new Error('The passed parameter is not a URL');
|
||||
if (!url.startsWith('https://'))
|
||||
throw new Error('The passed parameter is not a URL');
|
||||
|
||||
logger.info(`The schedule has been saved. The path of the graph 'chart/${filename}'`);
|
||||
axios({url, responseType: 'stream',}).then(
|
||||
response =>
|
||||
logger.info(
|
||||
`The schedule has been saved. The path of the graph 'chart/${filename}'`,
|
||||
);
|
||||
axios({ url, responseType: 'stream' }).then(
|
||||
(response) =>
|
||||
new Promise((resolve, reject) => {
|
||||
response.data
|
||||
.pipe(fs.createWriteStream(`../charts/${filename}`))
|
||||
.on('finish', () => resolve())
|
||||
.on('error', e => reject(e));
|
||||
})
|
||||
.on('error', (e) => reject(e));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { gen_chart, save_chart };
|
||||
module.exports = { gen_chart, save_chart };
|
||||
|
|
|
@ -12,7 +12,8 @@ function main() {
|
|||
|
||||
for (const file of files) {
|
||||
try {
|
||||
const fileSize = fs.statSync(`../charts/${file}`)['size'] / 1024;
|
||||
const fileSize =
|
||||
fs.statSync(`../charts/${file}`)['size'] / 1024;
|
||||
folderSize += fileSize;
|
||||
|
||||
if (folderSize > config['currency']['chart']['max_size']) {
|
||||
|
@ -20,7 +21,9 @@ function main() {
|
|||
fs.unlinkSync(`../charts/${files[i]}`);
|
||||
}
|
||||
}
|
||||
} catch { return; }
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -29,4 +32,4 @@ schedule.scheduleJob('9 20 * * *', async function () {
|
|||
main();
|
||||
});
|
||||
|
||||
main();
|
||||
main();
|
||||
|
|
Loading…
Add table
Reference in a new issue