Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application known metrics by topic class #95

Open
jeff-pf opened this issue Mar 28, 2024 · 0 comments
Open

Application known metrics by topic class #95

jeff-pf opened this issue Mar 28, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@jeff-pf
Copy link

jeff-pf commented Mar 28, 2024

After wrestling with this for the last couple of days - I came up with this solution. I have not found any issues yet. I think it might make sense for the library.

I added a class to handle known metrics by topic (i force birth messages to NDATA when added)

    /// <summary>
    /// A class to handle known metrics by topic.
    /// </summary>
    public class  KnownMetricsByNodeDevice
    {
        /// <summary>
        /// The all known metrics by topic (node/device).
        /// </summary>
        public ConcurrentDictionary<string, KnownMetricStorage> Dict = new();
        /// <summary>
        /// The all known metrics by topic (node/device).
        /// </summary>
        public List<T> AllMetrics()
        {
            List<T> m = new List<T>();

            return m;
        }
        /// <summary>
        /// get metrics for topic.
        /// </summary>
        public KnownMetricStorage GetTopicMetrics(string topic)
        {
            IEnumerable<T> metric = new List<T>();
            KnownMetricStorage metrics = new KnownMetricStorage(metric);
            this.Dict.TryGetValue(topic, out var tmpmetrics);

            if (tmpmetrics != null) { metrics = tmpmetrics; }

            return metrics;

        }
        /// <summary>
        /// add metric - should only be called from adding birth metrics.
        /// </summary>
        public void AddMetric(Metric metric)
        {
            if (metric.Topic is not null)
            {
                if (this.Dict.ContainsKey(metric.Topic.ToString()))
                {
                    metric.Topic = new SparkplugMessageTopic(SparkplugNamespace.VersionB, metric.Topic.GroupIdentifier, SparkplugMessageType.NodeData,metric.Topic.EdgeNodeIdentifier,null);
                    this.Dict[metric.Topic.ToString()].AddVersionB_BirthMetric(metric);
                }
                else
                {
                    List<T> metrics = new List<T>();
                    KnownMetricStorage ms = new KnownMetricStorage(metrics);
                    this.Dict[metric.Topic.ToString()] = ms;
                }
            }
            else
            {
                //add exception
            }
        }
        /// <summary>
        /// update metric.
        /// </summary>
        public void UpdateMetric(Metric metric)
        {
            if (metric.Topic is not null)
            {
                if (this.Dict.ContainsKey(metric.Topic.ToString()))
                {
                    this.Dict[metric.Topic.ToString()].UpdateMetric(metric);
                }
                else
                {
                    //add exception
                }
            }
            else
            {
                //add exception
            }
        }
    }

And then in the application add birth messages and update data messages (working just with nodes currently).

       while (!_stop)
       {
           lock (x)
           {
               if (birthList.Count > 0)
               {
                   foreach (var metric in birthList)
                   {
                       if (metric.Topic is not null)
                       {
                           application.AllKnownMetrics.AddMetric(metric);
                       }
                   }

                   birthList.Clear();
               }
           }

           if (nodeData.Count > 0)
           {
               lock (y)
               {
                   foreach (var metric in nodeData)
                   {
                       if (metric.Topic is not null)
                       {
                           application.AllKnownMetrics.UpdateMetric(metric);
                       }
                   }
               }

               nodeData.Clear();
           }

           Thread.Sleep(100);
       }

Looking for any feedback.

@SeppPenner SeppPenner self-assigned this Apr 22, 2024
@SeppPenner SeppPenner added enhancement New feature or request question Further information is requested labels Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants