added node stats caching

This commit is contained in:
Nicolas Constant 2021-02-18 22:04:47 -05:00
parent 45255fa39d
commit ed8242834e
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 29 additions and 4 deletions

View File

@ -6,6 +6,7 @@ using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Domain.Repository;
using BirdsiteLive.Statistics.Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
namespace BirdsiteLive.Component
{
@ -29,9 +30,7 @@ namespace BirdsiteLive.Component
var followerPolicy = _moderationRepository.GetModerationType(ModerationEntityTypeEnum.Follower);
var twitterAccountPolicy = _moderationRepository.GetModerationType(ModerationEntityTypeEnum.TwitterAccount);
var twitterUserMax = _twitterStatisticsHandler.GetStatistics().UserCallsMax;
var twitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync();
var saturation = (int)((double)twitterUserCount / twitterUserMax * 100);
var statistics = await GetStatisticsAsync();
var viewModel = new NodeInfoViewModel
{
@ -39,7 +38,7 @@ namespace BirdsiteLive.Component
twitterAccountPolicy == ModerationTypeEnum.BlackListing,
WhitelistingEnabled = followerPolicy == ModerationTypeEnum.WhiteListing ||
twitterAccountPolicy == ModerationTypeEnum.WhiteListing,
InstanceSaturation = saturation,
InstanceSaturation = statistics.Saturation
};
//viewModel = new NodeInfoViewModel
@ -50,6 +49,32 @@ namespace BirdsiteLive.Component
//};
return View(viewModel);
}
private static CachedStatistics _cachedStatistics;
private async Task<CachedStatistics> GetStatisticsAsync() {
if (_cachedStatistics == null ||
(DateTime.UtcNow - _cachedStatistics.RefreshedTime).TotalMinutes > 15)
{
var twitterUserMax = _twitterStatisticsHandler.GetStatistics().UserCallsMax;
var twitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync();
var saturation = (int)((double)twitterUserCount / twitterUserMax * 100);
_cachedStatistics = new CachedStatistics
{
RefreshedTime = DateTime.UtcNow,
Saturation = saturation
};
}
return _cachedStatistics;
}
class CachedStatistics
{
public DateTime RefreshedTime { get; set; }
public int Saturation { get; set; }
}
}
public class NodeInfoViewModel