Skip to content

Commit

Permalink
fix: include font to fix missing text in graph (#1), fix filepath for…
Browse files Browse the repository at this point in the history
… lambda filesystem
  • Loading branch information
Vilsepi committed Jan 21, 2025
1 parent f16cef7 commit 40ad98a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ coverage
dist
node_modules
secrets.yml

*.ttf

graph.png
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"private": true,
"scripts": {
"build": "tsc",
"build": "tsc && cp Roboto-Regular.ttf dist/telegram/",
"clean": "rm -rf dist",
"lint": "eslint .",
"postinstall": "npx husky install",
Expand Down
18 changes: 17 additions & 1 deletion src/telegram/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ChartJSNodeCanvas, ChartCallback } from 'chartjs-node-canvas';
import { ChartConfiguration } from 'chart.js';
import { TimeSegment } from '../datasource/datasourceTypes';
import { promises as fs } from 'fs';
import { registerFont } from 'canvas';
import path from 'path';

// Used kWh per euro (100 eurocents) per one use
const sauna_cost_multiplier = 10/100;
Expand Down Expand Up @@ -121,10 +123,24 @@ export const renderGraph = async (today: TimeSegment, tomorrow: TimeSegment): Pr
const chartCallback: ChartCallback = (ChartJS) => {
ChartJS.defaults.responsive = true;
ChartJS.defaults.maintainAspectRatio = false;
ChartJS.defaults.font.family = 'Roboto';
ChartJS.defaults.font.size = 14;
};

// Lambda runtime does not contain any fonts so we need to manually include one
registerFont(path.resolve(__dirname, 'Roboto-Regular.ttf'), { family: 'Roboto' });

const isInAws = !!process.env.LAMBDA_TASK_ROOT;
let filePath = '';
if (isInAws) {
filePath = '/tmp/elektro-scrooge-price-graph.png';
}
else {
filePath = 'graph.png';
}

const chartJSNodeCanvas = new ChartJSNodeCanvas({ width, height, chartCallback });
const buffer = await chartJSNodeCanvas.renderToBuffer(configuration);
const filePath = '/tmp/elektro-scrooge-price-graph.png';
await fs.writeFile(filePath, buffer, 'base64');
return filePath;
}

0 comments on commit 40ad98a

Please sign in to comment.