Axiom Time Stamp Math
Hello All,
I am building a data source diagnostics page in Axiom and would like to calculate the age of the last TVQ for each data source. I have two parts of this I am having trouble with. The first is how would I reference or get the current time. Once I have this, is there a way to take the current time and find the difference between a TVQ timestamp and the current time so I can calculate the "age" of the last TVQ. Any help would be much appreciated! Thank you in advance.
Version 23.2.1.24029
5 replies
-
, can you use the DurationSinceLastTVQ function?
-
Hey Damon,
I was trying to stay away from creating any new tags. I know I could use that in the Calcs & Events service but would like to stay in Axiom for the time calculation if I can. Thanks for the suggestion though bud.
Tim
-
, this isn't 100% what you described. But I think it meets your needs. Warning, this script is ugly and unrefined!
It takes a tag (line 69). Every 5s it will look over the previous 1 day period and return the last value found. Then it compares the timestamp of the value found to the current time (UTC!) and write the difference in seconds to a Label.
I wasn't able to find a method that simply returned last value. So I had to give it a reasonable bound (1 day). There's an if/then that write a descriptive method if no TVQs were found in the last day. I figured at that point a day compared to a day and 5 seconds were equally bad.
Is this in the vein of what you were looking for?!
using AxiomCore2.ControlProperties; using AxiomCore2.Controls; using AxiomCore2.Data; using AxiomCore2.Events; using AxiomCore2.Legacy; using AxiomCore2.Log; using AxiomCore2.Managers; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Collections; using System.Drawing; //using CanaryWebServiceHelper; //using CanaryWebServiceHelper.HistorianWebService; public partial class ScreenScript : IDisposable { // NOTE: click help icon in dialog header for API documentation and examples. // NOTE: use EventTimer class when timer is necessary. See API documentation. // axiom references public ControlApplication Application { get; } = ControlApplication.Instance; public ControlFactoryManager ControlFactory { get; } = ControlFactoryManager.Instance; public IDataProvider DataProvider { get; } = DataProviderManager.CreateInstance(); public ILog Log { get; } = ClientLog.UserScript; public NavigationManager Navigation { get; } = NavigationManager.Instance; public ControlScreen Screen => _screen; // EventTimer setup from documentation example https://axiomscripting.canarylabs.com/22.3/html/8675300e-3c1c-42c5-8590-9e4d4e9d7627.htm private EventTimer _timer = null; private int _initialTime = Convert.ToInt32(TimeSpan.FromSeconds(1).TotalMilliseconds); private int _interval = Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds); private ControlLabel _timeSinceLastTvqValueLabel; private ControlLabel _timeSinceLastTvqTimestampLabel; private ControlLabel _timeSinceLastTvqLabel; public void OnApplicationLoaded(UrlParameter[] urlParameters) { } public void OnScreenVisible() { _timeSinceLastTvqValueLabel = (ControlLabel)Screen.ScreenControls["LblTimeSinceLastTvqValue"]; _timeSinceLastTvqTimestampLabel = (ControlLabel)Screen.ScreenControls["LblTimeSinceLastTvqTimestamp"]; _timeSinceLastTvqLabel = (ControlLabel)Screen.ScreenControls["LblTimeSinceLastTvq"]; if (_timer == null) { // initialize timer _timer = new EventTimer( callback: TimerCallback, dueTime: _initialTime, period: _interval); } } public void OnScreenInvisible() { } public void Dispose() { } private void TimerCallback(EventTimer timer, object userData) { var result = DataProvider.GetProcessedData("<LOCALHOST>ENTERPRISE.Asset Team 2.Route 01.Briggs A1 Well.Casing Pressure",DateTime.Now.AddDays(-1),DateTime.Now,TimeSpan.FromDays(1),"End"); foreach(var tvq in result.Values){ Log.Info(tvq.ToString()); } var firstTvq = result.Values.FirstOrDefault(); if(firstTvq != null){ _timeSinceLastTvqValueLabel.Text = firstTvq.Timestamp.ToString(); _timeSinceLastTvqTimestampLabel.Text = firstTvq.Value.ToString(); var currentDateTime = DateTime.Now; _timeSinceLastTvqLabel.Text = (DateTime.UtcNow - firstTvq.Timestamp).TotalSeconds.ToString(); } else { _timeSinceLastTvqLabel.Text = "No TVQs in the last 1 day."; } } }