Skip to content

Commit 6649442

Browse files
Total number of API calls, Chart added.
1 parent 084a9b3 commit 6649442

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include LICENSE
2+
include README.MD
3+
recursive-include drf_api_logger/static *
4+
recursive-include drf_api_logger/templates *

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DRF API Logger
2-
![version](https://img.shields.io/badge/version-0.0.9-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.0-blue.svg)
33
[![Downloads](https://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
44
[![Downloads](https://pepy.tech/badge/drf-api-logger/month)](https://pepy.tech/project/drf-api-logger)
55
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)

drf_api_logger/admin.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from django.conf.urls import url
12
from django.contrib import admin
3+
from django.db.models import Count
4+
from django.shortcuts import render
5+
from django.views import View
26

37
from drf_api_logger.utils import database_log_enabled
48

9+
510
if database_log_enabled():
611
from drf_api_logger.models import APILogsModel
712

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

1419
added_on_time.admin_order_field = 'added_on'
15-
added_on_time.short_description = 'Added On'
20+
added_on_time.short_description = 'Added on'
1621

1722
list_per_page = 20
1823
list_display = ('id', 'api', 'method', 'status_code', 'execution_time', 'added_on_time',)
@@ -22,12 +27,23 @@ def added_on_time(self, obj):
2227
'execution_time', 'client_ip_address', 'api',
2328
'headers', 'body', 'method', 'response', 'status_code', 'added_on_time',
2429
)
30+
exclude = ('added_on',)
31+
32+
change_list_template = 'charts_change_list.html'
33+
date_hierarchy = 'added_on'
34+
35+
def changelist_view(self, request, extra_context=None):
36+
response = super(APILogsAdmin, self).changelist_view(request, extra_context)
37+
filtered_query_set = response.context_data["cl"].queryset
38+
analytics_model = filtered_query_set.values('added_on__date').annotate(total=Count('id')).order_by('total')
39+
extra_context = dict(analytics=analytics_model)
40+
response.context_data.update(extra_context)
41+
return response
2542

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

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

32-
3349
admin.site.register(APILogsModel, APILogsAdmin)

drf_api_logger/collectstatic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.core.management import call_command
2+
3+
from boot_django import boot_django
4+
5+
# call the django setup routine
6+
boot_django()
7+
8+
call_command("collectstatic")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{% extends "admin/change_list.html" %}
2+
{% load static %}
3+
4+
<!-- Override extrahead to add Chart.js -->
5+
{% block extrahead %}
6+
{{ block.super }}
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" />
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
9+
<script>
10+
document.addEventListener('DOMContentLoaded', () => {
11+
const ctx = document.getElementById('myChart').getContext('2d');
12+
13+
// Sample data
14+
const chartData = [
15+
{% for item in analytics %}
16+
{"date": "{{ item.added_on__date }}", "y": {{ item.total }}},
17+
{% endfor %}
18+
];
19+
20+
// Parse the dates to JS
21+
chartData.forEach((d) => {
22+
d.x = new Date(d.date);
23+
});
24+
25+
// Render the chart
26+
const chart = new Chart(ctx, {
27+
type: 'bar',
28+
data: {
29+
datasets: [
30+
{
31+
label: 'Number of API calls',
32+
data: chartData,
33+
backgroundColor: 'rgba(220,20,20,0.5)',
34+
},
35+
],
36+
},
37+
options: {
38+
responsive: true,
39+
scales: {
40+
xAxes: [
41+
{
42+
type: 'time',
43+
time: {
44+
unit: 'day',
45+
round: 'day',
46+
displayFormats: {
47+
day: 'MMM D',
48+
},
49+
},
50+
},
51+
],
52+
yAxes: [
53+
{
54+
ticks: {
55+
beginAtZero: true,
56+
},
57+
},
58+
],
59+
},
60+
},
61+
});
62+
});
63+
</script>
64+
{% endblock %}
65+
66+
{% block content %}
67+
<!-- Render our chart -->
68+
<div style="width: 80%;">
69+
<canvas style="margin-bottom: 30px; width: 60%; height: 50%;" id="myChart"></canvas>
70+
</div>
71+
<!-- Render the rest of the ChangeList view -->
72+
{{ block.super }}
73+
{% endblock %}

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def get_long_desc():
99

1010
setuptools.setup(
1111
name="drf_api_logger",
12-
version="0.0.9",
12+
version="1.0.0",
1313
author="Vishal Anand",
1414
author_email="vishalanandl177@gmail.com",
1515
description="An API Logger for your Django Rest Framework project.",
@@ -20,6 +20,7 @@ def get_long_desc():
2020
install_requires=["djangorestframework>=3.7.4", "bleach>=3.1.5"],
2121
license='GNU General Public License v3.0',
2222
python_requires='>=3.5',
23+
include_package_data=True,
2324
classifiers=[
2425
'Programming Language :: Python :: 3.5',
2526
'Programming Language :: Python :: 3.6',

0 commit comments

Comments
 (0)