Skip to content

Commit

Permalink
Added raw version of metrics #14
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtesobe committed Mar 8, 2017
1 parent dca7434 commit 5e7d60c
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 2 deletions.
1 change: 1 addition & 0 deletions apimanager/apimanager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'oauth',
'consumers',
'users',
'metrics',
#'api_calls',
#'api_config',
]
Expand Down
1 change: 1 addition & 0 deletions apimanager/apimanager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
url(r'^oauth/', include('oauth.urls')),
url(r'^consumers/', include('consumers.urls')),
url(r'^users/', include('users.urls')),
url(r'^metrics/', include('metrics.urls')),
#url(r'^api_calls/', include('api_calls.urls')),
#url(r'^api_config/', include('api_config.urls')),
]
6 changes: 4 additions & 2 deletions apimanager/base/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
{% url "consumers-index" as consumers_index_url %}
<li{% if consumers_index_url in request.path %} class="active" {% endif %}><a href="{{ consumers_index_url }}">Consumers</a></li>
<li{% if consumers_index_url in request.path %} class="active"{% endif %}><a href="{{ consumers_index_url }}">Consumers</a></li>
{% url "users-index" as users_index_url %}
<li{% if users_index_url in request.path %} class="active" {% endif %}><a href="{{ users_index_url }}">Users</a></li>
<li{% if users_index_url in request.path %} class="active"{% endif %}><a href="{{ users_index_url }}">Users</a></li>
{% url "metrics-index" as metrics_index_url %}
<li{% if metrics_index_url in request.path %} class="active"{% endif %}><a href="{{ metrics_index_url }}">Metrics</a></li>
{% comment %}
{% url "api-calls-grouped" as api_calls_grouped_url %}
<li{% ifequal request.path api_calls_grouped_url %} class="active" {% endifequal %}><a href="{{ api_calls_grouped_url }}">API Calls Grouped</a></li>
Expand Down
Empty file added apimanager/metrics/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions apimanager/metrics/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
"""
App config for metrics app
"""

from django.apps import AppConfig



class MetricsConfig(AppConfig):
"""Config for metrics"""
name = 'metrics'
15 changes: 15 additions & 0 deletions apimanager/metrics/static/metrics/css/metrics.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#metrics form {
margin-bottom: 10px;
}

#metrics #metrics-list {
margin-top: 20px;
}

#metrics #metrics-list ul {
margin-left: -25px;
}

#metrics .noborder {
border: none;
}
2 changes: 2 additions & 0 deletions apimanager/metrics/static/metrics/js/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$(document).ready(function($) {
});
166 changes: 166 additions & 0 deletions apimanager/metrics/templates/metrics/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{% extends 'base.html' %}
{% load static %}

{% block page_title %}{{ block.super }} / Metrics{% endblock page_title %}

{% block content %}
<div id="metrics">
<h1>Metrics</h1>

<h2>Filter</h2>
<form action="" method="GET">
<div class="row">
<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="from_start_date">From Start Date</label>
<input type="date" class="form-control" id="from_start_date" name="from_start_date" value="{{ request.GET.from_start_date }}" />
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="to_start_date">To Start Date</label>
<input type="date" class="form-control" id="to_start_date" name="to_start_date" value="{{ request.GET.to_start_date }}" />
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="limit">Limit</label>
<input type="number" class="form-control" id="limit" name="limit" min="1" value="{{ request.GET.limit|default:200 }}" />
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="offset">Offset</label>
<input type="number" class="form-control" id="offset" name="offset" min="0" value="{{ request.GET.offset|default:0 }}" />
</div>
</div>
</div>

<div class="row">
<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="consumer_id">Consumer ID</label>
<input type="text" class="form-control" id="consumer_id" name="consumer_id" value="{{ request.GET.consumer_id }}" />
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="user_id">User ID</label>
<input type="text" class="form-control" id="user_id" name="user_id" value="{{ request.GET.user_id }}" />
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="anon">Anonymous</label>
{% with request.GET.anon as anon %}
<select id="anon" name="anon" class="form-control">
<option value="true"{% if anon == 'true' %} selected="selected"{% endif %}>Yes</option>
<option value="false"{% if anon == 'false' %} selected="selected"{% endif %}>No</option>
<option value=""{% if not anon %} selected="selected"{% endif %}>Anonymous and Non-Anonymous</option>
</select>
{% endwith %}
</div>
</div>

<div class="col-xs-6 col-sm-3">
<div class="form-group">
<label for="app_name">App Name</label>
<input type="text" class="form-control" id="app_name" name="app_name" value="{{ request.GET.app_name }}" />
</div>
</div>

</div>

<div class="row">
<div class="col-xs-2">
<div class="form-group">
<label for="verb">Verb</label>
<input type="text" class="form-control" id="verb" name="verb" value="{{ request.GET.verb }}" />
</div>
</div>

<div class="col-xs-10">
<div class="form-group">
<label for="url">URL</label>
<input type="text" class="form-control" id="url" name="url" value="{{ request.GET.url }}" />
</div>
</div>
</div>

<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="implemented_by_partial_function">Implemented By Partial Function</label>
<input type="text" class="form-control" id="implemented_by_partial_function" name="implemented_by_partial_function" value="{{ request.GET.implemented_by_partial_function }}" />
</div>
</div>

<div class="col-xs-6">
<div class="form-group">
<label for="implemented_in_version">Implemented In Version</label>
<input type="text" class="form-control" id="implemented_in_version" name="implemented_in_version" value="{{ request.GET.implemented_in_version }}" />
</div>
</div>
</div>


<button type="submit" class="btn btn-primary">Update</button>
</form>

<div class="table-responsive">
<table class="table table-hover" id="metrics-list">
<thead>
<tr>
<th>#</th>
<th>Verb</th>
<th>URL</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{% for metric in metrics %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ metric.verb }}</td>
<td>
{{ metric.url }}
</td>
<td>{{ metric.date|date:"Y-m-d H:m:s" }}</td>
</tr>
<tr>
<td colspan="5" class="noborder">
<ul>
<li>User Name: {{ metric.user_name }}</li>
<li>User ID: {{ metric.user_id }}</li>
<li>Developer Email: {{ metric.developer_email }}</li>
<li>App Name: {{ metric.app_name }}</li>
<li>Consumer ID: {{ metric.consumer_id }}</li>
<li>Implemented by Partial Function: {{ metric.implemented_by_partial_function }}</li>
<li>Implemented In Version: {{ metric.implemented_in_version }}</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

{% block extrajs %}
{% comment %}
<script type="text/javascript" src="{% static 'metrics/js/metrics.js' %}"></script>
<script type="text/javascript">
</script>
{% endcomment %}
{% endblock extrajs %}


{% block extracss %}
<link href="{% static 'metrics/css/metrics.css' %}" rel="stylesheet">
{% endblock extracss %}
14 changes: 14 additions & 0 deletions apimanager/metrics/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
"""
URLs for metrics app
"""

from django.conf.urls import url

from .views import IndexView

urlpatterns = [
url(r'^$',
IndexView.as_view(),
name='metrics-index'),
]
56 changes: 56 additions & 0 deletions apimanager/metrics/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""
Views of metrics app
"""

from datetime import datetime

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView

from base.api import api, APIError



class IndexView(LoginRequiredMixin, TemplateView):
"""Index view for metrics"""
template_name = "metrics/index.html"

def scrub(self, metrics):
"""Scrubs data in the given consumers to adher to certain formats"""
for metric in metrics:
metric['date'] = datetime.strptime(
metric['date'], settings.API_DATETIMEFORMAT)
return metrics


def get_params(self, request_get):
"""
API treats empty parameters as actual values, so we have to filter
them out
"""
querydict = request_get.copy()
keys = list(querydict.keys())
for key in keys:
if not querydict[key]:
querydict.pop(key)
return querydict.urlencode()


def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
metrics = []
params = self.get_params(self.request.GET)
try:
urlpath = '/management/metrics?{}'.format(params)
metrics = api.get(self.request, urlpath)
metrics = self.scrub(metrics['metrics'])
except APIError as err:
messages.error(self.request, err)

context.update({
'metrics': metrics,
})
return context

0 comments on commit 5e7d60c

Please sign in to comment.