public async Task <ActionResult <IEnumerable <ScoringAssessmentDTO> > > GetCachedScoringAssessments(string sectionUniqueId) { string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, sectionUniqueId); var result = await _cacheProvider.Get <IEnumerable <ScoringAssessmentDTO> >(key); return(Ok(result)); }
public async Task <Assessment> GetByIdentifier(string identifier) { string key = CacheKeys.Composed(CacheKeys.AssessmentByIdentifier, identifier); bool assessmentIsCached = await _cacheProvider.TryHasKey(key); if (assessmentIsCached) { var cachedAssessment = await _cacheProvider.GetOrDefault <Assessment>(key); if (cachedAssessment != null) { return(cachedAssessment); } } var odsApi = await _odsApiClientProvider.NewResourcesClient(); var assessmentsFromApiv3 = await odsApi.Get <IList <AssessmentModelv3> >("assessments", new Dictionary <string, string>() { { "assessmentIdentifier", identifier }, }); var foundAssessmentv3 = assessmentsFromApiv3.FirstOrDefault(a => a.AssessmentIdentifier == identifier); var assessmentv2 = foundAssessmentv3.MapToAssessmentv2(); await _cacheProvider.TrySet(key, assessmentv2); return(assessmentv2); }
public async Task <ActionResult <IEnumerable <StudentInterventionsDTO> > > GetCachedScoringInterventions(string sectionUniqueId) { string key = CacheKeys.Composed(CacheKeys.InterventionScoringsBySectionUniqueId, sectionUniqueId); var result = await _cacheProvider.Get <IEnumerable <StudentInterventionsDTO> >(key); return(Ok(result)); }
public async Task <Intervention> GetByIdentificationCode(string identificationCode) { var key = CacheKeys.Composed(CacheKeys.InterventionByIdentificationCode, identificationCode); if (await _cacheProvider.TryHasKey(key)) { return(await _cacheProvider.Get <Intervention>(key)); } var ods = await _odsApiClientProvider.NewResourcesClient(); var interventions = await ods.Get <IList <Intervention> >("interventions", new Dictionary <string, string> { ["identificationCode"] = identificationCode, }); var intervention = interventions.FirstOrDefault(); if (intervention == null) { throw new InterventionNotFoundException(identificationCode); } await _cacheProvider.TrySet(key, intervention); return(intervention); }
public async Task CacheScoringInterventions(ScoringInterventionSearchFilters filters) { var associations = await ScoringInterventionsService.GetStudentInterventionsDTO(filters, getFromCache : false); string key = CacheKeys.Composed(CacheKeys.InterventionScoringsBySectionUniqueId, filters.SectionId); await _cache.Set(key, associations); }
private async Task <IEnumerable <ScoringAssessmentDTO> > GetAssessmentScoringsFromCacheOrDefault(string uniqueSectionCode) { string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, uniqueSectionCode); bool scoringsAreCached = await _cache.TryHasKey(key); if (!scoringsAreCached) { return(null); } var students = await _cache.GetOrDefault <IEnumerable <ScoringAssessmentDTO> >(key); if (students == null) { return(null); } // Remove sensitive data students = students.Select(dto => { dto.Student = _mapper.SecuredStudentDTO(dto.Student); return(dto); }); // Sort by last name students = students.OrderBy(student => student.Student.LastSurname); return(students.ToList()); }
public async Task CacheScoringAssessments(ScoringAssessmentSearchParams searchParams) { searchParams.GetFromCache = false; searchParams.StoreInCache = false; var scorings = await ScoringAssessmentsService.Search(searchParams); string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, searchParams.UniqueSectionCode); await _cache.Set(key, scorings); }
public async Task <OdsApiSettings> GetOdsApiSettings(string version) { ThrowIfStartupModeIsNotHosted(nameof(GetOdsApiSettings)); var key = CacheKeys.Composed(CacheKeys.OdsApiSettings, version); var odsApiSettings = await _cacheProvider.GetOrDefault <OdsApiSettings>(key); odsApiSettings.ClientSecret = null; return(odsApiSettings); }
private async Task <IList <string> > GetUserRoleAdminMappingsFromCache() { var edFiVersion = await GetEdFiVersion(); var key = CacheKeys.Composed(CacheKeys.UserRoleAdminMappings, edFiVersion); var staffClassificationDescriptors = await _cacheProvider.GetOrDefault <IList <string> >(key); return(staffClassificationDescriptors ?? new List <string>()); }
public async Task SetUserRoleTeacherMappings(IEnumerable <string> staffClassificationDescriptors) { if (_appSettings.IsStartupModeStandalone) { throw new UnsupportedDomainOperationException(nameof(SetUserRoleTeacherMappings), _appSettings.StartupMode); } var edFiVersion = await GetEdFiVersion(); var key = CacheKeys.Composed(CacheKeys.UserRoleTeacherMappings, edFiVersion); await _cacheProvider.Set(key, staffClassificationDescriptors); }
public async Task <IEnumerable <StudentInterventionsDTO> > GetStudentInterventionsDTO(ScoringInterventionSearchFilters filters, bool getFromCache = true, bool storeInCache = true) { if (getFromCache) { try { var fromCache = await GetStudentInterventionsDTOFromCacheOrDefaultOrThrowJsonReaderException(filters.SectionId); if (fromCache != null && fromCache.Count() > 0) { return(fromCache); } storeInCache = true; } catch (JsonReaderException ex) { storeInCache = true; } } var associations = new List <StudentInterventionsDTO>(); var studentsBySection = await _students.GetStudentsBySectionId(filters.SectionId); foreach (var student in studentsBySection) { var association = await GetStudentInterventionsDTOByStudent(student.StudentUniqueId); associations.Add(association); } if (storeInCache) { try { string key = CacheKeys.Composed(CacheKeys.InterventionScoringsBySectionUniqueId, filters.SectionId); await _cacheProvider.Set(key, associations); } catch (Exception ex) { // Sometimes caching takes too long so request timeout will expire // causing the method th throw an exception. Catch the error to return // the scorings back to the frontend even though this error happens } } return(associations .OrderBy(student => student.Student.LastSurname) .ToList()); }
public async Task SaveOdsApiSettings(OdsApiSettings settings) { ThrowIfStartupModeIsNotHosted(nameof(SaveOdsApiSettings)); if (!_environmentProvider.IsEnvironmentLocal) { var clientSecret = settings.ClientSecret; await _keyVaultProvider.SaveClientSecret(settings.Version, clientSecret); settings.ClientSecret = null; } var odsApiSettingsKey = CacheKeys.Composed(CacheKeys.OdsApiSettings, settings.Version); await _cacheProvider.Set(odsApiSettingsKey, settings); }
public async Task RefreshScoringInterventions() { var sections = await _catalog.GetSectionsAll(getFromCache : false, storeInCache : false); foreach (var section in sections) { var filters = new ScoringInterventionSearchFilters(section.Id); var studentInterventionAssociations = await _scoringInterventions.GetStudentInterventionsDTO(filters, getFromCache : false, storeInCache : false); string key = CacheKeys.Composed(CacheKeys.InterventionScoringsBySectionUniqueId, section.Id); await _cache.TrySet(key, studentInterventionAssociations); string refreshDateKey = CacheKeys.Composed(CacheKeys.RefreshDates.InterventionScoringsBySectionUniqueId, section.Id); await UpdateLastRefreshedDate(refreshDateKey); } }
public async Task DeleteStudentAssessmentAll(string uniqueSectionCode, string assessmentId, string date) { string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, uniqueSectionCode); if (await _cache.TryHasKey(key)) { var students = await _cache.Get <IEnumerable <ScoringAssessmentDTO> >(key); var odsApi = await _odsApiClientProvider.NewResourcesClient(); students = students.Select(dto => { _mapper.MapStudentDTO(dto.Student); dto.Student = _mapper.SecuredStudentDTO(dto.Student); foreach (StudentAssessmentAssociationDTO element in dto.Associations) { DateTime adminDate = Convert.ToDateTime(date); if (!string.IsNullOrWhiteSpace(element.AssociationModel.Id) && element.Assessment.Id == assessmentId && element.AssociationModel.AdministrationDate == adminDate) { odsApi.Delete($"studentAssessments/{element.AssociationModel.Id}"); } if (element.Assessment.Id == assessmentId && element.AssociationModel.AdministrationDate == adminDate) { dto.Associations = dto.Associations.Where(association => association.AssociationModel.Identifier != element.AssociationModel.Identifier).ToList(); } } return(dto); }).ToList(); try { await _cache.Set(key, students); } catch (Exception ex) { // Sometimes caching takes too long so request timeout will expire // causing the method th throw an exception. Catch the error to return // the scorings back to the frontend even though this error happens } } }
public async Task DeleteStudentAssessment(string studentAssessmentId, string uniqueSectionCode, string identifier) { var ods = await _odsApiClientProvider.NewResourcesClient(); if (!string.IsNullOrWhiteSpace(studentAssessmentId)) { await ods.Delete($"studentAssessments/{studentAssessmentId}"); } var key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, uniqueSectionCode); if (await _cache.TryHasKey(key)) { var students = await _cache.Get <IEnumerable <ScoringAssessmentDTO> >(key); students = students.Select(dto => { _mapper.MapStudentDTO(dto.Student); dto.Student = _mapper.SecuredStudentDTO(dto.Student); if (string.IsNullOrWhiteSpace(studentAssessmentId)) { dto.Associations = dto.Associations.Where(association => association.AssociationModel.Identifier != identifier).ToList(); } else { dto.Associations = dto.Associations.Where(association => association.AssociationModel.Id != studentAssessmentId).ToList(); } return(dto); }).ToList(); try { await _cache.Set(key, students); } catch (Exception ex) { // Sometimes caching takes too long so request timeout will expire // causing the method th throw an exception. Catch the error to return // the scorings back to the frontend even though this error happens } } }
public async Task <OdsApiSettings> GetOdsApiSettingsForHostedMode() { var edFiVersion = await _cacheProvider.GetOrDefault <string>(CacheKeys.EdFiVersion); if (edFiVersion == null) { throw new EdFiVersionNotSetException(); } var odsApiSettingsKey = CacheKeys.Composed(CacheKeys.OdsApiSettings, edFiVersion); var odsApiSettings = await _cacheProvider.Get <OdsApiSettings>(odsApiSettingsKey); if (!_environmentProvider.IsEnvironmentLocal) { odsApiSettings.ClientSecret = await _keyVaultProvider.GetClientSecret(odsApiSettings.Version); } return(odsApiSettings); }
public async Task <IEnumerable <ScoringAssessmentDTO> > Search(ScoringAssessmentSearchParams searchParams) { if (searchParams.GetFromCache) { var scoringsFromCache = await GetAssessmentScoringsFromCacheOrDefault(searchParams.UniqueSectionCode); if (scoringsFromCache != null && scoringsFromCache.Count() > 0) { var scoringsFromCacheList = scoringsFromCache .OrderBy(student => student.Student.LastSurname) .ToList(); return(await ApplyFilters(scoringsFromCacheList, searchParams)); } } var scoringsFromApi = new List <ScoringAssessmentDTO>(); var studentsBySection = await _students.GetStudentsBySectionId(searchParams.UniqueSectionCode); foreach (var student in studentsBySection) { try { var studentDTO = _mapper.MapStudentDTO(student); var associationDTO = await GetAssessmentScoringsByStudent(studentDTO, searchParams.Category); scoringsFromApi.Add(associationDTO); } catch (Exception ex) { // If an error ocurres when generating the scorings, skip and continue looping } } if (searchParams.StoreInCache) { string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, searchParams.UniqueSectionCode); bool successfullyCachedScorings = await _cache.TrySet(key, scoringsFromApi); } return(await ApplyFilters(scoringsFromApi, searchParams)); }
public async Task RefreshScoringAssessments() { var sections = await _catalog.GetSectionsAll(getFromCache : false, storeInCache : false); foreach (var section in sections) { var scoringAssessmentsBySection = await _scoringAssessments.Search(new ScoringAssessmentSearchParams { UniqueSectionCode = section.UniqueSectionCode, GetFromCache = false, StoreInCache = false, }); string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, section.UniqueSectionCode); await _cache.TrySet(key, scoringAssessmentsBySection); string refreshDateKey = CacheKeys.Composed(CacheKeys.RefreshDates.AssessmentScoringsBySectionUniqueId, section.UniqueSectionCode); await UpdateLastRefreshedDate(refreshDateKey); } }
public async Task <UserSessionProfile> GetUserByEmail(string email) { string normalizedEmail = email.Trim().ToLower(); string cacheKey = CacheKeys.Composed(CacheKeys.StaffIdByEmail, normalizedEmail); bool keyExists = await _cache.TryHasKey(cacheKey); if (!keyExists) { throw new Exception($"Staff member not cached with key {cacheKey}"); } string staffId = await _cache.GetOrDefault <string>(cacheKey); if (staffId == null) { throw new Exception($"Failed to get cached StaffId with key {cacheKey}"); } return(await GetUserByStaffId(staffId)); }
private async Task <IEnumerable <StudentInterventionsDTO> > GetStudentInterventionsDTOFromCacheOrDefaultOrThrowJsonReaderException(string uniqueSectionCode) { string key = CacheKeys.Composed(CacheKeys.InterventionScoringsBySectionUniqueId, uniqueSectionCode); if (!(await _cache.HasKey(key))) { return(null); } var studentInterventionsFromCache = await _cache.Get <IEnumerable <StudentInterventionsDTO> >(key); studentInterventionsFromCache = studentInterventionsFromCache.Select(dto => { dto.Student = _mapper.SecuredStudentDTO(dto.Student); return(dto); }).ToList(); return(studentInterventionsFromCache .OrderBy(student => student.Student.LastSurname) .ToList()); }
public async Task <Intervention> GetByIdentificationCode(string identificationCode) { var key = CacheKeys.Composed(CacheKeys.InterventionByIdentificationCode, identificationCode); if (await _cache.TryHasKey(key)) { return(await _cache.Get <Intervention>(key)); } var odsApi = await _odsApiClientProvider.NewResourcesClient(); var interventionsv3 = await odsApi.Get <IList <InterventionModelv3> >("interventions", new Dictionary <string, string> { { "interventionIdentificationCode", identificationCode }, }); var interventionv2 = interventionsv3.First().MapToInterventionV2(); await _cache.Set(key, interventionv2); return(interventionv2); }
public async Task <ActionResult <string> > GetScoringInterventionsLastRefreshedDate(string sectionUniqueCode) { string refreshDateKey = CacheKeys.Composed(CacheKeys.RefreshDates.InterventionScoringsBySectionUniqueId, sectionUniqueCode); return(await GetLastRefreshedDate(refreshDateKey)); }
public async Task <ActionResult <string> > GetStaffEmailIdPairsLastRefreshedDate(string email) { string staffIdByEmailDateRefreshKey = CacheKeys.Composed(CacheKeys.RefreshDates.StaffIdByEmail, email); return(await GetLastRefreshedDate(staffIdByEmailDateRefreshKey)); }
public async Task CacheScoringAssessmentsAll(ScoringAssessmentSearchParams searchParams, IEnumerable <ScoringAssessmentDTO> scorings) { string key = CacheKeys.Composed(CacheKeys.AssessmentScoringsBySectionUniqueId, searchParams.UniqueSectionCode); await _cache.TrySet(key, scorings); }