From 097938aa6b42d011614d56855fae77cf83d17101 Mon Sep 17 00:00:00 2001 From: Maximilian Grundke Date: Tue, 18 Sep 2018 10:34:38 +0200 Subject: [PATCH] Implement server side query building for flowr --- app/controllers/flowr_controller.rb | 42 +++++++++++++++++++++++++++++ app/policies/submission_policy.rb | 2 +- config/routes.rb | 2 ++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/controllers/flowr_controller.rb diff --git a/app/controllers/flowr_controller.rb b/app/controllers/flowr_controller.rb new file mode 100644 index 00000000..b56baf88 --- /dev/null +++ b/app/controllers/flowr_controller.rb @@ -0,0 +1,42 @@ +class FlowrController < ApplicationController + + def insights + unless current_user + skip_authorization + respond_to do |format| + format.html { render_not_authorized } + format.json { render json: {}, status: :unauthorized } + end + else + # get the latest submission for this user that also has a test run (i.e. structured_errors if applicable) + submission = Submission.joins(:testruns) + .where(submissions: {user_id: current_user.id, user_type: current_user.class.name}) + .order('testruns.created_at DESC').first + # verify authorization for the submission, as all queried errors are generated by this submission anyway + # and structured_errors don't have a policy yet + authorize(submission) + errors = StructuredError.where(submission_id: submission.id) + + # for each error get all attributes, filter out uninteresting ones, and build a query + insights = errors.map do |error| + attributes = error.structured_error_attributes.select do |attribute| + is_interesting(attribute) and attribute.match + end + # once the programming language model becomes available, the language name can be added to the query to + # produce more relevant results + query = attributes.map{|att| att.value}.join(' ') + { submission: submission, error: error, attributes: attributes, query: query } + end + + respond_to do |format| + format.html { render json: insights, status: :ok } + format.json { render json: insights, status: :ok } + end + end + end + + def is_interesting(attribute) + attribute.error_template_attribute.key.index(/error message|error type/i) != nil + end + private :is_interesting +end diff --git a/app/policies/submission_policy.rb b/app/policies/submission_policy.rb index 861f5695..ffb0c804 100644 --- a/app/policies/submission_policy.rb +++ b/app/policies/submission_policy.rb @@ -8,7 +8,7 @@ class SubmissionPolicy < ApplicationPolicy everyone end - [:download?, :download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?].each do |action| + [:download?, :download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?, :insights?].each do |action| define_method(action) { admin? || author? } end diff --git a/config/routes.rb b/config/routes.rb index 7c1f317b..bf07d7f8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,6 +42,8 @@ Rails.application.routes.draw do get '/help', to: 'application#help' + get '/insights', to: 'flowr#insights' + get 'statistics/', to: 'statistics#show' get 'statistics/graphs', to: 'statistics#graphs' get 'statistics/graphs/user-activity', to: 'statistics#user_activity'