added extraction statistics

This commit is contained in:
Nicolas Constant 2021-01-14 00:38:26 -05:00
parent 1d584b0df3
commit dea3089c06
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
6 changed files with 110 additions and 6 deletions

View file

@ -0,0 +1,80 @@
using System.Threading;
using System.Timers;
namespace BirdsiteLive.Domain.Statistics
{
public interface IExtractionStatisticsHandler
{
void ExtractedDescription(int mentionsCount);
void ExtractedStatus(int mentionsCount);
ExtractionStatistics GetStatistics();
}
public class ExtractionStatisticsHandler : IExtractionStatisticsHandler
{
private static int _lastDescriptionMentionsExtracted;
private static int _lastStatusMentionsExtracted;
private static int _descriptionMentionsExtracted;
private static int _statusMentionsExtracted;
private static System.Timers.Timer _resetTimer;
#region Ctor
public ExtractionStatisticsHandler()
{
if (_resetTimer == null)
{
_resetTimer = new System.Timers.Timer();
_resetTimer.Elapsed += OnTimeResetEvent;
_resetTimer.Interval = 24 * 60 * 60 * 1000; // 24h
_resetTimer.Enabled = true;
}
}
#endregion
private void OnTimeResetEvent(object sender, ElapsedEventArgs e)
{
_lastDescriptionMentionsExtracted = _descriptionMentionsExtracted;
_lastStatusMentionsExtracted = _statusMentionsExtracted;
// Reset
Interlocked.Exchange(ref _descriptionMentionsExtracted, 0);
Interlocked.Exchange(ref _statusMentionsExtracted, 0);
}
public void ExtractedDescription(int mentionsCount)
{
Interlocked.Increment(ref _descriptionMentionsExtracted);
}
public void ExtractedStatus(int mentionsCount)
{
Interlocked.Increment(ref _statusMentionsExtracted);
}
public ExtractionStatistics GetStatistics()
{
return new ExtractionStatistics(_descriptionMentionsExtracted, _statusMentionsExtracted, _lastDescriptionMentionsExtracted, _lastStatusMentionsExtracted);
}
}
public class ExtractionStatistics
{
#region Ctor
public ExtractionStatistics(int mentionsInDescriptionsExtraction, int mentionsInStatusesExtraction, int lastMentionsInDescriptionsExtraction, int lastMentionsInStatusesExtraction)
{
MentionsInDescriptionsExtraction = mentionsInDescriptionsExtraction;
MentionsInStatusesExtraction = mentionsInStatusesExtraction;
LastMentionsInDescriptionsExtraction = lastMentionsInDescriptionsExtraction;
LastMentionsInStatusesExtraction = lastMentionsInStatusesExtraction;
}
#endregion
public int MentionsInDescriptionsExtraction { get; }
public int MentionsInStatusesExtraction { get; }
public int LastMentionsInDescriptionsExtraction { get; }
public int LastMentionsInStatusesExtraction { get; }
}
}

View file

@ -7,6 +7,7 @@ using BirdsiteLive.ActivityPub;
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.ActivityPub.Models;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Domain.Statistics;
using BirdsiteLive.Domain.Tools;
using BirdsiteLive.Twitter.Models;
using Tweetinvi.Models;
@ -23,12 +24,14 @@ namespace BirdsiteLive.Domain
{
private readonly InstanceSettings _instanceSettings;
private readonly IStatusExtractor _statusExtractor;
private readonly IExtractionStatisticsHandler _statisticsHandler;
#region Ctor
public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor)
public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler)
{
_instanceSettings = instanceSettings;
_statusExtractor = statusExtractor;
_statisticsHandler = statisticsHandler;
}
#endregion
@ -41,6 +44,7 @@ namespace BirdsiteLive.Domain
var apPublic = "https://www.w3.org/ns/activitystreams#Public";
var extractedTags = _statusExtractor.ExtractTags(tweet.MessageContent);
_statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention"));
string inReplyTo = null;
if (tweet.InReplyToStatusId != default)

View file

@ -10,6 +10,7 @@ using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Cryptography;
using BirdsiteLive.Domain.BusinessUseCases;
using BirdsiteLive.Domain.Statistics;
using BirdsiteLive.Domain.Tools;
using BirdsiteLive.Twitter.Models;
using Tweetinvi.Core.Exceptions;
@ -33,9 +34,10 @@ namespace BirdsiteLive.Domain
private readonly ICryptoService _cryptoService;
private readonly IActivityPubService _activityPubService;
private readonly IStatusExtractor _statusExtractor;
private readonly IExtractionStatisticsHandler _statisticsHandler;
#region Ctor
public UserService(InstanceSettings instanceSettings, ICryptoService cryptoService, IActivityPubService activityPubService, IProcessFollowUser processFollowUser, IProcessUndoFollowUser processUndoFollowUser, IStatusExtractor statusExtractor)
public UserService(InstanceSettings instanceSettings, ICryptoService cryptoService, IActivityPubService activityPubService, IProcessFollowUser processFollowUser, IProcessUndoFollowUser processUndoFollowUser, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler)
{
_instanceSettings = instanceSettings;
_cryptoService = cryptoService;
@ -43,6 +45,7 @@ namespace BirdsiteLive.Domain
_processFollowUser = processFollowUser;
_processUndoFollowUser = processUndoFollowUser;
_statusExtractor = statusExtractor;
_statisticsHandler = statisticsHandler;
//_host = $"https://{instanceSettings.Domain.Replace("https://",string.Empty).Replace("http://", string.Empty).TrimEnd('/')}";
}
#endregion
@ -58,6 +61,8 @@ namespace BirdsiteLive.Domain
{
var extracted = _statusExtractor.ExtractTags(description);
description = extracted.content;
_statisticsHandler.ExtractedDescription(extracted.tags.Count(x => x.type == "Mention"));
}
var user = new Actor

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Domain.Statistics;
using BirdsiteLive.Statistics.Domain;
using Microsoft.AspNetCore.Mvc;
@ -13,13 +14,16 @@ namespace BirdsiteLive.Controllers
private readonly ITwitterUserDal _twitterUserDal;
private readonly IFollowersDal _followersDal;
private readonly ITwitterStatisticsHandler _twitterStatistics;
private readonly IExtractionStatisticsHandler _extractionStatistics;
#region Ctor
public StatisticsController(ITwitterUserDal twitterUserDal, IFollowersDal followersDal, ITwitterStatisticsHandler twitterStatistics)
public StatisticsController(ITwitterUserDal twitterUserDal, IFollowersDal followersDal, ITwitterStatisticsHandler twitterStatistics, IExtractionStatisticsHandler extractionStatistics)
{
_twitterUserDal = twitterUserDal;
_followersDal = followersDal;
_twitterStatistics = twitterStatistics;
_extractionStatistics = extractionStatistics;
}
#endregion
@ -29,7 +33,8 @@ namespace BirdsiteLive.Controllers
{
FollowersCount = await _followersDal.GetFollowersCountAsync(),
TwitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync(),
TwitterStatistics = _twitterStatistics.GetStatistics()
TwitterStatistics = _twitterStatistics.GetStatistics(),
ExtractionStatistics = _extractionStatistics.GetStatistics(),
};
return View(stats);
}

View file

@ -1,4 +1,5 @@
using BirdsiteLive.Twitter.Models;
using BirdsiteLive.Domain.Statistics;
using BirdsiteLive.Twitter.Models;
namespace BirdsiteLive.Models.StatisticsModels
{
@ -7,5 +8,6 @@ namespace BirdsiteLive.Models.StatisticsModels
public int FollowersCount { get; set; }
public int TwitterUserCount { get; set; }
public ApiStatistics TwitterStatistics { get; set; }
public ExtractionStatistics ExtractionStatistics { get; set; }
}
}

View file

@ -17,4 +17,12 @@
<li>Users Calls: @Model.TwitterStatistics.UserCallsCountMin ; @Model.TwitterStatistics.UserCallsCountAvg ; @Model.TwitterStatistics.UserCallsCountMax / @Model.TwitterStatistics.UserCallsMax</li>
<li>Tweets Calls: @Model.TwitterStatistics.TweetCallsCountMin ; @Model.TwitterStatistics.TweetCallsCountAvg ; @Model.TwitterStatistics.TweetCallsCountMax / @Model.TwitterStatistics.TweetCallsMax</li>
<li>Timeline Calls: @Model.TwitterStatistics.TimelineCallsCountMin ; @Model.TwitterStatistics.TimelineCallsCountAvg ; @Model.TwitterStatistics.TimelineCallsCountMax / @Model.TwitterStatistics.TimelineCallsMax</li>
</ul>
<h4>Mentions Extraction</h4>
<ul>
<li>Current day in descriptions: @Model.ExtractionStatistics.MentionsInDescriptionsExtraction</li>
<li>Current day in statuses: @Model.ExtractionStatistics.MentionsInStatusesExtraction</li>
<li>Last day in descriptions: @Model.ExtractionStatistics.LastMentionsInDescriptionsExtraction</li>
<li>Last day in statuses: @Model.ExtractionStatistics.LastMentionsInStatusesExtraction</li>
</ul>