github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/mobile/mysterium/sessions.go (about) 1 /* 2 * Copyright (C) 2018 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 mysterium 19 20 import ( 21 "encoding/json" 22 "math/big" 23 "time" 24 25 "github.com/mysteriumnetwork/node/consumer/session" 26 ) 27 28 // SessionStorage provides access to session history 29 type SessionStorage interface { 30 List(*session.Filter) ([]session.History, error) 31 } 32 33 // SessionFilter allows to filter by time slice 34 /* 35 * arguments will be attempted to be parsed by time.RFC3339 36 * 37 * @see https://golang.org/pkg/time/ 38 * 39 * @param StartedFrom - e.g. "2006-01-02T15:04:05Z" or null if undetermined 40 * @param StartedTo - e.g. "2006-04-02T15:04:05Z" or null if undetermined 41 */ 42 type SessionFilter struct { 43 StartedFrom string 44 StartedTo string 45 } 46 47 // ListConsumerSessions list consumer sessions 48 func (mb *MobileNode) ListConsumerSessions(filter *SessionFilter) ([]byte, error) { 49 d := session.DirectionConsumed 50 51 f := &session.Filter{ 52 Direction: &d, 53 } 54 55 if len(filter.StartedFrom) > 0 { 56 from, err := time.Parse(time.RFC3339, filter.StartedFrom) 57 if err != nil { 58 return nil, err 59 } 60 f.StartedFrom = &from 61 } 62 63 if len(filter.StartedTo) > 0 { 64 to, err := time.Parse(time.RFC3339, filter.StartedTo) 65 if err != nil { 66 return nil, err 67 } 68 f.StartedTo = &to 69 } 70 71 sessions, err := mb.sessionStorage.List(f) 72 if err != nil { 73 return nil, err 74 } 75 76 dtos := make([]SessionDTO, len(sessions)) 77 for i, s := range sessions { 78 dtos[i] = sessionDTO(s) 79 } 80 return json.Marshal(dtos) 81 } 82 83 // SessionDTO mobile session dto 84 type SessionDTO struct { 85 ID string `json:"id"` 86 Direction string `json:"direction"` 87 ConsumerID string `json:"consumer_id"` 88 HermesID string `json:"hermes_id"` 89 ProviderID string `json:"provider_id"` 90 ServiceType string `json:"service_type"` 91 ConsumerCountry string `json:"consumer_country"` 92 ProviderCountry string `json:"provider_country"` 93 CreatedAt string `json:"created_at"` 94 Duration uint64 `json:"duration"` 95 BytesReceived uint64 `json:"bytes_received"` 96 BytesSent uint64 `json:"bytes_sent"` 97 Tokens *big.Int `json:"tokens"` 98 Status string `json:"status"` 99 IPType string `json:"ip_type"` 100 } 101 102 func sessionDTO(se session.History) SessionDTO { 103 return SessionDTO{ 104 ID: string(se.SessionID), 105 Direction: se.Direction, 106 ConsumerID: se.ConsumerID.Address, 107 HermesID: se.HermesID, 108 ProviderID: se.ProviderID.Address, 109 ServiceType: se.ServiceType, 110 ConsumerCountry: se.ConsumerCountry, 111 ProviderCountry: se.ProviderCountry, 112 CreatedAt: se.Started.Format(time.RFC3339), 113 BytesReceived: se.DataReceived, 114 BytesSent: se.DataSent, 115 Duration: uint64(se.GetDuration().Seconds()), 116 Tokens: se.Tokens, 117 Status: se.Status, 118 IPType: se.IPType, 119 } 120 }