From dfeca48c52f135b043af43839a75f2909bf5d361 Mon Sep 17 00:00:00 2001 From: Marcelo Rodrigo Date: Wed, 18 Dec 2024 19:30:52 +0100 Subject: [PATCH] Using local indexes (#242) * Using local indexes * yarn install --- .github/workflows/update-indexes.yml | 31 ++++++++++++++ assets/indicadores.json | 11 +++-- package.json | 4 +- store/investment.ts | 30 +++----------- update-indexes.js | 62 ++++++++++++++++++++++++++++ yarn.lock | 19 +++++++++ 6 files changed, 128 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/update-indexes.yml create mode 100644 update-indexes.js diff --git a/.github/workflows/update-indexes.yml b/.github/workflows/update-indexes.yml new file mode 100644 index 0000000..cd2fcd9 --- /dev/null +++ b/.github/workflows/update-indexes.yml @@ -0,0 +1,31 @@ +name: Update Indexes +on: + workflow_dispatch: + schedule: + - cron: '0 */12 * * *' + +jobs: + update-indicadores: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + check-latest: true + cache: 'yarn' + - name: Install dependencies + run: yarn install --immutable --immutable-cache --check-cache + - name: Update indicadores.json + run: yarn update-indexes + - name: Commit and push changes + run: | + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + git add assets/indicadores.json + git commit -m 'Update indicadores.json with latest values' + git push + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/assets/indicadores.json b/assets/indicadores.json index b799ab1..cd3fa84 100644 --- a/assets/indicadores.json +++ b/assets/indicadores.json @@ -2,16 +2,19 @@ "poupanca": { "title": "Poupança", "description": "Depósitos de poupança a partir de 04.05.2012 - Rentabilidade no período", - "unitDisplay": "% a.m." + "unitDisplay": "% a.m.", + "value": 0.6291 }, "selic": { "title": "SELIC", "description": "Taxa de juros - Selic anualizada base 252", - "unitDisplay": "% a.a." + "unitDisplay": "% a.a.", + "value": 12.25 }, "cdi": { "title": "CDI", "description": "Taxa de juros - CDI anualizada base 252", - "unitDisplay": "% a.a." + "unitDisplay": "% a.a.", + "value": 10.340000000000002 } -} +} \ No newline at end of file diff --git a/package.json b/package.json index a25e645..d901628 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare", - "test": "vitest" + "test": "vitest", + "update-indexes": "node update-indexes.js" }, "devDependencies": { "@nuxt/devtools": "latest", @@ -23,6 +24,7 @@ "dependencies": { "@mdi/font": "^7.4.47", "@pinia/nuxt": "^0.5.1", + "axios": "^1.7.9", "nuxt-schema-org": "^4.0.4" } } diff --git a/store/investment.ts b/store/investment.ts index b5e9415..c651299 100644 --- a/store/investment.ts +++ b/store/investment.ts @@ -1,4 +1,5 @@ import { defineStore } from 'pinia' +import indicadores from '~/assets/indicadores.json' export enum PeriodTypes { Days = 'dias', @@ -43,31 +44,12 @@ export const useInvestmentStore = defineStore('investment', { this.selic = newSelic }, initializeStore() { - this.fetchPoupanca() - this.fetchDi() - this.fetchSelic() + this.loadIndexes() }, - fetchPoupanca() { - fetch('https://api.bcb.gov.br/dados/serie/bcdata.sgs.195/dados/ultimos/1?formato=json') - .then((response) => response.json()) - .then((data) => this.poupanca = parseFloat(data[0].valor)) - .catch((error) => console.log(error)); - }, - fetchDi() { - fetch('https://api.bcb.gov.br/dados/serie/bcdata.sgs.4391/dados/ultimos/13?formato=json') - .then((response) => response.json()) - .then((data: { valor: string }[]) => this.di = data - .slice(1) // Ignores the partial value of current month - .map((item: any) => parseFloat(item.valor)) - .reduce((acc, value) => acc + value, 0) - ) - .catch((error) => console.log(error)); - }, - fetchSelic() { - fetch('https://www.bcb.gov.br/api/servico/sitebcb/historicotaxasjuros') - .then((response) => response.json()) - .then((data) => this.selic = data.conteudo[0].MetaSelic) - .catch((error) => console.log(error)) + loadIndexes() { + this.di = indicadores.cdi.value + this.selic = indicadores.selic.value + this.poupanca = indicadores.poupanca.value } } }) diff --git a/update-indexes.js b/update-indexes.js new file mode 100644 index 0000000..1bda29d --- /dev/null +++ b/update-indexes.js @@ -0,0 +1,62 @@ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); + +const filePath = path.join(__dirname, 'assets', 'indicadores.json'); +const indicadores = require(filePath); + +async function fetchPoupanca() { + try { + console.log('Fetching Poupanca...'); + const response = await axios.get('https://api.bcb.gov.br/dados/serie/bcdata.sgs.195/dados/ultimos/1?formato=json'); + const value = parseFloat(response.data[0].valor); + console.log('Poupanca value fetched:', value); + return value; + } catch (error) { + console.error('Error fetching Poupanca:', error); + } +} + +async function fetchDi() { + try { + console.log('Fetching DI...'); + const response = await axios.get('https://api.bcb.gov.br/dados/serie/bcdata.sgs.4391/dados/ultimos/13?formato=json'); + const data = response.data.slice(1); // Ignores the partial value of current month + const value = data.map(item => parseFloat(item.valor)).reduce((acc, value) => acc + value, 0); + console.log('DI value fetched:', value); + return value; + } catch (error) { + console.error('Error fetching DI:', error); + } +} + +async function fetchSelic() { + try { + console.log('Fetching Selic...'); + const response = await axios.get('https://www.bcb.gov.br/api/servico/sitebcb/historicotaxasjuros'); + const value = response.data.conteudo[0].MetaSelic; + console.log('Selic value fetched:', value); + return value; + } catch (error) { + console.error('Error fetching Selic:', error); + } +} + +async function updateIndicadores() { + try { + const poupancaValue = await fetchPoupanca(); + const selicValue = await fetchSelic(); + const cdiValue = await fetchDi(); + + indicadores.poupanca.value = poupancaValue; + indicadores.selic.value = selicValue; + indicadores.cdi.value = cdiValue; + + fs.writeFileSync(filePath, JSON.stringify(indicadores, null, 2)); + console.log('indicadores.json updated successfully'); + } catch (error) { + console.error('Error updating indicadores.json:', error); + } +} + +updateIndicadores(); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 49fecf1..e261922 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1670,6 +1670,15 @@ autoprefixer@^10.4.20: picocolors "^1.0.1" postcss-value-parser "^4.2.0" +axios@^1.7.9: + version "1.7.9" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.9.tgz#d7d071380c132a24accda1b2cfc1535b79ec650a" + integrity sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw== + dependencies: + follow-redirects "^1.15.6" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + b4a@^1.6.4: version "1.6.7" resolved "https://registry.yarnpkg.com/b4a/-/b4a-1.6.7.tgz#a99587d4ebbfbd5a6e3b21bdb5d5fa385767abe4" @@ -2600,6 +2609,11 @@ flatted@^3.3.2: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.2.tgz#adba1448a9841bec72b42c532ea23dbbedef1a27" integrity sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA== +follow-redirects@^1.15.6: + version "1.15.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1" + integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ== + foreground-child@^3.1.0: version "3.3.0" resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-3.3.0.tgz#0ac8644c06e431439f8561db8ecf29a7b5519c77" @@ -4262,6 +4276,11 @@ protocols@^2.0.0, protocols@^2.0.1: resolved "https://registry.yarnpkg.com/protocols/-/protocols-2.0.1.tgz#8f155da3fc0f32644e83c5782c8e8212ccf70a86" integrity sha512-/XJ368cyBJ7fzLMwLKv1e4vLxOju2MNAIokcr7meSaNcVbWz/CPcW22cP04mwxOErdA5mwjA8Q6w/cdAQxVn7Q== +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + punycode@^2.3.1: version "2.3.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5"