Skip to content

Commit

Permalink
[relations] addresses #208
Browse files Browse the repository at this point in the history
  • Loading branch information
x0xMaximus committed Dec 14, 2016
1 parent b92a240 commit a164b6b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 108 deletions.
47 changes: 0 additions & 47 deletions mark2cure/api/commands/get-relations-v1.sql

This file was deleted.

40 changes: 0 additions & 40 deletions mark2cure/api/commands/get-relations-v2.sql

This file was deleted.

102 changes: 102 additions & 0 deletions mark2cure/api/commands/get-relations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
SELECT `document_relationships`.`document_pk`,
`document_relationships`.`pmid`,
`document_relationships`.`title`,

COUNT(DISTINCT `document_relationships`.`relation_id`) as `document_relationships`,
SUM(`document_relationships`.`community_completed`)/COUNT(DISTINCT `document_relationships`.`relation_id`) as `community_progress`,

/* (TODO) The Community and User Completed boolean
can't be based off if their relationship submissions
is equal to that in the docuemnt, but should be based
of a static number.
This supports the feature to prevent having to limit
documents b/c there are too many relationship
comparisons within it */

/* Yes it's redundant with the progress but the
boolean is nice to use */
IF(
SUM(`document_relationships`.`community_completed`) = COUNT(DISTINCT `document_relationships`.`relation_id`)
, TRUE, FALSE) as `community_completed`,

IF(
SUM(`document_relationships`.`user_completed`) = COUNT(DISTINCT `document_relationships`.`relation_id`)
, TRUE, FALSE) as `user_completed`

FROM (
SELECT `relationship_document`.`id` as `document_pk`,
`relationship_document`.`document_id` as `pmid`,
`relationship_document`.`title` as `title`,

`relation_relation`.`id` as `relation_id`,

# (TODO) What happends when there are no annotations?
# MIN(`document_annotation`.`created`) as `first_annotation`,
# MAX(`document_annotation`.`created`) as `last_annotation`,

/* If the relationship has been answered enough
times by a minimum number of unique users */
IF(
/* Ensure we count the unique number of users who
have completed the relationship */
COUNT(DISTINCT `document_view`.`user_id`) >= {completions}
, TRUE, FALSE) as `community_completed`,

/* If the provided users of interest has provided
an answer for the specific relationship */
IF(
SUM(`document_view`.`user_id` = {user_id})
, TRUE, FALSE) as `user_completed`

FROM (
/* Return back the PK, PMID and Title for all Documents
that are in active RelationGroups */
SELECT `document_document`.`id` as `id`,
`document_document`.`document_id`,
`document_document`.`title` as `title`

FROM `document_document`

INNER JOIN `relation_relationgroup_documents`
ON `relation_relationgroup_documents`.`document_id` = `document_document`.`id`

INNER JOIN `relation_relationgroup` as `group`
ON `group`.`id` = `relation_relationgroup_documents`.`relationgroup_id`

WHERE `group`.`enabled` = 1

) as `relationship_document`

/* This INNER join is somewhat redundant as enforces only
Documents with known potential relationships to show up.
However, I'm still keeping the subquery as I think there is value
to selectively fetch documents from a RelationGroup that have
it's own respective controls (eg. Enabled) */
INNER JOIN `relation_relation`
ON `relation_relation`.`document_id` = `relationship_document`.`id`

LEFT JOIN `relation_relationannotation`
ON `relation_relationannotation`.`relation_id` = `relation_relation`.`id`

LEFT JOIN `document_annotation`
ON (`document_annotation`.`object_id` = `relation_relationannotation`.`id`
AND `document_annotation`.`content_type_id` = 56)

/* We're only joining Views to get back the User ID,
We don't care about the "completed" boolean b/c
the Relation tasks are not all or none */
INNER JOIN `document_view`
ON `document_view`.`id` = `document_annotation`.`view_id`

GROUP BY `relation_id`
) as `document_relationships`

GROUP BY `document_relationships`.`document_pk`

HAVING `community_completed` = FALSE AND NOT `user_completed` = TRUE

ORDER BY `community_progress` DESC,
`document_relationships` DESC

LIMIT 25
20 changes: 6 additions & 14 deletions mark2cure/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,11 @@ class Meta:
'meta_url', 'user', 'progress')


class DocumentRelationSerializer(serializers.ModelSerializer):
class DocumentRelationSerializer(serializers.Serializer):

task_count = serializers.SerializerMethodField()
progress = serializers.SerializerMethodField('get_progress_status')

def get_task_count(self, document):
return document.get('relation_units')

def get_progress_status(self, document):
return {'required': settings.ENTITY_RECOGNITION_K,
'current': document.get('completions')}

class Meta:
model = Document
fields = ('id', 'document_id', 'title', 'task_count', 'progress')
id = serializers.IntegerField()
document_id = serializers.IntegerField()
title = serializers.CharField()
relationships = serializers.IntegerField()
progress = serializers.FloatField()

12 changes: 10 additions & 2 deletions mark2cure/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,22 @@ def relation_list(request):
Accessed through a JSON API endpoint
"""
cmd_str = ""
with open('mark2cure/api/commands/get-relations-v2.sql', 'r') as f:
with open('mark2cure/api/commands/get-relations.sql', 'r') as f:
cmd_str = f.read()
cmd_str = cmd_str.format(user_id=request.user.pk, completions=settings.ENTITY_RECOGNITION_K)

# Start the DB Connection
c = connection.cursor()
c.execute(cmd_str)
queryset = [{'id': x[0], 'document_id': x[1], 'title': x[2], 'relation_units': x[3], 'completions': x[4]} for x in c.fetchall()]

queryset = [{'id': x[0],
'document_id': x[1],
'title': x[2],
'relationships': x[3],
'progress': x[4]} for x in c.fetchall()]

# Close the connection
c.close()

serializer = DocumentRelationSerializer(queryset, many=True)
return Response(serializer.data)
Expand Down
7 changes: 3 additions & 4 deletions mark2cure/common/templates/common/dashboard.jade
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,12 @@ block post-footer
<script type="text/template" id="relation-list-item-template">
<div class="relation-item">

<p class="steps pull-right"><%= task_count %> steps</p>
<p class="steps pull-right"><%= relationships %> steps</p>
<p class="main-text"><%= title %></p>

<progress value='<%- (progress.current/progress.required)*100 %>' max='100' class='progress'>
<progress value='<%- progress*100 %>' max='100' class='progress'>
<div class='progress'>
<span style='width: <%- (progress.current/progress.required)*100 %>%;' class='progress-bar'>
<%- progress.current %> Completions
<span style='width: <%- progress*100 %>%;' class='progress-bar'>
</span>
</div>
</progress>
Expand Down
4 changes: 3 additions & 1 deletion mark2cure/task/relation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import connection

from django.views.decorators.http import require_http_methods
from django.views.generic import TemplateView
from django.contrib.auth.decorators import login_required
from django.template.response import TemplateResponse
from django.contrib.contenttypes.models import ContentType
Expand All @@ -19,7 +20,7 @@
from .serializers import RelationSerializer, RelationCereal


class RelationTask(View):
class RelationTask(TemplateView):

@login_required
# @method_decorator(user_passes_test(lambda u: u.is_superuser))
Expand Down Expand Up @@ -59,6 +60,7 @@ def post(self, request, *args, **kwargs):

return redirect('task-relation:task-complete', document_pk=document.pk)

relation_task = RelationTask.as_view()

@login_required
def relation_task_complete(request, document_pk):
Expand Down

0 comments on commit a164b6b

Please sign in to comment.