0

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

null
    • damon_vinciguerra.1
    • 2 wk ago
    • Reported - view

     , can you use the DurationSinceLastTVQ function?

    • timothy_vertin
    • 2 wk ago
    • Reported - view

    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

      • damon_vinciguerra.1
      • 2 wk ago
      • Reported - view

       Now you've got me interested? Where in Axiom are you trying to do this calculation? A Calculation Control, Value Transform, Scripting? Do you have a specific tag you're going to use as the indicator for each source? Also, do you want to be able to trend the Time Since Last TVQ that you calculate? Or you only care about what it is now?

      • timothy_vertin
      • 2 wk ago
      • Reported - view

       Hey Damon,

      Yea, so leveraging a value box, I would like to display the latest timestamp for a given TVQ.  I already have this by selecting a tag and configuring the ValueFormat like this: {time:MM-dd-hh:mm:ss:tt}  Then taking another tag that I know updates close to real time like server CPU usage% in a value box, formatted the same way.  Finally, a third value box that displays the result of the (time in value box 2) - (time in value box 1).  Everything compiles just fine but the calculation is not being performed on the time of the two value boxes but their tag values instead.  Hope that makes sense.  There may not be a way to do this in axiom, I may have to create in the Events & Calcs service.

    • damon_vinciguerra.1
    • 2 wk ago
    • Reported - view

     , 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.";
                }
        }
    }
    

Content aside

print this pagePrint this page
  • 2 wk agoLast active
  • 5Replies
  • 19Views
  • 2 Following