-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing FieldFilter for pageWeight endpoint
- Loading branch information
Showing
1 changed file
with
25 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +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 = 'page_weight' | ||
|
||
def list_data(params): | ||
ref = DB.collection(u'page_weight') | ||
|
||
query = ref | ||
print("params", params) | ||
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']) | ||
|
||
documents = query.stream() | ||
|
||
technology_array = convert_to_array(params['technology']) | ||
data = [] | ||
for doc in documents: | ||
data.append(doc.to_dict()) | ||
|
||
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 '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) |