import json
import logging

from rest_framework.response import Response
from rest_framework.views import APIView

from events.services.elk_string_search import ELKStringQuerySearchService, ELKIndexListService

_log = logging.getLogger(__name__)


class ELKStingSearchApiView(APIView):
    """Api for getting data from ELK by index."""

    def get(self, request, index: str, *args, **kwargs) -> Response:
        query_params = json.loads(json.dumps(request.GET))
        _log.debug('Start elastic request.')
        data, status = ELKStringQuerySearchService(index=index, query_params=query_params).data()
        return Response(data, status)


class ELKIndexListApiView(APIView):
    """Api for getting index list from ELK by pattern or all"""

    def get(self, request) -> Response:
        query_params = json.loads(json.dumps(request.GET))
        data, status = ELKIndexListService(query_params).data()
        return Response(data, status)
