public void SaveJsonFile(string path, string json, bool format = false)
        {
            // TODO: [TESTS] (FileSystemHelper.SaveJsonFile) Add tests
            if (format)
            {
                if (!_jsonHelper.TryDeserializeObject(json, out object parsed))
                {
                    throw new Exception("Invalid JSON");
                }

                json = _jsonHelper.SerializeObject(parsed, true);
            }

            if (_file.Exists(path))
            {
                _file.Delete(path);
            }

            var directoryPath = _path.GetDirectoryName(path);

            if (!_directory.Exists(directoryPath))
            {
                _directory.CreateDirectory(directoryPath);
            }

            _file.WriteAllText(path, json);
        }
Exemplo n.º 2
0
        // Output methods
        private async Task WritePoints(IEnumerable <LineProtocolPoint> points)
        {
            // TODO: [TESTS] (CsvMetricOutput.WritePoints) Add tests
            var filePath      = GenerateCsvFilePath();
            var fileDirectory = _path.GetDirectoryName(filePath);

            if (!_directory.Exists(fileDirectory))
            {
                _directory.CreateDirectory(fileDirectory);
            }

            var fileExists = _file.Exists(filePath);

            await using var writer = new StreamWriter(filePath, true, Encoding.UTF8);
            await using var csv    = new CsvWriter(writer, CultureInfo.InvariantCulture);
            csv.Configuration.RegisterClassMap <LineProtocolPointMap>();
            csv.Configuration.ShouldQuote = (s, context) => true;

            if (!fileExists)
            {
                csv.WriteHeader <LineProtocolPoint>();
                await csv.NextRecordAsync();
            }

            foreach (var point in points)
            {
                csv.WriteRecord(point);
                await csv.NextRecordAsync();
            }
        }
Exemplo n.º 3
0
    // Internal methods
    private bool ShiftConfigFiles()
    {
        try
        {
            var previousConfig = $"{CoreConfig.ConfigFile}.previous";

            if (_file.Exists(previousConfig))
            {
                _file.Delete(previousConfig);
            }

            _file.Copy(CoreConfig.ConfigFile, previousConfig);
            _file.Delete(CoreConfig.ConfigFile);

            return(true);
        }
        catch (Exception ex)
        {
            _logger.LogUnexpectedException(ex);
            return(false);
        }
    }
Exemplo n.º 4
0
        private string GetResponseBody(DevHttpResponse response)
        {
            // TODO: [TESTS] (DevGrafanaHttpClient.GetResponseBody) Add tests
            if (response.ResponseType == DevHttpClientResponseType.File)
            {
                if (string.IsNullOrWhiteSpace(response.GeneratedResponseBody))
                {
                    if (!_file.Exists(response.FilePath))
                    {
                        // TODO: [EX] (DevGrafanaHttpClient.GetResponseBody) Throw better exception here
                        _logger.Error("Unable to find response file: {path}", response.FilePath);
                        throw new Exception($"Unable to find response file: {response.FilePath}");
                    }

                    response.GeneratedResponseBody = _file.ReadAllText(response.FilePath);
                    _logger.Trace("Set GeneratedResponseBody using {path}", response.FilePath);
                }

                return(response.GeneratedResponseBody);
            }

            // TODO: [COMPLETE] (DevGrafanaHttpClient.GetResponseBody) Complete me
            return(string.Empty);
        }