I was trying to get the number of insurance claims by Day and what time it occurred. For Example, it is March 22 today and there were claims on 8 am another in 12noon. I was trying to get all the claim count this day and what hour did the claims occured.
I did try using modified_at__hour but it doesn't show what I needed. Sample code for claims per week and show what day it was claimed
class GetClaimsCompare_week(APIView):
def get_claim_count(self, claims_data, claim_type):
claims_count = claims_data.filter(claim_type = claim_type).count()
return claims_count
def get_claims_type(self, claims_per_day):
return claims_per_day.claim_type
#return claims_data.claim_type
def get(self, request):
today = datetime.now()
claims_data = Claims.objects.filter(modified_at__day = today.day)
claims_per_day = claims_data.annotate(day = TruncDay('modified_at')).values('day').annotate(claim_type=Count('id'))
labels = []
claims_type = list(set(map(self.get_claims_type, claims_data)))
final = {}
for claims in claims_per_day:
labels.append(claims['day'])
#data.append(claims['claims'])
for claim_type in claims_type:
final[claim_type] = self.get_claim_count(claims_data, claim_type)
context = {
'Date(s)': labels,
'Claims on this date': final
}
return Response(context)