Skip to content

Commit 10d952d

Browse files
committed
new endpoint for lighthouse report added
1 parent f57f05a commit 10d952d

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

functions/lighthouse/libs/__init__.py

Whitespace-only changes.

functions/lighthouse/libs/queries.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import json
3+
from google.cloud import firestore
4+
from .result import Result
5+
6+
DB = firestore.Client(project=os.environ.get('PROJECT'))
7+
8+
def list_data(params):
9+
ref = DB.collection(u'lighthouse')
10+
11+
query = ref
12+
print("params", params)
13+
if 'start' in params:
14+
query = query.where('date', '>=', params['start'])
15+
if 'end' in params:
16+
query = query.where('date', '<=', params['end'])
17+
if 'geo' in params:
18+
query = query.where('geo', '==', params['geo'])
19+
if 'technology' in params:
20+
params_array = json.loads(params['app'])
21+
query = query.where('technology', 'in', params_array)
22+
if 'rank' in params:
23+
query = query.where('rank', '==', params['rank'])
24+
25+
documents = query.stream()
26+
27+
data = []
28+
for doc in documents:
29+
data.append(doc.to_dict())
30+
31+
return Result(result=data)

functions/lighthouse/libs/result.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Result():
3+
def __init__(self, status=None, result=None, errors=[]):
4+
self._status = status
5+
self.result = result
6+
self.errors = errors
7+
8+
def success(self) -> bool:
9+
return not self.failure()
10+
11+
def failure(self) -> bool:
12+
return len(self.errors) > 0
13+
14+
@property
15+
def status(self):
16+
if self._status != None:
17+
return self._status
18+
19+
return "ok" if self.success else "error"
20+

functions/lighthouse/libs/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import json
2+
3+
def output(result):
4+
status = 200 if result.success() else 400
5+
payload = result.result if result.success() else convert_to_hashes(result.errors)
6+
return (json.dumps(payload), status)
7+
8+
def convert_to_hashes(arr):
9+
hashes_arr = []
10+
for inner_arr in arr:
11+
hash_dict = {inner_arr[0]: inner_arr[1]}
12+
hashes_arr.append(hash_dict)
13+
return hashes_arr
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from .result import Result
2+
3+
class Validator():
4+
def __init__(self, params):
5+
self.params = params
6+
self.errors = []
7+
self.normalizer_params = self.normalize(params)
8+
9+
def validate(self):
10+
result = Result(status="ok", result="()")
11+
12+
# if 'geo' not in self.params:
13+
# self.add_error("geo", "missing geo parameters")
14+
15+
return Result(errors=self.errors, result=self.params)
16+
17+
def add_error(self, key, error):
18+
self.errors.append([key, error])
19+
20+
def normalize(self, params):
21+
return ""

functions/lighthouse/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import functions_framework
2+
from .libs.validator import Validator
3+
from .libs.utils import output
4+
from .libs.queries import list_data
5+
6+
@functions_framework.http
7+
def dispatcher(request):
8+
args = request.args.to_dict()
9+
10+
validator = Validator(params=args)
11+
result = validator.validate()
12+
13+
if result.failure():
14+
print("error", result.errors)
15+
return output(result)
16+
17+
response = list_data(result.result)
18+
19+
return output(response)

functions/lighthouse/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
functions-framework
2+
google-cloud-firestore
3+
pytest

0 commit comments

Comments
 (0)