use std::collections::HashMap;
use crate::types::{node_ids::ObjectTypeId, service_types::ServiceCounterDataType};
use super::{
address_space::{address_space::AddressSpace, object::ObjectBuilder},
session::Session,
};
pub(crate) struct SessionDiagnostics {
total_request_count: u32,
unauthorized_request_count: u32,
service_counters: HashMap<&'static str, ServiceCounterDataType>,
}
impl Default for SessionDiagnostics {
fn default() -> Self {
Self {
total_request_count: 0,
unauthorized_request_count: 0,
service_counters: HashMap::new(),
}
}
}
impl SessionDiagnostics {
pub(crate) fn register_session(&self, session: &Session, address_space: &mut AddressSpace) {
let session_id = session.session_id();
debug!("register_session for session id {}", session_id);
debug!("Adding an object node for the session id {}", session_id);
let _ = ObjectBuilder::new(
session_id,
format!("{}", session_id),
format!("{}", session_id),
)
.has_type_definition(ObjectTypeId::SessionDiagnosticsObjectType)
.insert(address_space);
}
pub(crate) fn deregister_session(&self, session: &Session, address_space: &mut AddressSpace) {
address_space.delete(session.session_id(), true);
}
pub(crate) fn request(&mut self) {
self.total_request_count += 1;
}
pub(crate) fn unauthorized_request(&mut self) {
self.unauthorized_request_count += 1;
self.total_request_count += 1;
}
pub(crate) fn service_counter(
&mut self,
diagnostic_key: &'static str,
) -> ServiceCounterDataType {
if let Some(counter) = self.service_counters.get_mut(diagnostic_key) {
counter.clone()
} else {
ServiceCounterDataType::default()
}
}
pub(crate) fn service_success(&mut self, diagnostic_key: &'static str) {
if let Some(counter) = self.service_counters.get_mut(diagnostic_key) {
counter.success();
} else {
let mut counter = ServiceCounterDataType::default();
counter.success();
self.service_counters.insert(diagnostic_key, counter);
}
}
pub(crate) fn service_error(&mut self, diagnostic_key: &'static str) {
if let Some(counter) = self.service_counters.get_mut(diagnostic_key) {
counter.error();
} else {
let mut counter = ServiceCounterDataType::default();
counter.error();
self.service_counters.insert(diagnostic_key, counter);
}
}
}
pub(crate) const READ_COUNT: &str = "ReadCount";
pub(crate) const HISTORY_READ_COUNT: &str = "HistoryReadCount";
pub(crate) const WRITE_COUNT: &str = "WriteCount";
pub(crate) const HISTORY_UPDATE_COUNT: &str = "HistoryUpdateCount";
pub(crate) const CALL_COUNT: &str = "CallCount";
pub(crate) const CREATE_MONITORED_ITEMS_COUNT: &str = "CreateMonitoredItemsCount";
pub(crate) const MODIFY_MONITORED_ITEMS_COUNT: &str = "ModifyMonitoredItemsCount";
pub(crate) const SET_MONITORING_MODE_COUNT: &str = "SetMonitoringModeCount";
pub(crate) const SET_TRIGGERING_COUNT: &str = "SetTriggeringCount";
pub(crate) const DELETE_MONITORED_ITEMS_COUNT: &str = "DeleteMonitoredItemsCount";
pub(crate) const CREATE_SUBSCRIPTION_COUNT: &str = "CreateSubscriptionCount";
pub(crate) const MODIFY_SUBSCRIPTION_COUNT: &str = "ModifySubscriptionCount";
pub(crate) const SET_PUBLISHING_MODE_COUNT: &str = "SetPublishingModeCount";
pub(crate) const REPUBLISH_COUNT: &str = "RepublishCount";
pub(crate) const TRANSFER_SUBSCRIPTIONS_COUNT: &str = "TransferSubscriptionsCount";
pub(crate) const DELETE_SUBSCRIPTIONS_COUNT: &str = "DeleteSubscriptionsCount";
pub(crate) const ADD_NODES_COUNT: &str = "AddNodesCount";
pub(crate) const ADD_REFERENCES_COUNT: &str = "AddReferencesCount";
pub(crate) const DELETE_NODES_COUNT: &str = "DeleteNodesCount";
pub(crate) const DELETE_REFERENCES_COUNT: &str = "DeleteReferencesCount";
pub(crate) const BROWSE_COUNT: &str = "BrowseCount";
pub(crate) const BROWSE_NEXT_COUNT: &str = "BrowseNextCount";
pub(crate) const TRANSLATE_BROWSE_PATHS_TO_NODE_IDS_COUNT: &str =
"TranslateBrowsePathsToNodeIdsCount";
pub(crate) const REGISTER_NODES_COUNT: &str = "RegisterNodesCount";
pub(crate) const UNREGISTER_NODES_COUNT: &str = "UnregisterNodesCount";