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

feat: Adding a Knowledge Panel for data-quality #10

Merged
merged 22 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 44 additions & 1 deletion app/knowledge_panels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Union
import json
import requests

from urllib.parse import urlencode
from urllib.parse import urlencode, urljoin

from .models import HungerGameFilter

Expand Down Expand Up @@ -39,3 +41,44 @@ def hunger_game_kp(
],
},
}


def data_quality_kp(
facet,
value: Union[str, None] = None,
country: Union[str, None] = None,
):
if facet == "country":
path = f"{facet}/{value}"
description = "country"
else:
if country is not None:
path = f"country/{country}/{facet}/{value}"
description = f"{facet} based on {country}"
else:
path = f"{facet}/{value}"
description = facet
url = "https://world.openfoodfacts.org/"
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
source_url = urljoin(url, path)
description = f"data-quality issues releated to this {description}"
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
html = f"{source_url}/data-quality.json"
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
response_API = requests.get(html)
data = response_API.text
parse_json = json.loads(data)
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
numbers_of_issues = parse_json["count"]
tags = parse_json["tags"]
first_three_data = tags[0:3]

return {
"data-quality": {
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
"elements": [
{
"element_type": "text",
"total_issues": numbers_of_issues,
"text_element": first_three_data,
"source_url": html,
"description": description,
},
],
},
}
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import FastAPI

from .knowledge_panels import hunger_game_kp
from .knowledge_panels import data_quality_kp, hunger_game_kp
from .models import FacetName, HungerGameFilter

app = FastAPI()
Expand Down Expand Up @@ -30,5 +30,6 @@ def knowledge_panel(
hunger_game_filter=facet_name, value=facet_value, country=country
)
)
panels.append(data_quality_kp(facet=facet_name, value=facet_value, country=country))

return {"knowledge_panels": panels}
58 changes: 57 additions & 1 deletion tests/test_knowledge_panels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app.main import hunger_game_kp
from app.main import hunger_game_kp, data_quality_kp


def test_hunger_game_kp_with_filter_value_and_country():
Expand Down Expand Up @@ -91,3 +91,59 @@ def test_hunger_game_kp_label_with_value():
]
}
}


def test_data_quality_kp_with_country_and_facet():
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
assert data_quality_kp(
facet="packaging", value="plastic-box", country="hungary"
) == {
"data-quality": {
"elements": [
{
"element_type": "text",
"total_issues": 0,
"text_element": [],
"source_url": "https://world.openfoodfacts.org/country/hungary/packaging/plastic-box/data-quality.json",
"description": "data-quality issues releated to this packaging based on hungary",
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
}


def test_data_quality_kp_with_country_only():
assert data_quality_kp(facet="country", value="united kingdom") == {
"data-quality": {
"elements": [
{
"element_type": "text",
"total_issues": 241,
"text_element": [
{
"id": "en:ecoscore-production-system-no-label",
"known": 0,
"name": "ecoscore-production-system-no-label",
"products": 74390,
"url": "https://world.openfoodfacts.org/country/united-kingdom/data-quality/ecoscore-production-system-no-label",
},
{
"id": "en:ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
"known": 0,
"name": "ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
"products": 73371,
"url": "https://world.openfoodfacts.org/country/united-kingdom/data-quality/ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
},
{
"id": "en:ecoscore-threatened-species-ingredients-missing",
"known": 0,
"name": "ecoscore-threatened-species-ingredients-missing",
"products": 57542,
"url": "https://world.openfoodfacts.org/country/united-kingdom/data-quality/ecoscore-threatened-species-ingredients-missing",
},
],
"source_url": "https://world.openfoodfacts.org/country/united kingdom/data-quality.json",
"description": "data-quality issues releated to this country",
}
]
}
}
64 changes: 51 additions & 13 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def test_hello():
def test_knowledge_panel():
response = client.get("/knowledge_panel?facet_name=origin")
assert response.status_code == 200
response_body = response.json()
assert response_body["knowledge_panels"] == []


def test_knowledge_panel_badendpoint():
Expand All @@ -30,17 +28,18 @@ def test_knowledge_panel_badendpoint():

def test_knowledge_panel_ctegory_with_value_and_country():
assert knowledge_panel(
facet_name="category", facet_value="chocolate", country="belgium"
facet_name="packaging", facet_value="plastic-box", country="italy"
) == {
"knowledge_panels": [
{
"hunger-game": {
"data-quality": {
"elements": [
{
"element_type": "text",
"text_element": {
"html": "<p><a href='https://hunger.openfoodfacts.org/questions?country=belgium&type=category&value_tag=chocolate'>Answer robotoff questions about chocolate category</a></p>\n"
},
"total_issues": 0,
"text_element": [],
"source_url": "https://world.openfoodfacts.org/country/italy/packaging/plastic-box/data-quality.json",
"description": "data-quality issues releated to this packaging based on italy",
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Expand All @@ -50,16 +49,17 @@ def test_knowledge_panel_ctegory_with_value_and_country():


def test_knowledge_panel_ctegory_with_country():
assert knowledge_panel(facet_name="category", country="india") == {
assert knowledge_panel(facet_name="origin", country="india") == {
"knowledge_panels": [
{
"hunger-game": {
"data-quality": {
"elements": [
{
"element_type": "text",
"text_element": {
"html": "<p><a href='https://hunger.openfoodfacts.org/questions?country=india&type=category'>Answer robotoff questions about category</a></p>\n"
},
"total_issues": 0,
"text_element": [],
"source_url": "https://world.openfoodfacts.org/country/india/origin/None/data-quality.json",
"description": "data-quality issues releated to this origin based on india",
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Expand All @@ -69,4 +69,42 @@ def test_knowledge_panel_ctegory_with_country():


def test_knowledge_panel_with_allergen():
assert knowledge_panel(facet_name="allergen") == {"knowledge_panels": []}
assert knowledge_panel(facet_name="allergen") == {
"knowledge_panels": [
{
"data-quality": {
"elements": [
{
"element_type": "text",
"total_issues": 171,
"text_element": [
{
"id": "en:ingredients-percent-analysis-ok",
"known": 0,
"name": "ingredients-percent-analysis-ok",
"products": 1308,
"url": "https://world.openfoodfacts.org/allergen/none/data-quality/ingredients-percent-analysis-ok",
},
{
"id": "en:ecoscore-production-system-no-label",
"known": 0,
"name": "ecoscore-production-system-no-label",
"products": 1258,
"url": "https://world.openfoodfacts.org/allergen/none/data-quality/ecoscore-production-system-no-label",
},
{
"id": "en:ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
"known": 0,
"name": "ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
"products": 1168,
"url": "https://world.openfoodfacts.org/allergen/none/data-quality/ecoscore-origins-of-ingredients-origins-are-100-percent-unknown",
},
],
"source_url": "https://world.openfoodfacts.org/allergen/None/data-quality.json",
"description": "data-quality issues releated to this allergen",
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
}
]
}