Skip to content

Commit

Permalink
Implementing FieldFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
maceto committed Jul 22, 2024
1 parent bf9dbff commit adf62f5
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions functions/cwvtech/libs/queries.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit adf62f5

Please sign in to comment.