122 lines
3.6 KiB
Python
122 lines
3.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Bullish page views."""
|
|
|
|
# standard imports
|
|
import json
|
|
import logging
|
|
|
|
# third-party imports
|
|
import django_rq
|
|
|
|
from rest_framework import generics
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import api_view
|
|
from django_redis import get_redis_connection
|
|
|
|
# app imports
|
|
from app.models import BhavCopyEquity
|
|
from app.serializers import BhavCopyEquitySerializer
|
|
from app.utils import (
|
|
fetch_bhav_copy_equity_data,
|
|
stocks_csv_to_json,
|
|
populate_bhav_copy_data_into_postgres,
|
|
populate_bhav_copy_data_into_redis_v1,
|
|
populate_bhav_copy_data_into_redis_v2,
|
|
verify_date
|
|
)
|
|
|
|
|
|
cache = get_redis_connection("default")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Create your views here.
|
|
class EmptyRespoinseView(generics.RetrieveAPIView):
|
|
def get(self, request, *args, **kwargs):
|
|
return Response([])
|
|
|
|
|
|
@api_view(['GET'])
|
|
def dbBhavCopyEquityList(request):
|
|
req_date, ret_message = verify_date(request.query_params.get('date'), "")
|
|
|
|
queryset = BhavCopyEquity.objects.all().filter(date=req_date)
|
|
serializer = BhavCopyEquitySerializer(queryset, many=True)
|
|
|
|
# Fetch data if not present
|
|
if len(serializer.data) == 0:
|
|
logger.info('Enqueue background task to populate Postgres DB')
|
|
django_rq.enqueue(populate_bhav_copy_data_into_postgres, args=(None, req_date))
|
|
logger.info('Data not available in Postgres, redirecting to BSE')
|
|
return _bse_bhav_copy_equity_list(request)
|
|
return Response({
|
|
"data": serializer.data,
|
|
"message": ret_message
|
|
})
|
|
|
|
|
|
@api_view(['GET'])
|
|
def cacheBhavCopyEquityListV1(request):
|
|
date = request.query_params.get('date')
|
|
req_date, ret_message = verify_date(date, "")
|
|
datestr = req_date.strftime("%d%m%y")
|
|
|
|
stocks = cache.lrange(f"stocks:{datestr}", 0, -1)
|
|
if len(stocks) == 0:
|
|
logger.info('Enqueue background task to populate Redis')
|
|
django_rq.enqueue(populate_bhav_copy_data_into_redis_v1, args=(None, req_date))
|
|
logger.info('Data not available in Redis, trying to fetch from BSE')
|
|
return _bse_bhav_copy_equity_list(request)
|
|
pipe = cache.pipeline()
|
|
for stock in stocks:
|
|
pipe.hgetall(f"stock:{datestr}:{stock}")
|
|
return Response({
|
|
"data": pipe.execute(),
|
|
"message": ret_message
|
|
})
|
|
|
|
|
|
@api_view(['GET'])
|
|
def cacheBhavCopyEquityListV2(request):
|
|
date = request.query_params.get('date')
|
|
req_date, ret_message = verify_date(date, "")
|
|
datestr = req_date.strftime("%d%m%y")
|
|
|
|
stocks = cache.get(f"stocks:v2:{datestr}")
|
|
if stocks is None:
|
|
logger.info('Enqueue background task to populate Redis')
|
|
django_rq.enqueue(populate_bhav_copy_data_into_redis_v2, args=(None, req_date))
|
|
logger.info('Data not available in Redis, trying to fetch from BSE')
|
|
return _bse_bhav_copy_equity_list(request)
|
|
return Response({
|
|
"data": json.loads(stocks),
|
|
"message": ret_message
|
|
})
|
|
|
|
|
|
@api_view(['GET'])
|
|
def bseBhavCopyEquityList(request):
|
|
"""Fetch and return data from BSE."""
|
|
return _bse_bhav_copy_equity_list(request)
|
|
|
|
|
|
def _bse_bhav_copy_equity_list(request):
|
|
"""Fetchinig data from BSE helper."""
|
|
date = request.query_params.get('date')
|
|
req_date, _ = verify_date(date, "")
|
|
try:
|
|
data = fetch_bhav_copy_equity_data(date=req_date)
|
|
except:
|
|
return Response({
|
|
'data': [],
|
|
'message': "Unable to fetch data from BSE"
|
|
})
|
|
del data[0] # delete title row
|
|
stocks = stocks_csv_to_json(data)
|
|
return Response({
|
|
"data": stocks,
|
|
"message": 'Data was directly sourced from BSE!'
|
|
})
|
|
|