2
0

Compare commits

...

2 Commits

Author SHA1 Message Date
76604d84d8 Better way of handling progress, and childProgress. 2023-06-11 18:24:26 +02:00
af822febbe fixed nullable warning 2023-06-11 18:01:04 +02:00
5 changed files with 34 additions and 24 deletions

View File

@ -98,7 +98,7 @@ public class MangaDex : Connector
{ {
JsonArray relationships = manga["relationships"]!.AsArray(); JsonArray relationships = manga["relationships"]!.AsArray();
posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue<string>() == "cover_art")!["id"]!.GetValue<string>(); posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue<string>() == "cover_art")!["id"]!.GetValue<string>();
foreach (JsonNode node in relationships.Where(relationship => foreach (JsonNode? node in relationships.Where(relationship =>
relationship!["type"]!.GetValue<string>() == "author")) relationship!["type"]!.GetValue<string>() == "author"))
authorIds.Add(node!["id"]!.GetValue<string>()); authorIds.Add(node!["id"]!.GetValue<string>());
} }

View File

@ -137,7 +137,7 @@ public class TaskManager
DeleteTask(taskToRemove); DeleteTask(taskToRemove);
DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName, DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName,
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language, taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
taskToRemove.parentTask); (DownloadNewChaptersTask?)taskToRemove.parentTask);
AddTask(newTask); AddTask(newTask);
taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask); taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask);
} }
@ -427,13 +427,14 @@ public class TaskManager
this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
} }
foreach (TrangaTask task in this._allTasks.Where(task => task.GetType() == typeof(DownloadChapterTask))) foreach (TrangaTask task in this._allTasks.Where(tTask => tTask.parentTaskId is not null))
{ {
DownloadChapterTask dcTask = (DownloadChapterTask)task; TrangaTask? parentTask = this._allTasks.FirstOrDefault(pTask => pTask.taskId == task.parentTaskId);
IEnumerable<TrangaTask> dncTasks = this._allTasks.Where(pTask => pTask.GetType() == typeof(DownloadNewChaptersTask)); if (parentTask is not null)
DownloadNewChaptersTask? parentTask = (DownloadNewChaptersTask?)dncTasks.FirstOrDefault(pTask => pTask.taskId.Equals(dcTask.parentTaskId)); {
dcTask.parentTask = parentTask; task.parentTask = parentTask;
parentTask?.AddChildTask(dcTask); parentTask.AddChildTask(task);
}
} }

View File

@ -22,21 +22,16 @@ public abstract class TrangaTask
public DateTime lastExecuted { get; set; } public DateTime lastExecuted { get; set; }
public Task task { get; } public Task task { get; }
public string taskId { get; set; } public string taskId { get; set; }
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; } [Newtonsoft.Json.JsonIgnore] public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore]protected HashSet<TrangaTask> childTasks { get; } [Newtonsoft.Json.JsonIgnore] protected HashSet<TrangaTask> childTasks { get; }
[Newtonsoft.Json.JsonIgnore]public TrangaTask? parentTask { get; set; } [Newtonsoft.Json.JsonIgnore] public TrangaTask? parentTask { get; set; }
public string? parentTaskId { get; set; } public string? parentTaskId { get; set; }
[Newtonsoft.Json.JsonIgnore]public double progress { get; private set; } [Newtonsoft.Json.JsonIgnore]public double progress { get; private set; }
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; } [Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
[Newtonsoft.Json.JsonIgnore]
public DateTime executionApproximatelyFinished => this.progress != 0
? this.executionStarted.Add(DateTime.Now.Subtract(this.executionStarted).Divide(this.progress))
: DateTime.MaxValue;
[Newtonsoft.Json.JsonIgnore]public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; } [Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; }
[Newtonsoft.Json.JsonIgnore]public DateTime executionApproximatelyFinished => progress != 0 ? lastChange.Add(GetRemainingTime()) : DateTime.MaxValue;
[Newtonsoft.Json.JsonIgnore]public TimeSpan executionApproximatelyRemaining => executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
public enum ExecutionState { Waiting, Enqueued, Running } public enum ExecutionState { Waiting, Enqueued, Running }
@ -88,23 +83,37 @@ public abstract class TrangaTask
this.DecrementProgress(failed.progress); this.DecrementProgress(failed.progress);
} }
public void AddChildTask(DownloadChapterTask childTask) public void AddChildTask(TrangaTask childTask)
{ {
this.childTasks.Add(childTask); this.childTasks.Add(childTask);
} }
public void IncrementProgress(double amount) public void IncrementProgress(double amount)
{ {
this.lastChange = DateTime.Now;
this.progress += amount / (childTasks.Count > 0 ? childTasks.Count : 1); this.progress += amount / (childTasks.Count > 0 ? childTasks.Count : 1);
parentTask?.IncrementProgress(amount); if (parentTask is not null)
{
parentTask.IncrementProgress(amount);
parentTask.state = ExecutionState.Running;
}
} }
public void DecrementProgress(double amount) public void DecrementProgress(double amount)
{ {
this.lastChange = DateTime.Now;
this.progress -= amount / childTasks.Count > 0 ? childTasks.Count : 1; this.progress -= amount / childTasks.Count > 0 ? childTasks.Count : 1;
parentTask?.DecrementProgress(amount); parentTask?.DecrementProgress(amount);
} }
private TimeSpan GetRemainingTime()
{
if(progress == 0)
return DateTime.MaxValue.Subtract(DateTime.Now);
TimeSpan elapsed = lastChange.Subtract(executionStarted);
return elapsed.Divide(progress).Subtract(elapsed);
}
public enum Task : byte public enum Task : byte
{ {
DownloadNewChapters = 2, DownloadNewChapters = 2,

View File

@ -10,7 +10,7 @@ public class DownloadChapterTask : TrangaTask
public string language { get; } public string language { get; }
public Chapter chapter { get; } public Chapter chapter { get; }
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", TrangaTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask) public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask)
{ {
this.chapter = chapter; this.chapter = chapter;
this.connectorName = connectorName; this.connectorName = connectorName;

View File

@ -32,7 +32,7 @@ public class DownloadNewChaptersTask : TrangaTask
foreach (Chapter newChapter in newChapters) foreach (Chapter newChapter in newChapters)
{ {
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this); DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName, pub, newChapter, this.language, this);
taskManager.AddTask(newTask); taskManager.AddTask(newTask);
this.childTasks.Add(newTask); this.childTasks.Add(newTask);
} }