Пример #1
0
        public async void ExportHopkinsData()
        {
            Status = "Exporting Johns Hopkins Data";
            StorageFolder exportFolder = null;

            if (StorageApplicationPermissions.FutureAccessList.ContainsItem("ExportFolder"))
            {
                exportFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("ExportFolder");
            }
            else
            {
                var picker = new Windows.Storage.Pickers.FolderPicker();
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
                picker.FileTypeFilter.Add("*");

                exportFolder = await picker.PickSingleFolderAsync();

                if (exportFolder != null)
                {
                    StorageApplicationPermissions.FutureAccessList.AddOrReplace("ExportFolder", exportFolder);
                }
            }

            DateTime startDate = new DateTime(2020, 3, 1);
            Dictionary <string, List <CalculatedValue> > listOfFiles = null;

            listOfFiles = ProcessedJhStateData?.FirstOrDefault()?.OutputFiles;

            if (exportFolder == null || listOfFiles == null)
            {
                return;
            }

            foreach (KeyValuePair <string, List <CalculatedValue> > kvp in listOfFiles)
            {
                DateTime    currentDate = startDate;
                DateTime    latestDate  = listOfFiles.Values.FirstOrDefault().Max(cv => cv.Date);
                StorageFile csvFile     = await exportFolder?.CreateFileAsync(kvp.Key + ".csv", Windows.Storage.CreationCollisionOption.ReplaceExisting);

                using (CsvFileWriter dataWriter = new CsvFileWriter(await csvFile.OpenStreamForWriteAsync()))
                {
                    CsvRow headerRow = new CsvRow();
                    headerRow.Add("Date");
                    foreach (ProcessedJhState ps in ProcessedJhStateData)
                    {
                        headerRow.Add(ps.CoreStateData.State.Code.ToString());
                    }
                    dataWriter.WriteRow(headerRow);

                    while (currentDate.Date <= latestDate)
                    {
                        CsvRow nextRow = new CsvRow();
                        nextRow.Add(currentDate.Date.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture));
                        foreach (ProcessedJhState ps in ProcessedJhStateData)
                        {
                            var calcVals = ps.OutputFiles[kvp.Key].FirstOrDefault(calcv => calcv.Date.Date == currentDate.Date);
                            if (calcVals == null)
                            {
                                nextRow.Add("0");
                            }
                            else
                            {
                                nextRow.Add(calcVals.Value.ToString("F3", CultureInfo.InvariantCulture));
                            }
                        }

                        dataWriter.WriteRow(nextRow);
                        currentDate = currentDate + new TimeSpan(1, 0, 0, 0);
                    }
                }
            }

            Status = "Done with JH Data";
        }