Skip to content

Commit

Permalink
add loglevel instead of using console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
SathyaBhat committed Dec 19, 2023
1 parent de332f1 commit 2fec074
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"cron": "^2.4.3",
"express": "^4.18.2",
"loglevel": "^1.8.1",
"prom-client": "^14.2.0",
"source-map-support": "^0.5.21",
"ynab": "^1.32.0"
Expand Down
10 changes: 4 additions & 6 deletions src/collectors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import log from 'loglevel';
import {Gauge, Registry} from "prom-client";
import {Account} from "ynab";

export class YNABCollector {
accountBalances: Account[] = [];

public async collectAccountBalanceMetrics(register: Registry) {
console.log('Collecting account balance metrics..');
log.debug('Collecting account balance metrics..');

const accountLabels = ['account_name', 'type', 'closed'];
const accountClearedBalanceGauge = new Gauge({
Expand All @@ -14,7 +15,7 @@ export class YNABCollector {
help: "Account Cleared Balance amounts",
labelNames: accountLabels,
collect: async () => {
console.debug(`Collecting Cleared Balance for ${this.accountBalances.length} accounts`);
log.debug(`Collecting Cleared Balance for ${this.accountBalances.length} accounts`);
this.accountBalances.forEach(a => {
accountClearedBalanceGauge.labels({account_name: a.name, type: a.type, closed: String(a.closed)}).set(a.cleared_balance / 1000);
});
Expand All @@ -27,14 +28,11 @@ export class YNABCollector {
registers: [register],
labelNames: accountLabels,
collect: async () => {
console.debug(`Collecting Uncleared Balance for ${this.accountBalances.length} accounts`);
log.debug(`Collecting Uncleared Balance for ${this.accountBalances.length} accounts`);
this.accountBalances.forEach(a => {
accountUnClearedBalanceGauge.labels({account_name: a.name, type: a.type, closed: String(a.closed)}).set(a.uncleared_balance / 1000);
});
}

});

}

}
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {Registry} from "prom-client";
import 'source-map-support/register';
import {YnabAPI} from "./api";
import {YNABCollector} from "./collectors";
import {scheduledAccountBalanceUpdate} from "./jobs/accounts";;

import {scheduledAccountBalanceUpdate} from "./jobs/accounts";
import log, {LogLevelDesc} from 'loglevel';

async function main() {
const ynab = new YnabAPI();
Expand All @@ -22,7 +22,7 @@ async function main() {
cronTime: "*/15 * * * *",
onTick: async () => {
ynabCollector.accountBalances = (await scheduledAccountBalanceUpdate(ynab)).accounts;
console.log(`${ynabCollector.accountBalances.length} accounts refreshed`);
log.info(`${ynabCollector.accountBalances.length} accounts refreshed`);
},
start: true,
runOnInit: true
Expand All @@ -35,17 +35,19 @@ async function main() {

app.get('/metrics', async (req: Request, res: Response) => {
res.setHeader('Content-Type', register.contentType);
console.debug('getting metrics');
log.debug('getting metrics');
const results = await register.metrics();
res.send(results);
});

app.listen(port, () => {
console.log(`🔊 Publishing metrics on port ${port}`);
log.info(`🔊 Publishing metrics on port ${port}`);
});
}

if (require.main === module) {
console.log('Starting YNAB Exporter 💰💸');
const logLevel = (process.env.LOG_LEVEL) as LogLevelDesc || 'info';
log.setLevel(logLevel);
log.info('Starting YNAB Exporter 💰💸');
main();
}
7 changes: 4 additions & 3 deletions src/jobs/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {AccountsResponseData, TransactionsResponse} from "ynab";
import {YnabAPI} from "../api";
import log from "loglevel";

export type ynabTransactionResponse = (TransactionsResponse & {rateLimit: string | null;}) | undefined;

export async function scheduledAccountBalanceUpdate(ynab: YnabAPI): Promise<AccountsResponseData> {
console.log(`Starting scheduled account balance update at ${new Date().toLocaleString()} ...`);
log.info(`Starting scheduled account balance update at ${new Date().toLocaleString()} ...`);
const accounts = await ynab.client.accounts.getAccounts(ynab.budgetId);
console.log(`Fetched balances for ${accounts.data.accounts.length} accounts.`);
console.log(`Rate limit: ${accounts.rateLimit}`);
log.info(`Fetched balances for ${accounts.data.accounts.length} accounts.`);
log.info(`Rate limit: ${accounts.rateLimit}`);
return accounts.data;
}

0 comments on commit 2fec074

Please sign in to comment.