chairemobilite / evolution

Online survey platform for travel survey
MIT License
4 stars 9 forks source link

monitoring: Add a graph of completion rate by section #685

Open tahini opened 1 month ago

tahini commented 1 month ago

Such a graph exists in od_mtl, but we use an adminView to get the completion data. We could get it with a query directly, but it could be a long query for large surveys. The default implementation could be with a direct query to the responses and we can change to adminView when required.

Here's the query to get the section status:

select i.id, s.key as section_name, (s.value::json->>'_startedAt') as started_at, case when (s.value::json->>'_isCompleted')::boolean is not true then false else true end as is_completed
from sv_interviews i
left join json_each_text(responses->'_sections') s on true
where s.key != '_actions'

We can then use a group by and sum/count to get started/completed

tahini commented 1 month ago

Here's the complete query to give the completion ratio for a survey (note that for person specific sections, completion is per person, so this query will return 0 for those sections. It does not mean they are not complete:

select section_name, count(id), sum(is_completed) from (
select i.id, s.key as section_name, (s.value::json->>'_startedAt') as started_at, case when (s.value::json->>'_isCompleted')::boolean is not true then 0 else 1 end as is_completed
from sv_interviews i
left join json_each_text(responses->'_sections') s on true
where s.key != '_actions'
) group by section_name
order by count(id) desc

To implement the widget, search for getStartedAndCompletedInterviewsByDay and started-and-completed-interviews-by-day in the code to get inspiration.