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); }
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); }
public static long?GetMoodleUserId ( this Professor item, Modalidade modalidade, GetUserByUsernameClient client = null, HttpClient httpClient = null ) { return(InternalGetMoodleUserId(item.ProfessorCpf, modalidade, client, httpClient)); }
private static long?InternalGetMoodleUserId ( string cpf, Modalidade modalidade, GetUserByUsernameClient client = null, HttpClient httpClient = null ) { long?moodleUserId = MoodleFromToCache.GetCachedMoodleUser(modalidade.IdModalidade, cpf); if (moodleUserId.HasValue) { return(moodleUserId.Value); } GetUserByUsernameClient getClient = client; if (getClient == null) { getClient = new GetUserByUsernameClient(); } if (httpClient != null) { getClient.AddHttpClient(httpClient); } getClient.AddBaseUrl(modalidade.MoodleUrl) .AddServiceUrl(modalidade.MoodleServiceUrl) .AddToken(modalidade.MoodleGetInfoServiceToken); GetByUsernameRequest request = new GetByUsernameRequest() { Username = cpf.DesformatarCpf() }; Task <UserResponse> task = getClient.Post(request); task.Wait(); UserResponse response = task.Result; if (response?.Id > 0) { MoodleFromToCache.AddUser(modalidade.IdModalidade, cpf, response.Id); } return(response?.Id); }
public static SuspendedUserResult SuspendItem(this AbstractProxy proxy, object item, Modalidade modalidade, HttpClient httpClient) { SuspendedUserResult suspendedResult = new SuspendedUserResult() { MoodleId = null, LastUrl = "" }; UpdateUserClient client = new UpdateUserClient(); GetUserByUsernameClient getUserClient = new GetUserByUsernameClient(); client.AddHttpClient(httpClient); getUserClient.AddHttpClient(httpClient); proxy.BuildMoodleClient(client, MoodleTokenType.OfficialMoodleApiFunctions); proxy.BuildMoodleClient(getUserClient, MoodleTokenType.LocalMoodleExternalApiGetInfoToken); long?moodleId = null; if (item is Professor professor) { moodleId = professor?.GetMoodleUserId(modalidade, getUserClient, httpClient); } else if (item is Aluno aluno) { moodleId = aluno?.GetMoodleUserId(modalidade, getUserClient, httpClient); } if (!moodleId.HasValue) { return(suspendedResult); } UpdateUserRequest request = new UpdateUserRequest() { Id = moodleId.Value, Suspended = 1 }; Task <EmptyResponse> task = client.Post(request); task.Wait(); suspendedResult.LastUrl = client.LastUrl; suspendedResult.MoodleId = moodleId; return(suspendedResult); }
public override UserResponse VerifyIfExists(AbstractMoodleServiceClient client, string filter) { GetUserByUsernameClient verifyClient = (GetUserByUsernameClient)client; GetByUsernameRequest request = new GetByUsernameRequest() { Username = filter.DesformatarCpf() }; Task <UserResponse> task = verifyClient.Post(request); task.Wait(); LastUrl = client.LastUrl; UserResponse response = task.Result; return(response); }
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); }