@@ -43,7 +43,7 @@ type Repository interface {
4343 CreateASUser (ctx context.Context , user * DBASUser ) (int64 , error )
4444 UpdateAccountMoney (ctx context.Context , id int64 , amount int64 ) (int64 , error )
4545 UpdateASMoney (ctx context.Context , ia addr.IA , amount int64 ) (int64 , error )
46- SearchAssetsForStatistics (ctx context.Context , params * StatisticsQuery ) ([]* DBStat , error )
46+ SearchAssetsForStatistics (ctx context.Context , params * StatisticsQuery ) ([]* DBStat , [] * DBStat , error )
4747 FindUsedReservations (ctx context.Context , params * UsedReservationsQuery ) ([]* UsedReservation , error )
4848 CreateOrUpdateRedemptionDelegations (ctx context.Context , r * RedemptionDelegation ) (int64 , error )
4949 FindRedemptionDelegations (ctx context.Context ) ([]* RedemptionDelegation , error )
@@ -62,6 +62,7 @@ type Repository interface {
6262 AssignAsset (ctx context.Context , assetID int64 , accountIDFrom int64 , accountIDTo int64 ) (int64 , error )
6363 AssignReservation (ctx context.Context , id int64 , accountIDFrom int64 , accountIDTo int64 ) (int64 , error )
6464 RemoveAsset (ctx context.Context , assetID int64 ) error
65+ RegisterAssetEvent (ctx context.Context , a * DBAsset , eventType AssetEventType ) (int64 , error )
6566 TransitionAsset (ctx context.Context , assetID int64 , accountID * int64 , from AssetState , to AssetState ) (* DBAsset , error )
6667}
6768
@@ -548,53 +549,71 @@ func (e *executor) buildUsedReservationsQuery(params *UsedReservationsQuery) (st
548549 return query , args
549550}
550551
551- func (e * executor ) SearchAssetsForStatistics (
552- ctx context.Context ,
553- params * StatisticsQuery ,
554- ) ([]* DBStat , error ) {
552+ func (e * executor ) SearchAssetsForStatistics (ctx context.Context , params * StatisticsQuery ) ([]* DBStat , []* DBStat , error ) {
555553 if e .read == nil {
556- return nil , serrors .New ("No database open" )
554+ return nil , nil , serrors .New ("No database open" )
557555 }
558- stmt , args := e .buildStatisticsQuery (params )
559- rows , err := e .read .QueryContext (ctx , stmt , args ... )
556+ publishStmt , args := e .buildAssetEventsQuery (params , AssetPublished )
557+ publishRows , err := e .read .QueryContext (ctx , publishStmt , args ... )
560558 if err != nil {
561- return nil , serrors .New ("Error looking up assets" , "err" , err , "q" , stmt )
559+ return nil , nil , serrors .New ("Error looking up assets" , "err" , err , "q" , publishStmt )
562560 }
563- defer rows .Close ()
564- var res []* DBStat
565- for rows .Next () {
561+ defer publishRows .Close ()
562+ var publishedAssets []* DBStat
563+ for publishRows .Next () {
566564 a := & DBStat {}
567565 var startsAtString string
568566 var stopsAtString string
569- err = rows .Scan (& a . OwnerId , & a .Bandwidth , & a .Price , & startsAtString , & stopsAtString )
567+ err = publishRows .Scan (& a .Bandwidth , & a .Price , & startsAtString , & stopsAtString )
570568 if err != nil {
571- return nil , serrors .Wrap ("Error reading DB response" , err )
569+ return nil , nil , serrors .Wrap ("Error reading DB response" , err )
572570 }
573571 a .StartsAt , err = time .Parse (time .RFC3339 , startsAtString )
574572 if err != nil {
575- return nil , err
573+ return nil , nil , err
576574 }
577575 a .StopsAt , err = time .Parse (time .RFC3339 , stopsAtString )
578576 if err != nil {
579- return nil , err
577+ return nil , nil , err
580578 }
581- res = append (res , a )
579+ publishedAssets = append (publishedAssets , a )
582580 }
583- return res , nil
581+ boughtStmt , args := e .buildAssetEventsQuery (params , AssetBought )
582+ boughtRows , err := e .read .QueryContext (ctx , boughtStmt , args ... )
583+ if err != nil {
584+ return nil , nil , serrors .New ("Error looking up assets" , "err" , err , "q" , boughtStmt )
585+ }
586+ defer boughtRows .Close ()
587+ var boughtAssets []* DBStat
588+ for boughtRows .Next () {
589+ a := & DBStat {}
590+ var startsAtString string
591+ var stopsAtString string
592+ err = boughtRows .Scan (& a .Bandwidth , & a .Price , & startsAtString , & stopsAtString )
593+ if err != nil {
594+ return nil , nil , serrors .Wrap ("Error reading DB response" , err )
595+ }
596+ a .StartsAt , err = time .Parse (time .RFC3339 , startsAtString )
597+ if err != nil {
598+ return nil , nil , err
599+ }
600+ a .StopsAt , err = time .Parse (time .RFC3339 , stopsAtString )
601+ if err != nil {
602+ return nil , nil , err
603+ }
604+ boughtAssets = append (boughtAssets , a )
605+ }
606+ return publishedAssets , boughtAssets , nil
584607}
585608
586- func (e * executor ) buildStatisticsQuery (params * StatisticsQuery ) (string , []any ) {
609+ func (e * executor ) buildAssetEventsQuery (params * StatisticsQuery , eventType AssetEventType ) (string , []any ) {
587610 var args []any
588611 where := []string {}
589612 query := []string {
590- "SELECT account_id, bandwidth, price, starts_at, stops_at FROM Assets" ,
591- }
592- where = append (where , "(isd_id=?) AND (as_id=?) AND (stops_at > ?) AND (starts_at <= ?)" )
593- args = append (args ,
594- int64 (params .IA .ISD ()),
595- int64 (params .IA .AS ()),
596- params .WindowStart ,
597- params .WindowEnd )
613+ "SELECT bandwidth, price, starts_at, stops_at FROM Asset_Events" ,
614+ }
615+ where = append (where , "(event_type = ?) AND (isd_id=?) AND (as_id=?) AND (stops_at > ?) AND (starts_at <= ?)" )
616+ args = append (args , eventType , int64 (params .IA .ISD ()), int64 (params .IA .AS ()), params .WindowStart , params .WindowEnd )
598617 if params .Ingress != nil {
599618 where = append (where , "(ingress=?)" )
600619 args = append (args , * params .Ingress )
@@ -607,6 +626,21 @@ func (e *executor) buildStatisticsQuery(params *StatisticsQuery) (string, []any)
607626 return strings .Join (query , "\n " ), args
608627}
609628
629+ func (e * executor ) RegisterAssetEvent (ctx context.Context , a * DBAsset , eventType AssetEventType ) (int64 , error ) {
630+ if e .write == nil {
631+ return 0 , serrors .New ("No database open" )
632+ }
633+ var err error
634+ inst := `INSERT INTO Asset_Events (event_type, isd_id, as_id, ingress, egress, bandwidth, starts_at, stops_at, price)
635+ VALUES(?,?,?,?,?,?,?,?)`
636+ res , err := e .write .ExecContext (ctx , inst , eventType , a .IA .ISD (), a .IA .AS (), a .IfIdIngress , a .IfIdEgress , a .Bandwidth ,
637+ a .StartAt .UTC ().Format (time .RFC3339 ), a .StopsAt .UTC ().Format (time .RFC3339 ), a .Price )
638+ if err != nil {
639+ return 0 , err
640+ }
641+ return res .LastInsertId ()
642+ }
643+
610644func (e * executor ) Search (ctx context.Context , params * AssetQuery ) ([]* DBAsset , error ) {
611645 if e .read == nil {
612646 return nil , serrors .New ("No database open" )
0 commit comments