-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from NicolasConstant/develop
0.6.0 PR
- Loading branch information
Showing
7 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace BirdsiteLive.Twitter.Models | ||
{ | ||
public class ApiStatistics | ||
{ | ||
public int UserCallsCount { get; set; } | ||
public int UserCallsMax { get; set; } | ||
public int TweetCallsCount { get; set; } | ||
public int TweetCallsMax { get; set; } | ||
public int TimelineCallsCount { get; set; } | ||
public int TimelineCallsMax { get; set; } | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/BirdsiteLive.Twitter/Statistics/TwitterStatisticsHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Threading; | ||
using System.Timers; | ||
using BirdsiteLive.Twitter.Models; | ||
|
||
namespace BirdsiteLive.Statistics.Domain | ||
{ | ||
public interface ITwitterStatisticsHandler | ||
{ | ||
void CalledUserApi(); | ||
void CalledTweetApi(); | ||
void CalledTimelineApi(); | ||
ApiStatistics GetStatistics(); | ||
} | ||
|
||
//Rate limits: https://developer.twitter.com/en/docs/twitter-api/v1/rate-limits | ||
public class TwitterStatisticsHandler : ITwitterStatisticsHandler | ||
{ | ||
private static int _previousUserCalls; | ||
private static int _previousTweetCalls; | ||
private static int _previousTimelineCalls; | ||
|
||
private static int _userCalls; | ||
private static int _tweetCalls; | ||
private static int _timelineCalls; | ||
|
||
private static System.Timers.Timer _resetTimer; | ||
|
||
#region Ctor | ||
public TwitterStatisticsHandler() | ||
{ | ||
if (_resetTimer == null) | ||
{ | ||
_resetTimer = new System.Timers.Timer(); | ||
_resetTimer.Elapsed += OnTimeResetEvent; | ||
_resetTimer.Interval = 15 * 60 * 1000; // 15" | ||
_resetTimer.Enabled = true; | ||
} | ||
} | ||
#endregion | ||
|
||
private void OnTimeResetEvent(object sender, ElapsedEventArgs e) | ||
{ | ||
_previousUserCalls = _userCalls; | ||
_previousTweetCalls = _tweetCalls; | ||
_previousTimelineCalls = _timelineCalls; | ||
|
||
Interlocked.Exchange(ref _userCalls, 0); | ||
Interlocked.Exchange(ref _tweetCalls, 0); | ||
Interlocked.Exchange(ref _timelineCalls, 0); | ||
} | ||
|
||
public void CalledUserApi() //GET users/show - 900/15mins | ||
{ | ||
Interlocked.Increment(ref _userCalls); | ||
} | ||
|
||
public void CalledTweetApi() //GET statuses/lookup - 300/15mins | ||
{ | ||
Interlocked.Increment(ref _tweetCalls); | ||
} | ||
|
||
public void CalledTimelineApi() // GET statuses/user_timeline - 1500/15 mins | ||
{ | ||
Interlocked.Increment(ref _timelineCalls); | ||
} | ||
|
||
public ApiStatistics GetStatistics() | ||
{ | ||
return new ApiStatistics | ||
{ | ||
UserCallsCount = _previousUserCalls, | ||
UserCallsMax = 900, | ||
TweetCallsCount = _previousTweetCalls, | ||
TweetCallsMax = 300, | ||
TimelineCallsCount = _previousTimelineCalls, | ||
TimelineCallsMax = 1500 | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
namespace BirdsiteLive.Models.StatisticsModels | ||
using BirdsiteLive.Twitter.Models; | ||
|
||
namespace BirdsiteLive.Models.StatisticsModels | ||
{ | ||
public class Statistics | ||
{ | ||
public int FollowersCount { get; set; } | ||
public int TwitterUserCount { get; set; } | ||
public ApiStatistics TwitterStatistics { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters