Skip to content

Commit

Permalink
new endpoint page-weigth
Browse files Browse the repository at this point in the history
  • Loading branch information
maceto committed Nov 13, 2023
1 parent 750328f commit c429f8b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,42 @@ Returns a JSON object with the following schema:
]
```

### `GET /page-weight`

#### Parameters

The following parameters can be used to filter the data:

- `geo` (`required`): A string representing the geographic location.
- `technology` (`required`): A comma-separated string representing the technology name(s).
- `rank` (`required`): An string representing the rank.
- `start` (optional): A string representing the start date in the format `YYYY-MM-DD`.
- `end` (optional): A string representing the end date in the format `YYYY-MM-DD`.

#### Response

```bash
curl --request GET \
--url 'https://{{HOST}}/v1/page-weight?geo=ALL&technology=WordPress&rank=ALL'
```

Returns a JSON object with the following schema:

```json
[
{
"client": "desktop",
"date": "2023-07-01",
"geo": "ALL",
"median_bytes_image": "1048110",
"technology": "WordPress",
"median_bytes_total": "2600099",
"median_bytes_js": "652651",
"rank": "ALL"
}
...
]
```

### `GET /technologies`

Expand Down
Empty file.
32 changes: 32 additions & 0 deletions functions/page-weight/libs/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import json
from google.cloud import firestore
from .result import Result
from .utils import convert_to_array

DB = firestore.Client(project=os.environ.get('PROJECT'))

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()

data = []
for doc in documents:
data.append(doc.to_dict())

return Result(result=data)
20 changes: 20 additions & 0 deletions functions/page-weight/libs/result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Result():
def __init__(self, status=None, result=None, errors=[]):
self._status = status
self.result = result
self.errors = errors

def success(self) -> bool:
return not self.failure()

def failure(self) -> bool:
return len(self.errors) > 0

@property
def status(self):
if self._status != None:
return self._status

return "ok" if self.success else "error"

17 changes: 17 additions & 0 deletions functions/page-weight/libs/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json

def output(result, headers={}):
status = 200 if result.success() else 400
payload = result.result if result.success() else convert_to_hashes(result.errors)
return (json.dumps(payload), status, headers)

def convert_to_hashes(arr):
hashes_arr = []
for inner_arr in arr:
hash_dict = {inner_arr[0]: inner_arr[1]}
hashes_arr.append(hash_dict)
return hashes_arr

def convert_to_array(data_string):
list = data_string.split(',')
return list
27 changes: 27 additions & 0 deletions functions/page-weight/libs/validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from .result import Result

class Validator():
def __init__(self, params):
self.params = params
self.errors = []
self.normalizer_params = self.normalize(params)

def validate(self):
result = Result(status="ok", result="()")

if 'geo' not in self.params:
self.add_error("geo", "missing geo parameter")

if 'technology' not in self.params:
self.add_error("technology", "missing technology parameter")

if 'rank' not in self.params:
self.add_error("rank", "missing rank parameter")

return Result(errors=self.errors, result=self.params)

def add_error(self, key, error):
self.errors.append([key, error])

def normalize(self, params):
return ""
37 changes: 37 additions & 0 deletions functions/page-weight/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import functions_framework
from .libs.validator import Validator
from .libs.utils import output
from .libs.queries import list_data

@functions_framework.http
def dispatcher(request):
# For more information about CORS and CORS preflight requests, see:
# https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}

return ("", 204, headers)

# Set CORS headers for the main request
headers = {"Access-Control-Allow-Origin": "*"}
args = request.args.to_dict()

validator = Validator(params=args)
result = validator.validate()

if result.failure():
print("error", result.errors)
return output(result)

response = list_data(result.result)

return output(response, headers)
3 changes: 3 additions & 0 deletions functions/page-weight/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
functions-framework
google-cloud-firestore
pytest

0 comments on commit c429f8b

Please sign in to comment.