fix: improve task sorting accuracy for UI

This commit is contained in:
LordMZTE 2023-07-09 17:24:13 +02:00
parent bc97326ac6
commit 1e3abf343e
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 27 additions and 28 deletions

View file

@ -42,7 +42,7 @@ pub const Task = struct {
}
};
const TaskQueue = std.PriorityQueue(Task, void, Task.order);
pub const TaskQueue = std.PriorityQueue(Task, void, Task.order);
state: *State,
tasks: TaskQueue,
@ -99,28 +99,6 @@ pub fn setPaused(self: *DownloadQueue, paused: bool) void {
self.next_cond.signal();
}
/// Returns a new allocated slice of the queue's tasks sorted by priority.
/// This is required because the iterator has no specified order.
pub fn getSortedTasks(self: *DownloadQueue) ![]Task {
const tasks = try self.tasks.allocator.alloc(DownloadQueue.Task, self.tasks.count());
errdefer self.free(tasks);
var iter = self.tasks.iterator();
var i: usize = 0;
while (iter.next()) |t| : (i += 1)
tasks[i] = t;
const compareFn = struct {
fn cmp(ctx: void, a: DownloadQueue.Task, b: DownloadQueue.Task) bool {
_ = ctx;
return a.priority > b.priority;
}
}.cmp;
std.mem.sort(DownloadQueue.Task, tasks, {}, compareFn);
return tasks;
}
fn run(self: *DownloadQueue) noreturn {
std.log.info("starting download queue", .{});
while (true) {

View file

@ -82,10 +82,16 @@ pub fn indexRoute(
\\<table id="tasks_tbl"><tbody>
);
const tasks = try state.downloads.getSortedTasks();
defer state.downloads.tasks.allocator.free(tasks);
// TODO: wonk
var tasks_copy = DownloadQueue.TaskQueue{
.items = try alloc.dupe(DownloadQueue.Task, state.downloads.tasks.items),
.len = state.downloads.tasks.len,
.allocator = alloc,
.context = {},
};
defer tasks_copy.deinit();
for (tasks) |t| {
while (tasks_copy.removeOrNull()) |t| {
try w.print(
\\<tr>
\\ <td>{s}{s}{s}</td>
@ -214,8 +220,23 @@ pub fn indexJsonRoute(
defer arena.deinit();
const arena_alloc = arena.allocator();
const tasks = try state.downloads.getSortedTasks();
defer state.downloads.tasks.allocator.free(tasks);
// TODO: wonk
var tasks_copy = DownloadQueue.TaskQueue{
.items = try alloc.dupe(DownloadQueue.Task, state.downloads.tasks.items),
.len = state.downloads.tasks.len,
.allocator = alloc,
.context = {},
};
defer tasks_copy.deinit();
// TODO: optimize
const tasks = try alloc.alloc(DownloadQueue.Task, tasks_copy.count());
defer alloc.free(tasks);
var tasks_iter = tasks_copy.iterator();
var i: usize = 0;
while (tasks_iter.next()) |t| : (i += 1)
tasks[i] = t;
var vids_dir = try std.fs.cwd().openIterableDir(state.vids_dir, .{});
defer vids_dir.close();