Beispiel #1
0
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()
Beispiel #2
0
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
Beispiel #3
0
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)
Beispiel #4
0
    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)
Beispiel #5
0
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
Beispiel #6
0
    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)
Beispiel #7
0
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()
Beispiel #8
0
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)