Added scan history.

This commit is contained in:
Steven Hildreth 2018-12-08 21:16:05 -06:00
parent 43ff3ef654
commit 4fcbe4c990
8 changed files with 109 additions and 1 deletions

View file

@ -31,5 +31,12 @@ namespace Roadie.Api.Controllers
{
return Ok(await this.StatisticsService.LibraryStatistics());
}
[HttpGet("releasesByDate")]
[ProducesResponseType(200)]
public async Task<IActionResult> ReleasesByDate()
{
return Ok(await this.StatisticsService.ReleasesByDate());
}
}
}

View file

@ -1,5 +1,6 @@
using Roadie.Library;
using Roadie.Library.Models.Statistics;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Roadie.Api.Services
@ -7,5 +8,6 @@ namespace Roadie.Api.Services
public interface IStatisticsService
{
Task<OperationResult<LibraryStats>> LibraryStatistics();
Task<OperationResult<IEnumerable<DateAndCount>>> ReleasesByDate();
}
}

View file

@ -7,7 +7,9 @@ using Roadie.Library.Encoding;
using Roadie.Library.Models.Statistics;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
@ -25,6 +27,58 @@ namespace Roadie.Api.Services
{
}
public async Task<OperationResult<IEnumerable<DateAndCount>>> ReleasesByDate()
{
var sw = new Stopwatch();
sw.Start();
var result = new List<DateAndCount>();
using (var conn = new MySqlConnection(this.Configuration.ConnectionString))
{
conn.Open();
var sql = @"SELECT DATE_FORMAT(createdDate, '%Y-%m-%d') as date, count(1) as count
FROM `release`
group by DATE_FORMAT(createdDate, '%Y-%m-%d')
order by createdDate;";
using (var cmd = new MySqlCommand(sql, conn))
{
try
{
using (var rdr = cmd.ExecuteReader())
{
if (rdr.HasRows)
{
while (rdr.Read())
{
result.Add(new DateAndCount
{
Date = SafeParser.ToString(rdr["date"]),
Count = SafeParser.ToNumber<int?>(rdr["count"])
});
}
}
}
}
catch (Exception ex)
{
this.Logger.LogError(ex);
}
finally
{
conn.Close();
}
}
}
sw.Stop();
return new OperationResult<IEnumerable<DateAndCount>>
{
OperationTime = sw.ElapsedMilliseconds,
Data = result
};
}
public async Task<OperationResult<LibraryStats>> LibraryStatistics()
{
LibraryStats result = null;
@ -102,7 +156,11 @@ namespace Roadie.Api.Services
}
}
}
var lastScan = this.DbContext.ScanHistories.OrderByDescending(x => x.CreatedDate).FirstOrDefault();
if(lastScan != null)
{
result.LastScan = lastScan.CreatedDate;
}
sw.Stop();
return new OperationResult<LibraryStats>
{

View file

@ -32,6 +32,7 @@ namespace Roadie.Library.Data
DbSet<ReleaseMedia> ReleaseMedias { get; set; }
DbSet<Release> Releases { get; set; }
DbSet<Request> Requests { get; set; }
DbSet<ScanHistory> ScanHistories { get; set; }
DbSet<Submission> Submissions { get; set; }
DbSet<Track> Tracks { get; set; }
DbSet<UserArtist> UserArtists { get; set; }

View file

@ -26,6 +26,7 @@ namespace Roadie.Library.Data
public DbSet<ReleaseMedia> ReleaseMedias { get; set; }
public DbSet<Release> Releases { get; set; }
public DbSet<Request> Requests { get; set; }
public DbSet<ScanHistory> ScanHistories { get; set; }
public DbSet<Submission> Submissions { get; set; }
public DbSet<Track> Tracks { get; set; }
public DbSet<UserArtist> UserArtists { get; set; }

View file

@ -0,0 +1,24 @@
using Roadie.Library.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Roadie.Library.Data
{
[Table("scanHistory")]
public class ScanHistory : EntityBase
{
[Column("userId")]
public int UserId { get; set; }
public ApplicationUser User { get; set; }
public int? ForArtistId { get; set; }
public int? ForReleaseId { get; set; }
public int? NewArtists { get; set; }
public int? NewReleases { get; set; }
public int? NewTracks { get; set; }
public int TimeSpanInSeconds { get; set; }
[NotMapped]
public new bool? IsLocked { get; set; }
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Roadie.Library.Models.Statistics
{
[Serializable]
public class DateAndCount
{
public int? Count { get; set; }
public string Date { get; set; }
}
}

View file

@ -153,5 +153,7 @@ namespace Roadie.Library.Models.Statistics
public long? TotalTrackSize { get; set; }
public int? TrackCount { get; set; }
public int? UserCount { get; set; }
public DateTime? LastScan { get; set; }
}
}