Syntax for filters on reports API

Hi hi! I’m trying to run a filter for specific report types based on the form used to create them. I’m using the reports API and can get the basic call to export everything working no problem:

curl "https://medic:PASSWORD-HERE@SERVERHERE.org/api/v2/export/reports" > output.csv

However, when I go to filter this to only show reports named covid19_rdt_capture using the example on the docs page, it doesn’t work. The call I’m using is:

curl "https://medic:PASSWORD-HERE@SERVERHERE.org/api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=covid19_rdt_capture" > output.csv

Instead of CSV output in output.csv, I get JSON:

{"error":"not_found","reason":"Database does not exist."}

Calling the /api/v1/forms to validate the form name is correct endpoint gives me this JSON:

["contact:clinic:create.xml","contact:clinic:edit.xml","contact:district_hospital:create.xml","contact:district_hospital:edit.xml","contact:health_center:create.xml","contact:health_center:edit.xml","contact:person:create.xml","contact:person:edit.xml","covid19_rdt_capture.xml","covid19_rdt_provision.xml"]

I’m not familiar enough with the underlying search API to know why I’m hitting the wrong database, presumably the wrong CouchDB database? I can find the record I’m after in fauxton here:

/_utils/#database/medic/ID_OF_RECORD_HERE

This all based on the COVID-19 reference app we’re developing.

Your request looks correct. I tried it against my local install and against a test instance and got expected results:

curl -g "http://admin:pass@127.0.0.1:6500/api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=covid19_rdt_capture"
_id,form,patient_id,reported_date,from,contact.name,contact.parent.name,contact.parent.parent.name,contact.parent.parent.parent.name
curl -g "https://<redacted>@gamma.dev.medicmobile.org/api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=undo_death_report" 
_id,form,patient_id,reported_date,from,contact.name,contact.parent.name,contact.parent.parent.name,contact.parent.parent.parent.name,data.__confirm_undo,data.meta.__household_uuid,data.meta.__patient_id,data.meta.__patient_uuid,data.meta.__source,data.meta.__source_id,inputs.contact._id,inputs.contact.date_of_birth,inputs.contact.name,inputs.contact.parent.parent.contact.name,inputs.contact.parent.parent.contact.phone,inputs.contact.patient_id,inputs.contact.sex,inputs.contact.short_name,inputs.meta.deprecatedID,inputs.meta.location.error,inputs.meta.location.lat,inputs.meta.location.long,inputs.meta.location.message,inputs.source,inputs.source_id,meta.instanceID,patient_age_in_days,patient_age_in_months,patient_age_in_years,patient_display_name,patient_id,patient_name,patient_short_name,patient_uuid,undo.undo_information
"64bf7c75-e203-456b-8059-6196e5ffd160","undo_death_report",,<redacted>,"<redacted>","patient","hello",,,"yes","","07651","aef84016-a06d-48b7-8701-dc3334ce4ddd","user","","aef84016-a06d-48b7-8701-dc3334ce4ddd","2021-09-01","Steve Kshlerin I","","","07651","female","","","","","","","user","","uuid:7ce296fb-dd0a-4d82-bf61-5b3e393546a5","13","0","0","Steve Kshlerin I","07651","Steve Kshlerin I","","aef84016-a06d-48b7-8701-dc3334ce4ddd","yes"

Goodness me. I was baffled because indeed both your and my URIs worked. Checking my bash history more closely, I see all my earlier calls had an extra / in there every time, but I corrected that when I was crafting my post here. So this is wrong:

curl  "https://medic:PASSWORD-HERE@SERVERHERE.org//api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=covid19_rdt_capture"

And will result in the Database does not exist. error :sweat_smile:

Thanks for pointing me in the right direction @diana !

@diana I have tried the above suggestion and failed.
I would like to only export reports filter that by form and period: report_date.

Hi @Job_Isabai

Can you please share the URL you are calling which fails?
thanks!

Here is my URL.
I would like to filter by report date to:
https://xxxxxx.xx.org/api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=ped&filters[forms][selected][1][code]=ped_label_form_pause&filters[forms][selected][2][code]=yi_label_form_pause&filters[forms][selected][3][code]=yi

Hi @Job_Isabai

I tried a similar URL and it worked:

curl -g "http://admin:pass@localhost:5988/api/v2/export/reports?filters[search]=&filters[forms][selected][0][code]=pnc_danger_sign_follow_up_mother&filters[forms][selected][1][code]=pregnancy_facility_visit_reminder&filters[forms][selected][2][code]=child_health_registration&filters[forms][selected][3][code]=delivery"

What error are you getting?

Sorry let me rephrase my question.
How can I add a report_date filter on the URL?

The documentation for the search api can be found here: https://github.com/medic/cht-core/tree/master/shared-libs/search#reports

Here is an example for the date range filter:

curl -g "http://admin:pass@localhost:5988/api/v2/export/reports?filters[search]=&filters[date][from]=0&filters[date][to]=10000000&filters[forms][selected][0][code]=pnc_danger_sign_follow_up_mother"
1 Like