apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: data-quality-check
  labels:
    dagweave.io/catalog-item: data-quality-check
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.1"
    dagweave.io/catalog-category: "Data"
spec:
  entrypoint: check
  templates:
    - name: check
      inputs:
        parameters:
          # Path to a CSV dataset (with a header row) to run assertions on.
          - name: dataset-path
          # Maximum allowed fraction of empty cells per column (0.0-1.0).
          - name: max-null-fraction
            default: "1.0"
          # Minimum number of data rows the dataset must contain (0 = no minimum).
          - name: min-rows
            default: "0"
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # python:3.15-slim
        image: python@sha256:94d3426f1def7db4a106a232ac121bfd975393e5dfd40a619b955fae45fe7913
        # The script is a static program; all user values arrive via env vars
        # (read with os.environ), never interpolated into code or a shell.
        command: [python, -c]
        args:
          - |
            import csv, os, sys
            path = os.environ["DATASET_PATH"]
            max_null = float(os.environ.get("MAX_NULL_FRACTION", "1.0") or "1.0")
            min_rows = int(os.environ.get("MIN_ROWS", "0") or "0")
            with open(path, newline="") as f:
                reader = csv.reader(f)
                try:
                    header = [h.strip() for h in next(reader)]
                except StopIteration:
                    print("FAIL: dataset is empty (no header row)", file=sys.stderr)
                    sys.exit(1)
                nulls = [0] * len(header)
                total = 0
                for row in reader:
                    total += 1
                    for i in range(len(header)):
                        val = row[i].strip() if i < len(row) else ""
                        if val == "":
                            nulls[i] += 1
            failures = []
            if total < min_rows:
                failures.append("row count %d < min %d" % (total, min_rows))
            for i, name in enumerate(header):
                frac = (nulls[i] / total) if total else 0.0
                if frac > max_null:
                    failures.append("column '%s' null fraction %.3f > %.3f" % (name, frac, max_null))
            if failures:
                print("FAIL: " + "; ".join(failures), file=sys.stderr)
                sys.exit(1)
            print("OK: %d rows, %d columns; all null fractions <= %.3f" % (total, len(header), max_null))
        env:
          - name: DATASET_PATH
            value: "{{inputs.parameters.dataset-path}}"
          - name: MAX_NULL_FRACTION
            value: "{{inputs.parameters.max-null-fraction}}"
          - name: MIN_ROWS
            value: "{{inputs.parameters.min-rows}}"
