tools.win_pg_dump_controller

Yohn Y. 2022-05-29 Parent:a22dd63ba19e Child:92c88f8fb6c9

4:a38a008ce3e8 Go to Latest

tools.win_pg_dump_controller/win_pg_dump_controller/store_controller.py

+ Иная логика работы с эпохами

History
     1.1 --- a/win_pg_dump_controller/store_controller.py	Thu May 05 22:34:16 2022 +0300
     1.2 +++ b/win_pg_dump_controller/store_controller.py	Sun May 29 23:27:20 2022 +0300
     1.3 @@ -20,10 +20,11 @@
     1.4  
     1.5  
     1.6  class IndexItem(object):
     1.7 -    def __init__(self, controller: StoreControllerBase, time: int, filename: str):
     1.8 +    def __init__(self, controller: StoreControllerBase, time: int, filename: str, epoch: int = 0):
     1.9          self._controller = controller
    1.10          self.filename = filename
    1.11          self.time = time
    1.12 +        self.epoch = epoch
    1.13          self._my_path = None
    1.14  
    1.15      def get_path(self):
    1.16 @@ -32,9 +33,18 @@
    1.17  
    1.18          return self._my_path
    1.19  
    1.20 +    def set_epoch(self, num_days: int):
    1.21 +        """\
    1.22 +        Устанавливает значение эпохи для элемиента в соответствии с количеством дней в эпохе
    1.23 +        """
    1.24 +        self.epoch = divmod(self.time, num_days * 86400)[0]  # 86400 - секунд в сутках
    1.25 +
    1.26      def __str__(self) -> str:
    1.27          return self.filename
    1.28  
    1.29 +    def full_desc(self) -> str:
    1.30 +        return f'{self.filename} [epoch="{self.epoch}" created={self.get_datetime()}]'
    1.31 +
    1.32      def get_datetime(self):
    1.33          return datetime.fromtimestamp(self.time)
    1.34  
    1.35 @@ -42,7 +52,7 @@
    1.36          return exists(self.get_path())
    1.37  
    1.38      def to_dict(self) -> dict:
    1.39 -        return dict((i, getattr(self, i)) for i in ('filename', 'time'))
    1.40 +        return dict((i, getattr(self, i)) for i in ('filename', 'time', 'epoch'))
    1.41  
    1.42      @classmethod
    1.43      def from_dict(cls, controller: StoreControllerBase, d: Dict):
    1.44 @@ -87,6 +97,10 @@
    1.45  
    1.46          self.idx = result
    1.47  
    1.48 +    def set_epoch(self, num_days: int):
    1.49 +        for i in self.idx.values():
    1.50 +            i.set_epoch(num_days)
    1.51 +
    1.52      def save_index(self) -> None:
    1.53          with open(self.index_name, 'w') as OUT:
    1.54              json.dump(list(map(lambda x: x.to_dict(), self.idx.values())), OUT)
    1.55 @@ -118,11 +132,13 @@
    1.56          for i in sorted(self.idx, reverse=True):
    1.57              yield self.idx[i]
    1.58  
    1.59 -    def clean(self, tier1_days: int, tier2_copies_interval: int, tier2_store_days: int) -> List[str]:
    1.60 +    def clean(self, tier1_days: int, tier2_store_days: int, epoch_days: int) -> List[str]:
    1.61          to_remove = []
    1.62          tier2_idx = {}
    1.63          now = datetime.now()
    1.64  
    1.65 +        self.set_epoch(epoch_days)
    1.66 +
    1.67          for item in self:
    1.68              if not item.is_exists():
    1.69                  to_remove.append(item)
    1.70 @@ -135,21 +151,15 @@
    1.71                          to_remove.append(item)
    1.72  
    1.73                      else:
    1.74 -                        # Магия: Делим старые резервные копии на эпохи. Эпоха - целая часть от деления количества
    1.75 -                        #        дней, которое лежит резервная копия, на интервал, за который нам нужно хранить
    1.76 -                        #        хотя бы одну копию. В одной эпохе старший файл вытесняет младший. Из вычислений
    1.77 -                        #        убираем период tier1
    1.78 +                        tier2_storing_days = storing_days - tier1_days
    1.79  
    1.80 -                        storing_days_clean = storing_days - tier1_days
    1.81 +                        self.add_op_status(f'"{item.full_desc()}": storing_days={storing_days}'
    1.82 +                                           f' tier2_storing_days={tier2_storing_days}')
    1.83  
    1.84 -                        _epoch = divmod(storing_days_clean, tier2_copies_interval)[0]
    1.85 -                        self.add_op_status(f'"{item}": epoch={_epoch} storing_days={storing_days}'
    1.86 -                                           f' clean_storing_days={storing_days_clean}')
    1.87 +                        if item.epoch in tier2_idx:
    1.88 +                            to_remove.append(tier2_idx[item.epoch])
    1.89  
    1.90 -                        if _epoch in tier2_idx:
    1.91 -                            to_remove.append(tier2_idx[_epoch])
    1.92 -
    1.93 -                        tier2_idx[_epoch] = item
    1.94 +                        tier2_idx[item.epoch] = item
    1.95  
    1.96          result = []
    1.97          for item in to_remove: