public void InvalidNetworkStatistics_Throws_ArgumentException(string value)
        {
            var storage = new KongoDataStorage($"Data Source={Path.GetRandomFileName()}");

            storage.Database.EnsureCreated();
            _processor = new NetworkStatisticsProcessor(storage);
            storage.Database.EnsureDeleted();
            Assert.ThrowsAsync <ArgumentException>(() => _processor.ProcessNetworkStatistics(value));
        }
        public async Task ProcessValidNetworkStatisticsStreamAllNulls(string value)
        {
            var storage = new KongoDataStorage($"Data Source={Path.GetRandomFileName()}");

            storage.Database.EnsureCreated();
            _processor = new NetworkStatisticsProcessor(storage);
            var networkStats = await _processor.ProcessNetworkStatistics(value);

            storage.Database.EnsureDeleted();
            Assert.True(networkStats != null, "networkStats == null");
            Assert.True(networkStats.LastBlockReceivedAt == default(DateTimeOffset), $"LastBlockReceivedAt = {networkStats.LastFragmentReceivedAt}");
            Assert.True(networkStats.LastFragmentReceivedAt == default(DateTimeOffset), $"LastFragmentReceivedAt = {networkStats.LastFragmentReceivedAt}");
            Assert.True(networkStats.LastGossipReceivedAt == default(DateTimeOffset), $"LastGossipReceivedAt = {networkStats.LastGossipReceivedAt}");
            Assert.True(networkStats.TotalEstablishedConnections > 0, $"TotalEstablishedConnections = {networkStats.TotalEstablishedConnections}");
        }
Пример #3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                using (var client = new HttpClient())
                {
                    var uirScheme = _opts.RestUri.Split(':')[0];
                    var host      = _opts.RestUri.Split(':')[1].Substring(2);
                    var portPart  = _opts.RestUri.Split(':')[2];
                    Int32.TryParse(portPart, out int port);

                    var requestUri = new UriBuilder(uirScheme, host, port, "api/v0/network/stats");

                    try
                    {
                        var response = await _httpClient.GetAsync(requestUri.Uri);

                        string content = await response.Content.ReadAsStringAsync();

                        if (_opts.Verbose || _opts.VerboseNetworkStats)
                        {
                            var currentForeground = Console.ForegroundColor;
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.WriteLine(requestUri.Uri.ToString());
                            Console.WriteLine(response);
                            Console.WriteLine(content);
                            Console.WriteLine();
                            Console.ForegroundColor = currentForeground;
                        }

                        //will throw an exception if not successful
                        response.EnsureSuccessStatusCode();

                        var processedNetworkStatistics = await _processor.ProcessNetworkStatistics(content);

                        _sb.Clear();
                        _sb.AppendLine($"NetworkStatistics running on {_opts.PoolName}, at: {DateTimeOffset.Now}");
                        _sb.AppendLine();
                        _sb.AppendLine($"Total Established Connections: {processedNetworkStatistics.TotalEstablishedConnections}");
                        _sb.AppendLine();
                        _sb.AppendLine("Last Events Timestamps:");
                        _sb.AppendLine($"\t   Block Received: {processedNetworkStatistics.LastBlockReceivedAt}");
                        _sb.AppendLine($"\tFragment Received: {processedNetworkStatistics.LastFragmentReceivedAt}");
                        _sb.AppendLine($"\t  Gossip Received: {processedNetworkStatistics.LastGossipReceivedAt}");
                        _sb.AppendLine();
                        _sb.AppendLine("Past 30 Minutes stats:");
                        _sb.AppendLine($"\t    Nodes Sending Block: {processedNetworkStatistics.BlocksReceivedInPast30Min}");
                        _sb.AppendLine($"\tNodes Sending Fragments: {processedNetworkStatistics.FragmentsReceivedInPast30Min}");
                        _sb.AppendLine($"\t        Nodes Gossiping: {processedNetworkStatistics.GossipReceivedInPast30Min}");
                        _sb.AppendLine();

                        _logger.LogInformation(_sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.Message);
                    }
                }

                await Task.Delay(30000, stoppingToken);
            }
        }