# Copyright (C) 2005-2025 Splunk Inc. All Rights Reserved. import itsi_py3 import time from splunk.auth import getCurrentUser from splunk.util import normalizeBoolean from splunk import RESTException from ITOA.storage import itoa_storage from ITOA.itoa_common import get_current_utc_epoch class ITSICounter(object): def __init__(self, session_key, current_user_name=None, collection='itsi_counter', user='nobody', action_dispatch_config=None, **kwargs): # noqa E128 self.session_key = session_key self.user = user self.current_user_name = current_user_name self.object_type = 'itsi_counter' self.storage_interface = itoa_storage.ITOAStorage(collection=collection) def get(self, key, **kwargs): """ Get itsi_count object @type key: basestring @param key: name of counter @rtype: dict @return: return itsi_count schema """ try: result = self.storage_interface.get( self.session_key, self.user, self.object_type, key, current_user_name=self.current_user_name ) return result except Exception: return None def upsert(self, key, count, **kwargs): # noqa E128 """ Sets itsi_count object @type key: basestring @param key: name of counter @rtype: dict @return: return itsi_count schema """ count_obj = self.get(key) data = {"_key": key, "value": count} if count_obj: result = self.storage_interface.edit( self.session_key, self.user, self.object_type, key, data, current_user_name=self.current_user_name ) else: result = self.storage_interface.create( self.session_key, self.user, self.object_type, data, current_user_name=self.current_user_name ) return result def delete(self, key, **kwargs): """ Deletes itsi_count object @type key: basestring @param key: name of counter @rtype: dict @return: """ try: result = self.storage_interface.delete( self.session_key, self.user, self.object_type, key, current_user_name=self.current_user_name ) return result except Exception: return None