From 08bc9578aa870b4b1d6e15ff787f96bff21ca46d Mon Sep 17 00:00:00 2001 From: Carsten Larsen Date: Thu, 8 Sep 2016 21:28:21 +0200 Subject: [PATCH] Bugfixes in timezone logic --- Ntp.Analyzer.Objects/Reading.cs | 50 +++++++++++++++++++++++--- Ntp.Analyzer/Graph/HostGraph.cs | 6 ++-- Ntp.Analyzer/Graph/PeerGraph.cs | 29 ++++++++------- Ntp.Analyzer/Graph/TrafficGraph.cs | 29 +++++++-------- Ntp.Data.Provider/MySqlFactory.cs | 2 +- Ntp.Data.Provider/PostgreSqlFactory.cs | 2 +- 6 files changed, 80 insertions(+), 38 deletions(-) diff --git a/Ntp.Analyzer.Objects/Reading.cs b/Ntp.Analyzer.Objects/Reading.cs index a6ad6f08..9425e17e 100644 --- a/Ntp.Analyzer.Objects/Reading.cs +++ b/Ntp.Analyzer.Objects/Reading.cs @@ -35,6 +35,7 @@ namespace Ntp.Analyzer.Objects var adjusted = new DateTime( time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second, + time.Millisecond, DateTimeKind.Utc); Time = adjusted.AddMinutes(zone); Host = host; @@ -49,14 +50,43 @@ namespace Ntp.Analyzer.Objects Host = host; } + public Host Host { get; } + public DateTime Time { get; } - public DateTime RoundedTime => new DateTime( - Time.Year, Time.Month, Time.Day, - Time.Hour, Time.Minute, 0, - DateTimeKind.Utc); + public DateTime UtcTime + { + get + { + switch (Time.Kind) + { + case DateTimeKind.Utc: + case DateTimeKind.Unspecified: + return Time; + case DateTimeKind.Local: + return Time.Subtract(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow)); + default: + return Time; + } + } + } - public Host Host { get; } + public DateTime LocalTime + { + get + { + switch (Time.Kind) + { + case DateTimeKind.Utc: + case DateTimeKind.Unspecified: + return UtcTime.Add(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow)); + case DateTimeKind.Local: + return Time; + default: + return UtcTime.Add(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow)); + } + } + } public int UtcOffset { @@ -74,5 +104,15 @@ namespace Ntp.Analyzer.Objects } } } + + public DateTime RoundedUtcTime => new DateTime( + UtcTime.Year, UtcTime.Month, UtcTime.Day, + UtcTime.Hour, UtcTime.Minute, 0, + DateTimeKind.Utc); + + public DateTime RoundedLocalTime => new DateTime( + LocalTime.Year, LocalTime.Month, LocalTime.Day, + LocalTime.Hour, LocalTime.Minute, 0, + DateTimeKind.Local); } } \ No newline at end of file diff --git a/Ntp.Analyzer/Graph/HostGraph.cs b/Ntp.Analyzer/Graph/HostGraph.cs index 4cc1f636..bdc38ff7 100644 --- a/Ntp.Analyzer/Graph/HostGraph.cs +++ b/Ntp.Analyzer/Graph/HostGraph.cs @@ -71,13 +71,11 @@ namespace Ntp.Analyzer.Graph { FilteredSqlDatabaseMapper dataMapper = DataFace.Instance.HostReadings; dataMapper.FilterHost = host; - dataMapper.FilterTime = config.GraphTime == DateTimeKind.Local - ? DateTime.Now.Subtract(GraphTimeSpan) - : DateTime.UtcNow.Subtract(GraphTimeSpan); + dataMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan); foreach (var reading in dataMapper) { - Time.Add(reading.Time); + Time.Add(config.GraphTime == DateTimeKind.Local ? reading.LocalTime : reading.UtcTime); if (config.Offset.HasValue) Offset.Add(reading.Offset*config.Offset.Value); else Offset.Add(0.0); diff --git a/Ntp.Analyzer/Graph/PeerGraph.cs b/Ntp.Analyzer/Graph/PeerGraph.cs index 5eedc780..d047512b 100644 --- a/Ntp.Analyzer/Graph/PeerGraph.cs +++ b/Ntp.Analyzer/Graph/PeerGraph.cs @@ -73,16 +73,13 @@ namespace Ntp.Analyzer.Graph FilteredSqlDatabaseMapper peerMapper = DataFace.Instance.PeerReadings; peerMapper.FilterHost = host; peerMapper.FilterPeer = peer; - peerMapper.FilterTime = config.GraphTime == DateTimeKind.Local - ? DateTime.Now.Subtract(GraphTimeSpan) - : DateTime.UtcNow.Subtract(GraphTimeSpan); - + peerMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan); if (config.Balance.HasValue) { foreach (var reading in peerMapper) { - Time.Add(reading.Time); + Time.Add(config.GraphTime == DateTimeKind.Local ? reading.LocalTime : reading.UtcTime); if (config.Offset.HasValue) Offset.Add(reading.Offset*config.Offset.Value); else Offset.Add(0.0); @@ -100,21 +97,23 @@ namespace Ntp.Analyzer.Graph { FilteredSqlDatabaseMapper hostMapper = DataFace.Instance.HostReadings; hostMapper.FilterHost = host; - hostMapper.FilterTime = config.GraphTime == DateTimeKind.Local - ? DateTime.Now.Subtract(GraphTimeSpan) - : DateTime.UtcNow.Subtract(GraphTimeSpan); + hostMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan); // Prepare balance data foreach (var hostReading in hostMapper) { - if (!timedReading.ContainsKey(hostReading.RoundedTime)) - timedReading.Add(hostReading.RoundedTime, hostReading); + var indexTime = config.GraphTime == DateTimeKind.Local + ? hostReading.RoundedLocalTime + : hostReading.RoundedUtcTime; + + if (!timedReading.ContainsKey(indexTime)) + timedReading.Add(indexTime, hostReading); } // Add foreach (var reading in peerMapper) { - Time.Add(reading.RoundedTime); + Time.Add(config.GraphTime == DateTimeKind.Local ? reading.LocalTime : reading.UtcTime); if (config.Offset.HasValue) Offset.Add(reading.Offset*config.Offset.Value); else Offset.Add(0.0); @@ -125,8 +124,12 @@ namespace Ntp.Analyzer.Graph if (config.Delay.HasValue) delay.Add(reading.Delay*config.Delay.Value); else delay.Add(0.0); - if (timedReading.ContainsKey(reading.RoundedTime)) - balance.Add(reading.Offset - timedReading[reading.RoundedTime].Offset); + var indexTime = config.GraphTime == DateTimeKind.Local + ? reading.RoundedLocalTime + : reading.RoundedUtcTime; + + if (timedReading.ContainsKey(indexTime)) + balance.Add(reading.Offset - timedReading[indexTime].Offset); else balance.Add(0.0); } diff --git a/Ntp.Analyzer/Graph/TrafficGraph.cs b/Ntp.Analyzer/Graph/TrafficGraph.cs index dfc5f23b..6c441ffa 100644 --- a/Ntp.Analyzer/Graph/TrafficGraph.cs +++ b/Ntp.Analyzer/Graph/TrafficGraph.cs @@ -138,20 +138,18 @@ namespace Ntp.Analyzer.Graph { FilteredSqlDatabaseMapper dataMapper = DataFace.Instance.HostIoReadings; dataMapper.FilterHost = host; - dataMapper.FilterTime = config.GraphTime == DateTimeKind.Local - ? DateTime.Now.Subtract(GraphTimeSpan) - : DateTime.UtcNow.Subtract(GraphTimeSpan); + dataMapper.FilterTime = DateTime.UtcNow.Subtract(GraphTimeSpan); - DateTime startTime = DateTime.MinValue; + var startTime = DateTime.MinValue; - DateTime firstTime = DateTime.MinValue; + var firstTime = DateTime.MinValue; int firstRecieved = 0; int firstIgnored = 0; int firstDropped = 0; int firstSent = 0; int firstNotSent = 0; - DateTime lastTime = DateTime.MinValue; + var lastTime = DateTime.MinValue; int lastReceived = 0; int lastIgnored = 0; int lastDropped = 0; @@ -162,19 +160,22 @@ namespace Ntp.Analyzer.Graph int disFactor = config.PacketRate; int interval = config.PlotInterval*config.Timespan; - foreach (HostIoReading reading in dataMapper) + foreach (var reading in dataMapper) { bool readNext = false; + var readingTime = config.GraphTime == DateTimeKind.Local + ? reading.RoundedLocalTime + : reading.RoundedUtcTime; if (!firstRun) { - double minutes = reading.Time.Subtract(lastTime).TotalMinutes; - double totalMinutes = reading.Time.Subtract(firstTime).TotalMinutes; - double minutesSinceReset = reading.Time.Subtract(startTime).TotalMinutes; + double minutes = readingTime.Subtract(lastTime).TotalMinutes; + double totalMinutes = readingTime.Subtract(firstTime).TotalMinutes; + double minutesSinceReset = readingTime.Subtract(startTime).TotalMinutes; if (minutes >= interval) { - time.Add(reading.Time); + time.Add(readingTime); received.Add((reading.ReceivedPackets - lastReceived)*config.Received/minutes/disFactor ?? 0.0); ignored.Add((reading.IgnoredPackets - lastIgnored)*config.Ignored/minutes/disFactor ?? 0.0); @@ -223,9 +224,9 @@ namespace Ntp.Analyzer.Graph int mins = (reading.TimeSinceReset - hours*60*60)/60; int seconds = reading.TimeSinceReset - hours*60*60 - mins*60; var span = new TimeSpan(hours, mins, seconds); - startTime = reading.Time.Subtract(span); + startTime = readingTime.Subtract(span); - firstTime = reading.Time; + firstTime = readingTime; firstRecieved = reading.ReceivedPackets; firstIgnored = reading.IgnoredPackets; firstDropped = reading.DroppedPackets; @@ -235,7 +236,7 @@ namespace Ntp.Analyzer.Graph if (firstRun || readNext) { - lastTime = reading.Time; + lastTime = readingTime; lastReceived = reading.ReceivedPackets; lastIgnored = reading.IgnoredPackets; lastDropped = reading.DroppedPackets; diff --git a/Ntp.Data.Provider/MySqlFactory.cs b/Ntp.Data.Provider/MySqlFactory.cs index ca95f95c..8bbe583a 100644 --- a/Ntp.Data.Provider/MySqlFactory.cs +++ b/Ntp.Data.Provider/MySqlFactory.cs @@ -75,7 +75,7 @@ namespace Ntp.Data.Provider public override string DateAddMinutes(string dateColumn, string minuteColumn) { - return $"{dateColumn} + interval {minuteColumn} minute"; + return $"{dateColumn} - interval {minuteColumn} minute"; } public override string PrepareCheckTableSql(string table) diff --git a/Ntp.Data.Provider/PostgreSqlFactory.cs b/Ntp.Data.Provider/PostgreSqlFactory.cs index 9df1e348..928409d3 100644 --- a/Ntp.Data.Provider/PostgreSqlFactory.cs +++ b/Ntp.Data.Provider/PostgreSqlFactory.cs @@ -109,7 +109,7 @@ namespace Ntp.Data.Provider public override string DateAddMinutes(string dateColumn, string minuteColumn) { - return $"{dateColumn} + {minuteColumn} * INTERVAL '1 minute'"; + return $"{dateColumn} - {minuteColumn} * INTERVAL '1 minute'"; } public override string PrepareCheckTableSql(string table)