emacs-survey/app/resources/results/ResultsController.jl

73 lines
2.8 KiB
Julia
Raw Normal View History

2021-12-17 18:09:25 +00:00
module ResultsController
2022-02-10 13:31:07 +00:00
using Genie, Genie.Renderer, Genie.Renderers.Html, HTTP, JSON3, StatsBase
2022-10-02 08:17:02 +00:00
using ..Main.UserApp.Results, ..Main.UserApp.Surveys
2022-02-10 13:31:07 +00:00
function resultsindex(survey::Union{SurveyID, Nothing}=nothing)
if isnothing(survey)
html(:results, :listsurveys, layout=:base;
surveys, questions, responseids)
else
html(:results, :survey, layout=:base;
name=surveys()[survey], id=survey,
questions, sresults=results(survey),
countmap)
end
end
function resultsfile(survey::SurveyID, format::AbstractString)
@assert survey in keys(surveys())
if format == "txt"
WebRenderable(sprint(show, results(survey)), :text) |>
Genie.Renderer.respond
elseif format in ("csv", "tsv", "json")
WebRenderable(results(survey, format=Symbol(format)),
if format == "json"; :json else :text end) |>
Genie.Renderer.respond
elseif format == "db" || format == "sqlite"
HTTP.Response(200, ["Content-Type" => "application/octet-stream"],
body = results(survey, format=:sqlite))
2022-10-18 11:56:18 +00:00
elseif format == "jld2"
HTTP.Response(200, ["Content-Type" => "application/x-hdf5"],
body = results(survey, format=:jld2))
2022-02-10 13:31:07 +00:00
else
error("format $format not recognised")
end
end
2022-05-20 16:25:17 +00:00
function resultsfile(survey::SurveyID, responseid::ResponseID, format::AbstractString)
2022-10-02 08:17:02 +00:00
thesurvey = Surveys.current_survey
@assert survey == thesurvey.id
2022-05-20 16:25:17 +00:00
response = Results.response(survey, responseid)
if format == "txt"
WebRenderable(sprint(show, thesurvey => response), :text) |>
Genie.Renderer.respond
elseif format == "org"
WebRenderable(sprint(show, MIME("text/org"), thesurvey => response), :text) |>
Genie.Renderer.respond
elseif format in ("csv", "tsv", "json")
WebRenderable(results(survey, [responseid], format=Symbol(format)),
if format == "json"; :json else :text end) |>
Genie.Renderer.respond
elseif format == "db" || format == "sqlite"
HTTP.Response(200, ["Content-Type" => "application/octet-stream"],
body = results(survey, [responseid], format=:sqlite))
2022-10-18 11:56:18 +00:00
elseif format == "jld2"
HTTP.Response(200, ["Content-Type" => "application/x-hdf5"],
body = results(survey, [responseid], format=:jld2))
2022-05-20 16:25:17 +00:00
else
error("format $format not recognised")
end
end
2022-02-10 13:31:07 +00:00
function listsurveys()
WebRenderable(sprint(JSON3.pretty,
[Dict(:id => id,
:name => name)
for (id, name) in Results.surveys()]),
:json) |> Genie.Renderer.respond
end
2021-12-17 18:09:25 +00:00
end