Example #1
0
        public List <ScorePollV2> GetAreaScorePoll(string idArea)
        {
            var getData      = FinalScorePollCollection.Find(it => it.IdArea == idArea.ToUpper()).ToList();
            var groupByParty = getData.GroupBy(it => it.IdParty);
            var listCurrentScorePollOfArea = new List <ScorePollV2>();

            foreach (var item in groupByParty)
            {
                var getCurrent = item.OrderByDescending(it => it.datePoll).ToList().FirstOrDefault();
                listCurrentScorePollOfArea.Add(getCurrent);
            }
            return(listCurrentScorePollOfArea);
        }
        public async Task UpdateTable4()
        {
            var dataScorePoll            = FinalScorePollCollection.Find(it => true).ToList();
            var dataTable4               = Table4Collection.Find(it => true).ToList();
            var dataScorePollGroupByArea = dataScorePoll.GroupBy(it => it.IdArea).ToList();
            var listT4 = new List <ScoreArea>();

            foreach (var dataPoll in dataScorePollGroupByArea)
            {
                var dataScorePollGroupByParty = dataPoll.GroupBy(it => it.IdParty).ToList();
                foreach (var dataParty in dataScorePollGroupByParty)
                {
                    var getCurentData      = dataParty.OrderByDescending(it => it.datePoll).FirstOrDefault();
                    var getMatchDataTable4 = dataTable4.FirstOrDefault(it => it.IdArea == getCurentData.IdArea &&
                                                                       it.IdParty == getCurentData.IdParty);
                    if (getMatchDataTable4 != null)
                    {
                        getMatchDataTable4.Score          = getCurentData.Score;
                        getMatchDataTable4.Source         = getCurentData.Source;
                        getMatchDataTable4.StatusEdit     = false;
                        getMatchDataTable4.StatusAreaEdit = false;
                        getMatchDataTable4.Region         = getCurentData.Region;
                        getMatchDataTable4.IdRegion       = getCurentData.IdRegion;
                        // create tag
                        if (getMatchDataTable4.Tags == null)
                        {
                            getMatchDataTable4.Tags = new List <string>();
                        }
                        listT4.Add(getMatchDataTable4);
                        // Table4Collection.ReplaceOne(it => it.IdArea == getMatchDataTable4.IdArea
                        // && it.IdParty == getMatchDataTable4.IdParty, getMatchDataTable4);
                    }
                }
            }

            for (int i = 0; i < listT4.Count; i += 550)
            {
                var list = listT4.Skip(i).Take(550);
                ListT4Collection.InsertMany(list);
                await Task.Delay(1000);
            }

            foreach (var data in dataTable4.GroupBy(it => it.IdArea))
            {
                Table4Collection.DeleteMany(it => it.IdArea == data.Key);
            }
        }
        public async Task UploadFile()
        {
            // Read
            var listScoreCsv = new List <ScorePollCsv>();

            using (var csvReader = new StreamReader(Request.Form.Files.FirstOrDefault().OpenReadStream()))
            {
                listScoreCsv = csvReader.ReadToEnd()
                               .Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
                               .Skip(1)
                               .Select(it =>
                {
                    var getData = it.Split(',').ToList();
                    float.TryParse(getData[4], out float score);
                    return(new ScorePollCsv
                    {
                        Id = Guid.NewGuid().ToString(),
                        IdParty = getData[0],
                        NameParty = getData[3],
                        IdArea = getData[2],
                        NameArea = getData[1],
                        Score = score,
                        Region = getData[5],
                        IdRegion = getData[6]
                    });
                }).ToList();
            }

            // Fill in ScorePoll
            var listScorePoll = new List <ScorePollV2>();
            var groupByArea   = listScoreCsv.GroupBy(it => it.IdArea).ToList();

            foreach (var getList in groupByArea)
            {
                var totalScore1 = getList.FirstOrDefault(it => it.IdParty == "999").Score;
                foreach (var datas in getList)
                {
                    if (datas.IdParty != "999")
                    {
                        var ScoreParty = Math.Round(datas.Score / 100.0 * totalScore1);
                        listScorePoll.Add(new ScorePollV2
                        {
                            Id           = datas.Id,
                            IdParty      = datas.IdParty,
                            NameParty    = datas.NameParty,
                            IdArea       = datas.IdArea,
                            NameArea     = datas.NameArea,
                            datePoll     = DateTime.Now,
                            Score        = ScoreParty,
                            PercentScore = datas.Score,
                            Source       = "Poll",
                            Region       = datas.Region,
                            IdRegion     = datas.IdRegion
                        });
                    }
                    else
                    {
                        listScorePoll.Add(new ScorePollV2
                        {
                            Id           = datas.Id,
                            IdParty      = datas.IdParty,
                            NameParty    = datas.NameParty,
                            IdArea       = datas.IdArea,
                            NameArea     = datas.NameArea,
                            datePoll     = DateTime.Now,
                            Score        = datas.Score,
                            PercentScore = datas.Score,
                            Source       = "Poll",
                            Region       = datas.Region,
                            IdRegion     = datas.IdRegion
                        });
                    }
                }
            }

            for (int i = 0; i < listScorePoll.Count; i += AtATime)
            {
                var list = listScorePoll.Skip(i).Take(AtATime);
                FinalScorePollCollection.InsertMany(list);
                await Task.Delay(Delay);
            }
        }
Example #4
0
 public List <ScorePollV2> GetAllScorePoll()
 {
     return(FinalScorePollCollection.Find(it => true).ToList());
 }