price pipeline

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-13 12:33:39 +02:00
commit d9581d7f7e
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
16 changed files with 790 additions and 10 deletions

View file

@ -0,0 +1,41 @@
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
}