Skip to content

Commit

Permalink
Using local indexes (#242)
Browse files Browse the repository at this point in the history
* Using local indexes

* yarn install
  • Loading branch information
marcelorodrigo authored Dec 18, 2024
1 parent 99a4cc5 commit dfeca48
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 29 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/update-indexes.yml
Original file line number Diff line number Diff line change
@@ -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 }}
11 changes: 7 additions & 4 deletions assets/indicadores.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
30 changes: 6 additions & 24 deletions store/investment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineStore } from 'pinia'
import indicadores from '~/assets/indicadores.json'

export enum PeriodTypes {
Days = 'dias',
Expand Down Expand Up @@ -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
}
}
})
62 changes: 62 additions & 0 deletions update-indexes.js
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit dfeca48

Please sign in to comment.