diff --git a/mark2cure/api/commands/get-relations-v1.sql b/mark2cure/api/commands/get-relations-v1.sql deleted file mode 100644 index 871afeed..00000000 --- a/mark2cure/api/commands/get-relations-v1.sql +++ /dev/null @@ -1,47 +0,0 @@ -SELECT ANY_VALUE(`document_document`.`id`) as `id`, - `document_document`.`document_id`, - - ANY_VALUE(`document_document`.`title`) as `title`, - - ANY_VALUE(( - SELECT COUNT(*) - FROM `relation_relation` - WHERE `relation_relation`.`document_id` = `document_document`.`id` - )) as `relation_units`, - - COUNT(`view`.`id`) as `completions`, - - IF( - SUM(`view`.`user_id` = {user_id}) - , true, false) as `user_completed` - -FROM `document_document` - -/* Link up the document group information for filtering purposes */ -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` - -/* Link up the completed relationship views for each document - A View is considered complete when all relationships have been submitted (requires 100%) */ -INNER JOIN `document_section` - ON `document_section`.`document_id` = `document_document`.`id` - -INNER JOIN `document_view` as `view` - ON (`view`.`section_id` = `document_section`.`id` - AND `view`.`task_type` = 'ri' - AND `view`.`completed` = 1) - -WHERE `group`.`enabled` = 1 - -GROUP BY `document_document`.`document_id` - -/* Filter what we want to show on the dashboard */ -HAVING `relation_units` <= 20 - AND `completions` < {completions} - AND `user_completed` = false - -ORDER BY `completions` DESC - -LIMIT 20 diff --git a/mark2cure/api/commands/get-relations-v2.sql b/mark2cure/api/commands/get-relations-v2.sql deleted file mode 100644 index 69914612..00000000 --- a/mark2cure/api/commands/get-relations-v2.sql +++ /dev/null @@ -1,40 +0,0 @@ -SELECT `relationship_document`.`id` as `id`, - `relationship_document`.`document_id`, - `relationship_document`.`title` as `title`, - - COUNT(`relation_relation`.`id`) as `relation_id`, - IF( - SUM(`document_view`.`user_id` = {user_id}) - , true, false) as `user_completed` - -FROM ( - 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` - -INNER JOIN `relation_relation` - ON `relation_relation`.`document_id` = `relationship_document`.`id` - -INNER JOIN `relation_relationannotation` - ON `relation_relationannotation`.`relation_id` = `relation_relation`.`id` - -INNER JOIN `document_annotation` - ON (`document_annotation`.`object_id` = `relation_relationannotation`.`id` - AND `document_annotation`.`content_type_id` = 56) - -INNER JOIN `document_view` - ON `document_view`.`id` = `document_annotation`.`view_id` - -GROUP BY `relationship_document`.`document_id`, `relation_relation`.`id` diff --git a/mark2cure/api/commands/get-relations.sql b/mark2cure/api/commands/get-relations.sql new file mode 100644 index 00000000..98fa8a4a --- /dev/null +++ b/mark2cure/api/commands/get-relations.sql @@ -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 diff --git a/mark2cure/api/serializers.py b/mark2cure/api/serializers.py index 9a5abbd7..be646ebd 100644 --- a/mark2cure/api/serializers.py +++ b/mark2cure/api/serializers.py @@ -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() diff --git a/mark2cure/api/views.py b/mark2cure/api/views.py index a9cedfe7..78943f51 100644 --- a/mark2cure/api/views.py +++ b/mark2cure/api/views.py @@ -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) diff --git a/mark2cure/common/templates/common/dashboard.jade b/mark2cure/common/templates/common/dashboard.jade index d03da835..02f9e3b2 100755 --- a/mark2cure/common/templates/common/dashboard.jade +++ b/mark2cure/common/templates/common/dashboard.jade @@ -188,13 +188,12 @@ block post-footer