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

fix: Offline product knowledge panel issue #2693

Merged

Conversation

AshAman999
Copy link
Member

What

  • Don't update the already available products with KP when preloading data

Fixes bug(s)

Part of

@AshAman999 AshAman999 requested a review from a team as a code owner July 31, 2022 18:28
@AshAman999 AshAman999 requested a review from VaiTon August 1, 2022 10:25
@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2022

Codecov Report

Merging #2693 (35e074e) into develop (2ea0da3) will decrease coverage by 1.74%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           develop   #2693      +/-   ##
==========================================
- Coverage     8.86%   7.12%   -1.75%     
==========================================
  Files          161     218      +57     
  Lines         6623   10645    +4022     
==========================================
+ Hits           587     758     +171     
- Misses        6036    9887    +3851     
Impacted Files Coverage Δ
...kages/smooth_app/lib/widgets/attribute_button.dart 0.00% <0.00%> (-92.00%) ⬇️
...s/smooth_app/lib/data_models/user_preferences.dart 8.73% <0.00%> (-23.57%) ⬇️
packages/smooth_app/lib/themes/smooth_theme.dart 62.26% <0.00%> (-20.72%) ⬇️
...p/lib/generic_lib/dialogs/smooth_alert_dialog.dart 15.11% <0.00%> (-19.10%) ⬇️
...mooth_app/lib/data_models/product_preferences.dart 21.68% <0.00%> (-9.75%) ⬇️
packages/smooth_app/lib/main.dart 14.65% <0.00%> (-3.24%) ⬇️
.../smooth_app/lib/pages/onboarding/welcome_page.dart 0.00% <0.00%> (-3.13%) ⬇️
.../smooth_app/lib/pages/onboarding/scan_example.dart 0.00% <0.00%> (-2.28%) ⬇️
...ackages/smooth_app/lib/pages/scan/scan_header.dart 2.50% <0.00%> (-2.27%) ⬇️
...p/lib/pages/onboarding/consent_analytics_page.dart 0.00% <0.00%> (-1.57%) ⬇️
... and 237 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@VaiTon VaiTon requested a review from monsieurtanuki August 1, 2022 11:00
Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

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

Hi @AshAman999!
Not 100% convinced: please have a look at my comments.

packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
await daoProduct.getAll(allProductCodes);
allProducts.removeWhere(
(String key, Product value) => value.knowledgePanels == null);
allProductCodes = allProducts.keys.toList();
final List<ProductField> fields = ProductQuery.fields;
fields.remove(ProductField.KNOWLEDGE_PANELS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually that's not OK: you're also removing an item from the main fields.
You should copy all the fields you need instead, into your own list.

Copy link
Member Author

Choose a reason for hiding this comment

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

Had done a for each iteration but as a left suggestion I changed it

Might be something like this would work fine

  final List<String> allProductCodes = await daoProduct.getAllKeys();
    final Map<String, Product> allProducts =
        await daoProduct.getAll(allProductCodes);
    allProducts.forEach((String code, Product product) {
      if (product.knowledgePanels != null) {
        allProductCodes.remove(code);
      }
    });

Copy link
Contributor

Choose a reason for hiding this comment

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

You haven't implemented a fix, have you?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I did the changes that you asked about not loading all the products

Comment on lines 11 to 12
final Map<String, Product> allProducts =
await daoProduct.getAll(allProductCodes);
Copy link
Contributor

Choose a reason for hiding this comment

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

That means you're loading ALL the database.
I know it's only for dev mode, but that's not something I would recommend.
What about getting all the barcodes instead of barcodes AND products.
For the level of granularity we want that's good enough I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

@monsieurtanuki
Just to clarify, this is not always supposed to be in dev mode, after further fine-tuning and doing this task in background, we are probably thinking of showing a menu in onboarding screens to let preload data, but not just 100o, rather in numbers of 10k.

And why I loaded the products as well is because there is no way where I can be sure that which barcodes will have knowledge panels or not. I tried thinking of alternate solutions even using daoproductlist, would be helpful for me to get an alternate idea to do the same without loading all the products but with just the barcode strings

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess a SQL query for getting all the barcode with Products having knowledge panel could have probably helped here.
But since we already don't do much of magic with SQL, can't say what else we can do here

Copy link
Contributor

Choose a reason for hiding this comment

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

DaoProduct.getAllKeys()

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay I get all the keys now I updated the database with top 1000 products
Now, if I do again try to get the top 1000 products, just for sense of updating it,
When I do DaoProduct.getAllKeys(), I will get the list of barcodes along with the top 1000 products, so now the list of barcodes will have more than 1000, and hence I can't update it. Ryt?
I hope now you can understand the problem...

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I skipped that one:

Just to clarify, this is not always supposed to be in dev mode, after further fine-tuning and doing this task in background, we are probably thinking of showing a menu in onboarding screens to let preload data, but not just 100o, rather in numbers of 10k.

But now we're on dev mode, so let's focus on that. Background task later.
Regarding putting that in onboarding, that's a bold move considering the current popularity of the onboarding among the users.
Regarding 10K bulk load, I don't see how you can skip the "download first, then refresh locally if relevant, then the next 10K" for the whole server data.

Copy link
Member Author

Choose a reason for hiding this comment

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

@monsieurtanuki
About your query to not load all products to check if they have knowledge panels or not
What I propose is we add another column in the table with name "isHalfProduct", and we give it a default value of false
And when pushing a product to db, we can say that it is not fully downloaded by just passing an argument as true.

Would be best if you could suggest me if I should go ahead with this approach??

Copy link
Contributor

Choose a reason for hiding this comment

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

What I propose is we add another column in the table with name "isHalfProduct", and we give it a default value of false

I don't think it's a good idea to add computed columns to sqflite. In general that's a pain in the neck to add columns: not just adding them, but then initializing them, and keep the init script running forever, and then you add another column and you wind up with several scripts that run forever.
Especially in this case: there's not such thing as isHalfProduct. Unless that means "null knowledge panels" on August 5th, 2022. But what next?

My suggestion: you only have to code a single method that will solve all your problems. And luckily, that's easy to code, and to test as a background method:

  • 2 parameters, DaoProduct and Product newProduct
  • you try to get from DaoProduct a Product with barcode = newProduct.barcode!
  • if there's indeed already one product, you check its knowledgePanels field. If it's not null, you return false (that's data that you don't want to overwrite)
  • else you upsert your newProduct and return true (that's data that either did not exist locally, or that didn't include kwowledge panels so your new version is better)

Et voilà.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done that, plz re review

Copy link
Member Author

Choose a reason for hiding this comment

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

packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

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

Hi @AshAman999!
My main concern is about ProductQuery.fields field that you alter.
Please make ProductQuery.fields a static const.

await daoProduct.getAll(allProductCodes);
allProducts.removeWhere(
(String key, Product value) => value.knowledgePanels == null);
allProductCodes = allProducts.keys.toList();
final List<ProductField> fields = ProductQuery.fields;
fields.remove(ProductField.KNOWLEDGE_PANELS);
Copy link
Contributor

Choose a reason for hiding this comment

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

You haven't implemented a fix, have you?

packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
@AshAman999
Copy link
Member Author

Hi @AshAman999! My main concern is about ProductQuery.fields field that you alter. Please make ProductQuery.fields a static const.

@monsieurtanuki
Tbh I tried , I am not understanding why it's a problem, I hope you could suggest me what exactly to here

@monsieurtanuki
Copy link
Contributor

@monsieurtanuki Tbh I tried , I am not understanding why it's a problem, I hope you could suggest me what exactly to here

@AshAman999 I'm afraid you're right. ProductQuery.fields should probably be a static const. But it's not. Therefore your code is likely to work.

@AshAman999
Copy link
Member Author

@monsieurtanuki Tbh I tried , I am not understanding why it's a problem, I hope you could suggest me what exactly to here

@AshAman999 I'm afraid you're right. ProductQuery.fields should probably be a static const. But it's not. Therefore your code is likely to work.

I did most of the changes you suggested me here, If we could merge it,
I will probably add the translations later on and tweak this method as well

Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

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

Looks good to me @AshAman999

packages/smooth_app/lib/query/products_preload_helper.dart Outdated Show resolved Hide resolved
@teolemon
Copy link
Member

teolemon commented Aug 11, 2022

can I merge @AshAman999 @monsieurtanuki ?

@AshAman999
Copy link
Member Author

can I merge @AshAman999 @monsieurtanuki ?

Now it's good to go !!!
We can merge it

@teolemon teolemon merged commit 4052a84 into openfoodfacts:develop Aug 11, 2022
@teolemon
Copy link
Member

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partial download of a product for offline mode could remove KP
5 participants