Merge pull request #23 from NicolasConstant/develop

0.2.0 PR
This commit is contained in:
Nicolas Constant 2020-12-31 03:38:53 +01:00 committed by GitHub
commit f70cf6b5c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 164 additions and 7 deletions

View file

@ -0,0 +1,8 @@
namespace BirdsiteLive.Common.Settings
{
public class LogsSettings
{
public string Type { get; set; }
public string InstrumentationKey { get; set; }
}
}

View file

@ -4,11 +4,12 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.1.0</Version>
<Version>0.2.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lamar.Microsoft.DependencyInjection" Version="4.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
</ItemGroup>

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Models.StatisticsModels;
using Microsoft.AspNetCore.Mvc;
namespace BirdsiteLive.Controllers
{
public class StatisticsController : Controller
{
private readonly ITwitterUserDal _twitterUserDal;
private readonly IFollowersDal _followersDal;
#region Ctor
public StatisticsController(ITwitterUserDal twitterUserDal, IFollowersDal followersDal)
{
_twitterUserDal = twitterUserDal;
_followersDal = followersDal;
}
#endregion
public async Task<IActionResult> Index()
{
var stats = new Statistics
{
FollowersCount = await _followersDal.GetFollowersCountAsync(),
TwitterUserCount = await _twitterUserDal.GetTwitterUsersCountAsync()
};
return View(stats);
}
}
}

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Models;
using BirdsiteLive.Models.WellKnownModels;
using BirdsiteLive.Twitter;
@ -15,12 +16,14 @@ namespace BirdsiteLive.Controllers
public class WellKnownController : ControllerBase
{
private readonly ITwitterService _twitterService;
private readonly ITwitterUserDal _twitterUserDal;
private readonly InstanceSettings _settings;
#region Ctor
public WellKnownController(InstanceSettings settings, ITwitterService twitterService)
public WellKnownController(InstanceSettings settings, ITwitterService twitterService, ITwitterUserDal twitterUserDal)
{
_twitterService = twitterService;
_twitterUserDal = twitterUserDal;
_settings = settings;
}
#endregion
@ -48,9 +51,10 @@ namespace BirdsiteLive.Controllers
}
[Route("/nodeinfo/{id}.json")]
public IActionResult NodeInfo(string id)
public async Task<IActionResult> NodeInfo(string id)
{
var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(3);
var twitterUsersCount = await _twitterUserDal.GetTwitterUsersCountAsync();
if (id == "2.0")
{
@ -62,7 +66,7 @@ namespace BirdsiteLive.Controllers
localPosts = 0,
users = new Users()
{
total = 0
total = twitterUsersCount
}
},
software = new Software()
@ -97,7 +101,7 @@ namespace BirdsiteLive.Controllers
localPosts = 0,
users = new Users()
{
total = 0
total = twitterUsersCount
}
},
software = new SoftwareV21()

View file

@ -0,0 +1,8 @@
namespace BirdsiteLive.Models.StatisticsModels
{
public class Statistics
{
public int FollowersCount { get; set; }
public int TwitterUserCount { get; set; }
}
}

View file

@ -38,8 +38,12 @@ namespace BirdsiteLive
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.Configure<InstanceSettings>(Configuration.GetSection("Instance"));
//services.Configure<TwitterSettings>(Configuration.GetSection("Twitter"));
var logsSettings = Configuration.GetSection("Logging").Get<LogsSettings>();
if(string.Equals("insights", logsSettings.Type, StringComparison.OrdinalIgnoreCase))
{
var key = logsSettings.InstrumentationKey;
services.AddApplicationInsightsTelemetry(key);
}
services.AddControllersWithViews();
}
@ -55,6 +59,9 @@ namespace BirdsiteLive
var dbSettings = Configuration.GetSection("Db").Get<DbSettings>();
services.For<DbSettings>().Use(x => dbSettings);
var logsSettings = Configuration.GetSection("Logging").Get<LogsSettings>();
services.For<LogsSettings>().Use(x => logsSettings);
if (string.Equals(dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase))
{
var connString = $"Host={dbSettings.Host};Username={dbSettings.User};Password={dbSettings.Password};Database={dbSettings.Name}";

View file

@ -0,0 +1,12 @@
@model BirdsiteLive.Models.StatisticsModels.Statistics
@{
ViewBag.Title = "Statistics";
}
<h2>Statistics</h2>
<ul>
<li>Twitter Users: @Model.TwitterUserCount</li>
<li>Followers: @Model.FollowersCount</li>
</ul>

View file

@ -1,5 +1,7 @@
{
"Logging": {
"Type": "none",
"InstrumentationKey": "key",
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",

View file

@ -40,6 +40,19 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task<int> GetFollowersCountAsync()
{
var query = $"SELECT COUNT(*) FROM {_settings.FollowersTableName}";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = (await dbConnection.QueryAsync<int>(query)).FirstOrDefault();
return result;
}
}
public async Task<Follower> GetFollowerAsync(string acct, string host)
{
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host";

View file

@ -49,6 +49,19 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task<int> GetTwitterUsersCountAsync()
{
var query = $"SELECT COUNT(*) FROM {_settings.TwitterUserTableName}";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = (await dbConnection.QueryAsync<int>(query)).FirstOrDefault();
return result;
}
}
public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync()
{
var query = $"SELECT * FROM {_settings.TwitterUserTableName}";

View file

@ -13,5 +13,6 @@ namespace BirdsiteLive.DAL.Contracts
Task UpdateFollowerAsync(Follower follower);
Task DeleteFollowerAsync(int id);
Task DeleteFollowerAsync(string acct, string host);
Task<int> GetFollowersCountAsync();
}
}

View file

@ -10,5 +10,6 @@ namespace BirdsiteLive.DAL.Contracts
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync();
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId);
Task DeleteTwitterUserAsync(string acct);
Task<int> GetTwitterUsersCountAsync();
}
}

View file

@ -137,6 +137,43 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
Assert.AreEqual(0, result.Length);
}
[TestMethod]
public async Task CountFollowersAsync()
{
var dal = new FollowersPostgresDal(_settings);
var result = await dal.GetFollowersCountAsync();
Assert.AreEqual(0, result);
//User 1
var acct = "myhandle1";
var host = "domain.ext";
var following = new[] { 1, 2, 3 };
var followingSync = new Dictionary<int, long>();
var inboxRoute = "/myhandle1/inbox";
var sharedInboxRoute = "/inbox";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
//User 2
acct = "myhandle2";
host = "domain.ext";
following = new[] { 2, 4, 5 };
inboxRoute = "/myhandle2/inbox";
sharedInboxRoute = "/inbox2";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
//User 2
acct = "myhandle3";
host = "domain.ext";
following = new[] { 1 };
inboxRoute = "/myhandle3/inbox";
sharedInboxRoute = "/inbox3";
await dal.CreateFollowerAsync(acct, host, inboxRoute, sharedInboxRoute, following, followingSync);
result = await dal.GetFollowersCountAsync();
Assert.AreEqual(3, result);
}
[TestMethod]
public async Task CreateUpdateAndGetFollower_Add()
{

View file

@ -115,5 +115,21 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
Assert.IsFalse(result[0].LastTweetPostedId == default);
Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default);
}
[TestMethod]
public async Task CountTwitterUsers()
{
var dal = new TwitterUserPostgresDal(_settings);
for (var i = 0; i < 10; i++)
{
var acct = $"myid{i}";
var lastTweetId = 1548L;
await dal.CreateTwitterUserAsync(acct, lastTweetId);
}
var result = await dal.GetTwitterUsersCountAsync();
Assert.AreEqual(10, result);
}
}
}