public ValidationResult GetValidationResult(CommandOption option, ValidationContext context) { if (!option.HasValue()) { return(ValidationResult.Success !); } var filePath = option.Value(); if (filePath != null && !MustBeValidFilePath.ValidatePath(filePath)) { return(new ValidationResult($"The path '{option.Value()}' is invalid")); } return(ValidationResult.Success !); }
private int ExportDataToCSV() { if (PathToPriceFile != "@") { Configuration.PathToPriceFile = PathToPriceFile !; } _logger.Information("Starting export of price data to CSV file..."); var dbContext = AppServiceProvider.Instance.GetRequiredService <AlphaVantageContext>(); if (String.IsNullOrEmpty(Configuration.PathToPriceFile)) { var reportingYear = dbContext.HistoricalData .Select(hd => hd.Timestamp.Year) .FirstOrDefault(); if (reportingYear == 0) { reportingYear = DateTime.Today.Year; } Configuration.PathToPriceFile = $"{reportingYear} {ExportToFileOptionAttribute.DefaultPathStub}"; } if (!MustBeValidFilePath.ValidatePath(Configuration.PathToPriceFile)) { _logger.Error($"Export file '{Configuration.PathToPriceFile}' is invalid"); return(-1); } _logger.Information("Retrieving market days..."); var marketDays = dbContext.HistoricalData .Select(hd => hd.Timestamp) .Distinct() .OrderByDescending(x => x) .ToList(); var fracDays = marketDays.Count / 10; _logger.Information("Retrieving CUSIPs..."); var headers = dbContext.Securities .Where(s => s.Reportable) .Select(s => s.Cusip) .Distinct() .OrderBy(x => x) .ToList(); var prevHighs = new Dictionary <string, decimal>(); try { var outputFile = File.CreateText(Configuration.PathToPriceFile); outputFile.Write("Date"); headers.ForEach(h => outputFile.Write($",{h}")); outputFile.WriteLine(); var daysOutput = 0; var chunksOutput = 0; foreach (var marketDay in marketDays) { var highs = dbContext.HistoricalData .Where(hd => hd.Timestamp == marketDay) .Select(hd => new { hd.SecurityInfo.Cusip, hd.High }) .ToDictionary(x => x.Cusip, x => x.High); outputFile.Write(marketDay.ToShortDateString()); foreach (var curHeader in headers) { decimal toWrite = 0; if (highs.ContainsKey(curHeader)) { toWrite = highs[curHeader]; if (prevHighs.ContainsKey(curHeader)) { prevHighs[curHeader] = highs[curHeader]; } else { prevHighs.Add(curHeader, highs[curHeader]); } } else { if (prevHighs.ContainsKey(curHeader)) { toWrite = prevHighs[curHeader]; } } outputFile.Write($", {toWrite}"); } outputFile.WriteLine(); daysOutput++; if (daysOutput % fracDays == 0) { chunksOutput++; _logger.Information($"{10 * chunksOutput}% written..."); } prevHighs = highs; } outputFile.Flush(); outputFile.Close(); } catch (Exception e) { _logger.Error(e.Message); return(-1); } _logger.Information("Export completed"); return(0); }