candles based alerts

This commit is contained in:
yash 2026-02-26 16:02:11 +03:00
parent bec3b7de5b
commit 999f675da9
11 changed files with 316 additions and 15 deletions

View file

@ -0,0 +1,33 @@
package postgresql
import (
"context"
"fmt"
"time"
)
const getLastAlertCheckQuery = `select last_alert_check from alerter_state`
// GetLastAlertCheck returns the persisted time of the last completed alert check.
// Returns zero time if no check has been recorded yet (null in DB).
func (p *Postgresql) GetLastAlertCheck(ctx context.Context) (time.Time, error) {
var t *time.Time
err := p.db.QueryRow(ctx, getLastAlertCheckQuery).Scan(&t)
if err != nil {
return time.Time{}, fmt.Errorf("failed to exec getLastAlertCheckQuery: %w", err)
}
if t == nil {
return time.Time{}, nil
}
return *t, nil
}
const setLastAlertCheckQuery = `update alerter_state set last_alert_check = $1`
func (p *Postgresql) SetLastAlertCheck(ctx context.Context, t time.Time) error {
_, err := p.db.Exec(ctx, setLastAlertCheckQuery, t)
if err != nil {
return fmt.Errorf("failed to exec setLastAlertCheckQuery: %w", err)
}
return nil
}

View file

@ -1,3 +1,4 @@
drop table if exists alerter_state;
drop table if exists alert;
drop type alert_condition;
drop table if exists instrument;

View file

@ -34,3 +34,10 @@ insert into instrument (base_currency_id, quoted_currency_id) values
((select id from currency where symbol = 'BTC'), (select id from currency where symbol = 'USDT')),
((select id from currency where symbol = 'ETH'), (select id from currency where symbol = 'USDT')),
((select id from currency where symbol = 'SOL'), (select id from currency where symbol = 'USDT'));
create table if not exists alerter_state (
last_alert_check timestamptz
);
-- single row; UPDATE always succeeds without upsert logic
insert into alerter_state(last_alert_check) values (null);

View file

@ -2,6 +2,7 @@ package repository
import (
"context"
"time"
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
"github.com/shopspring/decimal"
@ -22,4 +23,9 @@ type Storage interface {
DeleteAlert(ctx context.Context, id entities.AlertID) error
DisableAlert(ctx context.Context, id entities.AlertID) error
UpdateAlertPrice(ctx context.Context, id entities.AlertID, price decimal.Decimal) error
// GetLastAlertCheck returns the time of the last completed alert check.
// Returns zero time if no check has been recorded yet.
GetLastAlertCheck(ctx context.Context) (time.Time, error)
SetLastAlertCheck(ctx context.Context, t time.Time) error
}