private async Task <int> CreateUsers(int howMany, bool household, bool quiet)
        {
            int created = 0;

            int[]   familyOptions = { 1, 2, 3, 4, 5, FamilyRandomIndicator, GroupRandomIndicator };
            float[] familyWeights = { 0.25F, 0.35F, 0.21F, 0.10F, 0.04F, 0.047F, 0.003F };

            var issues = new List <string>();

            // make the participants
            var users = await _userDataGenerator.Generate(Site.Id, howMany);

            var minDateTime = DateTime.MaxValue;
            var maxDateTime = DateTime.MinValue;

            if (!quiet)
            {
                Console.Write($"Inserting {howMany} users... ");
            }

            ProgressBar progress = quiet ? null : new ProgressBar();

            try
            {
                var rand                 = new Bogus.Randomizer();
                int familyMembers        = 0;
                Domain.Model.User parent = null;

                // insert the participants
                foreach (var user in users)
                {
                    bool currentUserParent = false;

                    // set an appropriate random date and time for insertion
                    var setDateTime = _dateTimeDataGenerator.SetRandom(Site);

                    if (setDateTime < minDateTime)
                    {
                        minDateTime = setDateTime;
                    }
                    if (setDateTime > maxDateTime)
                    {
                        maxDateTime = setDateTime;
                    }

                    if (familyMembers > 0)
                    {
                        // we are processing family members
                        user.User.LastName = parent.LastName;
                        if (rand.Int(1, 100) > 5)
                        {
                            user.User.Username = null;
                        }

                        // insert the family member
                        try
                        {
                            await _facade
                            .UserService
                            .AddHouseholdMemberAsync(parent.Id, user.User);

                            created++;
                        }
                        catch (GraException gex)
                        {
                            issues.Add($"Household username: {user.User.Username} - {gex.Message}");
                        }
                        familyMembers--;
                    }
                    else
                    {
                        // not processing family members, should this person be a head of household?
                        if (household && rand.Int(1, 100) <= 31)
                        {
                            currentUserParent = true;

                            familyMembers = rand.WeightedRandom <int>(familyOptions, familyWeights);
                            if (familyMembers == FamilyRandomIndicator)
                            {
                                familyMembers = rand.Int(6, 10);
                            }
                            else if (familyMembers == GroupRandomIndicator)
                            {
                                familyMembers = rand.Int(11, 100);
                            }
                        }

                        // insert the created user
                        try
                        {
                            var inserted = await _facade
                                           .UserService
                                           .RegisterUserAsync(user.User, user.Password);

                            if (currentUserParent)
                            {
                                parent = inserted;
                            }
                            created++;
                        }
                        catch (Exception ex)
                        {
                            issues.Add($"Username: {user.User.Username} - {ex.Message}");
                        }
                    }

                    if (progress != null)
                    {
                        progress.Report((double)created / howMany);
                    }
                }
            }
            finally
            {
                if (progress != null)
                {
                    progress.Dispose();
                }
            }

            Console.WriteLine($"Created {created} random users in {Site.Name}.");
            Console.WriteLine($"Users registered between {minDateTime} and {maxDateTime}.");

            if (issues.Count > 0)
            {
                Console.WriteLine("Some issues were encountered:");
                foreach (string issue in issues)
                {
                    Console.WriteLine($"- {issue}");
                }
            }

            await DisplayUserCount();

            return(howMany == created ? 0 : 1);
        }
예제 #2
0
        Generate(Site site, int count, int challengePercent, int codePercent, bool quiet)
        {
            int[]   minuteCeilings            = { 60, 120, 500 };
            float[] minuteCeilingDistribution = { 0.85F, 0.1F, 0.05F };

            var userList = await _userService.GetPaginatedUserListAsync(new UserFilter());

            var codeList = await _triggerService.GetPaginatedListAsync(new TriggerFilter
            {
                SecretCodesOnly = true
            });

            var challengeList = await _challengeService
                                .GetPaginatedChallengeListAsync(new ChallengeFilter());

            var activities = new List <GeneratedActivity>();

            var rand = new Bogus.Randomizer();

            if (!quiet)
            {
                Console.Write($"Generating {count} activity items... ");
            }

            ProgressBar progress = quiet ? null : new ProgressBar();

            try
            {
                for (int i = 0; i < count; i++)
                {
                    bool addActivity = false;
                    var  randomUser  = (await _userService.GetPaginatedUserListAsync(new UserFilter
                    {
                        SiteId = site.Id,
                        Skip = rand.Int(0, userList.Count - 1),
                        Take = 1
                    })).Data.First();

                    var act = new GeneratedActivity
                    {
                        User = randomUser,
                    };
                    if (challengePercent > 0 && rand.Int(1, 100) <= challengePercent)
                    {
                        bool isValid = false;
                        int  challengeLookupCount = 0;
                        await _configureUserSite.Lookup(randomUser.Id);

                        _challengeService.ClearCachedUserContext();
                        DataWithCount <IEnumerable <Challenge> > randomChallenge = null;
                        while (!isValid)
                        {
                            challengeLookupCount++;
                            var filter = new ChallengeFilter()
                            {
                                Take = rand.Int(0, challengeList.Count - 1),
                                Skip = 1
                            };
                            randomChallenge = await _challengeService.GetPaginatedChallengeListAsync(filter);

                            if (randomChallenge.Data != null &&
                                randomChallenge.Data.FirstOrDefault() != null)
                            {
                                isValid = randomChallenge.Data.First().IsValid;
                            }
                            if (challengeLookupCount > 20)
                            {
                                _logger.LogError($"Unable to find an eligible challenge for user id {randomUser.Id} after 20 tries, giving up.");
                                randomChallenge = null;
                                addActivity     = false;
                                break;
                            }
                        }
                        if (randomChallenge != null)
                        {
                            var randomTasks = await _challengeService
                                              .GetChallengeTasksAsync(randomChallenge.Data.First().Id);

                            var randomTask = randomTasks
                                             .Skip(rand.Int(0, randomTasks.Count() - 1)).First();
                            randomTask.IsCompleted = true;
                            act.ActivityType       = ActivityType.ChallengeTasks;
                            act.ChallengeId        = randomChallenge.Data.First().Id;
                            act.ChallengeTasks     = new List <ChallengeTask> {
                                randomTask
                            };
                            addActivity = true;
                        }
                    }
                    else
                    {
                        if (codePercent > 0 && rand.Int(1, 100) <= codePercent)
                        {
                            var randomCode = (await _triggerService.GetPaginatedListAsync(new TriggerFilter
                            {
                                SiteId = site.Id,
                                Skip = rand.Int(0, codeList.Count - 1),
                                Take = 1,
                                SecretCodesOnly = true
                            })).Data.First();

                            act.ActivityType = ActivityType.SecretCode;
                            act.SecretCode   = randomCode.SecretCode;
                            addActivity      = true;
                        }
                        else
                        {
                            act.ActivityAmount = rand.Int(1, rand
                                                          .WeightedRandom <int>(minuteCeilings, minuteCeilingDistribution));
                            act.ActivityType = ActivityType.Default;
                            addActivity      = true;
                        }
                    }
                    if (addActivity)
                    {
                        activities.Add(act);
                    }

                    if (progress != null)
                    {
                        progress.Report((double)i / count);
                    }
                }
            }
            finally
            {
                if (progress != null)
                {
                    progress.Dispose();
                    Console.WriteLine();
                }
            }
            return(activities);
        }