Feat/usage stats collection (#317)
* feat: implement anonymous usage data collection Co-authored-by: Joseph Milazzo <joseph.v.milazzo@gmail.com>
This commit is contained in:
parent
b25335acbd
commit
1c9b2572ae
23 changed files with 613 additions and 17 deletions
54
API/Services/HostedServices/StartupTasksHostedService.cs
Normal file
54
API/Services/HostedServices/StartupTasksHostedService.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace API.Services.HostedServices
|
||||
{
|
||||
public class StartupTasksHostedService : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public StartupTasksHostedService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_provider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var scope = _provider.CreateScope();
|
||||
|
||||
var taskScheduler = scope.ServiceProvider.GetRequiredService<ITaskScheduler>();
|
||||
taskScheduler.ScheduleTasks();
|
||||
|
||||
try
|
||||
{
|
||||
await ManageStartupStatsTasks(scope, taskScheduler);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//If stats startup fail the user can keep using the app
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ManageStartupStatsTasks(IServiceScope serviceScope, ITaskScheduler taskScheduler)
|
||||
{
|
||||
var settingsRepository = serviceScope.ServiceProvider.GetRequiredService<ISettingsRepository>();
|
||||
|
||||
var settingsDto = await settingsRepository.GetSettingsDtoAsync();
|
||||
|
||||
if (!settingsDto.AllowStatCollection) return;
|
||||
|
||||
taskScheduler.ScheduleStatsTasks();
|
||||
|
||||
var statsService = serviceScope.ServiceProvider.GetRequiredService<IStatsService>();
|
||||
|
||||
await statsService.CollectAndSendStatsData();
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue