From faa083af709ff9b9f3c77f36b38a01fc21b44cb2 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Mon, 22 Jul 2024 21:42:22 -0400 Subject: [PATCH] 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