2021-02-17 20:49:17 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-02-18 21:48:52 -05:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
2021-02-18 21:30:13 -05:00
|
|
|
|
using BirdsiteLive.Domain.Repository;
|
2021-02-18 21:48:52 -05:00
|
|
|
|
using BirdsiteLive.Statistics.Domain;
|
2021-02-17 20:49:17 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-02-18 22:04:47 -05:00
|
|
|
|
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
2021-02-17 20:49:17 -05:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Component
|
|
|
|
|
{
|
|
|
|
|
public class NodeInfoViewComponent : ViewComponent
|
|
|
|
|
{
|
2021-02-18 21:30:13 -05:00
|
|
|
|
private readonly IModerationRepository _moderationRepository;
|
2021-02-18 21:48:52 -05:00
|
|
|
|
private readonly ITwitterStatisticsHandler _twitterStatisticsHandler;
|
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
2021-02-18 21:30:13 -05:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-02-18 21:48:52 -05:00
|
|
|
|
public NodeInfoViewComponent(IModerationRepository moderationRepository, ITwitterStatisticsHandler twitterStatisticsHandler, ITwitterUserDal twitterUserDal)
|
2021-02-18 21:30:13 -05:00
|
|
|
|
{
|
|
|
|
|
_moderationRepository = moderationRepository;
|
2021-02-18 21:48:52 -05:00
|
|
|
|
_twitterStatisticsHandler = twitterStatisticsHandler;
|
|
|
|
|
_twitterUserDal = twitterUserDal;
|
2021-02-18 21:30:13 -05:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-02-18 21:48:52 -05:00
|
|
|
|
public async Task<IViewComponentResult> InvokeAsync()
|
2021-02-17 20:49:17 -05:00
|
|
|
|
{
|
2021-02-18 21:30:13 -05:00
|
|
|
|
var followerPolicy = _moderationRepository.GetModerationType(ModerationEntityTypeEnum.Follower);
|
|
|
|
|
var twitterAccountPolicy = _moderationRepository.GetModerationType(ModerationEntityTypeEnum.TwitterAccount);
|
|
|
|
|
|
2021-02-18 22:04:47 -05:00
|
|
|
|
var statistics = await GetStatisticsAsync();
|
2021-02-18 21:48:52 -05:00
|
|
|
|
|
2021-02-18 21:30:13 -05:00
|
|
|
|
var viewModel = new NodeInfoViewModel
|
|
|
|
|
{
|
|
|
|
|
BlacklistingEnabled = followerPolicy == ModerationTypeEnum.BlackListing ||
|
|
|
|
|
twitterAccountPolicy == ModerationTypeEnum.BlackListing,
|
|
|
|
|
WhitelistingEnabled = followerPolicy == ModerationTypeEnum.WhiteListing ||
|
|
|
|
|
twitterAccountPolicy == ModerationTypeEnum.WhiteListing,
|
2021-02-18 22:04:47 -05:00
|
|
|
|
InstanceSaturation = statistics.Saturation
|
2021-02-18 21:30:13 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-02-18 21:48:52 -05:00
|
|
|
|
//viewModel = new NodeInfoViewModel
|
|
|
|
|
//{
|
|
|
|
|
// BlacklistingEnabled = false,
|
|
|
|
|
// WhitelistingEnabled = false,
|
|
|
|
|
// InstanceSaturation = 175
|
|
|
|
|
//};
|
2021-02-18 21:30:13 -05:00
|
|
|
|
return View(viewModel);
|
2021-02-17 20:49:17 -05:00
|
|
|
|
}
|
2021-02-18 22:04:47 -05:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 20:49:17 -05:00
|
|
|
|
}
|
2021-02-18 21:30:13 -05:00
|
|
|
|
|
|
|
|
|
public class NodeInfoViewModel
|
|
|
|
|
{
|
|
|
|
|
public bool BlacklistingEnabled { get; set; }
|
|
|
|
|
public bool WhitelistingEnabled { get; set; }
|
|
|
|
|
public int InstanceSaturation { get; set; }
|
|
|
|
|
}
|
2021-02-17 20:49:17 -05:00
|
|
|
|
}
|