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: display insight/prediction for a product #815

Merged
merged 37 commits into from
Jul 23, 2022

Conversation

Jagrutiti
Copy link
Member

@Jagrutiti Jagrutiti commented Jun 26, 2022

Working on displaying insight/prediction etc to a product.

Following the steps listed in the description.
Currently: list all ProductInsight for this barcode - sort them in a predictable way : type - timestamp - id

Relates to

@teolemon
Copy link
Member

def test_popular_question_pagination(client, mocker):
    mocker.patch("robotoff.insights.question.get_product", return_value={})
    ProductInsight.delete().execute()  # remove default sample
    for i in range(0, 12):
        ProductInsightFactory(barcode=i, unique_scans_n=100 - i)
    result = client.simulate_get("/api/v1/questions/popular?count=5&page=1")
    assert result.status_code == 200
    data = result.json
    assert data["count"] == 12
    assert data["status"] == "found"
  assert [q["barcode"] for q in data["questions"]] == ["0", "1", "2", "3", "4"]

E AssertionError: assert ['11', '10', '9', '8', '7'] == ['0', '1', '2', '3', '4']
E At index 0 diff: '11' != '0'
E Full diff:
E - ['0', '1', '2', '3', '4']
E + ['11', '10', '9', '8', '7']
tests/integration/test_api.py:102: AssertionError
---------------------------- Captured stdout setup ---------

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my first review.

Comment on lines 129 to 130
query = query.order_by(ProductInsight.timestamp.desc())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add this only if we have a order_by == "most_recent" parameter, not always, because you are going to change existing code in an unpredictable manner.

This is what makes the test failed by the way (because your line removes previous ordering).

@Jagrutiti Jagrutiti marked this pull request as ready for review July 9, 2022 07:53
@Jagrutiti Jagrutiti requested a review from a team as a code owner July 9, 2022 07:53
Comment on lines 1061 to 1076
count: int = req.get_param_as_int("count", min_value=1, default=25)
keep_types: Optional[List[str]] = req.get_param_as_list(
"insight_types", required=False
)
barcode: Optional[str] = req.get_param("barcode")
value_tag: str = req.get_param("value_tag")
brands = req.get_param_as_list("brands") or None
server_domain: Optional[str] = req.get_param("server_domain")

if keep_types:
# Limit the number of types to prevent slow SQL queries
keep_types = keep_types[:10]

if brands is not None:
# Limit the number of brands to prevent slow SQL queries
brands = brands[:10]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can factor this code between InsightCollection and DisplayInsightPredictionForProducts.

The idea, is that the function can return a dict (say query_params), and then simply use:
get_predictions = functools.partial(get_predictions, **query_params)

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is to unblock you.

Comment on lines 1154 to 1156
api.add_route(
"/api/v1/insights/predictions/display", DisplayInsightPredictionForProducts()
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just "/api/v1/insights/predictions" to be coherent with the url for insights ?

@codecov
Copy link

codecov bot commented Jul 13, 2022

Codecov Report

Merging #815 (c0d3817) into master (1e63ca2) will increase coverage by 6.74%.
The diff coverage is 70.44%.

@@            Coverage Diff             @@
##           master     #815      +/-   ##
==========================================
+ Coverage   44.73%   51.47%   +6.74%     
==========================================
  Files          96       92       -4     
  Lines        6981     6840     -141     
==========================================
+ Hits         3123     3521     +398     
+ Misses       3858     3319     -539     
Impacted Files Coverage Δ
robotoff/cli/batch.py 0.00% <ø> (ø)
robotoff/cli/insights.py 0.00% <0.00%> (ø)
robotoff/insights/dataclass.py 100.00% <ø> (ø)
robotoff/prediction/ocr/brand.py 68.62% <0.00%> (ø)
robotoff/prediction/ocr/expiration_date.py 25.71% <ø> (ø)
robotoff/prediction/ocr/label.py 72.30% <0.00%> (ø)
robotoff/prediction/ocr/packager_code.py 73.07% <ø> (ø)
robotoff/prediction/ocr/product_weight.py 49.10% <0.00%> (+1.28%) ⬆️
robotoff/products.py 42.75% <ø> (+2.26%) ⬆️
robotoff/workers/listener.py 0.00% <0.00%> (ø)
... and 60 more

Help us with your feedback. Take ten seconds to tell us how you rate us.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to add more tests:

  • Right now your test is just testing status_code, it should check response content
  • also you should then add a test where you create some predictions (PredictionFactory will help you do that) and check that it returns predictions to you
  • Also you should check some filters to see they are working
  • You should also add some tests on get_predictions (there again using PredictionFactory)

Comment on lines 423 to 426
PredictionFactory(barcode="123", keep_types="category", value_tag="en:seeds")
PredictionFactory(type="category")
PredictionFactory(value_tag="en:seeds")
PredictionFactory(type="brand")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like those creations to reflect the data proposal here : #815 (comment)

More over it's better to keep created predictions in variables (prediction1, prediction2…), so that you can check for ids in you test (first request you assert the fields, but then you just assert ids).

assert actual_items2[0]["barcode"] == "123"
assert actual_items2[0]["type"] == "brand"
assert actual_items2[0]["value_tag"] == "en:seeds"
assert prediction2.id == prediction1.id + 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexgarel

I am not sure about this line though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not have to test that. First it's not related to your test (you are not supposed to be testing PredictionFactory here), and second it's not garanteed to be true.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great @Jagrutiti, we are almost there, some small changes and it will be ok.

assert actual_items2[0]["barcode"] == "123"
assert actual_items2[0]["type"] == "brand"
assert actual_items2[0]["value_tag"] == "en:seeds"
assert prediction2.id == prediction1.id + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not have to test that. First it's not related to your test (you are not supposed to be testing PredictionFactory here), and second it's not garanteed to be true.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's merge it :-)

(I will just commit my suggestion first)

@alexgarel alexgarel force-pushed the display-insight-productions-for-product branch from 4aff732 to 8791743 Compare July 22, 2022 12:26
@alexgarel alexgarel force-pushed the display-insight-productions-for-product branch from 1e80093 to c0d3817 Compare July 23, 2022 14:17
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot C 1 Security Hotspot
Code Smell A 100 Code Smells

0.0% 0.0% Coverage
0.1% 0.1% Duplication

@alexgarel alexgarel merged commit a9c9be8 into master Jul 23, 2022
@alexgarel alexgarel deleted the display-insight-productions-for-product branch July 23, 2022 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants