def get_stats_handler()

in modular_api_cli/modular_handler/usage_handler.py [0:0]


    def get_stats_handler(self, from_month, to_month, display_table, path):
        _LOG.info(f'Going to get usage statistic. Parameters: from_month '
                  f'\'{from_month}\', to_month \'{to_month}\', '
                  f'display_table \'{display_table}\', path \'{path}\'')
        try:
            if from_month:
                fd_dt = datetime.strptime(from_month, '%Y-%m')
                from_date = int(fd_dt.timestamp()) * 1000
            else:
                utc_time_now = datetime.now(timezone.utc)
                utc_time_now = utc_time_now.replace(
                    day=1, hour=0, minute=0, second=0)
                from_date = int(utc_time_now.timestamp() * 1000)

            if to_month:
                td_dt = datetime.strptime(to_month, '%Y-%m')
                td_dt = td_dt.replace(day=1, hour=0, minute=0, second=0)
                to_date = int(td_dt.timestamp()) * 1000
            else:
                to_date = None
        except ValueError:
            _LOG.error('Invalid date format')
            raise ModularApiBadRequestException(
                'Please check date(s) spelling, required format is "yyyy-mm"')

        if (from_date and to_date) and (from_date >= to_date):
            _LOG.error('Invalid period')
            raise ModularApiBadRequestException(
                'Start month can not be greater than or equal to the end '
                'month')

        it = chain.from_iterable(
            self.usage_service.get_stats(key, from_date, to_date)
            for key in self.usage_service.modules_info
        )
        raw_result = [i.get_json() for i in it]

        if not raw_result:
            _LOG.info('No usage statistic by provided filters')
            return CommandResponse(
                message='There is no data by provided filters')

        table_report = self._prettify_report(raw_result, display_table)
        if table_report:
            return CommandResponse(table_title='Statistic', items=table_report)

        file_path = self._save_report_and_get_path(raw_result, path)
        _LOG.info(f'Report file has been stored by path: \'{file_path}\'')
        return CommandResponse(
            message=f'Report file has been stored by path: \'{file_path}\'')