offload db population task to rq Signed-off-by: Ameya Shenoy <shenoy.ameya@gmail.com>
91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Bullish page views."""
|
|
|
|
# standard imports
|
|
import datetime
|
|
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 (
|
|
populate_bhav_copy_data,
|
|
fetch_bhav_copy_equity_data,
|
|
stocks_csv_to_json,
|
|
populate_bhav_copy_data_into_postgres
|
|
)
|
|
|
|
|
|
cache = get_redis_connection("default")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Create your views here.
|
|
@api_view(['GET'])
|
|
def bhavCopyEquityList(request):
|
|
ret_message = ""
|
|
|
|
# Verify Date
|
|
req_date = datetime.datetime.strptime(request.query_params.get('date'), '%Y-%m-%d')
|
|
# 18:00 IST == 12:30 UTC
|
|
today = datetime.datetime.now().replace(hour=12, minute=30, second=0, microsecond=0)
|
|
if req_date > today:
|
|
ret_message = "Time travel not yet invented! Returning latest available data."
|
|
req_date = today
|
|
if datetime.datetime.now() < today:
|
|
req_date = today - datetime.timedelta(days=1)
|
|
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('Data not available in DB, trying to fetch from BSE')
|
|
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
|
|
logger.info('Enqueue background task to populate Postgres DB')
|
|
django_rq.enqueue(populate_bhav_copy_data_into_postgres, args=(data, req_date))
|
|
logger.info('Return quick response')
|
|
stocks = stocks_csv_to_json(data)
|
|
return Response({
|
|
"data": stocks,
|
|
"message": 'Data was directly sourced from BSE!'
|
|
})
|
|
return Response({
|
|
"data": serializer.data,
|
|
"message": ret_message
|
|
})
|
|
|
|
|
|
class EmptyRespoinseView(generics.RetrieveAPIView):
|
|
def get(self, request, *args, **kwargs):
|
|
return Response([])
|
|
|
|
|
|
class BhavCopyEquityCustomRedisView(generics.RetrieveAPIView):
|
|
def get(self, request, *args, **kwargs):
|
|
# TODO: try stringified json
|
|
pipe = cache.pipeline()
|
|
stocks = cache.lrange("stocks", 0, -1)
|
|
if len(stocks) == 0:
|
|
populate_bhav_copy_data()
|
|
stocks = cache.lrange("stocks", 0, -1)
|
|
for stock in stocks:
|
|
pipe.hgetall(f"stock:{stock}")
|
|
return Response(pipe.execute())
|
|
|