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

            storage.Database.EnsureCreated();
            var opts = new KongoOptions()
            {
                ApplicationStartedOn = DateTimeOffset.UtcNow
            };

            _processor = new NodeStatisticsProcessor(storage, opts);
            storage.Database.EnsureDeleted();
            Assert.ThrowsAsync <ArgumentException>(() => _processor.ProcessNodeStatistics(value));
        }
        public async Task ProcessValidNodeStatisticStream(string value)
        {
            var storage = new KongoDataStorage($"Data Source={Path.GetRandomFileName()}");

            storage.Database.EnsureCreated();
            var opts = new KongoOptions()
            {
                ApplicationStartedOn = DateTimeOffset.UtcNow
            };

            _processor = new NodeStatisticsProcessor(storage, opts);
            var nodeStats = await _processor.ProcessNodeStatistics(value);

            storage.Database.EnsureDeleted();
            Assert.True(nodeStats != null, "nodeStats == null");
            Assert.True(nodeStats.BlockRecvCnt > 0, $"BlockRecvCnt = {nodeStats.BlockRecvCnt}");
            Assert.True(nodeStats.LastBlockTime != default, $"LastBlockTime = {nodeStats.LastBlockTime}");
        }
Пример #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/node/stats");

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

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

                        if (_opts.Verbose || _opts.VerboseNodeStats)
                        {
                            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 nodeStatistics = await _processor.ProcessNodeStatistics(content);

                        _kongoStatus.PoolState        = nodeStatistics.State;
                        _kongoStatus.CurrentBlockDate = nodeStatistics.LastBlockDate;

                        if (long.TryParse(nodeStatistics.LastBlockHeight, out long blockHeight))
                        {
                            _kongoStatus.CurrentBlockHeight = blockHeight;
                        }
                        else
                        {
                            _kongoStatus.CurrentBlockHeight = 0;
                        }

                        _kongoStatus.LastBlockReceivedAt = nodeStatistics.LastBlockTime.Value;
                        _kongoStatus.PoolUptime          = TimeSpan.FromSeconds(nodeStatistics.Uptime);

                        _sb.Clear();
                        _sb.AppendLine($"NodeStatistics running on {_opts.PoolName}, at: {DateTimeOffset.Now}");
                        _sb.AppendLine();
                        _sb.AppendLine($"BlockRecvCnt: {nodeStatistics.BlockRecvCnt}");
                        _sb.AppendLine($"LastBlockDate: {nodeStatistics.LastBlockDate}");
                        _sb.AppendLine($"LastBlockFees: {nodeStatistics.LastBlockFees}");
                        _sb.AppendLine($"LastBlockHash: {nodeStatistics.LastBlockHash}");
                        _sb.AppendLine($"LastBlockHeight: {nodeStatistics.LastBlockHeight}");
                        _sb.AppendLine($"lastBlockSum: {nodeStatistics.lastBlockSum}");
                        _sb.AppendLine($"LastBlockTime: {nodeStatistics.LastBlockTime}");
                        _sb.AppendLine($"LastBlockTx: {nodeStatistics.LastBlockTx}");
                        _sb.AppendLine($"TxRecvCnt: {nodeStatistics.TxRecvCnt}");
                        _sb.AppendLine($"Uptime: {nodeStatistics.Uptime}");
                        _sb.AppendLine();

                        _logger.LogInformation(_sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.Message);
                        if (ex.InnerException != null)
                        {
                            _logger.LogError(ex.InnerException, ex.InnerException.Message);
                        }
                    }
                }

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