github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/node/node_stats_tracker.go (about) 1 /* 2 * Copyright (C) 2022 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 node 19 20 import ( 21 "github.com/pkg/errors" 22 23 "github.com/mysteriumnetwork/node/identity" 24 ) 25 26 var errIdentityNotFound = errors.New("identity not found") 27 28 // MonitoringAgentStatuses a object represent a [service_type][status]amount of statuses for each service type. 29 type MonitoringAgentStatuses map[string]map[string]int 30 31 // ProviderStatuses should return provider statuses from monitoring agent 32 type ProviderStatuses func(providerID string) (MonitoringAgentStatuses, error) 33 34 // ProviderSessionsList should return provider sessions list 35 type ProviderSessionsList func(id identity.Identity, rangeTime string) ([]SessionItem, error) 36 37 // ProviderTransferredData should return total traffic served by the provider during a period of time 38 type ProviderTransferredData func(id identity.Identity, rangeTime string) (TransferredData, error) 39 40 // ProviderSessionsCount should return provider sessions count 41 type ProviderSessionsCount func(id identity.Identity, rangeTime string) (SessionsCount, error) 42 43 // ProviderConsumersCount should return unique consumers count 44 type ProviderConsumersCount func(id identity.Identity, rangeTime string) (ConsumersCount, error) 45 46 // ProviderEarningsSeries should return earnings data series metrics 47 type ProviderEarningsSeries func(id identity.Identity, rangeTime string) (EarningsSeries, error) 48 49 // ProviderSessionsSeries should return sessions data series metrics 50 type ProviderSessionsSeries func(id identity.Identity, rangeTime string) (SessionsSeries, error) 51 52 // ProviderTransferredDataSeries should return transferred bytes data series metrics 53 type ProviderTransferredDataSeries func(id identity.Identity, rangeTime string) (TransferredDataSeries, error) 54 55 // ProviderServiceEarnings should return earnings by service type 56 type ProviderServiceEarnings func(id identity.Identity) (EarningsPerService, error) 57 58 // ProviderActivityStats should return provider activity stats 59 type ProviderActivityStats func(id identity.Identity) (ActivityStats, error) 60 61 // ProviderQuality should return provider quality 62 type ProviderQuality func(id identity.Identity) (QualityInfo, error) 63 64 type currentIdentity interface { 65 GetUnlockedIdentity() (identity.Identity, bool) 66 } 67 68 // StatsTracker tracks metrics for service 69 type StatsTracker struct { 70 providerStatuses ProviderStatuses 71 providerSessionsList ProviderSessionsList 72 providerTransferredData ProviderTransferredData 73 providerSessionsCount ProviderSessionsCount 74 providerConsumersCount ProviderConsumersCount 75 providerEarningsSeries ProviderEarningsSeries 76 providerSessionsSeries ProviderSessionsSeries 77 providerTransferredDataSeries ProviderTransferredDataSeries 78 providerActivityStats ProviderActivityStats 79 providerQuality ProviderQuality 80 providerServiceEarnings ProviderServiceEarnings 81 currentIdentity currentIdentity 82 } 83 84 // NewNodeStatsTracker constructor 85 func NewNodeStatsTracker( 86 providerStatuses ProviderStatuses, 87 providerSessions ProviderSessionsList, 88 providerTransferredData ProviderTransferredData, 89 providerSessionsCount ProviderSessionsCount, 90 providerConsumersCount ProviderConsumersCount, 91 providerEarningsSeries ProviderEarningsSeries, 92 providerSessionsSeries ProviderSessionsSeries, 93 providerTransferredDataSeries ProviderTransferredDataSeries, 94 providerActivityStats ProviderActivityStats, 95 providerQuality ProviderQuality, 96 providerServiceEarnings ProviderServiceEarnings, 97 currentIdentity currentIdentity, 98 ) *StatsTracker { 99 mat := &StatsTracker{ 100 providerStatuses: providerStatuses, 101 providerSessionsList: providerSessions, 102 providerTransferredData: providerTransferredData, 103 providerSessionsCount: providerSessionsCount, 104 providerConsumersCount: providerConsumersCount, 105 providerEarningsSeries: providerEarningsSeries, 106 providerSessionsSeries: providerSessionsSeries, 107 providerTransferredDataSeries: providerTransferredDataSeries, 108 providerActivityStats: providerActivityStats, 109 providerQuality: providerQuality, 110 providerServiceEarnings: providerServiceEarnings, 111 currentIdentity: currentIdentity, 112 } 113 114 return mat 115 } 116 117 // Statuses retrieves and resolved monitoring status from quality oracle 118 func (m *StatsTracker) Statuses() (MonitoringAgentStatuses, error) { 119 id, ok := m.currentIdentity.GetUnlockedIdentity() 120 if ok { 121 return m.providerStatuses(id.Address) 122 } 123 124 return MonitoringAgentStatuses{}, errIdentityNotFound 125 } 126 127 // SessionItem represents information about session monitoring metrics. 128 type SessionItem struct { 129 ID string `json:"id"` 130 ConsumerCountry string `json:"consumer_country"` 131 ServiceType string `json:"service_type"` 132 Duration int64 `json:"duration"` 133 StartedAt int64 `json:"started_at"` 134 Earning string `json:"earning"` 135 Transferred int64 `json:"transferred"` 136 } 137 138 // TransferredData represent information about total traffic served by the provider during a period of time 139 type TransferredData struct { 140 Bytes int `json:"transferred_data_bytes"` 141 } 142 143 // SessionsCount represent a information about number of sessions during a period of time 144 type SessionsCount struct { 145 Count int `json:"count"` 146 } 147 148 // ConsumersCount represent a information about number of consumers served during a period of time 149 type ConsumersCount struct { 150 Count int `json:"count"` 151 } 152 153 // SeriesItem represents a general data series item 154 type SeriesItem struct { 155 Value string `json:"value"` 156 Timestamp int64 `json:"timestamp"` 157 } 158 159 // EarningsSeries represents data series metrics about earnings during a time 160 type EarningsSeries struct { 161 Data []SeriesItem `json:"data"` 162 } 163 164 // SessionsSeries represents data series metrics about started sessions during a time 165 type SessionsSeries struct { 166 Data []SeriesItem `json:"data"` 167 } 168 169 // TransferredDataSeries represents data series metrics about transferred bytes during a time 170 type TransferredDataSeries struct { 171 Data []SeriesItem `json:"data"` 172 } 173 174 // ActivityStats represent a provider activity stats 175 type ActivityStats struct { 176 Online float64 `json:"online_percent"` 177 Active float64 `json:"active_percent"` 178 } 179 180 // QualityInfo represents a provider quality info. 181 type QualityInfo struct { 182 Quality float64 `json:"quality"` 183 } 184 185 // EarningsPerService represents information about earnings per service 186 type EarningsPerService struct { 187 EarningsPublic string `json:"public"` 188 EarningsVPN string `json:"data_transfer"` 189 EarningsScraping string `json:"scraping"` 190 EarningsDVPN string `json:"dvpn"` 191 EarningsTotal string `json:"total"` 192 TotalEarningsPublic string `json:"total_public"` 193 TotalEarningsVPN string `json:"total_data_transfer"` 194 TotalEarningsScraping string `json:"total_scraping"` 195 TotalEarningsDVPN string `json:"total_dvpn"` 196 } 197 198 // Sessions retrieves and resolved monitoring status from quality oracle 199 func (m *StatsTracker) Sessions(rangeTime string) ([]SessionItem, error) { 200 id, ok := m.currentIdentity.GetUnlockedIdentity() 201 if ok { 202 return m.providerSessionsList(id, rangeTime) 203 } 204 205 return []SessionItem{}, errIdentityNotFound 206 } 207 208 // TransferredData retrieves and resolved total traffic served by the provider 209 func (m *StatsTracker) TransferredData(rangeTime string) (TransferredData, error) { 210 id, ok := m.currentIdentity.GetUnlockedIdentity() 211 if ok { 212 return m.providerTransferredData(id, rangeTime) 213 } 214 215 return TransferredData{}, errIdentityNotFound 216 } 217 218 // SessionsCount retrieves and resolved numbers of sessions 219 func (m *StatsTracker) SessionsCount(rangeTime string) (SessionsCount, error) { 220 id, ok := m.currentIdentity.GetUnlockedIdentity() 221 if ok { 222 return m.providerSessionsCount(id, rangeTime) 223 } 224 225 return SessionsCount{}, errIdentityNotFound 226 } 227 228 // ConsumersCount retrieves and resolved numbers of consumers server during period of time 229 func (m *StatsTracker) ConsumersCount(rangeTime string) (ConsumersCount, error) { 230 id, ok := m.currentIdentity.GetUnlockedIdentity() 231 if ok { 232 return m.providerConsumersCount(id, rangeTime) 233 } 234 235 return ConsumersCount{}, errIdentityNotFound 236 } 237 238 // EarningsSeries retrieves and resolved earnings data series metrics during a time range 239 func (m *StatsTracker) EarningsSeries(rangeTime string) (EarningsSeries, error) { 240 id, ok := m.currentIdentity.GetUnlockedIdentity() 241 if ok { 242 return m.providerEarningsSeries(id, rangeTime) 243 } 244 245 return EarningsSeries{}, errIdentityNotFound 246 } 247 248 // SessionsSeries retrieves and resolved sessions data series metrics during a time range 249 func (m *StatsTracker) SessionsSeries(rangeTime string) (SessionsSeries, error) { 250 id, ok := m.currentIdentity.GetUnlockedIdentity() 251 if ok { 252 return m.providerSessionsSeries(id, rangeTime) 253 } 254 255 return SessionsSeries{}, errIdentityNotFound 256 } 257 258 // TransferredDataSeries retrieves and resolved transferred bytes data series metrics during a time range 259 func (m *StatsTracker) TransferredDataSeries(rangeTime string) (TransferredDataSeries, error) { 260 id, ok := m.currentIdentity.GetUnlockedIdentity() 261 if ok { 262 return m.providerTransferredDataSeries(id, rangeTime) 263 } 264 265 return TransferredDataSeries{}, errIdentityNotFound 266 } 267 268 // ProviderQuality retrieves and resolved provider quality 269 func (m *StatsTracker) ProviderQuality() (QualityInfo, error) { 270 271 id, ok := m.currentIdentity.GetUnlockedIdentity() 272 if ok { 273 return m.providerQuality(id) 274 } 275 return QualityInfo{}, errIdentityNotFound 276 } 277 278 // ProviderActivityStats retrieves and resolved provider activity stats 279 func (m *StatsTracker) ProviderActivityStats() (ActivityStats, error) { 280 id, ok := m.currentIdentity.GetUnlockedIdentity() 281 if ok { 282 return m.providerActivityStats(id) 283 } 284 285 return ActivityStats{}, errIdentityNotFound 286 } 287 288 // EarningsPerService retrieves and resolved earnings per service type 289 func (m *StatsTracker) EarningsPerService() (EarningsPerService, error) { 290 id, ok := m.currentIdentity.GetUnlockedIdentity() 291 if ok { 292 return m.providerServiceEarnings(id) 293 } 294 295 return EarningsPerService{}, errIdentityNotFound 296 }