Files
sentinela-go/internal/db/market.go
Rainbow f7c8b446bf feat: Sentinela v0.2.0 — Brazilian Financial Data API in Go
- 20 Go source files, single 16MB binary
- SQLite + FTS5 full-text search (pure Go, no CGO)
- BCB integration: Selic, CDI, IPCA, USD/BRL, EUR/BRL
- CVM integration: 2,524 companies from registry
- Fiber v2 REST API with 42 handlers
- Auto-seeds on first run (~5s for BCB + CVM)
- Token bucket rate limiter, optional API key auth
- Periodic sync scheduler (configurable)
- Graceful shutdown, structured logging (slog)
- All endpoints tested with real data
2026-02-10 11:15:54 -03:00

216 lines
5.7 KiB
Go

package db
import (
"database/sql"
"fmt"
)
type SelicRecord struct {
ID int64 `json:"id"`
Date string `json:"date"`
DailyRate float64 `json:"daily_rate"`
AnnualRate *float64 `json:"annual_rate,omitempty"`
TargetRate *float64 `json:"target_rate,omitempty"`
}
type CDIRecord struct {
ID int64 `json:"id"`
Date string `json:"date"`
DailyRate float64 `json:"daily_rate"`
AnnualRate *float64 `json:"annual_rate,omitempty"`
}
type IPCARecord struct {
ID int64 `json:"id"`
Date string `json:"date"`
MonthlyRate float64 `json:"monthly_rate"`
Accumulated12m *float64 `json:"accumulated_12m,omitempty"`
}
type FXRecord struct {
ID int64 `json:"id"`
Date string `json:"date"`
Pair string `json:"pair"`
Rate float64 `json:"rate"`
}
// Selic
func (d *DB) InsertSelic(date string, daily float64, annual, target *float64) error {
_, err := d.Conn.Exec(`INSERT OR IGNORE INTO selic_history (date, daily_rate, annual_rate, target_rate) VALUES (?,?,?,?)`,
date, daily, annual, target)
return err
}
func (d *DB) ListSelic(limit int, from, to string) ([]SelicRecord, error) {
where, args := marketWhere(from, to)
query := fmt.Sprintf("SELECT id, date, daily_rate, annual_rate, target_rate FROM selic_history %s ORDER BY date DESC LIMIT ?", where)
args = append(args, limit)
rows, err := d.Conn.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []SelicRecord
for rows.Next() {
var r SelicRecord
rows.Scan(&r.ID, &r.Date, &r.DailyRate, &r.AnnualRate, &r.TargetRate)
out = append(out, r)
}
return out, nil
}
func (d *DB) CurrentSelic() (*SelicRecord, error) {
r := &SelicRecord{}
err := d.Conn.QueryRow("SELECT id, date, daily_rate, annual_rate, target_rate FROM selic_history ORDER BY date DESC LIMIT 1").
Scan(&r.ID, &r.Date, &r.DailyRate, &r.AnnualRate, &r.TargetRate)
if err == sql.ErrNoRows {
return nil, nil
}
return r, err
}
// CDI
func (d *DB) InsertCDI(date string, daily float64, annual *float64) error {
_, err := d.Conn.Exec(`INSERT OR IGNORE INTO cdi_history (date, daily_rate, annual_rate) VALUES (?,?,?)`,
date, daily, annual)
return err
}
func (d *DB) ListCDI(limit int, from, to string) ([]CDIRecord, error) {
where, args := marketWhere(from, to)
query := fmt.Sprintf("SELECT id, date, daily_rate, annual_rate FROM cdi_history %s ORDER BY date DESC LIMIT ?", where)
args = append(args, limit)
rows, err := d.Conn.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []CDIRecord
for rows.Next() {
var r CDIRecord
rows.Scan(&r.ID, &r.Date, &r.DailyRate, &r.AnnualRate)
out = append(out, r)
}
return out, nil
}
func (d *DB) CurrentCDI() (*CDIRecord, error) {
r := &CDIRecord{}
err := d.Conn.QueryRow("SELECT id, date, daily_rate, annual_rate FROM cdi_history ORDER BY date DESC LIMIT 1").
Scan(&r.ID, &r.Date, &r.DailyRate, &r.AnnualRate)
if err == sql.ErrNoRows {
return nil, nil
}
return r, err
}
// IPCA
func (d *DB) InsertIPCA(date string, monthly float64, acc12m *float64) error {
_, err := d.Conn.Exec(`INSERT OR IGNORE INTO ipca_history (date, monthly_rate, accumulated_12m) VALUES (?,?,?)`,
date, monthly, acc12m)
return err
}
func (d *DB) ListIPCA(limit int, from, to string) ([]IPCARecord, error) {
where, args := marketWhere(from, to)
query := fmt.Sprintf("SELECT id, date, monthly_rate, accumulated_12m FROM ipca_history %s ORDER BY date DESC LIMIT ?", where)
args = append(args, limit)
rows, err := d.Conn.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []IPCARecord
for rows.Next() {
var r IPCARecord
rows.Scan(&r.ID, &r.Date, &r.MonthlyRate, &r.Accumulated12m)
out = append(out, r)
}
return out, nil
}
func (d *DB) CurrentIPCA() (*IPCARecord, error) {
r := &IPCARecord{}
err := d.Conn.QueryRow("SELECT id, date, monthly_rate, accumulated_12m FROM ipca_history ORDER BY date DESC LIMIT 1").
Scan(&r.ID, &r.Date, &r.MonthlyRate, &r.Accumulated12m)
if err == sql.ErrNoRows {
return nil, nil
}
return r, err
}
// FX
func (d *DB) InsertFX(date, pair string, rate float64) error {
_, err := d.Conn.Exec(`INSERT OR IGNORE INTO fx_rates (date, pair, rate) VALUES (?,?,?)`, date, pair, rate)
return err
}
func (d *DB) ListFX(limit int, pair, from, to string) ([]FXRecord, error) {
where := "WHERE 1=1"
args := []any{}
if pair != "" {
where += " AND pair = ?"
args = append(args, pair)
}
if from != "" {
where += " AND date >= ?"
args = append(args, from)
}
if to != "" {
where += " AND date <= ?"
args = append(args, to)
}
query := fmt.Sprintf("SELECT id, date, pair, rate FROM fx_rates %s ORDER BY date DESC LIMIT ?", where)
args = append(args, limit)
rows, err := d.Conn.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []FXRecord
for rows.Next() {
var r FXRecord
rows.Scan(&r.ID, &r.Date, &r.Pair, &r.Rate)
out = append(out, r)
}
return out, nil
}
func (d *DB) CurrentFX() ([]FXRecord, error) {
rows, err := d.Conn.Query(`SELECT DISTINCT pair FROM fx_rates`)
if err != nil {
return nil, err
}
defer rows.Close()
var pairs []string
for rows.Next() {
var p string
rows.Scan(&p)
pairs = append(pairs, p)
}
var out []FXRecord
for _, p := range pairs {
var r FXRecord
err := d.Conn.QueryRow("SELECT id, date, pair, rate FROM fx_rates WHERE pair = ? ORDER BY date DESC LIMIT 1", p).
Scan(&r.ID, &r.Date, &r.Pair, &r.Rate)
if err == nil {
out = append(out, r)
}
}
return out, nil
}
func marketWhere(from, to string) (string, []any) {
where := "WHERE 1=1"
args := []any{}
if from != "" {
where += " AND date >= ?"
args = append(args, from)
}
if to != "" {
where += " AND date <= ?"
args = append(args, to)
}
return where, args
}