github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/consumer/session/filter.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package session 19 20 import ( 21 "time" 22 23 "github.com/asdine/storm/v3/q" 24 "github.com/mysteriumnetwork/node/identity" 25 ) 26 27 // NewFilter creates instance of filter. 28 func NewFilter() *Filter { 29 return &Filter{} 30 } 31 32 // Filter defines all flags for session filtering in session storage. 33 type Filter struct { 34 StartedFrom *time.Time 35 StartedTo *time.Time 36 Direction *string 37 ConsumerID *identity.Identity 38 HermesID *string 39 ProviderID *identity.Identity 40 ServiceType *string 41 Status *string 42 } 43 44 // SetStartedFrom filters fetched sessions from given time. 45 func (f *Filter) SetStartedFrom(from time.Time) *Filter { 46 from = from.UTC() 47 f.StartedFrom = &from 48 return f 49 } 50 51 // SetStartedTo filters fetched sessions to given time. 52 func (f *Filter) SetStartedTo(to time.Time) *Filter { 53 to = to.UTC() 54 f.StartedTo = &to 55 return f 56 } 57 58 // SetDirection filters fetched sessions by direction. 59 func (f *Filter) SetDirection(direction string) *Filter { 60 f.Direction = &direction 61 return f 62 } 63 64 // SetConsumerID filters fetched sessions by consumer. 65 func (f *Filter) SetConsumerID(id identity.Identity) *Filter { 66 f.ConsumerID = &id 67 return f 68 } 69 70 // SetHermesID filters fetched sessions by hermes. 71 func (f *Filter) SetHermesID(hermesID string) *Filter { 72 f.HermesID = &hermesID 73 return f 74 } 75 76 // SetProviderID filters fetched sessions by provider. 77 func (f *Filter) SetProviderID(id identity.Identity) *Filter { 78 f.ProviderID = &id 79 return f 80 } 81 82 // SetServiceType filters fetched sessions by service type. 83 func (f *Filter) SetServiceType(serviceType string) *Filter { 84 f.ServiceType = &serviceType 85 return f 86 } 87 88 // SetStatus filters fetched sessions by status. 89 func (f *Filter) SetStatus(status string) *Filter { 90 f.Status = &status 91 return f 92 } 93 94 func (f *Filter) toMatcher() q.Matcher { 95 where := make([]q.Matcher, 0) 96 if f.StartedFrom != nil { 97 where = append(where, q.Gte("Started", *f.StartedFrom)) 98 } 99 if f.StartedTo != nil { 100 where = append(where, q.Lte("Started", *f.StartedTo)) 101 } 102 if f.Direction != nil { 103 where = append(where, q.Eq("Direction", *f.Direction)) 104 } 105 if f.ConsumerID != nil { 106 where = append(where, q.Eq("ConsumerID", *f.ConsumerID)) 107 } 108 if f.HermesID != nil { 109 where = append(where, q.Eq("HermesID", *f.HermesID)) 110 } 111 if f.ProviderID != nil { 112 where = append(where, q.Eq("ProviderID", *f.ProviderID)) 113 } 114 if f.ServiceType != nil { 115 where = append(where, q.Eq("ServiceType", *f.ServiceType)) 116 } 117 if f.Status != nil { 118 where = append(where, q.Eq("Status", *f.Status)) 119 } 120 return q.And(where...) 121 }