All files / src/lib/overview completeness.ts

100% Statements 21/21
100% Branches 24/24
100% Functions 14/14
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                                        4x       59x         59x         59x         59x           59x         59x         59x         59x         59x         4x                                 60x 60x 1x       9x   531x 138x   59x 59x                   35x              
/**
 * Art. 77 dataset description completeness (issue #271 M6).
 *
 * The national catalogue describes each dataset with the HealthDCAT-AP
 * fields; Art. 77(1) obliges the holder to describe the dataset and
 * Art. 79 the access body to publish the catalogue. This scores how much of
 * the description a catalogue entry carries, as the first cut of a
 * completeness figure: the presence of the fields, not their quality.
 */
 
export interface CatalogEntryLike {
  id?: string | null;
  title?: string | null;
  description?: string | null;
  license?: string | null;
  conformsTo?: string[] | string | null;
  publisher?: string | null;
  theme?: string | null;
  datasetType?: string | null;
  legalBasis?: string | null;
  recordCount?: number | null;
}
 
/** The fields Art. 77 expects a description to carry, in the words of the catalogue. */
const DESCRIPTION_FIELDS: {
  key: keyof CatalogEntryLike;
  label: string;
  filled: (v: unknown) => boolean;
}[] = [
  {
    key: "title",
    label: "title",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "description",
    label: "description of at least forty characters",
    filled: (v) => typeof v === "string" && v.trim().length >= 40,
  },
  {
    key: "publisher",
    label: "publisher",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "license",
    label: "licence",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "conformsTo",
    label: "standard it conforms to",
    filled: (v) =>
      Array.isArray(v) ? v.length > 0 : typeof v === "string" && v.length > 0,
  },
  {
    key: "theme",
    label: "theme",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "datasetType",
    label: "dataset type",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "legalBasis",
    label: "legal basis",
    filled: (v) => typeof v === "string" && v.trim().length > 0,
  },
  {
    key: "recordCount",
    label: "record count",
    filled: (v) => typeof v === "number" && v > 0,
  },
];
 
/** The band below which a description counts as incomplete. */
export const COMPLETENESS_BAND = {
  low: 0.8,
  high: 1,
  text: "8 of 9 fields or more",
};
 
export interface Completeness {
  /** 0 to 1 */
  score: number;
  filled: number;
  total: number;
  missing: string[];
}
 
export function descriptionCompleteness(
  entry: CatalogEntryLike | null | undefined,
): Completeness {
  const total = DESCRIPTION_FIELDS.length;
  if (!entry)
    return {
      score: 0,
      filled: 0,
      total,
      missing: DESCRIPTION_FIELDS.map((f) => f.label),
    };
  const missing = DESCRIPTION_FIELDS.filter((f) => !f.filled(entry[f.key])).map(
    (f) => f.label,
  );
  const filled = total - missing.length;
  return {
    score: Math.round((filled / total) * 100) / 100,
    filled,
    total,
    missing,
  };
}
 
/** The fact line a dataset node carries. */
export function completenessFact(c: Completeness): [string, string] {
  return [
    "description (Art. 77)",
    `${c.filled} of ${c.total} fields${
      c.missing.length ? `, missing: ${c.missing.join(", ")}` : ""
    }`,
  ];
}