Skip to content

Collecting JSON reports

Jiří Kadlec edited this page Oct 25, 2024 · 3 revisions

The following example demonstrates how to collect all JSON reports for a specific product to a local folder using the API of a local QC tool installation:

  1. Get the list of individual tile delivery items that have been checked via the delivery-list API endpoint
  2. For each item in the list, fetch the JSON report using the job-result/{job_uuid} API endpoint (where job_uuid is the last_job_uuid property of the item)

This can be automated using the following python script (be sure to edit the QC_TOOL_API_URL, QC_TOOL_API_KEY, REPORTS_FOLDER and PRODUCT_DESCRIPTION variables) :

import json
import os
import requests

QC_TOOL_API_URL = "http://localhost:8000/api" # Set this your local QC tool API URL
QC_TOOL_API_KEY = "YOURAPIKEY" # Change this to your local QC tool API key
REPORTS_FOLDER = "YOUR/DIRECTORY" # Change this to the target folder where you want to save the reports to
PRODUCT_DESCRIPTION = "HRL Dominant Leaf Type 2020 (10m)"
STATUSES = ["ok", "partial"] # Change this to ["ok"] if you want to only collect reports with ok status


def fetch_deliveries(api_url, api_key):
    # Fetches all deliveries (tile items) from QC tool API
    all_items = []
    offset = 0
    delivery_list_url = os.path.join(api_url, "delivery-list")
    params = {'apikey': api_key}

    while True:
        # Set the pagination offset
        params['offset'] = offset

        # Make the API request
        response = requests.get(delivery_list_url, params=params)

        # Check if the request was successful
        if response.status_code != 200:
            print(f"Error: {response.status_code} {response.content}")
            break

        # Parse the JSON response
        data = response.json()

        # 'deliveries' is the key in the JSON that contains the list of items
        items = data.get('deliveries', [])
        all_items.extend(items)

        # Check if there's a next page
        if not data.get('next_offset', 0):
            break

        # To move to next page
        offset = data['next_offset']

    return all_items


def fetch_json_report(api_url, api_key, job_uuid):
    # Fetches a json report from QC tool API
    job_result_url = os.path.join(api_url, "job-result", job_uuid)
    params = {'apikey': api_key}
    response = requests.get(job_result_url, params=params)
    # Check if the request was successful
    if response.status_code != 200:
        print(f"Error fetching report: {response.status_code} {response.content}")
        return
    data = response.json()
    report = data['data']
    return report


def main():
    all_items = fetch_deliveries(QC_TOOL_API_URL, QC_TOOL_API_KEY)
    # filter the items by product and status
    for item in all_items:
        if item.get("product_description") == PRODUCT_DESCRIPTION and item.get("last_job_status") in STATUSES:
            print(f"Fetching report for {item['filename']}, status: {item['last_job_status']}, job: {item['last_job_uuid']}")
            report = fetch_json_report(QC_TOOL_API_URL, QC_TOOL_API_KEY, item['last_job_uuid'])
            if not report:
                continue
            # save report to a .json file in the REPORTS_FOLDER
            out_report_file = os.path.join(REPORTS_FOLDER, item["filename"] + ".json")
            with open(out_report_file, "w") as fp:
                json.dump(report, fp)


if __name__ == "__main__":
    main()

Clone this wiki locally