diff --git a/src/BirdsiteLive.Domain/Statistics/ExtractionStatisticsHandler.cs b/src/BirdsiteLive.Domain/Statistics/ExtractionStatisticsHandler.cs new file mode 100644 index 0000000..b9446df --- /dev/null +++ b/src/BirdsiteLive.Domain/Statistics/ExtractionStatisticsHandler.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index ab48e92..a1b3d38 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -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) diff --git a/src/BirdsiteLive.Domain/UserService.cs b/src/BirdsiteLive.Domain/UserService.cs index 3961bf4..6e498af 100644 --- a/src/BirdsiteLive.Domain/UserService.cs +++ b/src/BirdsiteLive.Domain/UserService.cs @@ -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 diff --git a/src/BirdsiteLive/Controllers/StatisticsController.cs b/src/BirdsiteLive/Controllers/StatisticsController.cs index 58e4a7a..5a9aef8 100644 --- a/src/BirdsiteLive/Controllers/StatisticsController.cs +++ b/src/BirdsiteLive/Controllers/StatisticsController.cs @@ -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); } diff --git a/src/BirdsiteLive/Models/StatisticsModels/Statistics.cs b/src/BirdsiteLive/Models/StatisticsModels/Statistics.cs index 59cff91..a2dd693 100644 --- a/src/BirdsiteLive/Models/StatisticsModels/Statistics.cs +++ b/src/BirdsiteLive/Models/StatisticsModels/Statistics.cs @@ -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; } } } \ No newline at end of file diff --git a/src/BirdsiteLive/Views/Statistics/Index.cshtml b/src/BirdsiteLive/Views/Statistics/Index.cshtml index 8b4f57a..9a0b536 100644 --- a/src/BirdsiteLive/Views/Statistics/Index.cshtml +++ b/src/BirdsiteLive/Views/Statistics/Index.cshtml @@ -17,4 +17,12 @@