Skip to content

Commit c429f8b

Browse files
committed
new endpoint page-weigth
1 parent 750328f commit c429f8b

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,42 @@ Returns a JSON object with the following schema:
205205
]
206206
```
207207

208+
### `GET /page-weight`
209+
210+
#### Parameters
211+
212+
The following parameters can be used to filter the data:
213+
214+
- `geo` (`required`): A string representing the geographic location.
215+
- `technology` (`required`): A comma-separated string representing the technology name(s).
216+
- `rank` (`required`): An string representing the rank.
217+
- `start` (optional): A string representing the start date in the format `YYYY-MM-DD`.
218+
- `end` (optional): A string representing the end date in the format `YYYY-MM-DD`.
219+
220+
#### Response
221+
222+
```bash
223+
curl --request GET \
224+
--url 'https://{{HOST}}/v1/page-weight?geo=ALL&technology=WordPress&rank=ALL'
225+
```
226+
227+
Returns a JSON object with the following schema:
228+
229+
```json
230+
[
231+
{
232+
"client": "desktop",
233+
"date": "2023-07-01",
234+
"geo": "ALL",
235+
"median_bytes_image": "1048110",
236+
"technology": "WordPress",
237+
"median_bytes_total": "2600099",
238+
"median_bytes_js": "652651",
239+
"rank": "ALL"
240+
}
241+
...
242+
]
243+
```
208244

209245
### `GET /technologies`
210246

functions/page-weight/libs/__init__.py

Whitespace-only changes.

functions/page-weight/libs/queries.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import json
3+
from google.cloud import firestore
4+
from .result import Result
5+
from .utils import convert_to_array
6+
7+
DB = firestore.Client(project=os.environ.get('PROJECT'))
8+
9+
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+
if 'technology' in params:
21+
params_array = convert_to_array(params['technology'])
22+
query = query.where('technology', 'in', params_array)
23+
if 'rank' in params:
24+
query = query.where('rank', '==', params['rank'])
25+
26+
documents = query.stream()
27+
28+
data = []
29+
for doc in documents:
30+
data.append(doc.to_dict())
31+
32+
return Result(result=data)

functions/page-weight/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/page-weight/libs/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import json
2+
3+
def output(result, headers={}):
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, headers)
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
14+
15+
def convert_to_array(data_string):
16+
list = data_string.split(',')
17+
return list
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 parameter")
14+
15+
if 'technology' not in self.params:
16+
self.add_error("technology", "missing technology parameter")
17+
18+
if 'rank' not in self.params:
19+
self.add_error("rank", "missing rank parameter")
20+
21+
return Result(errors=self.errors, result=self.params)
22+
23+
def add_error(self, key, error):
24+
self.errors.append([key, error])
25+
26+
def normalize(self, params):
27+
return ""

functions/page-weight/main.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# For more information about CORS and CORS preflight requests, see:
9+
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
10+
11+
# Set CORS headers for the preflight request
12+
if request.method == "OPTIONS":
13+
# Allows GET requests from any origin with the Content-Type
14+
# header and caches preflight response for an 3600s
15+
headers = {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Methods": "GET",
18+
"Access-Control-Allow-Headers": "Content-Type",
19+
"Access-Control-Max-Age": "3600",
20+
}
21+
22+
return ("", 204, headers)
23+
24+
# Set CORS headers for the main request
25+
headers = {"Access-Control-Allow-Origin": "*"}
26+
args = request.args.to_dict()
27+
28+
validator = Validator(params=args)
29+
result = validator.validate()
30+
31+
if result.failure():
32+
print("error", result.errors)
33+
return output(result)
34+
35+
response = list_data(result.result)
36+
37+
return output(response, headers)
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)