add MediaHash. Step1

This commit is contained in:
Oleksandr Bezdieniezhnykh
2025-11-17 07:46:05 +02:00
parent d355f81c63
commit fd95d2ba2c
27 changed files with 421 additions and 288 deletions
+31 -21
View File
@@ -62,7 +62,8 @@ public class AnnotationService : IAnnotationService
private async Task InitQueueConsumer(CancellationToken token = default)
{
if (!_api.CurrentUser.Role.IsValidator())
var currentUser = await _api.GetCurrentUserAsync();
if (!currentUser.Role.IsValidator())
return;
var consumerSystem = await StreamSystem.Create(new StreamSystemConfig
@@ -72,11 +73,11 @@ public class AnnotationService : IAnnotationService
Password = _queueConfig.ConsumerPassword
});
var offsets = _api.CurrentUser.UserConfig?.QueueOffsets ?? new UserQueueOffsets();
var offsets = currentUser.UserConfig?.QueueOffsets ?? new UserQueueOffsets();
_consumer = await Consumer.Create(new ConsumerConfig(consumerSystem, Constants.MQ_ANNOTATIONS_QUEUE)
{
Reference = _api.CurrentUser.Email,
Reference = currentUser.Email,
OffsetSpec = new OffsetTypeOffset(offsets.AnnotationsOffset),
MessageHandler = async (_, _, context, message) =>
{
@@ -87,13 +88,14 @@ public class AnnotationService : IAnnotationService
if (!Enum.TryParse<AnnotationStatus>((string)message.ApplicationProperties[nameof(AnnotationStatus)], out var annotationStatus))
return;
if (email != _api.CurrentUser.Email) //Don't process messages by yourself
if (email != currentUser.Email)
{
if (annotationStatus.In(AnnotationStatus.Created, AnnotationStatus.Edited))
{
var msg = MessagePackSerializer.Deserialize<AnnotationMessage>(message.Data.Contents);
await SaveAnnotationInner(
msg.CreatedDate,
msg.MediaHash,
msg.OriginalMediaName,
msg.Name,
msg.Time,
@@ -115,12 +117,9 @@ public class AnnotationService : IAnnotationService
}
}
offsets.AnnotationsOffset = context.Offset + 1; //to consume on the next launch from the next message
ThrottleExt.Throttle(() =>
{
_api.UpdateOffsets(offsets);
return Task.CompletedTask;
}, SaveQueueOffsetTaskId, TimeSpan.FromSeconds(10), scheduleCallAfterCooldown: true);
offsets.AnnotationsOffset = context.Offset + 1;
ThrottleExt.Throttle(async () => await _api.UpdateOffsetsAsync(offsets),
SaveQueueOffsetTaskId, TimeSpan.FromSeconds(10), scheduleCallAfterCooldown: true);
}
catch (Exception e)
{
@@ -137,17 +136,21 @@ public class AnnotationService : IAnnotationService
//AI
public async Task<Annotation> SaveAnnotation(AnnotationImage a, CancellationToken ct = default)
{
var currentUser = await _api.GetCurrentUserAsync();
a.Time = TimeSpan.FromMilliseconds(a.Milliseconds);
return await SaveAnnotationInner(DateTime.UtcNow, a.OriginalMediaName, a.Name, a.Time, a.Detections.ToList(),
SourceEnum.AI, new MemoryStream(a.Image), _api.CurrentUser.Role, _api.CurrentUser.Email, token: ct);
return await SaveAnnotationInner(DateTime.UtcNow, a.MediaHash, a.OriginalMediaName, a.Name, a.Time, a.Detections.ToList(),
SourceEnum.AI, new MemoryStream(a.Image), currentUser.Role, currentUser.Email, token: ct);
}
//Manual
public async Task<Annotation> SaveAnnotation(string originalMediaName, string annotationName, TimeSpan time, List<Detection> detections, Stream? stream = null, CancellationToken token = default) =>
await SaveAnnotationInner(DateTime.UtcNow, originalMediaName, annotationName, time, detections, SourceEnum.Manual, stream,
_api.CurrentUser.Role, _api.CurrentUser.Email, token: token);
public async Task<Annotation> SaveAnnotation(string mediaHash, string originalMediaName, string annotationName, TimeSpan time, List<Detection> detections,
Stream? stream = null, CancellationToken token = default)
{
var currentUser = await _api.GetCurrentUserAsync();
return await SaveAnnotationInner(DateTime.UtcNow, mediaHash, originalMediaName, annotationName, time, detections, SourceEnum.Manual, stream, currentUser.Role, currentUser.Email, token: token);
}
private async Task<Annotation> SaveAnnotationInner(DateTime createdDate, string originalMediaName, string annotationName, TimeSpan time,
private async Task<Annotation> SaveAnnotationInner(DateTime createdDate, string mediaHash, string originalMediaName, string annotationName, TimeSpan time,
List<Detection> detections, SourceEnum source, Stream? stream,
RoleEnum userRole,
string createdEmail,
@@ -163,13 +166,17 @@ public class AnnotationService : IAnnotationService
await db.Detections.DeleteAsync(x => x.AnnotationName == annotationName, token: token);
if (ann != null) //Annotation is already exists
if (ann != null)
{
status = AnnotationStatus.Edited;
var annotationUpdatable = db.Annotations
.Where(x => x.Name == annotationName)
.Set(x => x.Source, source);
.Set(x => x.Source, source)
.Set(x => x.OriginalMediaName, originalMediaName);
if (!string.IsNullOrEmpty(mediaHash))
annotationUpdatable = annotationUpdatable.Set(x => x.MediaHash, mediaHash);
if (userRole.IsValidator() && source == SourceEnum.Manual)
{
@@ -183,6 +190,7 @@ public class AnnotationService : IAnnotationService
.UpdateAsync(token: token);
ann.Detections = detections;
ann.MediaHash = mediaHash;
}
else
{
@@ -190,6 +198,7 @@ public class AnnotationService : IAnnotationService
{
CreatedDate = createdDate,
Name = annotationName,
MediaHash = mediaHash,
OriginalMediaName = originalMediaName,
Time = time,
ImageExtension = Constants.JPG_EXT,
@@ -244,7 +253,8 @@ public class AnnotationService : IAnnotationService
public async Task ValidateAnnotations(List<string> annotationNames, bool fromQueue = false, CancellationToken token = default)
{
if (!_api.CurrentUser.Role.IsValidator())
var currentUser = await _api.GetCurrentUserAsync();
if (!currentUser.Role.IsValidator())
return;
var annNames = annotationNames.ToHashSet();
@@ -254,7 +264,7 @@ public class AnnotationService : IAnnotationService
.Where(x => annNames.Contains(x.Name))
.Set(x => x.AnnotationStatus, AnnotationStatus.Validated)
.Set(x => x.ValidateDate, DateTime.UtcNow)
.Set(x => x.ValidateEmail, _api.CurrentUser.Email)
.Set(x => x.ValidateEmail, currentUser.Email)
.UpdateAsync(token: token);
});
if (!fromQueue)
@@ -265,6 +275,6 @@ public class AnnotationService : IAnnotationService
public interface IAnnotationService
{
Task<Annotation> SaveAnnotation(AnnotationImage a, CancellationToken ct = default);
Task<Annotation> SaveAnnotation(string originalMediaName, string annotationName, TimeSpan time, List<Detection> detections, Stream? stream = null, CancellationToken token = default);
Task<Annotation> SaveAnnotation(string mediaHash, string originalMediaName, string annotationName, TimeSpan time, List<Detection> detections, Stream? stream = null, CancellationToken token = default);
Task ValidateAnnotations(List<string> annotationNames, bool fromQueue = false, CancellationToken token = default);
}