From 879b6591258a49f58b68650d8834d1fe1950f48d Mon Sep 17 00:00:00 2001 From: Geir Horn Date: Sat, 27 Jan 2024 19:17:06 +0100 Subject: [PATCH] Errata in multiple metric definitions A repeated message with the same version as the previous would trigger a wrong message exception. Change-Id: Id575e13c0183c31eb56251d9b181911eb27ea4cb --- MetricUpdater.cpp | 70 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/MetricUpdater.cpp b/MetricUpdater.cpp index 6de0a2b..19e8b38 100644 --- a/MetricUpdater.cpp +++ b/MetricUpdater.cpp @@ -49,48 +49,48 @@ void MetricUpdater::AddMetricSubscription( const MetricTopic & TheMetrics, const Address OptimiserController ) { if( TheMetrics.is_object() && - TheMetrics.at( NebulOuS::MetricList ).is_array() && - (MetricsVersion < TheMetrics.at( MetricVersionCounter ).get()) ) + TheMetrics.at( NebulOuS::MetricList ).is_array() ) { - // The first step is to try inserting the metrics into the metric value map - // and if this is successful, a subscription is created for the publisher - // of this metric value. The metric names are recorded since some of them - // may correspond to known metrics, some of them may correspond to metrics - // that are new. - - std::set< std::string > MetricNames; - - for (auto & MetricRecord : TheMetrics.at( NebulOuS::MetricList ) ) + if( MetricsVersion < TheMetrics.at( MetricVersionCounter ).get() ) { - auto [ MetricRecordPointer, MetricAdded ] = MetricValues.try_emplace( - MetricRecord.at( NebulOuS::MetricName ), JSON() ); + // The first step is to try inserting the metrics into the metric value + // map and if this is successful, a subscription is created for the + // publisherof this metric value. The metric names are recorded since + // some of them may correspond to known metrics, some of them may + // correspond to metrics that are new. - MetricNames.insert( MetricRecordPointer->first ); + std::set< std::string > MetricNames; - if( MetricAdded ) - Send( Theron::AMQ::NetworkLayer::TopicSubscription( - Theron::AMQ::NetworkLayer::TopicSubscription::Action::Subscription, - std::string( MetricValueRootString ) + MetricRecordPointer->first ), - //Theron::AMQ::Network::GetAddress( Theron::Network::Layer::Session) ); - GetSessionLayerAddress() ); - } - - // There could be some metric value records that were defined by the previous - // metrics defined, but missing from the new metric set. If this is the case, - // the metric value records for the missing metrics should be unsubcribed - // and their metric records removed. - - for( const auto & TheMetric : std::views::keys( MetricValues ) ) - if( !MetricName.contains( TheMetric ) ) + for (auto & MetricRecord : TheMetrics.at( NebulOuS::MetricList ) ) { - Send( Theron::AMQ::NetworkLayer::TopicSubscription( - Theron::AMQ::NetworkLayer::TopicSubscription::Action::CloseSubscription, - std::string( MetricValueRootString ) + TheMetric ), - //Theron::AMQ::Network::GetAddress( Theron::Network::Layer::Session) ); - GetSessionLayerAddress() ); + auto [ MetricRecordPointer, MetricAdded ] = MetricValues.try_emplace( + MetricRecord.at( NebulOuS::MetricName ), JSON() ); - MetricValues.erase( TheMetric ); + MetricNames.insert( MetricRecordPointer->first ); + + if( MetricAdded ) + Send( Theron::AMQ::NetworkLayer::TopicSubscription( + Theron::AMQ::NetworkLayer::TopicSubscription::Action::Subscription, + std::string( MetricValueRootString ) + MetricRecordPointer->first ), + GetSessionLayerAddress() ); } + + // There could be some metric value records that were defined by the + // previous metrics defined, but missing from the new metric set. If + // this is the case, the metric value records for the missing metrics + // should be unsubcribed and their metric records removed. + + for( const auto & TheMetric : std::views::keys( MetricValues ) ) + if( !MetricName.contains( TheMetric ) ) + { + Send( Theron::AMQ::NetworkLayer::TopicSubscription( + Theron::AMQ::NetworkLayer::TopicSubscription::Action::CloseSubscription, + std::string( MetricValueRootString ) + TheMetric ), + GetSessionLayerAddress() ); + + MetricValues.erase( TheMetric ); + } + } } else {