feat / AEB-20 create csv parser
This commit is contained in:
0
backend/api/management/commands/__init__.py
Normal file
0
backend/api/management/commands/__init__.py
Normal file
64
backend/api/management/commands/parse_multiplexor_data.py
Normal file
64
backend/api/management/commands/parse_multiplexor_data.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
import logging
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
from ...parser.parse_csv import parse_multiplexor_data
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Парсит данные из CSV файлов мультиплексоров'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--data-dir',
|
||||
type=str,
|
||||
help='Директория с CSV файлами',
|
||||
default=os.path.join(settings.BASE_DIR, 'data', 'multiplexors')
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
data_dir = options['data_dir']
|
||||
|
||||
# проверяем существование директории
|
||||
if not os.path.exists(data_dir):
|
||||
logger.error(f"Директория {data_dir} не существует")
|
||||
return
|
||||
|
||||
# получаем список CSV файлов
|
||||
csv_files = [f for f in os.listdir(data_dir) if f.endswith('.csv')]
|
||||
if not csv_files:
|
||||
logger.info(f"CSV файлы не найдены в директории {data_dir}")
|
||||
return
|
||||
|
||||
total_metrics = 0
|
||||
total_alerts = 0
|
||||
errors = []
|
||||
|
||||
# обрабатываем каждый файл
|
||||
for csv_file in csv_files:
|
||||
file_path = os.path.join(data_dir, csv_file)
|
||||
logger.info(f"Обработка файла: {file_path}")
|
||||
|
||||
try:
|
||||
result = parse_multiplexor_data(file_path)
|
||||
|
||||
if result['success']:
|
||||
total_metrics += result['metrics_count']
|
||||
total_alerts += result['alerts_count']
|
||||
if result['errors']:
|
||||
errors.extend([f"{csv_file}: {err}" for err in result['errors']])
|
||||
else:
|
||||
errors.extend([f"{csv_file}: {err}" for err in result['errors']])
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Ошибка при обработке файла {csv_file}: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
errors.append(error_msg)
|
||||
|
||||
# выводим итоговую статистику
|
||||
logger.info(f"Обработка завершена. Создано метрик: {total_metrics}, алертов: {total_alerts}")
|
||||
if errors:
|
||||
logger.warning("Обнаружены ошибки:")
|
||||
for error in errors:
|
||||
logger.warning(error)
|
||||
Reference in New Issue
Block a user