From adf62f55407ac10542b43d23444b59e8370db618 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 19:23:40 -0400 Subject: [PATCH 1/7] Implementing FieldFilter --- functions/cwvtech/libs/queries.py | 36 ++++++++++++++----------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/functions/cwvtech/libs/queries.py b/functions/cwvtech/libs/queries.py index ae7b012..e395b03 100644 --- a/functions/cwvtech/libs/queries.py +++ b/functions/cwvtech/libs/queries.py @@ -1,35 +1,31 @@ import os -import json from google.cloud import firestore +from google.cloud.firestore_v1.base_query import FieldFilter from .result import Result from .utils import convert_to_array DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) +TABLE = 'core_web_vitals' def list_data(params): - ref = DB.collection(u'core_web_vitals') - - query = ref - - if 'start' in params: - query = query.where('date', '>=', params['start']) - if 'end' in params: - query = query.where('date', '<=', params['end']) - - if 'geo' in params: - query = query.where('geo', '==', params['geo']) + technology_array = convert_to_array(params['technology']) + data = [] + + for technology in technology_array: + query = DB.collection(TABLE) - if 'technology' in params: - params_array = convert_to_array(params['technology']) - query = query.where('technology', 'in', params_array) + if 'start' in params: + query = query.where('date', '>=', params['start']) + if 'end' in params: + query = query.where('date', '<=', params['end']) - if 'rank' in params: + query = query.where('geo', '==', params['geo']) query = query.where('rank', '==', params['rank']) + query = query.where(filter=FieldFilter('technology', '==', technology)) - documents = query.stream() + documents = query.stream() - data = [] - for doc in documents: - data.append(doc.to_dict()) + for doc in documents: + data.append(doc.to_dict()) return Result(result=data) From 121cd7f29b9bf1ef3bea14df1e2f543cd1765f0c Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 19:28:48 -0400 Subject: [PATCH 2/7] Implementing FieldFilter for rank, geo and dates --- functions/cwvtech/libs/queries.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions/cwvtech/libs/queries.py b/functions/cwvtech/libs/queries.py index e395b03..bd795cb 100644 --- a/functions/cwvtech/libs/queries.py +++ b/functions/cwvtech/libs/queries.py @@ -15,12 +15,12 @@ def list_data(params): query = DB.collection(TABLE) if 'start' in params: - query = query.where('date', '>=', params['start']) + query = query.where(filter=FieldFilter('date', '>=', params['start'])) if 'end' in params: - query = query.where('date', '<=', params['end']) + query = query.where(filter=FieldFilter('date', '<=', params['end'])) - query = query.where('geo', '==', params['geo']) - query = query.where('rank', '==', params['rank']) + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) query = query.where(filter=FieldFilter('technology', '==', technology)) documents = query.stream() From a31443f3f928b569e69eeb4217cefcbe27d28a8e Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 19:42:47 -0400 Subject: [PATCH 3/7] implementing FieldFilter for lighthouse endpoint --- functions/lighthouse/libs/queries.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/functions/lighthouse/libs/queries.py b/functions/lighthouse/libs/queries.py index 5bd7c96..fee3cc4 100644 --- a/functions/lighthouse/libs/queries.py +++ b/functions/lighthouse/libs/queries.py @@ -1,10 +1,11 @@ import os -import json from google.cloud import firestore +from google.cloud.firestore_v1.base_query import FieldFilter from .result import Result from .utils import convert_to_array DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) +TABLE = 'lighthouse' def list_data(params): @@ -12,16 +13,16 @@ def list_data(params): data = [] for technology in technology_array: - query = DB.collection(u'lighthouse') + query = DB.collection(TABLE) if 'start' in params: - query = query.where('date', '>=', params['start']) + query = query.where(filter=FieldFilter('date', '>=', params['start'])) if 'end' in params: - query = query.where('date', '<=', params['end']) + query = query.where(filter=FieldFilter('date', '<=', params['end'])) - query = query.where('geo', '==', params['geo']) - query = query.where('rank', '==', params['rank']) - query = query.where('technology', '==', technology) + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) + query = query.where(filter=FieldFilter('technology', '==', technology)) documents = query.stream() From 7f12b2c292a9373a07618c641221cf02f88e9636 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 20:58:44 -0400 Subject: [PATCH 4/7] implementing FieldFilter for categories endpoint --- functions/categories/libs/queries.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/categories/libs/queries.py b/functions/categories/libs/queries.py index b6f94ac..83ae07b 100644 --- a/functions/categories/libs/queries.py +++ b/functions/categories/libs/queries.py @@ -1,13 +1,14 @@ import os -import json from google.cloud import firestore +from google.cloud.firestore_v1.base_query import FieldFilter from .result import Result from .utils import convert_to_array DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) +TABLE = 'categories' def list_data(params): - ref = DB.collection(u'categories') + ref = DB.collection(TABLE) query = ref @@ -27,7 +28,7 @@ def list_data(params): category_array = convert_to_array(params['category']) for category in category_array: - results = query.where("category", "==", category).stream() + results = query.where(filter=FieldFilter("category", "==", category)).stream() for doc in results: data.append(doc.to_dict()) From 6965132b138b3207b9181225ea284856e34ab298 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 21:03:15 -0400 Subject: [PATCH 5/7] executing one query many where --- functions/lighthouse/libs/queries.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/functions/lighthouse/libs/queries.py b/functions/lighthouse/libs/queries.py index fee3cc4..36c2e63 100644 --- a/functions/lighthouse/libs/queries.py +++ b/functions/lighthouse/libs/queries.py @@ -12,21 +12,22 @@ def list_data(params): technology_array = convert_to_array(params['technology']) data = [] - for technology in technology_array: - query = DB.collection(TABLE) + query = DB.collection(TABLE) - if 'start' in params: - query = query.where(filter=FieldFilter('date', '>=', params['start'])) - if 'end' in params: - query = query.where(filter=FieldFilter('date', '<=', params['end'])) + if 'start' in params: + query = query.where(filter=FieldFilter('date', '>=', params['start'])) + if 'end' in params: + query = query.where(filter=FieldFilter('date', '<=', params['end'])) - query = query.where(filter=FieldFilter('geo', '==', params['geo'])) - query = query.where(filter=FieldFilter('rank', '==', params['rank'])) + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) + + for technology in technology_array: query = query.where(filter=FieldFilter('technology', '==', technology)) - documents = query.stream() + documents = query.stream() - for doc in documents: - data.append(doc.to_dict()) + for doc in documents: + data.append(doc.to_dict()) return Result(result=data) From f82110199957a9ac6c767c6cbf35f1b5dafa2928 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 21:27:39 -0400 Subject: [PATCH 6/7] revert multiple where --- functions/lighthouse/libs/queries.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/functions/lighthouse/libs/queries.py b/functions/lighthouse/libs/queries.py index 36c2e63..f0c7c5f 100644 --- a/functions/lighthouse/libs/queries.py +++ b/functions/lighthouse/libs/queries.py @@ -12,22 +12,21 @@ def list_data(params): technology_array = convert_to_array(params['technology']) data = [] - query = DB.collection(TABLE) + for technology in technology_array: + query = DB.collection(TABLE) - if 'start' in params: - query = query.where(filter=FieldFilter('date', '>=', params['start'])) - if 'end' in params: - query = query.where(filter=FieldFilter('date', '<=', params['end'])) + if 'start' in params: + query = query.where(filter=FieldFilter('date', '>=', params['start'])) + if 'end' in params: + query = query.where(filter=FieldFilter('date', '<=', params['end'])) - query = query.where(filter=FieldFilter('geo', '==', params['geo'])) - query = query.where(filter=FieldFilter('rank', '==', params['rank'])) - - for technology in technology_array: + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) query = query.where(filter=FieldFilter('technology', '==', technology)) - documents = query.stream() + documents = query.stream() - for doc in documents: - data.append(doc.to_dict()) + for doc in documents: + data.append(doc.to_dict()) - return Result(result=data) + return Result(result=data) \ No newline at end of file From faa083af709ff9b9f3c77f36b38a01fc21b44cb2 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 21:42:22 -0400 Subject: [PATCH 7/7] implementing more than 30 technologies --- functions/adoption/libs/queries.py | 43 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/functions/adoption/libs/queries.py b/functions/adoption/libs/queries.py index 24469d2..43faffa 100644 --- a/functions/adoption/libs/queries.py +++ b/functions/adoption/libs/queries.py @@ -1,32 +1,37 @@ import os -import json from google.cloud import firestore +from google.cloud.firestore_v1.base_query import FieldFilter from .result import Result from .utils import convert_to_array DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) +TABLE = 'adoption' def list_data(params): - ref = DB.collection(u'adoption') - query = ref + technology_array = convert_to_array(params['technology']) + data = [] - if 'start' in params: - query = query.where('date', '>=', params['start']) - if 'end' in params: - query = query.where('date', '<=', params['end']) - if 'geo' in params: - query = query.where('geo', '==', params['geo']) - if 'technology' in params: - params_array = convert_to_array(params['technology']) - query = query.where('technology', 'in', params_array) - if 'rank' in params: - query = query.where('rank', '==', params['rank']) + for technology in technology_array: + query = DB.collection(TABLE) - documents = query.stream() + if 'start' in params: + query = query.where(filter=FieldFilter('date', '>=', params['start'])) - data = [] - for doc in documents: - data.append(doc.to_dict()) + if 'end' in params: + query = query.where(filter=FieldFilter('date', '<=', params['end'])) + + if 'geo' in params: + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) + + if 'rank' in params: + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) + + query = query.where(filter=FieldFilter('technology', '==', technology)) + + documents = query.stream() + + for doc in documents: + data.append(doc.to_dict()) - return Result(result=data) + return Result(result=data) \ No newline at end of file