Пример #1
0
def get_statistics(stats_key=None):
    """
    return a list of all available statistics keys in the database if no *stats_key*
    is specified.

    If a stats_key is specified it returns the data of this key.
    The parameters "start" and "end" can be used to specify a time window,
    from which the statistics data should be fetched.
    """
    if stats_key is None:
        stats_keys = get_stats_keys()
        g.audit_object.log({"success": True})
        return send_result(stats_keys)
    else:
        param = request.all_data
        start = getParam(param, "start")
        if start:
            start = parse_legacy_time(start, return_date=True)
        end = getParam(param, "end")
        if end:
            end = parse_legacy_time(end, return_date=True)
        values = get_values(stats_key=stats_key,
                            start_timestamp=start,
                            end_timestamp=end)
        # convert timestamps to strings
        values_w_string = [(s[0].strftime(AUTH_DATE_FORMAT), s[1])
                           for s in values]
        g.audit_object.log({"success": True})
        return send_result(values_w_string)
Пример #2
0
def get_statistics(stats_key=None):
    """
    return a list of all available statistics keys in the database if no *stats_key*
    is specified.

    If a stats_key is specified it returns the data of this key.
    The parameters "start" and "end" can be used to specify a time window,
    from which the statistics data should be fetched.
    """
    if stats_key is None:
        stats_keys = get_stats_keys()
        g.audit_object.log({"success": True})
        return send_result(stats_keys)
    else:
        param = request.all_data
        start = getParam(param, "start")
        if start:
            start = parse_legacy_time(start, return_date=True)
        end = getParam(param, "end")
        if end:
            end = parse_legacy_time(end, return_date=True)
        values = get_values(stats_key=stats_key, start_timestamp=start, end_timestamp=end)
        # convert timestamps to strings
        values_w_string = [(s[0].strftime(AUTH_DATE_FORMAT), s[1]) for s in values]
        g.audit_object.log({"success": True})
        return send_result(values_w_string)
    def test_04_get_values(self):
        # delete old entries
        keys = get_stats_keys()
        for k in keys:
            delete_stats(k)

        ts = datetime.datetime.now(tzlocal())
        write_stats("key1", 1, timestamp=ts - timedelta(minutes=10))
        write_stats("key1", 2, timestamp=ts - timedelta(minutes=9))
        write_stats("key1", 3, timestamp=ts - timedelta(minutes=8))
        write_stats("key1", 4, timestamp=ts - timedelta(minutes=7))
        write_stats("key1", 5, timestamp=ts - timedelta(minutes=6))
        write_stats("key1", 6, timestamp=ts - timedelta(minutes=5))
        write_stats("key1", 7, timestamp=ts - timedelta(minutes=4))
        write_stats("key1", 8, timestamp=ts - timedelta(minutes=3))
        write_stats("key1", 9, timestamp=ts - timedelta(minutes=2))
        write_stats("key1", 10, timestamp=ts - timedelta(minutes=1))

        r = get_values("key1")
        self.assertEqual(len(r), 10)
        # The third entry is a 3
        self.assertEqual(r[2][1], 3)
        # The last value is a 10
        self.assertEqual(r[9][1], 10)

        r = get_values("key1",
                       start_timestamp=ts - timedelta(minutes=8),
                       end_timestamp=ts - timedelta(minutes=4))
        # We get 3,4,5,6,7
        self.assertEqual([entry[1] for entry in r], [3, 4, 5, 6, 7])
        # Assert it is the correct time, and timezone-aware UTC
        self.assertEqual(r[0][0], ts - timedelta(minutes=8))
        self.assertEqual(r[0][0].tzinfo, tzutc())
        self.assertEqual(r[-1][0], ts - timedelta(minutes=4))

        r = get_values("key1",
                       start_timestamp=ts - timedelta(minutes=8))
        self.assertEqual([entry[1] for entry in r], [3, 4, 5, 6, 7, 8, 9, 10])

        r = get_values("key1",
                       end_timestamp=ts - timedelta(minutes=8))
        self.assertEqual([entry[1] for entry in r], [1, 2, 3])

        # Get the last value of key1
        r = get_last_value("key1")
        self.assertEqual(r, 10)
    def test_04_get_values(self):
        # delete old entries
        keys = get_stats_keys()
        for k in keys:
            delete_stats(k)

        ts = datetime.datetime.now(tzlocal())
        write_stats("key1", 1, timestamp=ts - timedelta(minutes=10))
        write_stats("key1", 2, timestamp=ts - timedelta(minutes=9))
        write_stats("key1", 3, timestamp=ts - timedelta(minutes=8))
        write_stats("key1", 4, timestamp=ts - timedelta(minutes=7))
        write_stats("key1", 5, timestamp=ts - timedelta(minutes=6))
        write_stats("key1", 6, timestamp=ts - timedelta(minutes=5))
        write_stats("key1", 7, timestamp=ts - timedelta(minutes=4))
        write_stats("key1", 8, timestamp=ts - timedelta(minutes=3))
        write_stats("key1", 9, timestamp=ts - timedelta(minutes=2))
        write_stats("key1", 10, timestamp=ts - timedelta(minutes=1))

        db.session.commit()
        r = get_values("key1")
        self.assertEqual(len(r), 10)
        # The third entry is a 3
        self.assertEqual(r[2][1], 3)
        # The last value is a 10
        self.assertEqual(r[9][1], 10)

        r = get_values("key1",
                       start_timestamp=ts - timedelta(minutes=8),
                       end_timestamp=ts - timedelta(minutes=4))
        # We get 3,4,5,6,7
        self.assertEqual([entry[1] for entry in r], [3, 4, 5, 6, 7])
        # Assert it is the correct time, and timezone-aware UTC
        self.assertEqual(r[0][0], ts - timedelta(minutes=8))
        self.assertEqual(r[0][0].tzinfo, tzutc())
        self.assertEqual(r[-1][0], ts - timedelta(minutes=4))

        r = get_values("key1", start_timestamp=ts - timedelta(minutes=8))
        self.assertEqual([entry[1] for entry in r], [3, 4, 5, 6, 7, 8, 9, 10])

        r = get_values("key1", end_timestamp=ts - timedelta(minutes=8))
        self.assertEqual([entry[1] for entry in r], [1, 2, 3])

        # Get the last value of key1
        r = get_last_value("key1")
        self.assertEqual(r, 10)
    def test_00_read_simplestats_to_monitoringstats(self):
        self.setUp_user_realms()

        # create a simple statistics class
        sst = SimpleStatsTask(current_app.config)
        # and set all parameters to 'true'
        params = {}
        for o in sst.options.keys():
            params[o] = True

        # first we create a software token
        init_token({"type": "totp", "otpkey": self.otpkey, "serial": self.serials[0]})

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][0], get_values(o)[0][1],
                             msg="Current option: {0}".format(o))

        # add a hardware token
        init_token({"type": "totp", "otpkey": self.otpkey, "serial": self.serials[1]},
                   tokenkind=TOKENKIND.HARDWARE)

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][1], get_values(o)[1][1],
                             msg="Current option: {0}".format(o))

        # add a hardware token and assign it to a user
        token = init_token({"type": "totp", "otpkey": self.otpkey, "serial": self.serials[2]},
                           tokenkind=TOKENKIND.HARDWARE,
                           user=User(login="******", realm=self.realm1))
        self.assertEqual("cornelius", token.user.login)
        self.assertTrue(token.is_active())
        self.assertEqual(TOKENKIND.HARDWARE, token.get_tokeninfo('tokenkind'))

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][2], get_values(o)[2][1],
                             msg="Current option: {0}".format(o))

        # add a software token and assign it to a user
        token = init_token({"type": "totp", "otpkey": self.otpkey, "serial": self.serials[3]},
                           user=User(login="******", realm=self.realm1))
        self.assertEqual("selfservice", token.user.login)
        self.assertTrue(token.is_active())
        self.assertEqual(TOKENKIND.SOFTWARE, token.get_tokeninfo(u'tokenkind'))

        # check if getting only certain stats works
        params['assigned_tokens'] = False
        sst.do(params)
        self.assertEqual(3, len(get_values('assigned_tokens')))
        self.assertEqual(4, len(get_values('user_with_token')))
        for o in sst.options.keys():
            if o != 'assigned_tokens':
                self.assertEqual(simple_results[o][3], get_values(o)[3][1],
                                 msg="Current option: {0}".format(o))
        self.assertEqual(simple_results['assigned_tokens'][3],
                         get_values('assigned_tokens')[2][1])
    def test_00_read_eventcounter_to_monitoringstats(self):
        r = increase("counter1")
        self.assertEqual(r, 1)

        r = increase("counter2")
        self.assertEqual(r, 1)

        r = increase("counter1")
        self.assertEqual(r, 2)

        r = read("counter1")
        self.assertEqual(r, 2)

        et = EventCounterTask()
        params = {
            "event_counter": "counter1",
            "stats_key": "C1",
            "reset_event_counter": "True"
        }

        # Now we execute the task
        et.do(params)

        # The counter "counter1" should be resetted
        self.assertEqual(read("counter1"), 0)

        # The value "2" should be written to the statistics key C1.
        stats = get_values("C1")
        self.assertTrue(len(stats), 1)
        self.assertTrue(stats[0][1], 2)

        # Now we increase the event counter again
        increase("counter1")

        # ..and run the event counter task
        et.do(params)
        stats = get_values("C1")
        self.assertTrue(len(stats), 2)
        self.assertTrue(stats[0][1], 2)
        self.assertTrue(stats[0][1], 1)
    def test_00_read_eventcounter_to_monitoringstats(self):
        r = increase("counter1")
        self.assertEqual(r, 1)

        r = increase("counter2")
        self.assertEqual(r, 1)

        r = increase("counter1")
        self.assertEqual(r, 2)

        r = read("counter1")
        self.assertEqual(r, 2)

        et = EventCounterTask()
        params = {"event_counter": "counter1",
                  "stats_key": "C1",
                  "reset_event_counter": "True"}

        # Now we execute the task
        et.do(params)

        # The counter "counter1" should be resetted
        self.assertEqual(read("counter1"), 0)

        # The value "2" should be written to the statistics key C1.
        stats = get_values("C1")
        self.assertTrue(len(stats), 1)
        self.assertTrue(stats[0][1], 2)

        # Now we increase the event counter again
        increase("counter1")

        # ..and run the event counter task
        et.do(params)
        stats = get_values("C1")
        self.assertTrue(len(stats), 2)
        self.assertTrue(stats[0][1], 2)
        self.assertTrue(stats[0][1], 1)
Пример #8
0
    def test_00_read_simplestats_to_monitoringstats(self):
        self.setUp_user_realms()

        # create a simple statistics class
        sst = SimpleStatsTask(current_app.config)
        # and set all parameters to 'true'
        params = {}
        for o in sst.options.keys():
            params[o] = True

        # first we create a software token
        init_token({
            "type": "totp",
            "otpkey": self.otpkey,
            "serial": self.serials[0]
        })

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][0],
                             get_values(o)[0][1],
                             msg="Current option: {0}".format(o))

        # add a hardware token
        init_token(
            {
                "type": "totp",
                "otpkey": self.otpkey,
                "serial": self.serials[1]
            },
            tokenkind=TOKENKIND.HARDWARE)

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][1],
                             get_values(o)[1][1],
                             msg="Current option: {0}".format(o))

        # add a hardware token and assign it to a user
        token = init_token(
            {
                "type": "totp",
                "otpkey": self.otpkey,
                "serial": self.serials[2]
            },
            tokenkind=TOKENKIND.HARDWARE,
            user=User(login="******", realm=self.realm1))
        self.assertEqual("cornelius", token.user.login)
        self.assertTrue(token.is_active())
        self.assertEqual(TOKENKIND.HARDWARE, token.get_tokeninfo('tokenkind'))

        sst.do(params)
        for o in sst.options.keys():
            self.assertEqual(simple_results[o][2],
                             get_values(o)[2][1],
                             msg="Current option: {0}".format(o))

        # add a software token and assign it to a user
        token = init_token(
            {
                "type": "totp",
                "otpkey": self.otpkey,
                "serial": self.serials[3]
            },
            user=User(login="******", realm=self.realm1))
        self.assertEqual("selfservice", token.user.login)
        self.assertTrue(token.is_active())
        self.assertEqual(TOKENKIND.SOFTWARE, token.get_tokeninfo(u'tokenkind'))

        # check if getting only certain stats works
        params['assigned_tokens'] = False
        sst.do(params)
        self.assertEqual(3, len(get_values('assigned_tokens')))
        self.assertEqual(4, len(get_values('user_with_token')))
        for o in sst.options.keys():
            if o != 'assigned_tokens':
                self.assertEqual(simple_results[o][3],
                                 get_values(o)[3][1],
                                 msg="Current option: {0}".format(o))
        self.assertEqual(simple_results['assigned_tokens'][3],
                         get_values('assigned_tokens')[2][1])