Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions digital_land/expectations/checkpoints/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import json
import logging
import spatialite
from pathlib import Path
from jinja2 import Template
Expand Down Expand Up @@ -144,20 +145,33 @@ def run(self, prefetch_resources=False):
# TODO implement faillure on critical errors, this is also the zombie code below
# self.failed_expectation_with_error_severity = 0

resources_cache = (
fetch_active_resources_for_dataset(self.dataset)
if prefetch_resources
else None
logger = logging.getLogger(__name__)
logger.info(
f"[expectations] Starting run for dataset '{self.dataset}' with {len(self.expectations)} expectations"
)

for expectation in self.expectations:
resources_cache = None
if prefetch_resources:
resources_cache = fetch_active_resources_for_dataset(self.dataset)
logger.info("[expectations] Prefetched resources cache")

for i, expectation in enumerate(self.expectations):
if resources_cache is not None:
operation = expectation["operation"]
# only pass resources_cache to operations that explicitly accept it
if "resources_cache" in inspect.signature(operation).parameters:
expectation["parameters"]["resources_cache"] = resources_cache

passed, message, details = self.run_expectation(expectation)

# generating logging statements for each expectation per orgaisation
org = expectation.get("organisation", {})
org_name = org.get("organisation", "") if org else ""
label = f"{expectation['operation'].__name__}({org_name})"
logger.info(
f"[expectations] {i+1}/{len(self.expectations)} {label} — {'PASSED' if passed else 'FAILED'}"
)

self.log.add(
{
"organisation": (
Expand Down
3 changes: 3 additions & 0 deletions digital_land/expectations/operations/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def fetch_active_resources_for_dataset(dataset_name):
for attempt in range(max_retries):
try:
df = pd.read_csv(base_url)
logging.info(
f"[expectations] Fetched {len(df)} active resources for '{dataset_name}' from datasette"
)
cache = {}
for _, row in df.iterrows():
cache.setdefault(row["organisation_entity"], []).append(row["resource"])
Expand Down
Loading