using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using ResourcesManager.Models; using ResourcesManager.ViewModels; using System.Data.Entity.Core.Objects; using ResourcesManager.Interfaces; using ResourcesManager.Repositories; using ResourcesManager.Handlers; namespace ResourcesManager.Controllers { public class StatisticsController : Controller { private ApplicationDbContext context; private IStatisticRepository statisticRepository; private IResourceRepository resourceRepo; private IUserRepository userRepo; readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public StatisticsController() { context = new ApplicationDbContext(); statisticRepository = new StatisticRepository(context); resourceRepo = new ResourceRepository(context); userRepo = new UserRepository(context); } public StatisticsController(IStatisticRepository statisticRepository, IResourceRepository resourceRepo, IUserRepository userRepo) { this.statisticRepository = statisticRepository; this.resourceRepo = resourceRepo; this.userRepo = userRepo; } public ActionResult Index(StatisticType? statTypeId, int? resId) { try { if (statTypeId == null) { statTypeId = Models.StatisticType.Reserv; } if (resId == null) { resId = resourceRepo.GetResouces().FirstOrDefault().Id; } var statistics = statisticRepository.GetStatistics(); var model = new StatisticViewModel(); var today = DateTime.Today.AddDays(-30.0); model.ByType = statistics.Where(k => k.StatisticType == statTypeId && k.DateTime > today)?.GroupBy(l => l.DateTime.Date)?.Select(j => new ByType() { Name = (DateTime)j.Key, Value = j.Count() })?.ToList(); model.ByResource = statistics.Where(k => k.ResourceId == resId & k.DateTime > today)?.GroupBy(l => l.DateTime.Date)?.Select(j => new ByResource() { Name = (DateTime)j.Key, Value = j.Count() })?.ToList(); model.ByTypeCircle = statistics.GroupBy(l => l.StatisticType).Select(j => new ByTypeCircle() { Name = ((StatisticType)j.Key).ToString(), Value = j.Count() }).ToList(); var subscibedUser = resourceRepo.GetResouces().Select(k => k.Users.Count()).Sum(); var allUsers = userRepo.GetUsers().Count() * resourceRepo.GetResouces().Count(); model.Subscribe = new List(); model.Subscribe.Add(new Subscribe() { Name = "Subscribed user", Value = subscibedUser }); model.Subscribe.Add(new Subscribe() { Name = "Unsubscibed user", Value = allUsers - subscibedUser }); model.Resources = new SelectList(resourceRepo.GetResouces(), "Id", "Name", resId); model.StatisticType = new SelectList(Enum.GetValues(typeof(StatisticType)).Cast(), (int)statTypeId); return View(model); } catch (Exception ex) { log.Error(ex); return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } } protected override void Dispose(bool disposing) { if (disposing) { context.Dispose(); statisticRepository.Dispose(); resourceRepo.Dispose(); userRepo.Dispose(); } base.Dispose(disposing); } } }