Store the user statistics for the performance.
This commit is contained in:
@@ -9,27 +9,27 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public bool Contains(CacheType type, int id = 0)
|
||||
public bool Contains(CacheType type, int userId = 0)
|
||||
{
|
||||
var key = GetKey(id, type);
|
||||
var key = GetKey(userId, type);
|
||||
return MemoryCache.Default[key.ToString()] != null;
|
||||
}
|
||||
|
||||
public void Delete(CacheType type, int id = 0)
|
||||
public void Delete(CacheType type, int userId = 0)
|
||||
{
|
||||
var key = GetKey(id, type);
|
||||
var key = GetKey(userId, type);
|
||||
MemoryCache.Default.Remove(key.ToString());
|
||||
}
|
||||
|
||||
public T Get<T>(CacheType type, int id = 0)
|
||||
public T Get<T>(CacheType type, int userId = 0)
|
||||
{
|
||||
var key = GetKey(id, type);
|
||||
var key = GetKey(userId, type);
|
||||
return (T)MemoryCache.Default[key.ToString()];
|
||||
}
|
||||
|
||||
public void Put(CacheType type, object value, int duration, int id = 0)
|
||||
public void Put(CacheType type, object value, int duration, int userId = 0)
|
||||
{
|
||||
var key = GetKey(id, type);
|
||||
var key = GetKey(userId, type);
|
||||
if (duration <= 0)
|
||||
throw new ArgumentException("Duration cannot be less or equal to zero", "duration");
|
||||
|
||||
@@ -41,9 +41,9 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
MemoryCache.Default.Set(key.ToString(), value, policy);
|
||||
}
|
||||
|
||||
private string GetKey(int id, CacheType type)
|
||||
private string GetKey(int userId, CacheType type)
|
||||
{
|
||||
return $"{id}-{type}";
|
||||
return $"{userId}-{type}";
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
_gearRepository.Add(newGear);
|
||||
|
||||
var userId = _identityService.ConnectedUser.Id;
|
||||
_cacheService.Delete(CacheType.Gear, id: userId);
|
||||
_cacheService.Delete(CacheType.Gear, userId: userId);
|
||||
}
|
||||
|
||||
public void AddRentalGear(User newUser)
|
||||
@@ -58,13 +58,13 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
public IEnumerable<Gear> GetAllGears()
|
||||
{
|
||||
var userId = _identityService.ConnectedUser.Id;
|
||||
if (!_cacheService.Contains(CacheType.Gear, id: userId))
|
||||
if (!_cacheService.Contains(CacheType.Gear, userId: userId))
|
||||
_cacheService.Put(CacheType.Gear,
|
||||
_gearRepository.GetAll(_identityService.ConnectedUser),
|
||||
5 * 60 * 1000,
|
||||
id: userId);
|
||||
userId: userId);
|
||||
|
||||
return _cacheService.Get<IEnumerable<Gear>>(CacheType.Gear, id: userId);
|
||||
return _cacheService.Get<IEnumerable<Gear>>(CacheType.Gear, userId: userId);
|
||||
}
|
||||
|
||||
public Gear GetGearById(int id)
|
||||
|
||||
@@ -6,13 +6,13 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
bool Contains(CacheType type, int id = 0);
|
||||
bool Contains(CacheType type, int userId = 0);
|
||||
|
||||
void Delete(CacheType type, int id = 0);
|
||||
void Delete(CacheType type, int userId = 0);
|
||||
|
||||
T Get<T>(CacheType type, int id = 0);
|
||||
T Get<T>(CacheType type, int userId = 0);
|
||||
|
||||
void Put(CacheType type, object value, int duration, int id = 0);
|
||||
void Put(CacheType type, object value, int duration, int userId = 0);
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces
|
||||
IEnumerable<Statistic> GetStatsForLastYearByDz();
|
||||
|
||||
IEnumerable<Statistic> GetStatsForLastYearByJumpType();
|
||||
void Reset();
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
IDropZoneService dropZoneService,
|
||||
IGearService gearService,
|
||||
IJumpRepository jumpRepository,
|
||||
IIdentityService identityService)
|
||||
IIdentityService identityService,
|
||||
IStatsService statsService)
|
||||
{
|
||||
_jumpTypeService = jumpTypeService;
|
||||
_aircraftService = aircraftService;
|
||||
@@ -22,6 +23,7 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
_gearService = gearService;
|
||||
_jumpRepository = jumpRepository;
|
||||
_identityService = identityService;
|
||||
_statsService = statsService;
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
@@ -46,11 +48,13 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
jump.User = _identityService.ConnectedUser;
|
||||
|
||||
_jumpRepository.Add(jump);
|
||||
_statsService.Reset();
|
||||
}
|
||||
|
||||
public void DeleteJumpById(int id)
|
||||
{
|
||||
_jumpRepository.DeleteById(id);
|
||||
_statsService.Reset();
|
||||
}
|
||||
|
||||
public IEnumerable<Jump> GetAllJumps()
|
||||
@@ -83,6 +87,7 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
private readonly IIdentityService _identityService;
|
||||
private readonly IJumpRepository _jumpRepository;
|
||||
private readonly IJumpTypeService _jumpTypeService;
|
||||
private readonly IStatsService _statsService;
|
||||
|
||||
#endregion Private Fields
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using skydiveLogs_api.Domain;
|
||||
using skydiveLogs_api.DomainBusiness.Interfaces;
|
||||
using skydiveLogs_api.DomainService.Repositories;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -9,9 +10,13 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public StatsService(IJumpService jumpService)
|
||||
public StatsService(IJumpService jumpService,
|
||||
IUserStatsRepository userStatsRepository,
|
||||
IIdentityService identityService)
|
||||
{
|
||||
_jumpService = jumpService;
|
||||
_identityService = identityService;
|
||||
_userStatsRepository = userStatsRepository;
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
@@ -40,207 +45,296 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
|
||||
public IEnumerable<Statistic> GetStatsByAircraft()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ByAircraft.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.Aircraft.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.Aircraft.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ByAircraft = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ByAircraft;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsByDz()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ByDz.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ByDz = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ByDz;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsByGear()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ByGear.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => $"{j.Gear.Name} / {j.Gear.MainCanopy}",
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => $"{j.Gear.Name} / {j.Gear.MainCanopy}",
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ByGear = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ByGear;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsByJumpType()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ByJumpType.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ByJumpType = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ByJumpType;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsByYear()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ByYear.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.JumpDate.Year,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
{
|
||||
results = allJumps.GroupBy(j => j.JumpDate.Year,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ByYear = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ByYear;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsForLastMonthByDz()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ForLastMonthByDz.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var monthOfLastJump = lastJump.JumpDate.Month;
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
|
||||
.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
if (allJumps.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var monthOfLastJump = lastJump.JumpDate.Month;
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
|
||||
.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ForLastMonthByDz = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ForLastMonthByDz;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsForLastMonthByJumpType()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ForLastMonthByJumpType.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var monthOfLastJump = lastJump.JumpDate.Month;
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
|
||||
.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
if (allJumps.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var monthOfLastJump = lastJump.JumpDate.Month;
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
|
||||
.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ForLastMonthByJumpType = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ForLastMonthByJumpType;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsForLastYearByDz()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ForLastYearByDz.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
|
||||
.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
if (allJumps.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
|
||||
.GroupBy(j => j.DropZone.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ForLastYearByDz = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ForLastYearByDz;
|
||||
}
|
||||
|
||||
public IEnumerable<Statistic> GetStatsForLastYearByJumpType()
|
||||
{
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
if (allJumps.Any())
|
||||
var allStats = GetAllStats();
|
||||
if (!allStats.ForLastYearByJumpType.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
var allJumps = _jumpService.GetAllJumps();
|
||||
var results = new List<Statistic>();
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
|
||||
.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
if (allJumps.Any())
|
||||
{
|
||||
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
|
||||
var yearOfLastJump = lastJump.JumpDate.Year;
|
||||
|
||||
results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
|
||||
.GroupBy(j => j.JumpType.Name,
|
||||
j => j,
|
||||
(groupby, jumps) => new Statistic
|
||||
{
|
||||
Label = groupby.ToString(),
|
||||
Nb = jumps.Count()
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
allStats.ForLastYearByJumpType = results;
|
||||
_userStatsRepository.Update(allStats);
|
||||
}
|
||||
|
||||
return results;
|
||||
return allStats.ForLastYearByJumpType;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
var tmp = new UserStats();
|
||||
tmp.User = _identityService.ConnectedUser;
|
||||
_userStatsRepository.Add(tmp);
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private UserStats GetAllStats()
|
||||
{
|
||||
var allStats = _userStatsRepository.GetAll(_identityService.ConnectedUser);
|
||||
if (allStats == null)
|
||||
{
|
||||
allStats = new UserStats();
|
||||
allStats.User = _identityService.ConnectedUser;
|
||||
_userStatsRepository.Add(allStats);
|
||||
}
|
||||
|
||||
return allStats;
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private readonly IIdentityService _identityService;
|
||||
private readonly IJumpService _jumpService;
|
||||
private readonly IUserStatsRepository _userStatsRepository;
|
||||
|
||||
#endregion Private Fields
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user