generated from NetCoreTemplates/blazor-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
552 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.Collections.Concurrent; | ||
using MyApp.ServiceModel; | ||
using ServiceStack; | ||
using ServiceStack.IO; | ||
|
||
namespace MyApp.Data; | ||
|
||
public class QuestionFiles(int id, string dir1, string dir2, string fileId, List<IVirtualFile> files, bool remote=false) | ||
{ | ||
public const int MostVotedScore = 10; | ||
public const int AcceptedScore = 9; | ||
public static Dictionary<string,int> ModelScores = new() | ||
{ | ||
["phi"] = 1, //2.7B | ||
["gemma:2b"] = 2, | ||
["qwen:4b"] = 3, //4B | ||
["codellama"] = 4, //7B | ||
["gemma"] = 5, //7B | ||
["deepseek-coder:6.7b"] = 5, //6.7B | ||
["mistral"] = 7, //7B | ||
["mixtral"] = 8, //47B | ||
["accepted"] = 9, | ||
["most-voted"] = 10, | ||
}; | ||
|
||
public int Id { get; init; } = id; | ||
public string Dir1 { get; init; } = dir1; | ||
public string Dir2 { get; init; } = dir2; | ||
public string DirPath = "/{Dir1}/{Dir2}"; | ||
public string FileId { get; init; } = fileId; | ||
public List<IVirtualFile> Files { get; init; } = files; | ||
public bool LoadedRemotely { get; set; } = remote; | ||
public ConcurrentDictionary<string, string> FileContents { get; } = []; | ||
public QuestionAndAnswers? Question { get; set; } | ||
|
||
public async Task<QuestionAndAnswers?> GetQuestionAsync() | ||
{ | ||
if (Question == null) | ||
{ | ||
await LoadQuestionAndAnswersAsync(); | ||
} | ||
return Question; | ||
} | ||
|
||
public async Task LoadContentsAsync() | ||
{ | ||
if (FileContents.Count > 0) return; | ||
var tasks = new List<Task>(); | ||
tasks.AddRange(Files.Select(async file => { | ||
FileContents[file.VirtualPath] = await file.ReadAllTextAsync(); | ||
})); | ||
await Task.WhenAll(tasks); | ||
} | ||
|
||
public async Task LoadQuestionAndAnswersAsync() | ||
{ | ||
var questionFileName = FileId + ".json"; | ||
await LoadContentsAsync(); | ||
|
||
var to = new QuestionAndAnswers(); | ||
foreach (var entry in FileContents) | ||
{ | ||
var fileName = entry.Key.LastRightPart('/'); | ||
if (fileName == questionFileName) | ||
{ | ||
to.Post = entry.Value.FromJson<Post>(); | ||
} | ||
else if (fileName.StartsWith(FileId + ".a.")) | ||
{ | ||
to.Answers.Add(entry.Value.FromJson<Answer>()); | ||
} | ||
else if (fileName.StartsWith(FileId + ".h.")) | ||
{ | ||
var post = entry.Value.FromJson<Post>(); | ||
var userName = fileName.Substring((FileId + ".h.").Length).LeftPart('.'); | ||
var answer = new Answer | ||
{ | ||
Id = $"{post.Id}", | ||
Model = userName, | ||
UpVotes = userName == "most-voted" ? MostVotedScore : AcceptedScore, | ||
Choices = [ | ||
new() | ||
{ | ||
Index = 1, | ||
Message = new() { Role = userName, Content = post.Body ?? "" } | ||
} | ||
] | ||
}; | ||
if (to.Answers.All(x => x.Id != answer.Id)) | ||
to.Answers.Add(answer); | ||
} | ||
} | ||
|
||
if (to.Post == null) | ||
return; | ||
|
||
to.Answers.Each(x => x.UpVotes = x.UpVotes == 0 ? ModelScores.GetValueOrDefault(x.Model, 1) : x.UpVotes); | ||
to.Answers.Sort((a, b) => b.Votes - a.Votes); | ||
Question = to; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.Extensions.Logging; | ||
using ServiceStack; | ||
using ServiceStack.IO; | ||
using ServiceStack.Messaging; | ||
|
||
namespace MyApp.Data; | ||
|
||
public class QuestionsProvider(ILogger<QuestionsProvider> log, IMessageProducer mqClient, IVirtualFiles fs, R2VirtualFiles r2) | ||
{ | ||
public QuestionFiles GetLocalQuestionFiles(int id) | ||
{ | ||
var (dir1, dir2, fileId) = id.ToFileParts(); | ||
|
||
var files = fs.GetDirectory($"{dir1}/{dir2}").GetAllMatchingFiles($"{fileId}.*") | ||
.OrderByDescending(x => x.LastModified) | ||
.ToList(); | ||
|
||
return new QuestionFiles(id: id, dir1: dir1, dir2: dir2, fileId: fileId, files: files); | ||
} | ||
|
||
public async Task<QuestionFiles> GetRemoteQuestionFilesAsync(int id) | ||
{ | ||
var (dir1, dir2, fileId) = id.ToFileParts(); | ||
|
||
var files = (await r2.EnumerateFilesAsync($"{dir1}/{dir2}").ToListAsync()) | ||
.Where(x => x.Name.Glob($"{fileId}.*")) | ||
.OrderByDescending(x => x.LastModified) | ||
.Cast<IVirtualFile>() | ||
.ToList(); | ||
|
||
return new QuestionFiles(id: id, dir1: dir1, dir2: dir2, fileId: fileId, files: files, remote:true); | ||
} | ||
|
||
public async Task<QuestionFiles> GetQuestionFilesAsync(int id) | ||
{ | ||
var localFiles = GetLocalQuestionFiles(id); | ||
if (localFiles.Files.Count > 0) | ||
return localFiles; | ||
|
||
log.LogInformation("No local cached files for question {Id}, fetching from R2...", id); | ||
var r = await GetRemoteQuestionFilesAsync(id); | ||
if (r.Files.Count > 0) | ||
{ | ||
var lastModified = r.Files.Max(x => x.LastModified); | ||
log.LogInformation("Fetched {Count} files from R2 for question {Id}, last modified: '{LastModified}'", r.Files.Count, id, lastModified); | ||
} | ||
return r; | ||
} | ||
|
||
public async Task<QuestionFiles> GetQuestionAsync(int id) | ||
{ | ||
var questionFiles = await GetQuestionFilesAsync(id); | ||
await questionFiles.GetQuestionAsync(); | ||
if (questionFiles.LoadedRemotely) | ||
{ | ||
log.LogInformation("Caching question {Id}'s {Count} remote files locally...", id, questionFiles.FileContents.Count); | ||
foreach (var entry in questionFiles.FileContents) | ||
{ | ||
await fs.WriteFileAsync(entry.Key, entry.Value); | ||
} | ||
} | ||
return questionFiles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using MyApp.ServiceModel; | ||
|
||
namespace MyApp.Data; | ||
|
||
public static class StatUtils | ||
{ | ||
public static T WithRequest<T>(this T stat, HttpContext? ctx) where T : StatBase | ||
{ | ||
var user = ctx?.User; | ||
stat.UserName = user?.Identity?.Name; | ||
stat.RemoteIp = ctx?.Connection.RemoteIpAddress?.ToString(); | ||
stat.CreatedDate = DateTime.UtcNow; | ||
return stat; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.