Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra metadata fields #103

Merged
merged 8 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from . import htmx
from .forms import HarvestSourceForm, OrganizationForm
from .paginate import Pagination
import json

logger = logging.getLogger("harvest_admin")

Expand Down Expand Up @@ -731,6 +732,17 @@ def get_harvest_record(record_id=None):

return db._to_dict(record)

@mod.route("/harvest_record/<record_id>/raw", methods=["GET"])
def get_harvest_record_raw(record_id=None):
record = db.get_harvest_record(record_id)
if record:
try:
source_raw_json = json.loads(record.source_raw)
return source_raw_json, 200
except json.JSONDecodeError:
return {"error": "Invalid JSON format in source_raw"}, 500
else:
return {"error": "Not Found"}, 404

### Add record
@mod.route("/harvest_record/add", methods=["POST", "GET"])
Expand Down
4 changes: 2 additions & 2 deletions database/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _clear_harvest_records():

ckan = RemoteCKAN(os.getenv("CKAN_API_URL"), apikey=os.getenv("CKAN_API_TOKEN"))

result = ckan.action.package_search(fq=f"owner_org:{organization_id}")
result = ckan.action.package_search(fq=f"harvest_source_id:{source_id}")
ckan_datasets = result["count"]
start = datetime.now(timezone.utc)
retry_count = 0
Expand All @@ -258,7 +258,7 @@ def _clear_harvest_records():
# Retry loop to handle timeouts from cloud.gov and CKAN's Solr backend,
# ensuring datasets are cleared despite possible interruptions.
while ckan_datasets > 0 and retry_count < retry_max:
result = ckan.action.package_search(fq=f"owner_org:{organization_id}")
result = ckan.action.package_search(fq=f"harvest_source_id:{source_id}")
ckan_datasets = result["count"]
logger.info(
f"Attempt {retry_count + 1}: "
Expand Down
3 changes: 1 addition & 2 deletions harvester/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def write_compare_to_db(self) -> dict:
"ckan_name": record.ckan_name,
}
)

self.internal_records_lookup_table = self.db_interface.add_harvest_records(
records
)
Expand Down Expand Up @@ -515,7 +514,7 @@ def update_self_in_db(self) -> bool:
def ckanify_dcatus(self) -> None:
try:
self.ckanified_metadata = ckanify_dcatus(
self.metadata, self.harvest_source.organization_id
self.metadata, self.harvest_source
FuhuXia marked this conversation as resolved.
Show resolved Hide resolved
)
except Exception as e:
self.status = "error"
Expand Down
28 changes: 23 additions & 5 deletions harvester/utils/ckan_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def munge_tag(tag: str) -> str:
return tag


def create_ckan_extras(metadata: dict) -> list[dict]:
def create_ckan_extras(metadata: dict, harvest_source) -> list[dict]:
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved
extras = [
"accessLevel",
"bureauCode",
Expand All @@ -161,7 +161,25 @@ def create_ckan_extras(metadata: dict) -> list[dict]:
"publisher",
]

output = [{"key": "resource-type", "value": "Dataset"}]
output = [
{
"key": "resource-type",
"value": "Dataset"
},
{
"key": "harvest_object_id",
"value": harvest_source.internal_records_lookup_table[
metadata["identifier"]]
},
{
"key": "harvest_source_id",
"value": harvest_source.id,
},
{
"key": "harvest_source_title",
"value": harvest_source.name,
}
]

for extra in extras:
if extra not in metadata:
Expand Down Expand Up @@ -283,14 +301,14 @@ def simple_transform(metadata: dict, owner_org: str) -> dict:
return output


def ckanify_dcatus(metadata: dict, owner_org: str) -> dict:
ckanified_metadata = simple_transform(metadata, owner_org)
def ckanify_dcatus(metadata: dict, harvest_source) -> dict:
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved
ckanified_metadata = simple_transform(metadata, harvest_source.organization_id)

ckanified_metadata["resources"] = create_ckan_resources(metadata)
ckanified_metadata["tags"] = (
create_ckan_tags(metadata["keyword"]) if "keyword" in metadata else []
)
ckanified_metadata["extras"] = create_ckan_extras(metadata)
ckanified_metadata["extras"] = create_ckan_extras(metadata, harvest_source)

return ckanified_metadata

Expand Down
15 changes: 15 additions & 0 deletions tests/integration/harvest/test_ckan_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ def test_ckanify_dcatus(
harvest_source = HarvestSource(harvest_job.id)
harvest_source.prepare_external_data()

records = [(
{
"identifier": 'cftc-dc1',
"harvest_job_id": job_data_dcatus["id"],
"harvest_source_id": job_data_dcatus["harvest_source_id"]
}
)]
interface.add_harvest_records(records)
harvest_source.get_record_changes()
harvest_source.write_compare_to_db()
record_id = harvest_source.internal_records_lookup_table['cftc-dc1']

expected_result = {
"name": "commitment-of-traders",
"owner_org": "d925f84d-955b-4cb7-812f-dcfd6681a18f",
Expand All @@ -110,6 +122,9 @@ def test_ckanify_dcatus(
],
"extras": [
{"key": "resource-type", "value": "Dataset"},
{"key": "harvest_object_id", "value": record_id},
{"key": "harvest_source_id", "value": "2f2652de-91df-4c63-8b53-bfced20b276b"},
{"key": "harvest_source_title", "value": "Test Source"},
{"key": "accessLevel", "value": "public"},
{"key": "bureauCode", "value": "339:00"},
{"key": "identifier", "value": "cftc-dc1"},
Expand Down
Loading