def test_25_eventcounter(self): counter = EventCounter("test_counter", 10) counter.save() counter2 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter2.counter_value, 10) self.assertEqual(counter2.node, "") counter2.increase() counter2.increase() counter3 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter3.counter_value, 12) counter3.decrease() counter4 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter4.counter_value, 11) counter4.decrease() counter5 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter5.counter_value, 10) counter6 = EventCounter("test_counter", 4, "othernode") self.assertEqual(counter6.counter_value, 4) self.assertEqual(counter6.node, "othernode") counter_value = db.session.query(func.sum(EventCounter.counter_value))\ .filter(EventCounter.counter_name == "test_counter").one()[0] self.assertEqual(counter_value, 14) counters7 = EventCounter.query.filter_by( counter_name="test_counter").all() self.assertEqual(len(counters7), 2) counter8 = EventCounter.query.filter_by(counter_name="test_counter", node="othernode") counter8.delete() counters9 = EventCounter.query.filter_by( counter_name="test_counter").all() self.assertEqual(len(counters9), 1) counters9[0].delete() counter10 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter10, None)
def reset(counter_name): """ Reset the counter value in the database to 0. If the counter does not exist yet, create the counter. :param counter_name: The name/identifier of the counter :return: """ counter = EventCounter.query.filter_by(counter_name=counter_name).first() if not counter: counter = EventCounter(counter_name, 0) counter.save() counter.reset()
def increase(counter_name): """ Increase the counter value in the database. If the counter does not exist, yet, create the counter. :param counter_name: The name/identifier of the counter :return: the new integer value of the counter """ counter = EventCounter.query.filter_by(counter_name=counter_name).first() if not counter: counter = EventCounter(counter_name, 0) counter.save() counter.increase() return counter.counter_value
def reset(counter_name): """ Reset the counter value in the database to 0. If the counter does not exist yet, create the counter. :param counter_name: The name/identifier of the counter :return: """ node = get_privacyidea_node() counters = EventCounter.query.filter_by(counter_name=counter_name).count() if not counters: counter = EventCounter(counter_name, 0, node=node) counter.save() else: _reset_counter_on_all_nodes(counter_name)
def test_25_eventcounter(self): counter = EventCounter("test_counter", 10) counter.save() counter2 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter2.counter_value, 10) counter2.increase() counter2.increase() counter3 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter3.counter_value, 12) counter3.decrease() counter4 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter4.counter_value, 11) counter4.decrease(allow_negative=True) counter5 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter5.counter_value, 10) counter5.reset() counter6 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter6.counter_value, 0) counter6.decrease(allow_negative=True) counter6.decrease(allow_negative=True) counter7 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter7.counter_value, -2) counter7.decrease(allow_negative=False) counter8 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter8.counter_value, 0) counter8.delete() counter9 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter9, None)
def decrease(counter_name, allow_negative=False): """ Decrease the counter value in the database. If the counter does not exist yet, create the counter. Also checks whether the counter is allowed to become negative. :param counter_name: The name/identifier of the counter :param allow_negative: Whether the counter can become negative :return: the new integer value of the counter """ counter = EventCounter.query.filter_by(counter_name=counter_name).first() if not counter: counter = EventCounter(counter_name, 0) counter.save() counter.decrease(allow_negative) return counter.counter_value
def increase(counter_name): """ Increase the counter value in the database. If the counter does not exist yet, create the counter. :param counter_name: The name/identifier of the counter :return: None """ # If there is no table row for the current node, create one. node = get_privacyidea_node() counter = EventCounter.query.filter_by(counter_name=counter_name, node=node).first() if not counter: counter = EventCounter(counter_name, 0, node=node) counter.save() counter.increase()
def test_25_eventcounter(self): counter = EventCounter("test_counter", 10) counter.save() counter2 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter2.counter_value, 10) counter2.increase() counter2.increase() counter3 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter3.counter_value, 12) counter3.delete() counter3 = EventCounter.query.filter_by( counter_name="test_counter").first() self.assertEqual(counter3, None)
def decrease(counter_name, allow_negative=False): """ Decrease the counter value in the database. If the counter does not exist yet, create the counter. Also checks whether the counter is allowed to become negative. :param counter_name: The name/identifier of the counter :param allow_negative: Whether the counter can become negative. Note that even if this flag is not set, the counter may become negative due to concurrent queries. :return: None """ node = get_privacyidea_node() counter = EventCounter.query.filter_by(counter_name=counter_name, node=node).first() if not counter: counter = EventCounter(counter_name, 0, node=node) counter.save() # We are allowed to decrease the current counter object only if the overall # counter value is positive (because individual rows may be negative then), # or if we allow negative values. Otherwise, we need to reset all rows of all nodes. if read(counter_name) > 0 or allow_negative: counter.decrease() else: _reset_counter_on_all_nodes(counter_name)