LimitJournalAction -> LimitAction; LimitScope (#606)

This commit is contained in:
Michael Quigley 2024-05-14 13:24:48 -04:00
parent 4d67598acb
commit 75d58e84e4
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 50 additions and 42 deletions

View File

@ -71,7 +71,7 @@ func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if alj.Action == store.LimitAction {
if alj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -98,7 +98,7 @@ func (a *Agent) CanCreateShare(acctId, envId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if alj.Action == store.LimitAction {
if alj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -110,7 +110,7 @@ func (a *Agent) CanCreateShare(acctId, envId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if elj.Action == store.LimitAction {
if elj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -150,7 +150,7 @@ func (a *Agent) CanAccessShare(shrId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if slj.Action == store.LimitAction {
if slj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -166,7 +166,7 @@ func (a *Agent) CanAccessShare(shrId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if elj.Action == store.LimitAction {
if elj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -183,7 +183,7 @@ func (a *Agent) CanAccessShare(shrId int, trx *sqlx.Tx) (bool, error) {
if err != nil {
return false, err
}
if alj.Action == store.LimitAction {
if alj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
@ -257,7 +257,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var enforcedAt time.Time
if empty, err := a.str.IsAccountLimitJournalEmpty(int(u.AccountId), trx); err == nil && !empty {
if latest, err := a.str.FindLatestAccountLimitJournal(int(u.AccountId), trx); err == nil {
enforced = latest.Action == store.LimitAction
enforced = latest.Action == store.LimitLimitAction
enforcedAt = latest.UpdatedAt
}
}
@ -267,7 +267,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
AccountId: int(u.AccountId),
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.LimitAction,
Action: store.LimitLimitAction,
}, trx)
if err != nil {
return err
@ -294,7 +294,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var warnedAt time.Time
if empty, err := a.str.IsAccountLimitJournalEmpty(int(u.AccountId), trx); err == nil && !empty {
if latest, err := a.str.FindLatestAccountLimitJournal(int(u.AccountId), trx); err == nil {
warned = latest.Action == store.WarningAction || latest.Action == store.LimitAction
warned = latest.Action == store.WarningLimitAction || latest.Action == store.LimitLimitAction
warnedAt = latest.UpdatedAt
}
}
@ -304,7 +304,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
AccountId: int(u.AccountId),
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.WarningAction,
Action: store.WarningLimitAction,
}, trx)
if err != nil {
return err
@ -333,7 +333,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var enforcedAt time.Time
if empty, err := a.str.IsEnvironmentLimitJournalEmpty(int(u.EnvironmentId), trx); err == nil && !empty {
if latest, err := a.str.FindLatestEnvironmentLimitJournal(int(u.EnvironmentId), trx); err == nil {
enforced = latest.Action == store.LimitAction
enforced = latest.Action == store.LimitLimitAction
enforcedAt = latest.UpdatedAt
}
}
@ -343,7 +343,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
EnvironmentId: int(u.EnvironmentId),
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.LimitAction,
Action: store.LimitLimitAction,
}, trx)
if err != nil {
return err
@ -370,7 +370,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var warnedAt time.Time
if empty, err := a.str.IsEnvironmentLimitJournalEmpty(int(u.EnvironmentId), trx); err == nil && !empty {
if latest, err := a.str.FindLatestEnvironmentLimitJournal(int(u.EnvironmentId), trx); err == nil {
warned = latest.Action == store.WarningAction || latest.Action == store.LimitAction
warned = latest.Action == store.WarningLimitAction || latest.Action == store.LimitLimitAction
warnedAt = latest.UpdatedAt
}
}
@ -380,7 +380,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
EnvironmentId: int(u.EnvironmentId),
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.WarningAction,
Action: store.WarningLimitAction,
}, trx)
if err != nil {
return err
@ -414,7 +414,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var enforcedAt time.Time
if empty, err := a.str.IsShareLimitJournalEmpty(shr.Id, trx); err == nil && !empty {
if latest, err := a.str.FindLatestShareLimitJournal(shr.Id, trx); err == nil {
enforced = latest.Action == store.LimitAction
enforced = latest.Action == store.LimitLimitAction
enforcedAt = latest.UpdatedAt
}
}
@ -424,7 +424,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
ShareId: shr.Id,
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.LimitAction,
Action: store.LimitLimitAction,
}, trx)
if err != nil {
return err
@ -452,7 +452,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
var warnedAt time.Time
if empty, err := a.str.IsShareLimitJournalEmpty(shr.Id, trx); err == nil && !empty {
if latest, err := a.str.FindLatestShareLimitJournal(shr.Id, trx); err == nil {
warned = latest.Action == store.WarningAction || latest.Action == store.LimitAction
warned = latest.Action == store.WarningLimitAction || latest.Action == store.LimitLimitAction
warnedAt = latest.UpdatedAt
}
}
@ -462,7 +462,7 @@ func (a *Agent) enforce(u *metrics.Usage) error {
ShareId: shr.Id,
RxBytes: rxBytes,
TxBytes: txBytes,
Action: store.WarningAction,
Action: store.WarningLimitAction,
}, trx)
if err != nil {
return err
@ -509,10 +509,10 @@ func (a *Agent) relax() error {
if sljs, err := a.str.FindAllLatestShareLimitJournal(trx); err == nil {
for _, slj := range sljs {
if shr, err := a.str.GetShare(slj.ShareId, trx); err == nil {
if slj.Action == store.WarningAction || slj.Action == store.LimitAction {
if slj.Action == store.WarningLimitAction || slj.Action == store.LimitLimitAction {
if enforce, warning, rxBytes, txBytes, err := a.checkShareLimit(shr.Token); err == nil {
if !enforce && !warning {
if slj.Action == store.LimitAction {
if slj.Action == store.LimitLimitAction {
// run relax actions for share
for _, action := range a.shrRelaxActions {
if err := action.HandleShare(shr, rxBytes, txBytes, a.cfg.Bandwidth.PerShare, trx); err != nil {
@ -545,10 +545,10 @@ func (a *Agent) relax() error {
if eljs, err := a.str.FindAllLatestEnvironmentLimitJournal(trx); err == nil {
for _, elj := range eljs {
if env, err := a.str.GetEnvironment(elj.EnvironmentId, trx); err == nil {
if elj.Action == store.WarningAction || elj.Action == store.LimitAction {
if elj.Action == store.WarningLimitAction || elj.Action == store.LimitLimitAction {
if enforce, warning, rxBytes, txBytes, err := a.checkEnvironmentLimit(int64(elj.EnvironmentId)); err == nil {
if !enforce && !warning {
if elj.Action == store.LimitAction {
if elj.Action == store.LimitLimitAction {
// run relax actions for environment
for _, action := range a.envRelaxActions {
if err := action.HandleEnvironment(env, rxBytes, txBytes, a.cfg.Bandwidth.PerEnvironment, trx); err != nil {
@ -581,10 +581,10 @@ func (a *Agent) relax() error {
if aljs, err := a.str.FindAllLatestAccountLimitJournal(trx); err == nil {
for _, alj := range aljs {
if acct, err := a.str.GetAccount(alj.AccountId, trx); err == nil {
if alj.Action == store.WarningAction || alj.Action == store.LimitAction {
if alj.Action == store.WarningLimitAction || alj.Action == store.LimitLimitAction {
if enforce, warning, rxBytes, txBytes, err := a.checkAccountLimit(int64(alj.AccountId)); err == nil {
if !enforce && !warning {
if alj.Action == store.LimitAction {
if alj.Action == store.LimitLimitAction {
// run relax actions for account
for _, action := range a.acctRelaxActions {
if err := action.HandleAccount(acct, rxBytes, txBytes, a.cfg.Bandwidth.PerAccount, trx); err != nil {

View File

@ -127,7 +127,7 @@ func (h *overviewHandler) isAccountLimited(principal *rest_model_zrok.Principal,
return false, err
}
}
return alj != nil && alj.Action == store.LimitAction, nil
return alj != nil && alj.Action == store.LimitLimitAction, nil
}
type sharesLimitedMap struct {
@ -145,7 +145,7 @@ func newSharesLimitedMap(shrs []*store.Share, trx *sqlx.Tx) (*sharesLimitedMap,
}
slm := &sharesLimitedMap{v: make(map[int]struct{})}
for i := range shrsLimited {
if shrsLimited[i].Action == store.LimitAction {
if shrsLimited[i].Action == store.LimitLimitAction {
slm.v[shrsLimited[i].ShareId] = struct{}{}
}
}
@ -172,7 +172,7 @@ func newEnvironmentsLimitedMap(envs []*store.Environment, trx *sqlx.Tx) (*enviro
}
elm := &environmentsLimitedMap{v: make(map[int]struct{})}
for i := range envsLimited {
if envsLimited[i].Action == store.LimitAction {
if envsLimited[i].Action == store.LimitLimitAction {
elm.v[envsLimited[i].EnvironmentId] = struct{}{}
}
}

View File

@ -10,7 +10,7 @@ type AccountLimitJournal struct {
AccountId int
RxBytes int64
TxBytes int64
Action LimitJournalAction
Action LimitAction
}
func (str *Store) CreateAccountLimitJournal(j *AccountLimitJournal, trx *sqlx.Tx) (int, error) {

View File

@ -21,7 +21,7 @@ func TestAccountLimitJournal(t *testing.T) {
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: WarningAction}, trx)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId, RxBytes: 1024, TxBytes: 2048, Action: WarningLimitAction}, trx)
assert.Nil(t, err)
aljEmpty, err = str.IsAccountLimitJournalEmpty(acctId, trx)
@ -33,9 +33,9 @@ func TestAccountLimitJournal(t *testing.T) {
assert.NotNil(t, latestAlj)
assert.Equal(t, int64(1024), latestAlj.RxBytes)
assert.Equal(t, int64(2048), latestAlj.TxBytes)
assert.Equal(t, WarningAction, latestAlj.Action)
assert.Equal(t, WarningLimitAction, latestAlj.Action)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId, RxBytes: 2048, TxBytes: 4096, Action: LimitAction}, trx)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId, RxBytes: 2048, TxBytes: 4096, Action: LimitLimitAction}, trx)
assert.Nil(t, err)
latestAlj, err = str.FindLatestAccountLimitJournal(acctId, trx)
@ -43,7 +43,7 @@ func TestAccountLimitJournal(t *testing.T) {
assert.NotNil(t, latestAlj)
assert.Equal(t, int64(2048), latestAlj.RxBytes)
assert.Equal(t, int64(4096), latestAlj.TxBytes)
assert.Equal(t, LimitAction, latestAlj.Action)
assert.Equal(t, LimitLimitAction, latestAlj.Action)
}
func TestFindAllLatestAccountLimitJournal(t *testing.T) {
@ -58,17 +58,17 @@ func TestFindAllLatestAccountLimitJournal(t *testing.T) {
acctId1, err := str.CreateAccount(&Account{Email: "nobody@nowehere.com", Salt: "salt1", Password: "password1", Token: "token1", Limitless: false, Deleted: false}, trx)
assert.Nil(t, err)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: WarningAction}, trx)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: WarningLimitAction}, trx)
assert.Nil(t, err)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: ClearAction}, trx)
_, err = str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: ClearLimitAction}, trx)
assert.Nil(t, err)
aljId13, err := str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: LimitAction}, trx)
aljId13, err := str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId1, RxBytes: 2048, TxBytes: 4096, Action: LimitLimitAction}, trx)
assert.Nil(t, err)
acctId2, err := str.CreateAccount(&Account{Email: "someone@somewhere.com", Salt: "salt2", Password: "password2", Token: "token2", Limitless: false, Deleted: false}, trx)
assert.Nil(t, err)
aljId21, err := str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId2, RxBytes: 2048, TxBytes: 4096, Action: WarningAction}, trx)
aljId21, err := str.CreateAccountLimitJournal(&AccountLimitJournal{AccountId: acctId2, RxBytes: 2048, TxBytes: 4096, Action: WarningLimitAction}, trx)
assert.Nil(t, err)
aljs, err := str.FindAllLatestAccountLimitJournal(trx)

View File

@ -11,7 +11,7 @@ type EnvironmentLimitJournal struct {
EnvironmentId int
RxBytes int64
TxBytes int64
Action LimitJournalAction
Action LimitAction
}
func (str *Store) CreateEnvironmentLimitJournal(j *EnvironmentLimitJournal, trx *sqlx.Tx) (int, error) {

View File

@ -1,11 +1,19 @@
package store
type LimitJournalAction string
type LimitAction string
const (
LimitAction LimitJournalAction = "limit"
WarningAction LimitJournalAction = "warning"
ClearAction LimitJournalAction = "clear"
LimitLimitAction LimitAction = "limit"
WarningLimitAction LimitAction = "warning"
ClearLimitAction LimitAction = "clear"
)
type LimitScope string
const (
AccountLimitScope LimitScope = "account"
EnvironmentLimitScope LimitScope = "environment"
ShareLimitScope LimitScope = "share"
)
type PermissionMode string

View File

@ -11,7 +11,7 @@ type ShareLimitJournal struct {
ShareId int
RxBytes int64
TxBytes int64
Action LimitJournalAction
Action LimitAction
}
func (str *Store) CreateShareLimitJournal(j *ShareLimitJournal, trx *sqlx.Tx) (int, error) {