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

[Up for Adoption] Allow to scan barcodes offline #2069

Open
21 tasks
Tracked by #4210
teolemon opened this issue Jan 7, 2019 · 24 comments · May be fixed by #3288
Open
21 tasks
Tracked by #4210

[Up for Adoption] Allow to scan barcodes offline #2069

teolemon opened this issue Jan 7, 2019 · 24 comments · May be fixed by #3288
Labels
barcode Issues related to the barcode scanner, and anything else barcode related help wanted offline Offline mode P1 scanning
Milestone

Comments

@teolemon
Copy link
Member

teolemon commented Jan 7, 2019

What

Why

  • No network or slow in supermarkets

Datasource

I've created a special excerpt of the database, which has the required data to scan products for France. This is 11 Mb zipped, and 37Mb unzipped.

http://fr.openfoodfacts.org/data/offline/fr.openfoodfacts.org.products.small.csv.tgz
https://world.openfoodfacts.org/data/offline/en.openfoodfacts.org.products.small.csv.zip

Can we download this data and store it in the DB ?
I can create it for other countries on demand, but right now it's a test.
And I believe the current export is broken, here's the Python script that converts the daily CSV export: https://github.com/openfoodfacts/openfoodfacts-server/blob/main/scripts/generate_dump_for_offline_apps_off.py

I have fixed the CSV export for NOVA which wasn't exported correctly:
https://world.openfoodfacts.org/data/offline/en.openfoodfacts.org.products.small.csv.zip

Equivalent code on iOS

Data structure

It's basically a very minified version of the database.
code product_name quantity brands nutriscore_grade nova_group ecoscore_grade
8938506580127 Dien Bao sweet potato vermiceilli
8938506854082 Virgin Coconut Oil 1 l Vietcoco
8938506854471 Lait De Coco Sterilisé 4.0
8938506854518 Thanh vinh Organic
8938506854563 coconut cream 13.5 fl.oz vietcoco 4.0

Proposed visual assets

https://storage.googleapis.com/icons.google.com.a.appspot.com/google-assets/system_icons/quantum/file/res-export/ic_cloud_off_24px.svg

Part of

Screenshots from the existing PR and the iOS app and other apps based on OFF

image
image
image
screenshot_20161020-195217
screenshot_20161020-195226

Random

https://world.openfoodfacts.org/data/output-small.csv
Database at 1GO, new export: 87MB
https://github.com/wmentzel/dump-creator

@teolemon teolemon added the offline Offline mode label Jan 7, 2019
@prashantkh19
Copy link
Contributor

prashantkh19 commented Jan 7, 2019

We can download the file on the first run and then store it in the DB. I will try that.

prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Jan 12, 2019
@prashantkh19
Copy link
Contributor

@teolemon please review... as of now, since the data is too big (~700000 products), so it's taking too much time to store in the database.

@teolemon
Copy link
Member Author

teolemon commented Jan 12, 2019

@jinqian @huzaifaiftikhar @PrajwalM2212 any ideas to massively increase it client side ?
Can we perhaps generate and store a separate db server side ?
@prashantkh19 Did you manage to load it completely ? if so, how long did it take ?

@prashantkh19
Copy link
Contributor

No, I didn't. I did for a subset of 10,000 products and it took around 5-6 minutes.

@teolemon
Copy link
Member Author

Do you have an idea of the size of the DB for 10k products ?

@PrajwalM2212
Copy link
Member

PrajwalM2212 commented Jan 12, 2019

Your code is trying to download it during the first run. That is great but I am more worried about the time that will take.
Even at present without the database the splash screen takes a lot of time to load. In fact we must try to reduce the splash screen time as it is.

  1. I think we should allow the user to schedule a time for download if he wants offline capability just like how we can schedule software updates on the phone. This way we don't end up consuming more time or memory.
  2. Allow we should allow them to download it in packets if they should feel lot is being downloaded

@prashantkh19
Copy link
Contributor

prashantkh19 commented Jan 12, 2019

@teolemon No, I didn't check that.
@PrajwalM2212 Yeah, I was thinking the same, maybe we can divide it into two processes, downloading and saving and make an Intent service for both. I tried this already by the way, but it was taking more time through an intent service. For the second point, the size for approx. 700000 products is only 11 mb (zipped). I think it's fine.

@PrajwalM2212
Copy link
Member

PrajwalM2212 commented Jan 12, 2019

What if we don't save the data in a database. When a user scans a product, we will
Scan the csv file if the product exists and then fetch that record and display it to them.
We can improve the organization of the csv file so that we take less time to fetch the product from csv
For example we can try to create some sort of group indexing so that when a barcode starts with abc... we move that group and find the product there. This way we may not have to loop through the entire file.
@teolemon @prashantkh19

@PrajwalM2212
Copy link
Member

@prashantkh19 That is why I was saying that we download like 10000 products at every attempt rather than as whole

@prashantkh19
Copy link
Contributor

@PrajwalM2212 yeah, it can be good, we can divide the csv file based on sorted barcodes, each file having let say, 10000 products and a separate file which contains the metadata of all the csv files. That way, we will need to scan just 2 files for each product scan, and it will take hardly 5 seconds to get the results. And maybe we can try hashing each csv also.

@teolemon
Copy link
Member Author

teolemon commented Jan 12, 2019

prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Jan 12, 2019
@prashantkh19
Copy link
Contributor

Optimized Version: took around 4 minutes to read csv(~ 2 minutes) and saving to the database(~ remaining 2 minutes) apart from downloading time for ~700000 products (complete csv file data).

@prashantkh19
Copy link
Contributor

It can still be optimized more, as I am saving a list of max 15000 entities each time as of now. If I increase the list size, then time can be reduced but increasing the size of the list may result in crashes due to memory. A better Data Structure (space optimized) than ArrayList may help or if we somehow can store List<String[]> records returned by reader.readAll() directly to greenDao database.

@prashantkh19
Copy link
Contributor

@teolemon @PrajwalM2212 can you review the latest commit?

prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 2, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 2, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 2, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 2, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 13, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 13, 2019
@serpico
Copy link

serpico commented Feb 18, 2019

I've created a special excerpt of the database, which has the required data to scan products for France. This is 11 Mb zipped, and 37Mb unzipped.
http://fr.openfoodfacts.org/data/offline/fr.openfoodfacts.org.products.small.csv.tgz
Can we download this data and store it in the DB ?

@teolemon what about optionally limiting the loading/excerpt to a specific store? Through the preferences maybe. I know I do most of my grocery shopping in one store so...
Also, is there a way to test your file directly from the phone with the current app ?

@teolemon
Copy link
Member Author

@serpico That will be V2 or 3. For now, we can afford to load everything. In the future, we'll try to let people control how much detail they want (like full products) and what countries, what stores they want. Besides, right now, we don't know if a product is sold everywhere.
@prashantkh19 hasn't implemented scanning yet. All it does is load the DB right now :-)

prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 25, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 25, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 25, 2019
prashantkh19 added a commit to prashantkh19/openfoodfacts-androidapp that referenced this issue Feb 25, 2019
@teolemon teolemon added P1 and removed hacktoberfest labels May 31, 2020
@teolemon teolemon added this to the 4.0 milestone Sep 5, 2020
@teolemon teolemon added barcode Issues related to the barcode scanner, and anything else barcode related scanning labels Nov 15, 2020
@teolemon teolemon pinned this issue Jan 17, 2021
@kartikaysharma01
Copy link
Contributor

@teolemon Can you break this down in small tasks ?

@naivekook
Copy link
Contributor

naivekook commented Feb 2, 2021

Hi!
I'm a new here, excuse me if I ask fool questions and tell obvious things and of course for hoping into the train in last moment :)

@teolemon Do you consider an approach with downloading the whole sqlite DB file?
I had a project where we chose this approach and quick googling shows that converting csv to sqlite on server side is quite easy task. The main idea is to download .sqlite database from serer as is, put it on sdcard and just work with it, without parsing csv file and copying all data to local DB.

From top of my head I see offline flow V1 next:

  • we have a screen with available regions to download
  • the app downloads the database and store somewhere info about region and DB version
  • once per N day the app check on server is there a new version of DB for user
  • we have toogle button in left menu "Enable offline mode" which tuns on/off search from local DB or from server

@kartikaysharma01
Copy link
Contributor

I'd suggest we keep a Go Offline tab in the sidebar. On the landing page, we can keep a link to the tab , for better user awareness of the feature.
In the tab, we can give user a choice to which country's database he wants to download. And once the download button is clicked, we can run the process in the background with a notification.
I like the idea given by @naivekook , but I don't think parsing and copying data to local DB is a blocker issue as it is a one time process, and we can use server side sqlite database as and when it is available.

@VaiTon
Copy link
Member

VaiTon commented Feb 3, 2021

Also, we keep the size problem. I think the best approach would just to store a country into the local db with just a few attributes: name, ecoscore, nutriscore, brand and quantity maybe

@kartikaysharma01
Copy link
Contributor

kartikaysharma01 commented Feb 25, 2021

@VaiTon @teolemon I would like to implement this.

These are the subtasks :

  • The user will have a option in preference to download the data for their country.
  • Parse and store the downloaded file in background and show user a progress bar.
  • When the user scans a barcode in offline mode the data will be shown in bottom sheet but the user will unable to swipe it up.

For this we will need to store the nutriscore, nova group (or CO2) , eco score and ingredient analysis.
Have I got this right?

@kartikaysharma01
Copy link
Contributor

@teolemon teolemon unpinned this issue Apr 28, 2021
@teolemon teolemon changed the title Allow to scan offline (not open the products) Allow to scan barcodes offline Aug 7, 2021
@teolemon teolemon changed the title Allow to scan barcodes offline [UP for Adoption] Allow to scan barcodes offline Aug 7, 2021
@teolemon
Copy link
Member Author

teolemon commented Aug 7, 2021

@kartikaysharma01 It's basically a very minified version of the database.
code product_name quantity brands nutriscore_grade nova_group ecoscore_grade
8938506580127 Dien Bao sweet potato vermiceilli
8938506854082 Virgin Coconut Oil 1 l Vietcoco
8938506854471 Lait De Coco Sterilisé 4.0
8938506854518 Thanh vinh Organic
8938506854563 coconut cream 13.5 fl.oz vietcoco 4.0

@teolemon
Copy link
Member Author

teolemon commented Aug 7, 2021

And I believe the current export is broken, here's the Python script that converts the daily CSV export: https://github.com/openfoodfacts/openfoodfacts-server/blob/main/scripts/generate_dump_for_offline_apps_off.py

@teolemon teolemon changed the title [UP for Adoption] Allow to scan barcodes offline [Up for Adoption] Allow to scan barcodes offline Aug 31, 2021
This was referenced Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
barcode Issues related to the barcode scanner, and anything else barcode related help wanted offline Offline mode P1 scanning
Projects
None yet
7 participants