Example #1
0
        private static SuccessFailCode WriteSessions(Options opts)
        {
            var client = new DocumentClient(new Uri(opts.Cosmos.Endpoint), opts.Cosmos.Key);

            var title = opts.QuestionVersionKey.Split('-').Last();
            var questionSetRepository =
                new QuestionSetRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos));
            var sessionRepository =
                new UserSessionRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos));

            var questionSet = questionSetRepository.GetLatestQuestionSetByTypeAndKey("short", title)
                              .GetAwaiter().GetResult();

            using (var fs = File.OpenWrite(opts.FileName + ".csv"))
                using (var sw = new StreamWriter(fs))
                    using (var csv = new CsvWriter(sw))
                    {
                        csv.WriteHeader <UserSessionEnty>();
                        csv.NextRecord();

                        for (var i = 0; i < opts.NumberOfSessions; i++)
                        {
                            var session = CreateSession(questionSet.QuestionSetVersion, questionSet.MaxQuestions);
                            Console.WriteLine($"Creating User Session: {i} {session.UserSessionId}");

                            sessionRepository.CreateUserSession(session).GetAwaiter().GetResult();

                            csv.WriteRecord(new UserSessionEnty {
                                SessionId = session.UserSessionId, UserName = ""
                            });
                            csv.NextRecord();
                        }
                    }

            return(SuccessFailCode.Succeed);
        }
Example #2
0
        public static SuccessFailCode Execute(IServiceProvider services, Options opts)
        {
            try
            {
                var configuration = services.GetService <IConfiguration>();
                configuration.Bind(opts);

                var client = new DocumentClient(new Uri(opts.Cosmos.Endpoint), opts.Cosmos.Key);

                var title = opts.QuestionVersionKey.Split('-').Last();
                var questionSetRepository = new QuestionSetRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos));
                var questionSet           = new QuestionSet()
                {
                    AssessmentType     = "short",
                    Version            = 1,
                    Title              = title,
                    QuestionSetKey     = title.ToLower(),
                    MaxQuestions       = 40,
                    LastUpdated        = DateTime.Now,
                    PartitionKey       = "ncs",
                    IsCurrent          = true,
                    QuestionSetVersion = opts.QuestionVersionKey + "-1"
                };
                questionSetRepository.CreateOrUpdateQuestionSet(questionSet).GetAwaiter().GetResult();

                var questionRepository = new QuestionRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos));
                using (var fileStream = File.OpenRead(opts.CsvFile))
                    using (var streamReader = new StreamReader(fileStream))
                        using (var reader = new CsvReader(streamReader))
                        {
                            var questionPartitionKey = questionSet.QuestionSetVersion;
                            var questionNumber       = 1;

                            foreach (var question in reader.GetRecords <QuestionStatement>())
                            {
                                var questionId = $"{questionPartitionKey}-{questionNumber}";
                                Console.WriteLine($"Creating question id: {questionId}");

                                questionRepository.CreateQuestion(new Question {
                                    IsNegative   = question.IsFlipped == 1,
                                    Order        = questionNumber,
                                    QuestionId   = questionId,
                                    TraitCode    = question.Trait.ToUpper(),
                                    PartitionKey = questionPartitionKey,
                                    Texts        = new [] {
                                        new QuestionText {
                                            LanguageCode = "EN", Text = question.Statement
                                        }
                                    }
                                }).GetAwaiter().GetResult();

                                questionNumber++;
                            }
                        }
                return(SuccessFailCode.Succeed);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"An Error ocurred loading while loading questions to: {opts.Cosmos.Endpoint} - {opts.Cosmos.DatabaseName} -- {ex.Message}");
                Console.ForegroundColor = ConsoleColor.White;
                return(SuccessFailCode.Fail);
            }
        }