Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add analytics dashboard based on Flexmonster #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ env
/humanitarian_feedback/mediafiles/
.env.*
.env
.*.sw[op]
Empty file.
3 changes: 3 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DashboardFlexmonsterConfig(AppConfig):
name = 'dashboard_flexmonster'
Empty file.
3 changes: 3 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<head>
<meta charset="UTF-8">
<title>Dashboard with Flexmonster</title>
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link rel="stylesheet" href="https://cdn.flexmonster.com/demo.css">
</head>
<body>
<!-- TODO: Somehow I need to pass pivot_data url / function the survey_id parameter -->
<div id="pivot-table-container" data-url="{% url 'pivot_data' survey_id=4%}"></div>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexxxH I think this is the line I need to fix now - somehow I need to pass survey_id through and doing it here works, but is obviously hard-coded the survey ID - is there way to access this parameter at this point or some alternative approach I should use?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think I might have understood what I was missing (or at least one way to fix this): I should pass survey_id as a parameter to the overall template via this line: https://github.com/THEPortatCERN/Hackathon2020-Pier84/pull/1/files#diff-89918cd6e497bfe3ee6e33b23d2ef640R11

<div id="pivot-chart-container"></div>
<script type="text/javascript">
function processData(dataset) {
var result = []
dataset = JSON.parse(dataset);
dataset.forEach(item => result.push(item.fields));
return result;
}
$.ajax({
url: $("#pivot-table-container").attr("data-url"),
dataType: 'json',
success: function(data) {
new Flexmonster({
container: "#pivot-table-container",
componentFolder: "https://cdn.flexmonster.com/",
width: "100%",
height: 430,
toolbar: true,
report: {
dataSource: {
type: "json",
data: processData(data)
},
slice: {}
}
});
new Flexmonster({
container: "#pivot-chart-container",
componentFolder: "https://cdn.flexmonster.com/",
width: "100%",
height: 430,
//toolbar: true,
report: {
dataSource: {
type: "json",
data: processData(data)
},
slice: {},
"options": {
"viewType": "charts",
"chart": {
"type": "pie"
}
}
}
});
}
});
</script>
</body>
3 changes: 3 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf.urls import url
from django.urls import path
from . import views

urlpatterns = [path('', views.dashboard_with_pivot, name='dashboard_with_pivot'),
path('data/', views.pivot_data, name='pivot_data', ),
]
25 changes: 25 additions & 0 deletions humanitarian_feedback/dashboard_flexmonster/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.http import JsonResponse
from django.shortcuts import render
#from dashboard_flexmonster.models import Order
from django.core import serializers


from sms_auto_surveys.models import Survey


def dashboard_with_pivot(request, survey_id):
return render(request, 'dashboard_with_pivot.html', {})


def pivot_data(request, survey_id):
print("BEK", survey_id)
# DONE: BEK change this line to get data from our survey models
# dataset = Order.objects.all()

# TODO: add the survey_id as a parameter of the request
#survey_id = 4
survey = Survey.objects.get(id=survey_id)
dataset = [response for response in survey.responses]

data = serializers.serialize('json', dataset)
return JsonResponse(data, safe=False)
1 change: 1 addition & 0 deletions humanitarian_feedback/humanitarian_feedback/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
# Local Apps
'users',
'sms_auto_surveys',
'dashboard_flexmonster',
]

MIDDLEWARE = [
Expand Down
5 changes: 4 additions & 1 deletion humanitarian_feedback/sms_auto_surveys/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls import url
from django.conf.urls import include, url
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required

Expand Down Expand Up @@ -46,4 +46,7 @@
name='csv_download'),

url(r"^set-questions/", login_required(SetQuestionsView.as_view()), name="set_questions"),

# Analytics dashboard
url(r'survey/(?P<survey_id>\d+)/dashboard/', include('dashboard_flexmonster.urls')),
]