|
1 | 1 | import os
|
2 |
| -import json |
3 | 2 | from google.cloud import firestore
|
| 3 | +from google.cloud.firestore_v1.base_query import FieldFilter |
4 | 4 | from .result import Result
|
5 | 5 | from .utils import convert_to_array
|
6 | 6 |
|
7 | 7 | DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE'))
|
| 8 | +TABLE = 'page_weight' |
8 | 9 |
|
9 | 10 | def list_data(params):
|
10 |
| - ref = DB.collection(u'page_weight') |
11 |
| - |
12 |
| - query = ref |
13 |
| - print("params", params) |
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 |
| - if 'geo' in params: |
19 |
| - query = query.where('geo', '==', params['geo']) |
20 |
| - |
21 |
| - if 'technology' in params: |
22 |
| - params_array = convert_to_array(params['technology']) |
23 |
| - query = query.where('technology', 'in', params_array) |
24 |
| - |
25 |
| - if 'rank' in params: |
26 |
| - query = query.where('rank', '==', params['rank']) |
27 |
| - |
28 |
| - documents = query.stream() |
29 | 11 |
|
| 12 | + technology_array = convert_to_array(params['technology']) |
30 | 13 | data = []
|
31 |
| - for doc in documents: |
32 |
| - data.append(doc.to_dict()) |
| 14 | + |
| 15 | + for technology in technology_array: |
| 16 | + query = DB.collection(TABLE) |
| 17 | + |
| 18 | + if 'start' in params: |
| 19 | + query = query.where(filter=FieldFilter('date', '>=', params['start'])) |
| 20 | + |
| 21 | + if 'end' in params: |
| 22 | + query = query.where(filter=FieldFilter('date', '<=', params['end'])) |
| 23 | + |
| 24 | + if 'geo' in params: |
| 25 | + query = query.where(filter=FieldFilter('geo', '==', params['geo'])) |
| 26 | + |
| 27 | + if 'rank' in params: |
| 28 | + query = query.where(filter=FieldFilter('rank', '==', params['rank'])) |
| 29 | + |
| 30 | + query = query.where(filter=FieldFilter('technology', '==', technology)) |
| 31 | + |
| 32 | + documents = query.stream() |
| 33 | + |
| 34 | + for doc in documents: |
| 35 | + data.append(doc.to_dict()) |
33 | 36 |
|
34 | 37 | return Result(result=data)
|
0 commit comments