Skip to content

Commit adf62f5

Browse files
committed
Implementing FieldFilter
1 parent bf9dbff commit adf62f5

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

functions/cwvtech/libs/queries.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
import os
2-
import json
32
from google.cloud import firestore
3+
from google.cloud.firestore_v1.base_query import FieldFilter
44
from .result import Result
55
from .utils import convert_to_array
66

77
DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
8+
TABLE = 'core_web_vitals'
89

910
def list_data(params):
10-
ref = DB.collection(u'core_web_vitals')
11-
12-
query = ref
13-
14-
if 'start' in params:
15-
query = query.where('date', '>=', params['start'])
16-
if 'end' in params:
17-
query = query.where('date', '<=', params['end'])
18-
19-
if 'geo' in params:
20-
query = query.where('geo', '==', params['geo'])
11+
technology_array = convert_to_array(params['technology'])
12+
data = []
13+
14+
for technology in technology_array:
15+
query = DB.collection(TABLE)
2116

22-
if 'technology' in params:
23-
params_array = convert_to_array(params['technology'])
24-
query = query.where('technology', 'in', params_array)
17+
if 'start' in params:
18+
query = query.where('date', '>=', params['start'])
19+
if 'end' in params:
20+
query = query.where('date', '<=', params['end'])
2521

26-
if 'rank' in params:
22+
query = query.where('geo', '==', params['geo'])
2723
query = query.where('rank', '==', params['rank'])
24+
query = query.where(filter=FieldFilter('technology', '==', technology))
2825

29-
documents = query.stream()
26+
documents = query.stream()
3027

31-
data = []
32-
for doc in documents:
33-
data.append(doc.to_dict())
28+
for doc in documents:
29+
data.append(doc.to_dict())
3430

3531
return Result(result=data)

0 commit comments

Comments
 (0)