Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
🎉 feat(values): Get values from reference repository (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot authored Mar 27, 2023
1 parent 1e4bdde commit cef267f
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v1.15
uses: JRubics/poetry-publish@v1.16
with:
pypi_token: ${{ secrets.PYPI_TOKEN }}
python_version: "3.10"
33 changes: 33 additions & 0 deletions .github/workflows/update-reference-values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Update reference values
on:
schedule:
- cron: "0 0 * * *"

jobs:
generate-openapi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install poetry
poetry install
- name: "♻️ Get last values from reference repository"
run: |
poetry run update-values
poetry run black ./ecoindex/data
- name: Update PR with new openapi version
uses: gr2m/create-or-update-pull-request-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
title: "⬆️ chore(values): Update reference values"
branch: "chore/ecoindex-reference-values"
path: "ecoindex/data/"
commit-message: "⬆️ chore(values): Update reference values"
auto-merge: squash
body: "Update reference values from repository [cnumr/ecoindex_reference](https://www.github.com/cnumr/ecoindex_reference)"
4 changes: 4 additions & 0 deletions ecoindex/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .data.grades import A, B, C, D, E, F, G
from .data.medians import median_dom, median_req, median_size
from .data.targets import target_dom, target_req, target_size
from .ecoindex import get_ecoindex
from .utils import update_values
7 changes: 7 additions & 0 deletions ecoindex/data/grades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A = 80
B = 70
C = 55
D = 40
E = 25
F = 10
G = 0
3 changes: 3 additions & 0 deletions ecoindex/data/medians.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
median_dom = 693
median_req = 78
median_size = 2420
File renamed without changes.
3 changes: 3 additions & 0 deletions ecoindex/data/targets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target_dom = 600
target_req = 40
target_size = 1024
25 changes: 8 additions & 17 deletions ecoindex/ecoindex.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import List, Union
from typing import List

from ecoindex.data.grades import A, B, C, D, E, F, G
from ecoindex.data.quantiles import quantiles_dom, quantiles_req, quantiles_size
from ecoindex.models import Ecoindex
from ecoindex.quantiles import quantiles_dom, quantiles_req, quantiles_size


async def get_quantile(
quantiles: List[Union[int, float]], value: Union[int, float]
) -> float:
async def get_quantile(quantiles: List[int | float], value: int | float) -> float:
for i in range(1, len(quantiles)):
if value < quantiles[i]:
return (
Expand Down Expand Up @@ -36,18 +35,10 @@ async def get_ecoindex(dom: int, size: float, requests: int) -> Ecoindex:


async def get_grade(ecoindex: float) -> str:
if ecoindex > 80:
return "A"
if ecoindex > 70:
return "B"
if ecoindex > 55:
return "C"
if ecoindex > 40:
return "D"
if ecoindex > 25:
return "E"
if ecoindex > 10:
return "F"
for grade in "ABCDEF":
if ecoindex > globals()[grade]:
return grade

return "G"


Expand Down
1 change: 0 additions & 1 deletion ecoindex/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pkg_resources import get_distribution
from pydantic import AnyHttpUrl, BaseModel, Field
from pydantic.networks import HttpUrl

PageType = str

Expand Down
50 changes: 50 additions & 0 deletions ecoindex/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from asyncio import run
from json import dumps
from os import getcwd

from aiofile import async_open
from requests import get


async def update_values_async() -> None:
response = get(
"https://cdn.jsdelivr.net/gh/cnumr/ecoindex_reference@1/ecoindex_reference.json",
)

data = response.json()
data_folder = f"{getcwd()}/ecoindex/data/"

async with async_open(f"{data_folder}quantiles.py", "w") as quantile_file:
quantiles = f"quantiles_dom = {dumps(data['quantiles']['dom_size'])}\n"
quantiles += f"quantiles_req = {dumps(data['quantiles']['nb_request'])}\n"
quantiles += f"quantiles_size = {dumps(data['quantiles']['response_size'])}\n"

await quantile_file.write(quantiles)

async with async_open(f"{data_folder}targets.py", "w") as target_file:
targets = f"target_dom = {dumps(data['targets']['dom_size'])}\n"
targets += f"target_req = {dumps(data['targets']['nb_request'])}\n"
targets += f"target_size = {dumps(data['targets']['response_size'])}\n"

await target_file.write(targets)

async with async_open(f"{data_folder}medians.py", "w") as median_file:
medians = f"median_dom = {dumps(data['medians']['dom_size'])}\n"
medians += f"median_req = {dumps(data['medians']['nb_request'])}\n"
medians += f"median_size = {dumps(data['medians']['response_size'])}\n"

await median_file.write(medians)

async with async_open(f"{data_folder}grades.py", "w") as grades_file:
grades = ""

for grade in data["grades"]:
grades += f"{grade['grade']} = {grade['value']}\n"

await grades_file.write(grades)

print("Values updated")


def update_values() -> None:
run(update_values_async())
Loading

0 comments on commit cef267f

Please sign in to comment.