working out the hitches in the limit journals (#273)

This commit is contained in:
Michael Quigley 2023-03-22 13:09:21 -04:00
parent d0dd04a141
commit b103195f88
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 73 additions and 12 deletions

View File

@ -114,6 +114,12 @@ func (a *Agent) enforce(u *metrics.Usage) error {
if enforce, warning, err := a.checkAccountLimits(u, trx); err == nil { if enforce, warning, err := a.checkAccountLimits(u, trx); err == nil {
if enforce { if enforce {
logrus.Warn("enforcing account limit") logrus.Warn("enforcing account limit")
alje, err := a.str.FindLatestAccountLimitJournal(int(u.AccountId), trx)
if err != nil {
return err
}
} else if warning { } else if warning {
logrus.Warn("reporting account warning") logrus.Warn("reporting account warning")
} else { } else {

View File

@ -19,15 +19,23 @@ func (self *Store) CreateAccountLimitJournal(j *AccountLimitJournal, tx *sqlx.Tx
return 0, errors.Wrap(err, "error preparing account_limit_journal insert statement") return 0, errors.Wrap(err, "error preparing account_limit_journal insert statement")
} }
var id int var id int
if err := stmt.QueryRow(j.AccountId, j.RxBytes, j.TxBytes, j.AccountId).Scan(&id); err != nil { if err := stmt.QueryRow(j.AccountId, j.RxBytes, j.TxBytes, j.Action).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing account_limit_journal insert statement") return 0, errors.Wrap(err, "error executing account_limit_journal insert statement")
} }
return id, nil return id, nil
} }
func (self *Store) FindLatestAccountJournal(acctId int, tx *sqlx.Tx) (*AccountLimitJournal, error) { func (self *Store) IsAccountLimitJournalEmpty(acctId int, tx *sqlx.Tx) (bool, error) {
count := 0
if err := tx.QueryRowx("select count(0) from account_limit_journal where account_id = $1", acctId).Scan(&count); err != nil {
return false, err
}
return count == 0, nil
}
func (self *Store) FindLatestAccountLimitJournal(acctId int, tx *sqlx.Tx) (*AccountLimitJournal, error) {
j := &AccountLimitJournal{} j := &AccountLimitJournal{}
if err := tx.QueryRowx("select * from account_limit_journal where account_id = $1", acctId).StructScan(j); err != nil { if err := tx.QueryRowx("select * from account_limit_journal where account_id = $1 order by created_at desc limit 1", acctId).StructScan(j); err != nil {
return nil, errors.Wrap(err, "error finding account_limit_journal by account_id") return nil, errors.Wrap(err, "error finding account_limit_journal by account_id")
} }
return j, nil return j, nil

View File

@ -0,0 +1,47 @@
package store
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAccountLimitJournal(t *testing.T) {
str, err := Open(&Config{Path: ":memory:", Type: "sqlite3"})
assert.Nil(t, err)
assert.NotNil(t, str)
trx, err := str.Begin()
assert.Nil(t, err)
assert.NotNil(t, trx)
aljEmpty, err := str.IsAccountLimitJournalEmpty(1, trx)
assert.Nil(t, err)
assert.True(t, aljEmpty)
acctId, err := str.CreateAccount(&Account{Email: "nobody@nowehere.com", Salt: "salt", Password: "password", Token: "token", Limitless: false, Deleted: false}, trx)
assert.Nil(t, err)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId, RxBytes: 1024, TxBytes: 2048, Action: "warning"}, trx)
assert.Nil(t, err)
aljEmpty, err = str.IsAccountLimitJournalEmpty(acctId, trx)
assert.Nil(t, err)
assert.False(t, aljEmpty)
latestAlj, err := str.FindLatestAccountLimitJournal(acctId, trx)
assert.Nil(t, err)
assert.NotNil(t, latestAlj)
assert.Equal(t, int64(1024), latestAlj.RxBytes)
assert.Equal(t, int64(2048), latestAlj.TxBytes)
assert.Equal(t, "warning", latestAlj.Action)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId, RxBytes: 2048, TxBytes: 4096, Action: "limit"}, trx)
assert.Nil(t, err)
latestAlj, err = str.FindLatestAccountLimitJournal(acctId, trx)
assert.Nil(t, err)
assert.NotNil(t, latestAlj)
assert.Equal(t, int64(2048), latestAlj.RxBytes)
assert.Equal(t, int64(4096), latestAlj.TxBytes)
assert.Equal(t, "limit", latestAlj.Action)
}

View File

@ -1,31 +1,31 @@
-- +migrate Up -- +migrate Up
create table account_limit_journal ( create table account_limit_journal (
id serial primary key, id integer primary key,
account_id integer references accounts(id), account_id integer references accounts(id),
rx_bytes bigint not null, rx_bytes bigint not null,
tx_bytes bigint not null, tx_bytes bigint not null,
action limit_action_type not null, action limit_action_type not null,
created_at timestamptz not null default(current_timestamp), created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
updated_at timestamptz not null default(current_timestamp) updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now'))
); );
create table environment_limit_journal ( create table environment_limit_journal (
id serial primary key, id integer primary key,
environment_id integer references environments(id), environment_id integer references environments(id),
rx_bytes bigint not null, rx_bytes bigint not null,
tx_bytes bigint not null, tx_bytes bigint not null,
action limit_action_type not null, action limit_action_type not null,
created_at timestamptz not null default(current_timestamp), created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
updated_at timestamptz not null default(current_timestamp) updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now'))
); );
create table share_limit_journal ( create table share_limit_journal (
id serial primary key, id integer primary key,
share_id integer references shares(id), share_id integer references shares(id),
rx_bytes bigint not null, rx_bytes bigint not null,
tx_bytes bigint not null, tx_bytes bigint not null,
action limit_action_type not null, action limit_action_type not null,
created_at timestamptz not null default(current_timestamp), created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
updated_at timestamptz not null default(current_timestamp) updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now'))
); );