Compare commits

...

4 Commits

Author SHA1 Message Date
017701867d Fixed logic on API GET Jobs/Progress 2023-09-08 19:58:44 +02:00
c3d62bd337 Added ProgressToken timeRemaining 2023-09-08 19:58:29 +02:00
dc9e9e705c Fix FileLogger filePath 2023-09-08 19:28:44 +02:00
9eee6683fa Add API GET Ping 2023-09-08 16:31:38 +02:00
4 changed files with 26 additions and 9 deletions

View File

@ -10,11 +10,12 @@ public class FileLogger : LoggerBase
public FileLogger(string logFilePath, Encoding? encoding = null) : base (encoding) public FileLogger(string logFilePath, Encoding? encoding = null) : base (encoding)
{ {
this.logFilePath = logFilePath; this.logFilePath = logFilePath;
DirectoryInfo dir = Directory.CreateDirectory(new FileInfo(logFilePath).DirectoryName!);
//Remove oldest logfile if more than MaxNumberOfLogFiles //Remove oldest logfile if more than MaxNumberOfLogFiles
string parentFolderPath = Path.GetDirectoryName(logFilePath)!; for (int fileCount = dir.EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later
for (int fileCount = new DirectoryInfo(parentFolderPath).EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later File.Delete(dir.EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName);
File.Delete(new DirectoryInfo(parentFolderPath).EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName);
} }
protected override void Write(LogMessage logMessage) protected override void Write(LogMessage logMessage)

View File

@ -23,14 +23,15 @@ public class Logger : TextWriter
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
{ {
this.Encoding = encoding ?? Encoding.UTF8; this.Encoding = encoding ?? Encoding.UTF8;
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) if(enabledLoggers.Contains(LoggerType.FileLogger) && (logFilePath is null || logFilePath == ""))
_fileLogger = new FileLogger(logFilePath, encoding);
else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null)
{ {
DateTime now = DateTime.Now;
logFilePath = Path.Join(LogDirectoryPath, logFilePath = Path.Join(LogDirectoryPath,
$"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}.log"); $"{now.ToShortDateString()}_{now.Hour}-{now.Minute}-{now.Second}.log");
_fileLogger = new FileLogger(logFilePath, encoding); _fileLogger = new FileLogger(logFilePath, encoding);
} }else if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
_fileLogger = new FileLogger(logFilePath, encoding);
if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null) if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null)
{ {

View File

@ -7,6 +7,9 @@ public class ProgressToken
public int incrementsCompleted { get; set; } public int incrementsCompleted { get; set; }
public float progress => GetProgress(); public float progress => GetProgress();
public DateTime executionStarted { get; private set; }
public TimeSpan timeRemaining => GetTimeRemaining();
public enum State { Running, Complete, Standby, Cancelled } public enum State { Running, Complete, Standby, Cancelled }
public State state { get; private set; } public State state { get; private set; }
@ -16,6 +19,7 @@ public class ProgressToken
this.increments = increments; this.increments = increments;
this.incrementsCompleted = 0; this.incrementsCompleted = 0;
this.state = State.Complete; this.state = State.Complete;
this.executionStarted = DateTime.UnixEpoch;
} }
private float GetProgress() private float GetProgress()
@ -25,6 +29,13 @@ public class ProgressToken
return 0; return 0;
} }
private TimeSpan GetTimeRemaining()
{
if (increments > 0 && incrementsCompleted > 0)
return DateTime.Now.Subtract(this.executionStarted).Divide(incrementsCompleted).Multiply(increments - incrementsCompleted);
return TimeSpan.MaxValue;
}
public void Increment() public void Increment()
{ {
this.incrementsCompleted++; this.incrementsCompleted++;
@ -40,6 +51,7 @@ public class ProgressToken
public void Start() public void Start()
{ {
state = State.Running; state = State.Running;
this.executionStarted = DateTime.Now;
} }
public void Complete() public void Complete()

View File

@ -177,7 +177,7 @@ public class Server : GlobalBase
SendResponse(HttpStatusCode.OK, response, _parent.jobBoss.jobs); SendResponse(HttpStatusCode.OK, response, _parent.jobBoss.jobs);
break; break;
case "Jobs/Progress": case "Jobs/Progress":
if (!requestVariables.TryGetValue("jobId", out jobId)) if (requestVariables.TryGetValue("jobId", out jobId))
{ {
if(!_parent.jobBoss.jobs.Any(jjob => jjob.id == jobId)) if(!_parent.jobBoss.jobs.Any(jjob => jjob.id == jobId))
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
@ -213,6 +213,9 @@ public class Server : GlobalBase
SendResponse(HttpStatusCode.OK, response, SendResponse(HttpStatusCode.OK, response,
Enum.GetValues<LibraryConnector.LibraryType>().Select(lc => new KeyValuePair<byte, string?>((byte)lc, Enum.GetName(lc)))); Enum.GetValues<LibraryConnector.LibraryType>().Select(lc => new KeyValuePair<byte, string?>((byte)lc, Enum.GetName(lc))));
break; break;
case "Ping":
SendResponse(HttpStatusCode.OK, response, "Pong");
break;
default: default:
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;