tools.win_pg_dump_controller

Yohn Y. 2022-05-29 Parent:34db5b44491d Child:7de4fb4ef2f5

4:a38a008ce3e8 Browse Files

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

win_pg_dump_controller/executor.py win_pg_dump_controller/store_controller.py

     1.1 --- a/win_pg_dump_controller/executor.py	Thu May 05 22:34:16 2022 +0300
     1.2 +++ b/win_pg_dump_controller/executor.py	Sun May 29 23:27:20 2022 +0300
     1.3 @@ -55,14 +55,14 @@
     1.4      else:
     1.5          stor.add_item(backup_item)
     1.6  
     1.7 -    cleaned = stor.clean(config.teir1_days, config.teir2_copies_interval, config.tier2_store_days)
     1.8 +    cleaned = stor.clean(config.teir1_days, config.tier2_store_days, config.teir2_copies_interval)
     1.9  
    1.10      if cleaned:
    1.11          log(log_t('Очистка старых копий'))
    1.12 -        log(log_t('\n'.join(map(lambda x: f'- {x}', cleaned))))
    1.13 +        log('\n'.join(map(lambda x: log_t(f'- {x}'), cleaned)))
    1.14  
    1.15      if stor.op_adv_status:
    1.16          log(log_t('Расширенное состояние хранимых копий второго класса'))
    1.17 -        log(log_t('\n'.join(map(lambda x: f'- {x}', stor.op_adv_status))))
    1.18 +        log('\n'.join(map(lambda x: log_t(f'- {x}'), stor.op_adv_status)))
    1.19  
    1.20      stor.save_index()
     2.1 --- a/win_pg_dump_controller/store_controller.py	Thu May 05 22:34:16 2022 +0300
     2.2 +++ b/win_pg_dump_controller/store_controller.py	Sun May 29 23:27:20 2022 +0300
     2.3 @@ -20,10 +20,11 @@
     2.4  
     2.5  
     2.6  class IndexItem(object):
     2.7 -    def __init__(self, controller: StoreControllerBase, time: int, filename: str):
     2.8 +    def __init__(self, controller: StoreControllerBase, time: int, filename: str, epoch: int = 0):
     2.9          self._controller = controller
    2.10          self.filename = filename
    2.11          self.time = time
    2.12 +        self.epoch = epoch
    2.13          self._my_path = None
    2.14  
    2.15      def get_path(self):
    2.16 @@ -32,9 +33,18 @@
    2.17  
    2.18          return self._my_path
    2.19  
    2.20 +    def set_epoch(self, num_days: int):
    2.21 +        """\
    2.22 +        Устанавливает значение эпохи для элемиента в соответствии с количеством дней в эпохе
    2.23 +        """
    2.24 +        self.epoch = divmod(self.time, num_days * 86400)[0]  # 86400 - секунд в сутках
    2.25 +
    2.26      def __str__(self) -> str:
    2.27          return self.filename
    2.28  
    2.29 +    def full_desc(self) -> str:
    2.30 +        return f'{self.filename} [epoch="{self.epoch}" created={self.get_datetime()}]'
    2.31 +
    2.32      def get_datetime(self):
    2.33          return datetime.fromtimestamp(self.time)
    2.34  
    2.35 @@ -42,7 +52,7 @@
    2.36          return exists(self.get_path())
    2.37  
    2.38      def to_dict(self) -> dict:
    2.39 -        return dict((i, getattr(self, i)) for i in ('filename', 'time'))
    2.40 +        return dict((i, getattr(self, i)) for i in ('filename', 'time', 'epoch'))
    2.41  
    2.42      @classmethod
    2.43      def from_dict(cls, controller: StoreControllerBase, d: Dict):
    2.44 @@ -87,6 +97,10 @@
    2.45  
    2.46          self.idx = result
    2.47  
    2.48 +    def set_epoch(self, num_days: int):
    2.49 +        for i in self.idx.values():
    2.50 +            i.set_epoch(num_days)
    2.51 +
    2.52      def save_index(self) -> None:
    2.53          with open(self.index_name, 'w') as OUT:
    2.54              json.dump(list(map(lambda x: x.to_dict(), self.idx.values())), OUT)
    2.55 @@ -118,11 +132,13 @@
    2.56          for i in sorted(self.idx, reverse=True):
    2.57              yield self.idx[i]
    2.58  
    2.59 -    def clean(self, tier1_days: int, tier2_copies_interval: int, tier2_store_days: int) -> List[str]:
    2.60 +    def clean(self, tier1_days: int, tier2_store_days: int, epoch_days: int) -> List[str]:
    2.61          to_remove = []
    2.62          tier2_idx = {}
    2.63          now = datetime.now()
    2.64  
    2.65 +        self.set_epoch(epoch_days)
    2.66 +
    2.67          for item in self:
    2.68              if not item.is_exists():
    2.69                  to_remove.append(item)
    2.70 @@ -135,21 +151,15 @@
    2.71                          to_remove.append(item)
    2.72  
    2.73                      else:
    2.74 -                        # Магия: Делим старые резервные копии на эпохи. Эпоха - целая часть от деления количества
    2.75 -                        #        дней, которое лежит резервная копия, на интервал, за который нам нужно хранить
    2.76 -                        #        хотя бы одну копию. В одной эпохе старший файл вытесняет младший. Из вычислений
    2.77 -                        #        убираем период tier1
    2.78 +                        tier2_storing_days = storing_days - tier1_days
    2.79  
    2.80 -                        storing_days_clean = storing_days - tier1_days
    2.81 +                        self.add_op_status(f'"{item.full_desc()}": storing_days={storing_days}'
    2.82 +                                           f' tier2_storing_days={tier2_storing_days}')
    2.83  
    2.84 -                        _epoch = divmod(storing_days_clean, tier2_copies_interval)[0]
    2.85 -                        self.add_op_status(f'"{item}": epoch={_epoch} storing_days={storing_days}'
    2.86 -                                           f' clean_storing_days={storing_days_clean}')
    2.87 +                        if item.epoch in tier2_idx:
    2.88 +                            to_remove.append(tier2_idx[item.epoch])
    2.89  
    2.90 -                        if _epoch in tier2_idx:
    2.91 -                            to_remove.append(tier2_idx[_epoch])
    2.92 -
    2.93 -                        tier2_idx[_epoch] = item
    2.94 +                        tier2_idx[item.epoch] = item
    2.95  
    2.96          result = []
    2.97          for item in to_remove: