Skip to content

Commit

Permalink
Always set end time (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
einarmo authored Jul 2, 2024
1 parent 144def2 commit 3036f7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
37 changes: 33 additions & 4 deletions Extractor/History/HistoryScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class HistoryScheduler : SharedResourceScheduler<HistoryReadNode>

private readonly HistoryReadType type;
private readonly DateTime historyStartTime;
private readonly DateTime? historyEndTime;
private readonly DateTime historyEndTime;
private readonly TimeSpan historyGranularity;
private readonly ILogger log;

Expand Down Expand Up @@ -130,7 +130,23 @@ public HistoryScheduler(
});

historyStartTime = GetStartTime(config.History.StartTime);
if (!string.IsNullOrWhiteSpace(config.History.EndTime)) historyEndTime = CogniteTime.ParseTimestampString(config.History.EndTime)!;
if (!string.IsNullOrWhiteSpace(config.History.EndTime))
{
var endTime = CogniteTime.ParseTimestampString(config.History.EndTime)!;
if (endTime.HasValue)
{
historyEndTime = endTime.Value;
}
else
{
log.LogWarning("Failed to parse timestamp from end-time string {Conf}", config.History.EndTime);
historyEndTime = DefaultEndTime();
}
}
else
{
historyEndTime = DefaultEndTime();
}

if (historyStartTime != null && historyEndTime != null && historyStartTime >= historyEndTime)
{
Expand All @@ -143,6 +159,19 @@ public HistoryScheduler(
metrics = new HistoryMetrics(type);
}

private DateTime DefaultEndTime()
{
// Need to avoid reading far into the future if maxReadLength is specified, since that can cause crazy issues.
// If users want to use maxReadLength properly they just have to set their own end time.
if (maxReadLength.HasValue)
{
log.LogWarning("max-read-length is set without setting end-time. End time is set to current time + max-read-length, "
+ "but it is strongly recommended to explicitly configure end-time when using max-read-length.");
return DateTime.UtcNow.Add(maxReadLength.Value);
}
return DateTime.UtcNow.AddDays(1);
}

private static DateTime GetStartTime(string? start)
{
if (string.IsNullOrWhiteSpace(start)) return CogniteTime.DateTimeEpoch;
Expand Down Expand Up @@ -207,11 +236,11 @@ private static DateTime Max(DateTime t1, DateTime t2)
if (Frontfill)
{
min = Max(nodes.First().Time, historyStartTime);
if (maxReadLength == null) max = historyEndTime ?? DateTime.MinValue;
if (maxReadLength == null) max = historyEndTime;
else
{
max = min + maxReadLength.Value;
if (max > (historyEndTime ?? DateTime.UtcNow)) max = historyEndTime ?? DateTime.MinValue;
if (max > historyEndTime) max = historyEndTime;
}
}
else
Expand Down
5 changes: 5 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ schema:
- "https://raw.githubusercontent.com/"

versions:
"2.30.1":
description: Always set history end time.
changelog:
changed:
- History end time is now always set in read requests to the server, even if history.end-time is not configured.
"2.30.0":
description: Add support for configuring filters from a list of values.
changelog:
Expand Down

0 comments on commit 3036f7b

Please sign in to comment.