Several bug fixes.

This commit is contained in:
Steven Hildreth 2019-02-24 20:26:54 -06:00
parent d25524a741
commit ed4030dcda
11 changed files with 203 additions and 149 deletions

View file

@ -95,13 +95,29 @@ namespace Roadie.Library.Engines
Data = resultInCache
};
}
var searchName = metaData.Release.NormalizeName().ToLower();
var specialSearchName = metaData.Release.ToAlphanumericName();
var altStart = $"{ searchName }|";
var altIn = $"|{ searchName }|";
var altEnds = $"|{ searchName }";
var altStartSpecial = $"{ specialSearchName }|";
var altInSpecial = $"|{ specialSearchName }|";
var altEndsSpecial = $"|{ specialSearchName }";
var release = (from r in this.DbContext.Releases
where (r.ArtistId == artist.Id)
where (r.Title.Contains(searchName) ||
r.AlternateNames.Contains(searchName) ||
r.AlternateNames.Contains(specialSearchName))
where (r.Title.ToLower() == searchName ||
r.AlternateNames.ToLower() == searchName ||
r.AlternateNames.ToLower() == specialSearchName ||
r.AlternateNames.ToLower().Contains(altStart) ||
r.AlternateNames.ToLower().Contains(altIn) ||
r.AlternateNames.ToLower().Contains(altEnds) ||
r.AlternateNames.ToLower().Contains(altStartSpecial) ||
r.AlternateNames.ToLower().Contains(altInSpecial) ||
r.AlternateNames.ToLower().Contains(altEndsSpecial))
select r
).FirstOrDefault();

View file

@ -536,7 +536,7 @@ namespace Roadie.Api.Services
return await this.ScanFolder(d, dest, user, isReadOnly);
}
public async Task<OperationResult<bool>> ScanRelease(ApplicationUser user, Guid releaseId, bool isReadOnly = false)
public async Task<OperationResult<bool>> ScanRelease(ApplicationUser user, Guid releaseId, bool isReadOnly = false, bool wasDoneForInvalidTrackPlay = false)
{
var sw = new Stopwatch();
sw.Start();
@ -563,6 +563,7 @@ namespace Roadie.Api.Services
errors.Add(ex);
}
sw.Stop();
this.DbContext.ScanHistories.Add(new data.ScanHistory
{
UserId = user.Id,
@ -571,7 +572,7 @@ namespace Roadie.Api.Services
TimeSpanInSeconds = (int)sw.Elapsed.TotalSeconds
});
await this.DbContext.SaveChangesAsync();
await this.LogAndPublish($"ScanRelease `{release}`, By User `{user}`", LogLevel.Information);
await this.LogAndPublish($"ScanRelease `{release}`, By User `{user}`, WasDoneForInvalidTrackPlay [{ wasDoneForInvalidTrackPlay }]", LogLevel.Information);
return new OperationResult<bool>
{
IsSuccess = !errors.Any(),

View file

@ -696,35 +696,43 @@ namespace Roadie.Api.Services
}
if (includes.Contains("stats"))
{
tsw.Restart();
var artistTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
where (r.ArtistId == artist.Id || t.ArtistId == artist.Id)
select new
{
t.Id,
size = t.FileSize,
time = t.Duration,
isMissing = t.Hash == null
});
var validCartistTracks = artistTracks.Where(x => !x.isMissing);
var trackTime = validCartistTracks.Sum(x => x.time);
result.Statistics = new CollectionStatistics
try
{
FileSize = artistTracks.Sum(x => (long?)x.size).ToFileSize(),
MissingTrackCount = artistTracks.Where(x => x.isMissing).Count(),
ReleaseCount = artist.ReleaseCount,
ReleaseMediaCount = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
where r.ArtistId == artist.Id
select rm.Id).Count(),
TrackTime = validCartistTracks.Any() ? new TimeInfo((decimal)trackTime).ToFullFormattedString() : "--:--",
TrackCount = validCartistTracks.Count(),
TrackPlayedCount = artist.PlayedCount
};
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
tsw.Restart();
var artistTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
where (r.ArtistId == artist.Id || t.ArtistId == artist.Id)
select new
{
t.Id,
size = t.FileSize,
time = t.Duration,
isMissing = t.Hash == null
});
var validCartistTracks = artistTracks.Where(x => !x.isMissing);
long? trackTime = validCartistTracks.Sum(x => (long?)x.time);
result.Statistics = new CollectionStatistics
{
FileSize = artistTracks.Sum(x => (long?)x.size).ToFileSize(),
MissingTrackCount = artistTracks.Where(x => x.isMissing).Count(),
ReleaseCount = artist.ReleaseCount,
ReleaseMediaCount = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
where r.ArtistId == artist.Id
select rm.Id).Count(),
TrackTime = validCartistTracks.Any() ? new TimeInfo((decimal)trackTime).ToFullFormattedString() : "--:--",
TrackCount = validCartistTracks.Count(),
TrackPlayedCount = artist.PlayedCount
};
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
}
catch (Exception ex)
{
this.Logger.LogError(ex, $"Error Getting Statistics for Artist `{ artist }`");
}
}
if (includes.Contains("images"))
{

View file

@ -28,6 +28,6 @@ namespace Roadie.Api.Services
Task<OperationResult<bool>> ScanLibraryFolder(ApplicationUser user, bool isReadOnly = false);
Task<OperationResult<bool>> ScanRelease(ApplicationUser user, Guid releaseId, bool isReadOnly = false);
Task<OperationResult<bool>> ScanRelease(ApplicationUser user, Guid releaseId, bool isReadOnly = false, bool wasDoneForInvalidTrackPlay = false);
}
}

View file

@ -16,6 +16,6 @@ namespace Roadie.Api.Services
OperationResult<Track> StreamCheckAndInfo(User roadieUser, Guid id);
Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes);
Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes, User roadieUser);
}
}

View file

@ -38,122 +38,131 @@ namespace Roadie.Api.Services
public async Task<OperationResult<PlayActivityList>> CreatePlayActivity(User roadieUser, TrackStreamInfo streamInfo)
{
var sw = Stopwatch.StartNew();
var track = this.DbContext.Tracks
.Include(x => x.ReleaseMedia)
.Include(x => x.ReleaseMedia.Release)
.Include(x => x.ReleaseMedia.Release.Artist)
.Include(x => x.TrackArtist)
.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(streamInfo.Track.Value));
if (track == null)
try
{
return new OperationResult<PlayActivityList>($"CreatePlayActivity: Unable To Find Track [{ streamInfo.Track.Value }]");
}
if (!track.IsValid)
{
return new OperationResult<PlayActivityList>($"CreatePlayActivity: Invalid Track. Track Id [{streamInfo.Track.Value}], FilePath [{track.FilePath}], Filename [{track.FileName}]");
}
data.UserTrack userTrack = null;
var now = DateTime.UtcNow;
track.PlayedCount = (track.PlayedCount ?? 0) + 1;
track.LastPlayed = now;
var user = this.GetUser(roadieUser?.UserId);
if (user != null)
{
userTrack = user.TrackRatings.FirstOrDefault(x => x.TrackId == track.Id);
if (userTrack == null)
var sw = Stopwatch.StartNew();
var track = this.DbContext.Tracks
.Include(x => x.ReleaseMedia)
.Include(x => x.ReleaseMedia.Release)
.Include(x => x.ReleaseMedia.Release.Artist)
.Include(x => x.TrackArtist)
.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(streamInfo.Track.Value));
if (track == null)
{
userTrack = new data.UserTrack(now)
return new OperationResult<PlayActivityList>($"CreatePlayActivity: Unable To Find Track [{ streamInfo.Track.Value }]");
}
if (!track.IsValid)
{
return new OperationResult<PlayActivityList>($"CreatePlayActivity: Invalid Track. Track Id [{streamInfo.Track.Value}], FilePath [{track.FilePath}], Filename [{track.FileName}]");
}
data.UserTrack userTrack = null;
var now = DateTime.UtcNow;
track.PlayedCount = (track.PlayedCount ?? 0) + 1;
track.LastPlayed = now;
var user = roadieUser != null ? this.DbContext.Users
.Include(x => x.TrackRatings)
.FirstOrDefault(x => x.RoadieId == roadieUser.UserId) : null;
if (user != null)
{
userTrack = user.TrackRatings.FirstOrDefault(x => x.TrackId == track.Id);
if (userTrack == null)
{
UserId = user.Id,
TrackId = track.Id
};
this.DbContext.UserTracks.Add(userTrack);
userTrack = new data.UserTrack(now)
{
UserId = user.Id,
TrackId = track.Id
};
this.DbContext.UserTracks.Add(userTrack);
}
userTrack.LastPlayed = now;
userTrack.PlayedCount = (userTrack.PlayedCount ?? 0) + 1;
this.CacheManager.ClearRegion(user.CacheRegion);
}
userTrack.LastPlayed = now;
userTrack.PlayedCount++;
userTrack.PlayedCount = (userTrack.PlayedCount ?? 0) + 1;
this.CacheManager.ClearRegion(user.CacheRegion);
}
var release = this.DbContext.Releases.Include(x => x.Artist).FirstOrDefault(x => x.RoadieId == track.ReleaseMedia.Release.RoadieId);
release.LastPlayed = now;
release.PlayedCount = (release.PlayedCount ?? 0) + 1;
var release = this.DbContext.Releases.Include(x => x.Artist).FirstOrDefault(x => x.RoadieId == track.ReleaseMedia.Release.RoadieId);
release.LastPlayed = now;
release.PlayedCount = (release.PlayedCount ?? 0) + 1;
var artist = this.DbContext.Artists.FirstOrDefault(x => x.RoadieId == release.Artist.RoadieId);
artist.LastPlayed = now;
artist.PlayedCount = (artist.PlayedCount ?? 0) + 1;
var artist = this.DbContext.Artists.FirstOrDefault(x => x.RoadieId == release.Artist.RoadieId);
artist.LastPlayed = now;
artist.PlayedCount = (artist.PlayedCount ?? 0) + 1;
data.Artist trackArtist = null;
if (track.ArtistId.HasValue)
{
trackArtist = this.DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId);
trackArtist.LastPlayed = now;
trackArtist.PlayedCount = (trackArtist.PlayedCount ?? 0) + 1;
this.CacheManager.ClearRegion(trackArtist.CacheRegion);
}
this.CacheManager.ClearRegion(track.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
var pl = new PlayActivityList
{
Artist = new DataToken
data.Artist trackArtist = null;
if (track.ArtistId.HasValue)
{
Text = track.ReleaseMedia.Release.Artist.Name,
Value = track.ReleaseMedia.Release.Artist.RoadieId.ToString()
},
TrackArtist = track.TrackArtist == null ? null : new DataToken
{
Text = track.TrackArtist.Name,
Value = track.TrackArtist.RoadieId.ToString()
},
Release = new DataToken
{
Text = track.ReleaseMedia.Release.Title,
Value = track.ReleaseMedia.Release.RoadieId.ToString()
},
Track = new DataToken
{
Text = track.Title,
Value = track.RoadieId.ToString()
},
User = new DataToken
{
Text = roadieUser.UserName,
Value = roadieUser.UserId.ToString()
},
PlayedDateDateTime = userTrack?.LastPlayed,
ReleasePlayUrl = $"{ this.HttpContext.BaseUrl }/play/release/{ track.ReleaseMedia.Release.RoadieId}",
Rating = track.Rating,
UserRating = userTrack?.Rating,
TrackPlayUrl = $"{ this.HttpContext.BaseUrl }/play/track/{ track.RoadieId}.mp3",
ArtistThumbnail = this.MakeArtistThumbnailImage(track.TrackArtist != null ? track.TrackArtist.RoadieId : track.ReleaseMedia.Release.Artist.RoadieId),
ReleaseThumbnail = this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId),
UserThumbnail = this.MakeUserThumbnailImage(roadieUser.UserId)
};
if (!roadieUser.IsPrivate)
{
try
{
await this.PlayActivityHub.Clients.All.SendAsync("SendActivity", pl);
trackArtist = this.DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId);
trackArtist.LastPlayed = now;
trackArtist.PlayedCount = (trackArtist.PlayedCount ?? 0) + 1;
this.CacheManager.ClearRegion(trackArtist.CacheRegion);
}
catch (Exception ex)
{
this.Logger.LogError(ex);
}
}
await this.DbContext.SaveChangesAsync();
sw.Stop();
return new OperationResult<PlayActivityList>
this.CacheManager.ClearRegion(track.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
var pl = new PlayActivityList
{
Artist = new DataToken
{
Text = track.ReleaseMedia.Release.Artist.Name,
Value = track.ReleaseMedia.Release.Artist.RoadieId.ToString()
},
TrackArtist = track.TrackArtist == null ? null : new DataToken
{
Text = track.TrackArtist.Name,
Value = track.TrackArtist.RoadieId.ToString()
},
Release = new DataToken
{
Text = track.ReleaseMedia.Release.Title,
Value = track.ReleaseMedia.Release.RoadieId.ToString()
},
Track = new DataToken
{
Text = track.Title,
Value = track.RoadieId.ToString()
},
User = new DataToken
{
Text = roadieUser.UserName,
Value = roadieUser.UserId.ToString()
},
PlayedDateDateTime = userTrack?.LastPlayed,
ReleasePlayUrl = $"{ this.HttpContext.BaseUrl }/play/release/{ track.ReleaseMedia.Release.RoadieId}",
Rating = track.Rating,
UserRating = userTrack?.Rating,
TrackPlayUrl = $"{ this.HttpContext.BaseUrl }/play/track/{ track.RoadieId}.mp3",
ArtistThumbnail = this.MakeArtistThumbnailImage(track.TrackArtist != null ? track.TrackArtist.RoadieId : track.ReleaseMedia.Release.Artist.RoadieId),
ReleaseThumbnail = this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId),
UserThumbnail = this.MakeUserThumbnailImage(roadieUser.UserId)
};
if (!roadieUser.IsPrivate)
{
try
{
await this.PlayActivityHub.Clients.All.SendAsync("SendActivity", pl);
}
catch (Exception ex)
{
this.Logger.LogError(ex);
}
}
await this.DbContext.SaveChangesAsync();
sw.Stop();
return new OperationResult<PlayActivityList>
{
Data = pl,
IsSuccess = userTrack != null,
OperationTime = sw.ElapsedMilliseconds
};
}
catch (Exception ex)
{
Data = pl,
IsSuccess = userTrack != null,
OperationTime = sw.ElapsedMilliseconds
};
this.Logger.LogError(ex, $"CreatePlayActivity RoadieUser `{ roadieUser }` StreamInfo `{ streamInfo }`");
}
return new OperationResult<PlayActivityList>();
}
public Task<Library.Models.Pagination.PagedResult<PlayActivityList>> List(PagedRequest request, User roadieUser = null, DateTime? newerThan = null)

View file

@ -813,7 +813,7 @@ namespace Roadie.Api.Services
rm.Id,
rm.MediaNumber
});
var releaseTime = releaseTracks?.Sum(x => x.time) ?? 0;
long releaseTime = releaseTracks?.Sum(x => (long?)x.time) ?? 0;
var releaseStats = new ReleaseStatistics
{
MediaCount = release.MediaCount,

View file

@ -28,6 +28,7 @@ namespace Roadie.Api.Services
public class TrackService : ServiceBase, ITrackService
{
private IBookmarkService BookmarkService { get; } = null;
private IAdminService AdminService { get; }
public TrackService(IRoadieSettings configuration,
IHttpEncoder httpEncoder,
@ -35,10 +36,12 @@ namespace Roadie.Api.Services
data.IRoadieDbContext dbContext,
ICacheManager cacheManager,
ILogger<TrackService> logger,
IBookmarkService bookmarkService)
IBookmarkService bookmarkService,
IAdminService adminService)
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
this.BookmarkService = bookmarkService;
this.AdminService = adminService;
}
public static long DetermineByteEndFromHeaders(IHeaderDictionary headers, long fileLength)
@ -587,12 +590,28 @@ namespace Roadie.Api.Services
};
}
public async Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes)
public async Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes, User roadieUser)
{
var track = this.GetTrack(trackId);
var track = this.DbContext.Tracks.FirstOrDefault(x => x.RoadieId == trackId);
if (track == null)
{
return new OperationResult<TrackStreamInfo>($"TrackStreamInfo: Unable To Find Track [{ trackId }]");
// Not Found try recanning release
var release = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
where rm.Id == track.ReleaseMediaId
select r).FirstOrDefault();
if (!release.IsLocked ?? false)
{
await this.AdminService.ScanRelease(new Library.Identity.ApplicationUser
{
Id = roadieUser.Id.Value
}, release.RoadieId, false, true);
}
track = this.DbContext.Tracks.FirstOrDefault(x => x.RoadieId == trackId);
if(track == null)
{
return new OperationResult<TrackStreamInfo>($"TrackStreamInfo: Unable To Find Track [{ trackId }]");
}
}
if (!track.IsValid)
{

View file

@ -115,7 +115,7 @@ namespace Roadie.Api.Controllers
}
catch (Exception ex)
{
this.Logger.LogError(ex, "Error in CreateToken");
this.Logger.LogError(ex, $"Error in CreateToken For User [{ model.Username }]");
return BadRequest();
}
}

View file

@ -98,8 +98,9 @@ namespace Roadie.Api.Controllers
tsw.Restart();
var info = await trackService.TrackStreamInfo(id,
Services.TrackService.DetermineByteStartFromHeaders(this.Request.Headers),
Services.TrackService.DetermineByteEndFromHeaders(this.Request.Headers, track.Data.FileSize));
TrackService.DetermineByteStartFromHeaders(this.Request.Headers),
TrackService.DetermineByteEndFromHeaders(this.Request.Headers, track.Data.FileSize),
user);
if (!info?.IsSuccess ?? false || info?.Data == null)
{
if (info?.Errors != null && (info?.Errors.Any() ?? false))

View file

@ -3,7 +3,7 @@
"Roadie.Api": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:5123/"
}