#2182 frontend: delete rmodels.py file
Merged 4 months ago by praiskup. Opened 4 months ago by frostyx.
copr/ frostyx/copr drop-rmodels  into  main

@@ -1,116 +0,0 @@ 

- # coding: utf-8

- 

- """ Models to redis entities """

- import time

- from math import ceil

- from datetime import timedelta

- 

- 

- class GenericRedisModel(object):

-     _KEY_BASE = "copr:generic"

- 

-     @classmethod

-     def _get_key(cls, name, prefix=None):

-         if prefix:

-             return "{}:{}:{}".format(prefix, cls._KEY_BASE, name)

-         else:

-             return "{}:{}".format(cls._KEY_BASE, name)

- 

- 

- class TimedStatEvents(GenericRedisModel):

-     """

-         Wraps hset structure, where:

-         **key** - name of event, fix prefix specifying events type

-         **member** - bucket representing one day

-         **score** - events count

-     """

-     _KEY_BASE = "copr:tse"

- 

-     @staticmethod

-     def timestamp_to_day(ut):

-         """

-         :param ut: unix timestamp

-         :type ut: float

-         :return: name for the day bucket

-         """

-         td = timedelta(days=1).total_seconds()

-         return int(ceil(ut / td))

- 

-     @classmethod

-     def gen_days_interval(cls, min_ts, max_ts):

-         """

-         Generate list of days bucket names which contains

-             all events between `min_ts` and `max_ts`

-         :param min_ts: min unix timestamp

-         :param max_ts: max unix timestamp

-         :rtype: list

-         """

-         start_ut = cls.timestamp_to_day(min_ts)

-         end_ut = cls.timestamp_to_day(max_ts)

- 

-         return range(start_ut, end_ut + 1)

- 

-     @classmethod

-     def add_event(cls, rconnect, name, timestamp, count=1, prefix=None):

-         """

-         Stoted new event to redist

-         :param rconnect: Connection to a redis

-         :type rconnect: StrictRedis

-         :param name: statistics name

-         :param timestamp: timestamp of event

-         :param count: number of events, default=1

-         :param prefix: prefix for statistics, default is None

-         """

-         count = int(count)

-         ut_day = cls.timestamp_to_day(timestamp)

- 

-         key = cls._get_key(name, prefix)

- 

-         rconnect.hincrby(key, ut_day, count)

- 

-     @classmethod

-     def get_count(cls, rconnect, name, day_min=None, prefix=None, day_max=None):

-         """

-         Count total event occurency between day_min and day_max

-         :param rconnect: Connection to a redis

-         :type rconnect: StrictRedis

-         :param name: statistics name

-         :param day_min: default: seven days ago

-         :param day_max: default: tomorrow

-         :param prefix: prefix for statistics, default is None

- 

-         :rtype: int

-         """

-         key = cls._get_key(name, prefix)

-         if day_min is None:

-             day_min = time.time() - timedelta(days=7).total_seconds()

- 

-         if day_max is None:

-             day_max = time.time() + timedelta(days=1).total_seconds()

- 

-         interval = cls.gen_days_interval(day_min, day_max)

-         if len(interval) == 0:

-             return 0

- 

-         res = rconnect.hmget(key, interval)

-         return sum(int(amount) for amount in res if amount is not None)

- 

- 

-     @classmethod

-     def trim_before(cls, rconnect, name, threshold_timestamp,

-                     prefix=None):

-         """

-         Removes all records occurred before `threshold_timestamp`

-         :param rconnect: StrictRedis

-         :param name: statistics name

-         :param threshold_timestamp: int

-         :param prefix: prefix for statistics, default is None

-         """

- 

-         key = cls._get_key(name, prefix)

- 

-         threshold_day = cls.timestamp_to_day(threshold_timestamp) + 1

-         all_members = rconnect.hgetall(key)

-         to_del = [mb for mb in all_members.keys() if int(mb) < threshold_day]

- 

-         rconnect.hdel(key, *to_del)

@@ -1,52 +0,0 @@ 

- """

- Test the class TimedStatEvents

- """

- 

- import time

- 

- from coprs.rmodels import TimedStatEvents

- from coprs import rcp

- 

- 

- class TestRModels:

-     rc = None  # pylint: disable=invalid-name

-     disabled = None

-     prefix = "copr:test:r_models"

-     time_now = None

- 

-     def setup_method(self):

-         self.rc = rcp.get_connection()

-         self.rc.ping()

-         self.time_now = time.time()

- 

-     def teardown_method(self):

-         keys = self.rc.keys('{}*'.format(self.prefix))

-         if keys:

-             self.rc.delete(*keys)

- 

-     def test_timed_stats_events(self):

-         TimedStatEvents.add_event(self.rc, name="foobar", prefix=self.prefix,

-                                   timestamp=self.time_now, )

- 

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,) == 1

-         TimedStatEvents.add_event(self.rc, name="foobar", prefix=self.prefix,

-                                   timestamp=self.time_now, count=2)

- 

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,) == 3

- 

-         TimedStatEvents.add_event(self.rc, name="foobar", prefix=self.prefix,

-                                   timestamp=self.time_now - 1000000, count=2)

-         TimedStatEvents.add_event(self.rc, name="foobar", prefix=self.prefix,

-                                   timestamp=self.time_now - 3000000, count=3)

- 

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,) == 3

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,

-                                          day_min=self.time_now - 2000000) == 5

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,

-                                          day_min=self.time_now - 5000000) == 8

- 

-         TimedStatEvents.trim_before(self.rc, name="foobar",

-                                     prefix=self.prefix, threshold_timestamp=self.time_now - 200000)

- 

-         assert TimedStatEvents.get_count(self.rc, name="foobar", prefix=self.prefix,

-                                          day_min=self.time_now - 5000000) == 3

After PR#2037, we don't use redis for RPM download statistics, so we
can now remove this leftover file.

Build succeeded.

Commit 31d8312 fixes this pull-request

Pull-Request has been merged by praiskup

4 months ago