Exemple #1
0
        public void CalculateChunk(int page)
        {
            var goldContext = new GoldContext();

            //goldContext.ChangeTracker.AutoDetectChangesEnabled = false;
            goldContext.ChangeTracker.LazyLoadingEnabled = true;
            //goldContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

            Logger.Log($"Statrting to load page {page} of companies");

            var companies = goldContext.Company.AsNoTracking()
                            //.Include(x => x.Hq)
                            //.Include(x => x.Legal)
                            .Include(x => x.CompanyNames)
                            .Include(x => x.CompanyQuestionnaires)
                            .Include(x => x.Roles).ThenInclude(x => x.Person).ThenInclude(x => x.PersonCountries)
                            .Skip(page * PAGE_SIZE).Take(PAGE_SIZE).ToList();

            var models = new IGeneralModel[]
            {
                new CertifiedCompanyWeightedSumModel(),
                new QuantitativeCompanyWeightedSumModel()
            };

            foreach (var company in companies)
            {
                var scores = models.Select(x => new { x.ScoreType, Rating = x.CalculateRating(company) });

                goldContext.AddRange(scores.Select(x =>
                                                   new Ratings()
                {
                    CompanyId   = company.Id,
                    RatingType  = (int)x.ScoreType,
                    RatingValue = x.Rating
                }));
            }

            goldContext.SaveChanges();
            goldContext.Dispose();
        }
        private void LoadCompanies()
        {
            Semaphore s = new Semaphore(0, THREAD_COUNT);

            ThreadStart loadCompaniesChunk = () =>
            {
                try
                {
                    int page = 0;
                    while (true)
                    {
                        page = Interlocked.Increment(ref currentPage);

                        var silverContext = new SilverContext();
                        silverContext.ChangeTracker.AutoDetectChangesEnabled = false;
                        silverContext.ChangeTracker.LazyLoadingEnabled       = true;
                        silverContext.ChangeTracker.QueryTrackingBehavior    = QueryTrackingBehavior.NoTracking;

                        var goldContext = new GoldContext();
                        goldContext.ChangeTracker.AutoDetectChangesEnabled = false;
                        goldContext.ChangeTracker.LazyLoadingEnabled       = true;
                        goldContext.ChangeTracker.QueryTrackingBehavior    = QueryTrackingBehavior.NoTracking;

                        Logger.Log($"Statrting to load page {page} of companies");

                        var companies = silverContext.Companies.AsNoTracking()
                                        .Include(x => x.Hq)
                                        .Include(x => x.Legal)
                                        .Include(x => x.CompanyNames)
                                        .Include(x => x.CompanyQuestionnaires)
                                        //.Include(x => x.CompanyOperation)
                                        .Include(x => x.Roles).ThenInclude(x => x.Person).ThenInclude(x => x.PersonCountries)
                                        .Skip(currentPage * PAGE_SIZE).Take(PAGE_SIZE).ToList();

                        if (companies.Count == 0)
                        {
                            break;
                        }

                        var companiesToSave = new List <Company>();
                        var namesToSave     = new List <CompanyName>();
                        var rolesToSave     = new List <Role>();
                        var personsToSave   = new List <Person>();
                        var countriesToSave = new List <PersonCountry>();

                        foreach (var company in companies)
                        {
                            var newCompany = new Company()
                            {
                                LegalJurisdiction = company.LegalJurisdiction,
                                LegalName         = company.LegalName,
                                Lei          = company.Lei,
                                NumEmployees = company.NumEmployees,
                                Status       = company.Status,
                            };

                            var hqAddress = new Address()
                            {
                                AddressLine   = company.Hq.AddressLine,
                                AddressNumber = company.Hq.AddressNumber,
                                City          = company.Hq.City,
                                PostalCode    = company.Hq.PostalCode,
                                Region        = company.Hq.Region,
                            };

                            //hqAddress.CompanyHq.Add(newCompany);
                            newCompany.Hq = hqAddress;
                            goldContext.Add(hqAddress);

                            var legalAddress = new Address()
                            {
                                AddressLine   = company.Legal.AddressLine,
                                AddressNumber = company.Legal.AddressNumber,
                                City          = company.Legal.City,
                                PostalCode    = company.Legal.PostalCode,
                                Region        = company.Legal.Region,
                            };

                            //legalAddress.CompanyLegal.Add(newCompany);
                            newCompany.Legal = legalAddress;
                            goldContext.Add(legalAddress);

                            foreach (var companyName in company.CompanyNames)
                            {
                                var newCompanyName = new CompanyName()
                                {
                                    Company = newCompany,
                                    Name    = companyName.Name,
                                    Type    = companyName.Type,
                                };
                                newCompany.CompanyNames.Add(newCompanyName);
                                namesToSave.Add(newCompanyName);
                                //goldContext.Add(newCompanyName);
                            }

                            // todo: add when becomes relevant

                            /*foreach (var companyOperation in company.CompanyOperation)
                             * {
                             *  var newCompanyOperation = new CompanyOperation()
                             *  {
                             *      Company = newCompany,
                             *      Name = companyOperation.,
                             *  };
                             *  newCompany.CompanyOperation.Add(newCompanyOperation);
                             *  GoldContext.Add(newCompanyOperation);
                             * }*/

                            foreach (var companyQuestion in company.CompanyQuestionnaires)
                            {
                                var newCompanyQuestion = new CompanyQuestion()
                                {
                                    Company  = newCompany,
                                    Question = (DB.Gold.Enums.CompanyQuestion)(int) companyQuestion.Question,
                                    Answer   = companyQuestion.Answer
                                };
                                newCompany.CompanyQuestionnaires.Add(newCompanyQuestion);
                                goldContext.Add(newCompanyQuestion);
                            }

                            foreach (var role in company.Roles)
                            {
                                var newRole = new Role()
                                {
                                    Company          = newCompany,
                                    BaseSalary       = role.BaseSalary,
                                    IncentiveOptions = role.IncentiveOptions,
                                    RoleType         = role.RoleType,
                                    Title            = role.Title,
                                };

                                var person    = role.Person;
                                var newPerson = new Person()
                                {
                                    BirthYear      = person.BirthYear,
                                    EduInstitute   = person.EduInstitute,
                                    EduSubject     = person.EduSubject,
                                    Gender         = person.Gender,
                                    HighEdu        = person.HighEdu,
                                    Married        = person.Married,
                                    Name           = person.Name,
                                    Picture        = person.Picture,
                                    Race           = person.Race,
                                    Religion       = person.Religion,
                                    Sexuality      = person.Sexuality,
                                    BaseSalary     = person.BaseSalary,
                                    OtherIncentive = person.OtherIncentive,
                                };

                                newPerson.Roles.Add(newRole);
                                newRole.Person = newPerson;
                                personsToSave.Add(newPerson);
                                //goldContext.Add(newPerson);

                                newCompany.Roles.Add(newRole);
                                rolesToSave.Add(newRole);
                                //goldContext.Add(role);

                                foreach (var nation in person.PersonCountries)
                                {
                                    countriesToSave.Add(
                                        new PersonCountry()
                                    {
                                        Person    = newPerson,
                                        CountryId = countryMap[nation.CountryId.Value]
                                    }
                                        );
                                }
                            }
                            companiesToSave.Add(newCompany);
                        }

                        goldContext.SaveChanges();

                        foreach (var c in companiesToSave)
                        {
                            goldContext.Add(c);
                        }

                        foreach (var cn in namesToSave)
                        {
                            goldContext.Add(cn);
                        }

                        foreach (var r in rolesToSave)
                        {
                            goldContext.Add(r);
                        }

                        foreach (var p in personsToSave)
                        {
                            goldContext.Add(p);
                        }

                        goldContext.SaveChanges();

                        foreach (var c in countriesToSave)
                        {
                            goldContext.Add(c);
                        }

                        goldContext.SaveChanges();

                        goldContext.Dispose();
                    }
                    Logger.Log($"No data for page {page}, finishing loading thread");
                }
                catch (Exception ex)
                {
                    Logger.Log($"ERROR: Unhandled exception: {ex.ToString()}");
                }
                finally
                {
                    s.Release();
                }
            };

            for (int i = 0; i < THREAD_COUNT; i++)
            {
                new Thread(loadCompaniesChunk).Start();
            }

            for (int i = 0; i < THREAD_COUNT; i++)
            {
                s.WaitOne();
            }
        }