kobotoolbox / kpi

kpi is the (frontend) server for KoboToolbox. It includes an API for users to access data and manage their forms, question library, sharing settings, create reports, and export data.
https://www.kobotoolbox.org
GNU Affero General Public License v3.0
131 stars 176 forks source link

Auto-report crashing app: `Invalid attempt to spread non-iterable instance` #2562

Open jnm opened 4 years ago

jnm commented 4 years ago

With a particular project (see details) the application crashes when attempting to view the auto-report. If the backend is not doing its job correctly (I haven't had time to check yet), then let's open a new issue about that. Still, the frontend shouldn't crash in this scenario :wink:

Support request: https://github.com/orgs/kobotoolbox/projects/23#card-29620986

Sentry: https://sentry.kbtdev.org/kobo/kpi-frontend-dev/issues/307009/

./jsapp/js/components/reportViewItem.es6 at line 221:30

data.values.forEach(function(val, i){
var item = {};
var choiceLabel = val[2] || val[0];
item.label = _this.truncateLabel(choiceLabel, 20);
item.data = val[1].percentages;
/******* Line 221 is below *******/
allPercentages = [...new Set([...allPercentages ,...val[1].percentages])];
item.backgroundColor = colors[i];
datasets.push(item);
});
maxPercentage = Math.max.apply(Math, allPercentages);
magicznyleszek commented 4 years ago

First debugging observations:

  1. for https://kf.beta.kbtdev.org/reports/aKb3643pENrrJpKuRwhjG3/?names=Departamento item in list has data.percentages with value [100.0]
  2. for https://kf.beta.kbtdev.org/reports/aKb3643pENrrJpKuRwhjG3/?names=Departamento&split_by=Clasificaci_n_al_ingreso item in list has data.values[0][1].percentage with value []
  3. FE code is looking for data.values[0][1].percentages (mind the s) and this is causing the crash as spread operator is merging array with undefined
  4. if I fix FE code to look for percentage then it doesn't crash, but still empty percentage array is causing the charts to render empty
jnm commented 4 years ago

Interesting; just looking quickly now and percentages happens when KPI aggregates some percentage (singular) data that comes from formpack: https://github.com/kobotoolbox/kpi/blob/ff4326e85a33fd1008a0525359d67718ced01501/kobo/apps/reports/report_data.py#L142-L153

Until we figure out what's really happening, it would definitely be an improvement to show blank or no graphs. That would at least let someone remove whatever report setting had broken stuff (e.g. "group by") and keep working.

magicznyleszek commented 4 years ago

@jnm here you are: https://github.com/kobotoolbox/kpi/compare/two-databases...2562-fix-auto-report-crash - should I make a PR out of it?