Skip to content

Commit

Permalink
Update PostJob + add GetQuestionFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 25, 2024
1 parent 5aa2656 commit e418df7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 37 deletions.
22 changes: 21 additions & 1 deletion MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,30 @@ public async Task Any(DbWrites request)
if (request.CompleteJobIds?.Count > 0)
{
await Db.UpdateOnlyAsync(() => new PostJob {
Body = null,
CompletedDate = DateTime.UtcNow,
},
x => request.CompleteJobIds.Contains(x.PostId));
var postJobs = await Db.SelectAsync(Db.From<PostJob>()
.Where(x => request.CompleteJobIds.Contains(x.PostId)));

foreach (var postJob in postJobs)
{
// If there's no outstanding model answer jobs for this post, add a rank job
if (!Db.Exists(Db.From<PostJob>()
.Where(x => x.PostId == postJob.PostId && x.CompletedDate == null)))
{
var rankJob = new PostJob
{
PostId = postJob.PostId,
Model = "rank",
Title = postJob.Title,
CreatedDate = DateTime.UtcNow,
CreatedBy = nameof(DbWrites),
};
await Db.InsertAsync(rankJob);
modelWorkers.Enqueue(rankJob);
}
}
}

if (request.AnswerAddedToPost != null)
Expand Down
1 change: 1 addition & 0 deletions MyApp.ServiceInterface/Data/ModelWorkerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ModelWorkerQueue
["deepseek-coder:6.7b"] = new(),
["mistral"] = new(),
["mixtral"] = new(),
["rank"] = new(),
};

public void Enqueue(PostJob job)
Expand Down
1 change: 1 addition & 0 deletions MyApp.ServiceInterface/Data/QuestionFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static Meta DeserializeMeta(string json)
return meta;
}

public IVirtualFile? GetQuestionFile() => Files.FirstOrDefault(x => x.Name == $"{FileId}.json");
public IVirtualFile? GetMetaFile() => Files.FirstOrDefault(x => x.Name == $"{FileId}.meta.json");

public static List<IVirtualFile> WithoutDuplicateAnswers(List<IVirtualFile> files)
Expand Down
25 changes: 1 addition & 24 deletions MyApp.ServiceInterface/JobServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,6 @@ namespace MyApp.ServiceInterface;

public class JobServices(QuestionsProvider questions, ModelWorkerQueue workerQueues) : Service
{
public async Task<object> Get(CheckPostJobs request)
{
// Place holder for the actual implementation
var post = Db.Single<Post>(x => x.Id == 105372);
JobIdCount++;
var questionFile = await questions.GetQuestionAsync(post.Id);
var question = await questionFile.GetQuestionAsync();

var result = new List<PostJob>
{
new PostJob
{
Id = JobIdCount,
Body = question?.Post.Body,
Tags = post.Tags,
Title = post.Title,
PostId = post.Id
}
};

return new CheckPostJobsResponse { Results = result };
}

public object Any(ViewModelQueues request)
{
var jobs = workerQueues.GetAll(request.Models);
Expand All @@ -47,7 +24,7 @@ public object Any(GetNextJobs request)
return to;

MessageProducer.Publish(new DbWrites {
StartJob = new() { Id = job.Id, Worker = request.Worker, WorkerIp = Request?.RemoteIp }
StartJob = new() { Id = job.Id, Worker = request.Worker, WorkerIp = Request!.RemoteIp }
});
return new GetNextJobsResponse
{
Expand Down
20 changes: 15 additions & 5 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public async Task<object> Any(AskQuestion request)
if (tags.Count > 5)
throw new ArgumentException("Maximum of 5 tags allowed", nameof(request.Tags));

var userName = Request.GetClaimsPrincipal().GetUserName();
var userName = Request.GetClaimsPrincipal().GetUserName()
?? throw new ArgumentNullException(nameof(ApplicationUser.UserName));

var now = DateTime.UtcNow;
var post = new Post
Expand All @@ -28,9 +29,10 @@ public async Task<object> Any(AskQuestion request)
Slug = request.Title.GenerateSlug(200),
Summary = request.Body.StripHtml().SubstringWithEllipsis(0,200),
CreationDate = now,
LastActivityDate = now,
CreatedBy = userName,
LastActivityDate = now,
Body = request.Body,
RefId = request.RefId,
};

MessageProducer.Publish(new DbWrites
Expand All @@ -40,10 +42,9 @@ public async Task<object> Any(AskQuestion request)
.Select(model => new PostJob
{
PostId = post.Id,
Title = request.Title,
Body = request.Body,
Tags = tags,
Model = model,
Title = request.Title,
CreatedBy = userName,
CreatedDate = now,
}).ToList(),
});
Expand All @@ -58,6 +59,15 @@ public async Task<object> Any(AskQuestion request)
};
}

public async Task<object> Any(GetQuestionFile request)
{
var questionFiles = await questions.GetQuestionFilesAsync(request.Id);
var file = questionFiles.GetQuestionFile();
if (file == null)
throw HttpError.NotFound($"Question {request.Id} not found");
return new HttpResult(file, MimeTypes.Json);
}

public async Task Any(CreateWorkerAnswer request)
{
var json = request.Json;
Expand Down
18 changes: 13 additions & 5 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ public class Post

public int? AnswerCount { get; set; }

public string? CreatedBy { get; set; }

public string? ModifiedBy { get; set; }

public string? RefId { get; set; }

[Ignore] public string? Body { get; set; }
[Ignore] public string? CreatedBy { get; set; }
[Ignore] public string? ModifiedBy { get; set; }
[Ignore] public string? RefId { get; set; }
}

public class PostJob
Expand All @@ -61,8 +64,7 @@ public class PostJob
public int PostId { get; set; }
public string Model { get; set; }
public string Title { get; set; }
public string? Body { get; set; }
public List<string> Tags { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? StartedDate { get; set; }
public string? Worker { get; set; }
Expand Down Expand Up @@ -223,6 +225,12 @@ public class CreateWorkerAnswer : IReturn<IdResponse>
public int? PostJobId { get; set; }
}

[ValidateHasRole(Roles.Moderator)]
public class GetQuestionFile : IGet, IReturn<string>
{
public int Id { get; set; }
}

[ValidateIsAuthenticated]
public class AskQuestion : IPost, IReturn<AskQuestionResponse>
{
Expand Down
3 changes: 1 addition & 2 deletions MyApp/Migrations/Migration1000.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public class PostJob
public int PostId { get; set; }
public string Model { get; set; }
public string Title { get; set; }
public string? Body { get; set; }
public List<string> Tags { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? StartedDate { get; set; }
public string? Worker { get; set; }
Expand Down

0 comments on commit e418df7

Please sign in to comment.