コード例 #1
0
        public override ParallelSendResult <Professor, UserResponse> SendAll()
        {
            Result = new ParallelSendResult <Professor, UserResponse>();
            System.GC.Collect();

            IEnumerable <Professor> data = GetData("");

            if (data == null)
            {
                return(Result);
            }

            var factory = new HttpClientFactory();

            using (var httpClient = factory.CreateMoodleHttpClient())
            {
                CreateUserClient        createClient = new CreateUserClient();
                GetUserByUsernameClient verifyClient = new GetUserByUsernameClient();

                // Sharing the same HttpClient instance to improve performance
                verifyClient.AddHttpClient(httpClient);
                createClient.AddHttpClient(httpClient);

                foreach (var modalidade in Modalidades)
                {
                    ModalidadeAtual = modalidade;

                    this.AddMoodleBaseUrl(modalidade.MoodleUrl)
                    .AddMoodleToken(modalidade.MoodleToken)
                    .AddMoodleGetInfoServiceToken(modalidade.MoodleGetInfoServiceToken)
                    .AddMoodleServiceUrl(modalidade.MoodleServiceUrl);

                    BuildMoodleClient(createClient, MoodleTokenType.OfficialMoodleApiFunctions);
                    BuildMoodleClient(verifyClient, MoodleTokenType.LocalMoodleExternalApiGetInfoToken);

                    var professores = data.Where
                                      (
                        x =>
                        (
                            x.Disciplinas
                            .Where(d => d.IdModalidade == modalidade.IdModalidade)
                            .Count() > 0
                        )
                                      )
                                      .ToArray();

                    if (UseParallelism)
                    {
                        ProcessWithParallelism(professores, createClient, verifyClient);
                    }
                    else
                    {
                        ProcessWithRegularForeach(professores, createClient, verifyClient);
                    }
                }
            }

            return(Result);
        }
コード例 #2
0
        public override ParallelSendResult <AlunoDisciplinaViewModel, GetEnrolmentsByUserIdResponse> SendAll()
        {
            Result = new ParallelSendResult <AlunoDisciplinaViewModel, GetEnrolmentsByUserIdResponse>();

            IEnumerable <AlunoDisciplinaViewModel> data = GetData("");

            if (data == null)
            {
                return(Result);
            }

            var factory = new HttpClientFactory();

            HttpClient                    = factory.CreateMoodleHttpClient();
            CreateCourseClient            = new CreateCourseClient();
            CreateCategoryClient          = new CreateCategoryClient();
            CreateUserClient              = new CreateUserClient();
            GetCategoryByNameClient       = new GetCategoryByNameClient();
            GetCourseByNameClient         = new GetCourseByNameClient();
            GetUserByUsernameClient       = new GetUserByUsernameClient();
            EnrolmentClient               = new EnrolmentClient();
            GetEnrolementsrByUserIdClient = new GetEnrolementsrByUserIdClient();

            CreateCourseClient.AddHttpClient(HttpClient);
            CreateCategoryClient.AddHttpClient(HttpClient);
            CreateUserClient.AddHttpClient(HttpClient);
            GetCategoryByNameClient.AddHttpClient(HttpClient);
            GetCourseByNameClient.AddHttpClient(HttpClient);
            GetUserByUsernameClient.AddHttpClient(HttpClient);
            EnrolmentClient.AddHttpClient(HttpClient);
            GetEnrolementsrByUserIdClient.AddHttpClient(HttpClient);

            foreach (var modalidade in Modalidades)
            {
                ModalidadeAtual = modalidade;

                this.AddMoodleBaseUrl(modalidade.MoodleUrl)
                .AddMoodleToken(modalidade.MoodleToken)
                .AddMoodleGetInfoServiceToken(modalidade.MoodleGetInfoServiceToken)
                .AddMoodleServiceUrl(modalidade.MoodleServiceUrl);

                this.AddMoodleCategoryParent(modalidade.MoodleCategoryParent)
                .AddMoodleDescriptionFormat(modalidade.MoodleDescriptionFormat);

                var alunos = data.Where(x => x.Aluno.IdModalidade == modalidade.IdModalidade).ToArray();

                if (UseParallelism)
                {
                    ProcessWithParallelism(alunos, null, null);
                }
                else
                {
                    ProcessWithRegularForeach(alunos, null, null);
                }
            }

            return(Result);
        }
コード例 #3
0
        public MoodleFromToCacheAdapter FillCategories()
        {
            var cursos = Service.GetCursosBySemestre(Configuration.SemestreAtual);

            if (cursos == null)
            {
                return(this);
            }


            var factory = new HttpClientFactory();

            using (var httpClient = factory.CreateMoodleHttpClient())
            {
                var getCategoryClient = new GetCategoryByNameClient();

                // Sharing the same HttpClient instance to improve performance
                getCategoryClient.AddHttpClient(httpClient);

                foreach (var modalidade in Modalidades)
                {
                    IEnumerable <CategoryResponse> moodleCategories = GetMoodleCategories(modalidade, httpClient);


                    if (moodleCategories?.Count() == 0)
                    {
                        continue;
                    }

                    getCategoryClient
                    .AddBaseUrl(modalidade.MoodleUrl)
                    .AddToken(modalidade.MoodleGetInfoServiceToken)
                    .AddServiceUrl(modalidade.MoodleServiceUrl);

                    var filtered = cursos.Where(x => x.IdModalidade == modalidade.IdModalidade).ToArray();

                    filtered.AsParallel()
                    .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                    .ForAll((item) =>
                    {
                        CategoryResponse moodleCategory = moodleCategories.Where(x => x.Name.ToLower() == item.CursoDescricao.ToLower())
                                                          .FirstOrDefault();

                        if (moodleCategory != null)
                        {
                            MoodleFromToCache.AddCategory(modalidade.IdModalidade, item.CursoDescricao, moodleCategory.Id);
                        }
                    });
                }
            }

            return(this);
        }
コード例 #4
0
        public MoodleFromToCacheAdapter FillCourses()
        {
            var disciplinas = Service.GetDisciplinas(Configuration.SemestreAtual);

            if (disciplinas == null)
            {
                return(this);
            }

            var factory = new HttpClientFactory();

            using (var httpClient = factory.CreateMoodleHttpClient())
            {
                var getCourseClient = new GetCourseByNameClient();

                // Sharing the same HttpClient instance to improve performance
                getCourseClient.AddHttpClient(httpClient);

                foreach (var modalidade in Modalidades)
                {
                    var moodleCourses = GetMoodleCourses(modalidade, httpClient);

                    if (moodleCourses?.Count() == 0)
                    {
                        continue;
                    }

                    getCourseClient
                    .AddBaseUrl(modalidade.MoodleUrl)
                    .AddToken(modalidade.MoodleGetInfoServiceToken)
                    .AddServiceUrl(modalidade.MoodleServiceUrl);

                    var filtered    = disciplinas.Where(x => x.IdModalidade == modalidade.IdModalidade).ToArray();
                    var total       = filtered.Length;
                    var rowsPerPage = 2000;

                    if (total <= rowsPerPage)
                    {
                        filtered.AsParallel()
                        .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                        .ForAll((item) =>
                        {
                            item.DisciplinaNome = item.GetNomeDisciplina(Configuration, modalidade);
                            item.ShortName      = item.GetShortNameDisciplina(Configuration, modalidade);

                            var moodleCourse = moodleCourses.Where(x => x.Fullname == item.DisciplinaNome).FirstOrDefault();

                            if (moodleCourse != null)
                            {
                                MoodleFromToCache.AddCourse(modalidade.IdModalidade, item.DisciplinaNome, moodleCourse.Id);
                            }
                        });
                    }
                    else
                    {
                        double div   = total / rowsPerPage;
                        var    pages = (int)(Math.Floor(div));
                        var    rest  = total % rowsPerPage;

                        Parallel.For(1, pages + 1, (page) =>
                        {
                            var pageable = filtered.Skip((page - 1) * rowsPerPage).Take(rowsPerPage);

                            pageable.AsParallel()
                            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                            .WithDegreeOfParallelism(400)
                            .ForAll((item) =>
                            {
                                item.DisciplinaNome = item.GetNomeDisciplina(Configuration, modalidade);
                                item.ShortName      = item.GetShortNameDisciplina(Configuration, modalidade);

                                var moodleCourse = moodleCourses.Where(x => x.Fullname == item.DisciplinaNome).FirstOrDefault();

                                if (moodleCourse != null)
                                {
                                    MoodleFromToCache.AddCourse(modalidade.IdModalidade, item.DisciplinaNome, moodleCourse.Id);
                                }
                            });
                        });

                        if (rest > 0)
                        {
                            filtered.Skip((pages - 1) * rowsPerPage)
                            .Take(rest)
                            .AsParallel()
                            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                            .WithDegreeOfParallelism(400)
                            .ForAll((item) =>
                            {
                                item.DisciplinaNome = item.GetNomeDisciplina(Configuration, modalidade);
                                item.ShortName      = item.GetShortNameDisciplina(Configuration, modalidade);

                                var moodleCourse = moodleCourses.Where(x => x.Fullname == item.DisciplinaNome).FirstOrDefault();

                                if (moodleCourse != null)
                                {
                                    MoodleFromToCache.AddCourse(modalidade.IdModalidade, item.DisciplinaNome, moodleCourse.Id);
                                }
                            });
                        }
                    }
                }
            }

            return(this);
        }
コード例 #5
0
        public MoodleFromToCacheAdapter FillTeachers()
        {
            var professores = Service.GetProfessores();

            if (professores == null)
            {
                return(this);
            }

            var factory = new HttpClientFactory();

            using (var httpClient = factory.CreateMoodleHttpClient())
            {
                var getUserClient = new GetUserByUsernameClient();

                // Sharing the same HttpClient instance to improve performance
                getUserClient.AddHttpClient(httpClient);

                foreach (var modalidade in Modalidades)
                {
                    var moodleUsers = GetMoodleUsers(modalidade, httpClient);

                    if (moodleUsers?.Count() == 0)
                    {
                        continue;
                    }

                    getUserClient.AddBaseUrl(modalidade.MoodleUrl)
                    .AddToken(modalidade.MoodleGetInfoServiceToken)
                    .AddServiceUrl(modalidade.MoodleServiceUrl);

                    var filtered    = professores.ToArray();
                    var total       = filtered.Length;
                    var rowsPerPage = 2000;

                    if (total <= rowsPerPage)
                    {
                        filtered.AsParallel()
                        .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                        .ForAll((item) =>
                        {
                            var moodleUser = moodleUsers.Where(x => x.Username == item.ProfessorCpf.DesformatarCpf()).FirstOrDefault();

                            if (moodleUser != null)
                            {
                                MoodleFromToCache.AddUser(modalidade.IdModalidade, item.ProfessorCpf, moodleUser.Id);
                            }
                        });
                    }
                    else
                    {
                        double div   = total / rowsPerPage;
                        var    pages = (int)(Math.Floor(div));
                        var    rest  = total % rowsPerPage;

                        Parallel.For(1, pages + 1, (page) =>
                        {
                            var pageable = filtered.Skip((page - 1) * rowsPerPage).Take(rowsPerPage);

                            pageable.AsParallel()
                            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                            .WithDegreeOfParallelism(400)
                            .ForAll((item) =>
                            {
                                var moodleUser = moodleUsers.Where(x => x.Username == item.ProfessorCpf.DesformatarCpf()).FirstOrDefault();

                                if (moodleUser != null)
                                {
                                    MoodleFromToCache.AddUser(modalidade.IdModalidade, item.ProfessorCpf, moodleUser.Id);
                                }
                            });
                        });

                        if (rest > 0)
                        {
                            filtered.Skip((pages - 1) * rowsPerPage)
                            .Take(rest)
                            .AsParallel()
                            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                            .WithDegreeOfParallelism(400)
                            .ForAll((item) =>
                            {
                                var moodleUser = moodleUsers.Where(x => x.Username == item.ProfessorCpf.DesformatarCpf()).FirstOrDefault();

                                if (moodleUser != null)
                                {
                                    MoodleFromToCache.AddUser(modalidade.IdModalidade, item.ProfessorCpf, moodleUser.Id);
                                }
                            });
                        }
                    }
                }
            }

            return(this);
        }