示例#1
0
        public async Task <ScatterplotViewModel> GetNationalScatterplotData()
        {
            _logger.LogInformation("Executing GetNationalScatterplotData method in RedisCache class.");

            ScatterplotViewModel nationalData = null;
            bool dataInCache = false;

            try
            {
                //Check if National ScatterplotViewModel data is in cache
                dataInCache = await _redisCacheClient.Db0.ExistsAsync("NationalScatterplotViewModel");

                if (dataInCache)
                {
                    nationalData = await _redisCacheClient
                                   .Db0
                                   .GetAsync <ScatterplotViewModel>("NationalScatterplotViewModel");
                }
            }
            catch (Exception e)
            {
                _logger.LogError("An exception occurred when attempting to save NationalScatterplotViewModel data to cache." +
                                 " Stack trace: " + e.StackTrace);
            }

            return(nationalData);
        }
示例#2
0
        public async Task GetNationalScatterplotDataReturnsEmptyObjectIfDataIsNotInCache()
        {
            //Arrange
            SetupRedisCacheClient();

            //Act
            ScatterplotViewModel result = await _redisCache.GetNationalScatterplotData();

            //Assert
            Assert.IsNull(result);
        }
示例#3
0
        public async Task SaveNationalScatterplotData(ScatterplotViewModel nationalScatterplotData)
        {
            _logger.LogInformation("Executing SaveNationalScatterplotData method in RedisCache class.");

            try
            {
                await _redisCacheClient
                .Db0
                .AddAsync("NationalScatterplotData", nationalScatterplotData, DateTimeOffset.Now.AddMinutes(30));
            }
            catch (Exception e)
            {
                _logger.LogError("An exception occurred when attempting to save NationalScatterplotData data to cache." +
                                 " Stack trace: " + e.StackTrace);
            }
        }
        public void ResultModelToScatterplotViewModel()
        {
            //Arrange
            SchoolResult result = MockData.GetSchoolResult(true);

            //Act
            ScatterplotViewModel resultViewModel = result;

            //Assert

            //Checks the SchoolResult object gets converted to ScatterplotViewModel
            Assert.IsNotNull(resultViewModel);

            //Checks the School name in the school model is included
            //when converting from SchoolResult to ScatterplotViewModel
            Assert.AreEqual(result.School.SCHNAME, resultViewModel.SCHNAME);
        }
        public async Task <IActionResult> Index()
        {
            _logger.LogInformation("Request made to view Scatterplot page");

            //Check if data is in cache and if so get the data from cache
            IEnumerable <ScatterplotViewModel> schoolLst = await _cache.GetScatterplotData();

            ScatterplotViewModel national = await _cache.GetNationalScatterplotData();

            //if data is not in cache get data from database and save data in cache
            if (schoolLst.Count() == 0)
            {
                _logger.LogInformation("Scatterplot data is being retrieved from the database");

                var result = await _result.Get(r => r.PTFSM6CLA1A != null,
                                               r => r.OrderBy(s => s.School.SCHNAME),
                                               r => r.School);

                //Converts from list of SchoolResult to List of ScatterplotViewModel
                schoolLst = result.ConvertToScatterplotViewModel();

                await _cache.SaveScatterplotData(schoolLst);
            }

            //if national data is not in cache get data from database and save data in cache
            if (national == null)
            {
                _logger.LogInformation("National Scatterplot data is being retrieved from the database");

                var result = await _result.GetNational(r => r.School);

                national = result.First();

                await _cache.SaveNationalScatterplotData(national);
            }

            var resultViewModel = new ScatterplotListViewModel()
            {
                schoolData   = schoolLst,
                nationalData = national,
            };

            return(View(resultViewModel));
        }