2023-03-17 17:02:18 +01:00
package store
import (
2023-05-15 20:14:52 +02:00
"fmt"
2023-03-17 17:02:18 +01:00
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type ShareLimitJournal struct {
Model
ShareId int
2023-03-17 17:57:26 +01:00
RxBytes int64
TxBytes int64
2023-03-22 19:10:07 +01:00
Action LimitJournalAction
2023-03-17 17:02:18 +01:00
}
2023-03-23 20:13:59 +01:00
func ( str * Store ) CreateShareLimitJournal ( j * ShareLimitJournal , trx * sqlx . Tx ) ( int , error ) {
stmt , err := trx . Prepare ( "insert into share_limit_journal (share_id, rx_bytes, tx_bytes, action) values ($1, $2, $3, $4) returning id" )
2023-03-17 17:02:18 +01:00
if err != nil {
return 0 , errors . Wrap ( err , "error preparing share_limit_journal insert statement" )
}
var id int
2023-03-17 17:57:26 +01:00
if err := stmt . QueryRow ( j . ShareId , j . RxBytes , j . TxBytes , j . Action ) . Scan ( & id ) ; err != nil {
2023-03-17 17:02:18 +01:00
return 0 , errors . Wrap ( err , "error executing share_limit_journal insert statement" )
}
return id , nil
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) IsShareLimitJournalEmpty ( shrId int , trx * sqlx . Tx ) ( bool , error ) {
2023-03-22 19:10:07 +01:00
count := 0
if err := trx . QueryRowx ( "select count(0) from share_limit_journal where share_id = $1" , shrId ) . Scan ( & count ) ; err != nil {
return false , err
}
return count == 0 , nil
}
2023-03-23 20:13:59 +01:00
func ( str * Store ) FindLatestShareLimitJournal ( shrId int , trx * sqlx . Tx ) ( * ShareLimitJournal , error ) {
2023-03-17 17:02:18 +01:00
j := & ShareLimitJournal { }
2023-03-23 20:13:59 +01:00
if err := trx . QueryRowx ( "select * from share_limit_journal where share_id = $1 order by created_at desc limit 1" , shrId ) . StructScan ( j ) ; err != nil {
2023-03-17 17:02:18 +01:00
return nil , errors . Wrap ( err , "error finding share_limit_journal by share_id" )
}
return j , nil
}
2023-03-23 20:13:59 +01:00
2023-05-15 20:14:52 +02:00
func ( str * Store ) FindSelectedLatestShareLimitjournal ( shrIds [ ] int , trx * sqlx . Tx ) ( [ ] * ShareLimitJournal , error ) {
if len ( shrIds ) < 1 {
return nil , nil
}
in := "("
for i := range shrIds {
if i > 0 {
in += ", "
}
in += fmt . Sprintf ( "%d" , shrIds [ i ] )
}
in += ")"
rows , err := trx . Queryx ( "select id, share_id, rx_bytes, tx_bytes, action, created_at, updated_at from share_limit_journal where id in (select max(id) as id from share_limit_journal group by share_id) and share_id in " + in )
if err != nil {
return nil , errors . Wrap ( err , "error selecting all latest share_limit_journal" )
}
var sljs [ ] * ShareLimitJournal
for rows . Next ( ) {
slj := & ShareLimitJournal { }
if err := rows . StructScan ( slj ) ; err != nil {
return nil , errors . Wrap ( err , "error scanning share_limit_journal" )
}
sljs = append ( sljs , slj )
}
return sljs , nil
}
2023-03-23 20:13:59 +01:00
func ( str * Store ) FindAllLatestShareLimitJournal ( trx * sqlx . Tx ) ( [ ] * ShareLimitJournal , error ) {
rows , err := trx . Queryx ( "select id, share_id, rx_bytes, tx_bytes, action, created_at, updated_at from share_limit_journal where id in (select max(id) as id from share_limit_journal group by share_id)" )
if err != nil {
return nil , errors . Wrap ( err , "error selecting all latest share_limit_journal" )
}
var sljs [ ] * ShareLimitJournal
for rows . Next ( ) {
slj := & ShareLimitJournal { }
if err := rows . StructScan ( slj ) ; err != nil {
return nil , errors . Wrap ( err , "error scanning share_limit_journal" )
}
sljs = append ( sljs , slj )
}
return sljs , nil
}
func ( str * Store ) DeleteShareLimitJournalForShare ( shrId int , trx * sqlx . Tx ) error {
if _ , err := trx . Exec ( "delete from share_limit_journal where share_id = $1" , shrId ) ; err != nil {
return errors . Wrapf ( err , "error deleting share_limit_journal for '#%d'" , shrId )
}
return nil
}