account/internal/store/price_history.go
GnomeZworc d9581d7f7e
price pipeline
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-06-13 12:33:39 +02:00

41 lines
1.3 KiB
Go

package store
import (
"context"
"time"
)
func (s *Store) UpsertPrice(ctx context.Context, instrumentID int32, fetchedAt time.Time, prix float64) error {
_, err := s.pool.Exec(ctx,
`INSERT INTO price_history (instrument_id, fetched_at, prix)
VALUES ($1, $2, $3)
ON CONFLICT (instrument_id, fetched_at) DO UPDATE SET prix = EXCLUDED.prix`,
instrumentID, fetchedAt, prix)
return err
}
// GetLastPrice retourne le dernier prix connu pour un instrument.
func (s *Store) GetLastPrice(ctx context.Context, instrumentID int32) (float64, time.Time, error) {
var prix float64
var fetchedAt time.Time
err := s.pool.QueryRow(ctx,
`SELECT prix, fetched_at FROM price_history
WHERE instrument_id = $1
ORDER BY fetched_at DESC LIMIT 1`,
instrumentID).Scan(&prix, &fetchedAt)
return prix, fetchedAt, err
}
// CleanPastDays supprime pour chaque jour passé toutes les entrées sauf la dernière par instrument.
func (s *Store) CleanPastDays(ctx context.Context) error {
_, err := s.pool.Exec(ctx, `
DELETE FROM price_history ph
WHERE ph.fetched_at::date < CURRENT_DATE
AND ph.fetched_at < (
SELECT MAX(ph2.fetched_at)
FROM price_history ph2
WHERE ph2.instrument_id = ph.instrument_id
AND ph2.fetched_at::date = ph.fetched_at::date
)`)
return err
}