Skip to content

Commit

Permalink
Total number of API calls, Chart added.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalanandl177 committed Feb 18, 2021
1 parent 084a9b3 commit 6649442
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include LICENSE
include README.MD
recursive-include drf_api_logger/static *
recursive-include drf_api_logger/templates *
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DRF API Logger
![version](https://img.shields.io/badge/version-0.0.9-blue.svg)
![version](https://img.shields.io/badge/version-1.0.0-blue.svg)
[![Downloads](https://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
[![Downloads](https://pepy.tech/badge/drf-api-logger/month)](https://pepy.tech/project/drf-api-logger)
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
Expand Down
20 changes: 18 additions & 2 deletions drf_api_logger/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django.conf.urls import url
from django.contrib import admin
from django.db.models import Count
from django.shortcuts import render
from django.views import View

from drf_api_logger.utils import database_log_enabled


if database_log_enabled():
from drf_api_logger.models import APILogsModel

Expand All @@ -12,7 +17,7 @@ def added_on_time(self, obj):
return obj.added_on.strftime("%d %b %Y %H:%M:%S")

added_on_time.admin_order_field = 'added_on'
added_on_time.short_description = 'Added On'
added_on_time.short_description = 'Added on'

list_per_page = 20
list_display = ('id', 'api', 'method', 'status_code', 'execution_time', 'added_on_time',)
Expand All @@ -22,12 +27,23 @@ def added_on_time(self, obj):
'execution_time', 'client_ip_address', 'api',
'headers', 'body', 'method', 'response', 'status_code', 'added_on_time',
)
exclude = ('added_on',)

change_list_template = 'charts_change_list.html'
date_hierarchy = 'added_on'

def changelist_view(self, request, extra_context=None):
response = super(APILogsAdmin, self).changelist_view(request, extra_context)
filtered_query_set = response.context_data["cl"].queryset
analytics_model = filtered_query_set.values('added_on__date').annotate(total=Count('id')).order_by('total')
extra_context = dict(analytics=analytics_model)
response.context_data.update(extra_context)
return response

def has_add_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
return False


admin.site.register(APILogsModel, APILogsAdmin)
8 changes: 8 additions & 0 deletions drf_api_logger/collectstatic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.core.management import call_command

from boot_django import boot_django

# call the django setup routine
boot_django()

call_command("collectstatic")
73 changes: 73 additions & 0 deletions drf_api_logger/templates/charts_change_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{% extends "admin/change_list.html" %}
{% load static %}

<!-- Override extrahead to add Chart.js -->
{% block extrahead %}
{{ block.super }}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const ctx = document.getElementById('myChart').getContext('2d');

// Sample data
const chartData = [
{% for item in analytics %}
{"date": "{{ item.added_on__date }}", "y": {{ item.total }}},
{% endfor %}
];

// Parse the dates to JS
chartData.forEach((d) => {
d.x = new Date(d.date);
});

// Render the chart
const chart = new Chart(ctx, {
type: 'bar',
data: {
datasets: [
{
label: 'Number of API calls',
data: chartData,
backgroundColor: 'rgba(220,20,20,0.5)',
},
],
},
options: {
responsive: true,
scales: {
xAxes: [
{
type: 'time',
time: {
unit: 'day',
round: 'day',
displayFormats: {
day: 'MMM D',
},
},
},
],
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
});
</script>
{% endblock %}

{% block content %}
<!-- Render our chart -->
<div style="width: 80%;">
<canvas style="margin-bottom: 30px; width: 60%; height: 50%;" id="myChart"></canvas>
</div>
<!-- Render the rest of the ChangeList view -->
{{ block.super }}
{% endblock %}
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get_long_desc():

setuptools.setup(
name="drf_api_logger",
version="0.0.9",
version="1.0.0",
author="Vishal Anand",
author_email="[email protected]",
description="An API Logger for your Django Rest Framework project.",
Expand All @@ -20,6 +20,7 @@ def get_long_desc():
install_requires=["djangorestframework>=3.7.4", "bleach>=3.1.5"],
license='GNU General Public License v3.0',
python_requires='>=3.5',
include_package_data=True,
classifiers=[
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
Expand Down

0 comments on commit 6649442

Please sign in to comment.