Skip to content

Improvement: getOverviewStats avgCompletionRate variable is misleading (returns field count, not rate) #34

Description

@ChitkulLakshya

Bug: getOverviewStats does not calculate actual completion rate

File: server/utils/analyticsUtils.js:19-28

Problem

The avgCompletionRate (returned as avgFieldsPerTemplate) does not calculate an actual completion rate. It simply counts enabled fields per template:

const completionRates = templates.map(t => {
  const config = t.config || {};
  const toggles = config.toggles || {};
  const enabledFields = Object.values(toggles).filter(Boolean).length;
  if (enabledFields === 0) return 0;
  return enabledFields;  // <-- returns count, not a rate
});
const avgCompletionRate = completionRates.length > 0
  ? Math.round((completionRates.reduce((a, b) => a + b, 0) / completionRates.length))
  : 0;

This returns the average number of enabled fields per template, not a completion rate. The KPI card in the frontend labels this as "Avg Fields/Template" which matches the actual behavior, but the variable name avgCompletionRate is misleading and the original intent was likely to calculate submission completion rates.

Fix

Rename the variable to match what it actually computes, or implement actual completion rate calculation:

// Option A: Just rename (current behavior is useful as-is)
const avgFieldsPerTemplate = ...

// Option B: Calculate actual completion rate across all submissions
const totalFields = templates.reduce((sum, t) => {
  const toggles = t.config?.toggles || {};
  return sum + Object.values(toggles).filter(Boolean).length;
}, 0);
const totalFilled = /* query submissions for filled field counts */;
const avgCompletionRate = totalFields > 0 ? Math.round((totalFilled / totalFields) * 100) : 0;

Severity

Low — The frontend label matches the actual behavior. The issue is misleading naming only.

Phase

Introduced in Phase 1 (PR #26, merged).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions