Esempio n. 1
0
        private async Task <HttpStatusCode> SyncCovidDataAsync(DateTimeOffset?lastCheck)
        {
            var regionRequest = new CommitRequest()
            {
                Since = lastCheck,
                Path  = "dati-regioni"
            };

            var provinceRequest = new CommitRequest()
            {
                Since = lastCheck,
                Path  = "dati-province"
            };

            var fieldTableRequest = new CommitRequest()
            {
                Since = lastCheck,
                Path  = "dati-andamento-covid19-italia.md"
            };

            var commitsRegion = await gitHubRepo.Commit.GetAll("pcm-dpc", "COVID-19", regionRequest);

            var commitsProvince = await gitHubRepo.Commit.GetAll("pcm-dpc", "COVID-19", provinceRequest);

            var commitsFieldTable = await gitHubRepo.Commit.GetAll("pcm-dpc", "COVID-19", fieldTableRequest);

            var    csvRegion   = new List <string>();
            var    csvProvince = new List <string>();
            string?fieldTable  = null;
            var    regexReg    = new Regex("dati-regioni/.*-\\d*\\.csv");
            var    regexProv   = new Regex("dati-province/.*-\\d*\\.csv");

            foreach (var item in commitsRegion)
            {
                var commit = await gitHubRepo.Commit.Get("pcm-dpc", "COVID-19", item.Sha);

                foreach (var file in commit.Files)
                {
                    if (regexReg.IsMatch(file.Filename))
                    {
                        csvRegion.Add(file.RawUrl);
                    }
                }
            }

            foreach (var item in commitsProvince)
            {
                var commit = await gitHubRepo.Commit.Get("pcm-dpc", "COVID-19", item.Sha);

                foreach (var file in commit.Files)
                {
                    if (regexProv.IsMatch(file.Filename))
                    {
                        csvProvince.Add(file.RawUrl);
                    }
                }
            }

            foreach (var item in commitsFieldTable)
            {
                var commit = await gitHubRepo.Commit.Get("pcm-dpc", "COVID-19", item.Sha);

                foreach (var file in commit.Files)
                {
                    if (file.Filename == "dati-andamento-covid19-italia.md")
                    {
                        fieldTable = file.BlobUrl;
                    }
                }
            }

            var regionService   = covid19Services["regionData"];
            var provinceService = covid19Services["provinceData"];

            var response = HttpStatusCode.OK;

            if (csvRegion.Count > 0)
            {
                foreach (string csv in csvRegion)
                {
                    var trentinoList = new List <ItemRegioneDto>(2);
                    await foreach (var record in dataCollector.GetDataAsync <ItemRegioneDto>(csv, SerializerKind.CSV))
                    {
                        var value = new ItemRegione();
                        if (new string[] { "04", "21", "22" }.Contains(record.CodiceRegione))
                        {
                            trentinoList.Add(record);
                            continue;
                        }
                        else
                        {
                            value.Data = NormalizeDate(DateTime.Parse(record.Data, CultureInfo.InvariantCulture));
                            (_,
                             _,
                             value.CodiceRegione,
                             _,
                             _,
                             _,
                             value.RicoveratiConSintomi,
                             value.TerapiaIntensiva,
                             value.TotaleOspedalizzati,
                             value.IsolamentoDomiciliare,
                             value.TotalePositivi,
                             value.VariazioneTotalePositivi,
                             value.NuoviPositivi,
                             value.DimessiGuariti,
                             value.Deceduti,
                             value.TotaleCasi,
                             value.Tamponi,
                             value.CasiTestati,
                             value.NoteIt,
                             value.NoteEn) = record;

                            value.Id           = $"{value.Data}_{value.CodiceRegione}";
                            value.PartitionKey = value.Data;
                        }

                        response = await regionService.UpdateDataAsync(value, value.PartitionKey);

                        if ((int)response > 299)
                        {
                            return(response);
                        }
                    }

                    var trentino = MergeTrentino(trentinoList.ToArray());
                    if (trentino != null)
                    {
                        response = await regionService.UpdateDataAsync(trentino, trentino.PartitionKey);

                        if ((int)response > 299)
                        {
                            return(response);
                        }
                    }
                }
            }

            if (csvProvince.Count > 0)
            {
                foreach (string item in csvProvince)
                {
                    await foreach (var record in dataCollector.GetDataAsync <ItemProvinciaDto>(item, SerializerKind.CSV))
                    {
                        var value = new ItemProvincia
                        {
                            Data = NormalizeDate(DateTime.Parse(record.Data, CultureInfo.InvariantCulture))
                        };
                        (_,
                         _,
                         value.CodiceRegione,
                         _,
                         value.CodiceProvincia,
                         _,
                         _,
                         _,
                         _,
                         value.TotaleCasi,
                         value.NoteIt,
                         value.NoteEn) = record;

                        value.Id           = $"{value.Data}_{value.CodiceRegione}_{value.CodiceProvincia}";
                        value.PartitionKey = value.Data;

                        if (new string[] { "04", "21", "22" }.Contains(record.CodiceRegione))
                        {
                            value.CodiceRegione = "04";
                        }

                        response = await provinceService.UpdateDataAsync(value, value.PartitionKey);

                        if ((int)response > 299)
                        {
                            return(response);
                        }
                    }
                }
            }

            if (fieldTable != null)
            {
                response = HttpStatusCode.OK;
            }

            return(response);
        }