Skip to content

Commit

Permalink
Resolve "Feature install FastAPI cache"
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Coliac authored and javierblancoNS committed Dec 31, 2021
1 parent fd8f120 commit fbeab09
Show file tree
Hide file tree
Showing 29 changed files with 259 additions and 333 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ data.ms
node_modules

.DS_Store
.mypy_cache
3 changes: 2 additions & 1 deletion backend/Dockerfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ RUN pip install --no-warn-script-location --prefix=/install --no-cache-dir \
passlib[bcrypt] pydantic[email] emails tenacity psycopg2-binary python-jose \
python-multipart pytest pytest-cov jinja2 geopandas xlrd slug celery redis \
sqlalchemy-utils casbin casbin_sqlalchemy_adapter XLsxWriter aiofiles openpyxl \
python-slugify fastapi-pagination[sqlalchemy] nanoid oso sqlalchemy_oso meilisearch
python-slugify fastapi-pagination[sqlalchemy] nanoid oso sqlalchemy_oso meilisearch \
fastapi-cache2
52 changes: 44 additions & 8 deletions backend/app/app/api/api_v1/endpoints/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
from app.core import set_policies, authorization, permissive_authorization, get_current_user, settings
from app.worker import import_geofile_task


from fastapi.responses import StreamingResponse
import geopandas as gpd
import imghdr
import requests

from fastapi_cache import FastAPICache


router = APIRouter()
policies = {
Expand Down Expand Up @@ -117,20 +122,51 @@ def trees_export(
return response

@router.get("/metrics", dependencies=[Depends(permissive_authorization("trees:get"))], response_model=schemas.tree.Metrics)
def get_metrics(
async def get_metrics(
organization_id: int,
fields: str,
db: Session = Depends(get_db)
) -> Any:
""" Get Trees properties metrics"""
requested_fields = fields.split(",")
ratio = crud.tree.get_properties_completion_ratio(db, organization_id, requested_fields)
aggregates = crud.tree.get_properties_aggregates(db, organization_id, requested_fields)
metrics = schemas.tree.Metrics(
ratio=ratio,
aggregates=aggregates
backend = FastAPICache.get_backend()
coder = FastAPICache.get_coder()
key = f'trees_metrics_{organization_id}_{fields}'
ret = await backend.get(key)

if ret is None:
requested_fields = fields.split(",")
ratio = crud.tree.get_properties_completion_ratio(db, organization_id, requested_fields)
aggregates = crud.tree.get_properties_aggregates(db, organization_id, requested_fields)
mostRepresented = [item for item in aggregates["canonicalName"] if item["value"] != " "][:6]

for index, item in enumerate(mostRepresented):
canonical_name = item["value"].replace(" x ", " ").replace("‹", "i")
item["value"] = canonical_name
response_eol_search = requests.get(f'https://eol.org/api/search/1.0.json?q={canonical_name}')
if response_eol_search.status_code == 200 and len(response_eol_search.json()["results"]) > 0:
id = response_eol_search.json()["results"][0]["id"]
response_eol_pages = requests.get(f'https://eol.org/api/pages/1.0/{id}.json?details=true&images_per_page=10')
if response_eol_pages.status_code == 200:
json = response_eol_pages.json()
if "dataObjects" in json["taxonConcept"] and len(json["taxonConcept"]["dataObjects"]) > 0:
mostRepresented[index]["thumbnail"] = json["taxonConcept"]["dataObjects"][0]["eolThumbnailURL"]
else:
response_wikispecies = requests.get(f'https://species.wikimedia.org/api/rest_v1/page/summary/{canonical_name.replace(" ", "_")}')
if response_wikispecies.status_code == 200 and "thumbnail" in response_wikispecies.json():
mostRepresented[index]["thumbnail"] = response_wikispecies.json()["thumbnail"]["source"]


canonicalNameTotalCount = sum(item["total"] for item in aggregates["canonicalName"])
metrics = schemas.tree.Metrics(
canonicalNameTotalCount=canonicalNameTotalCount,
mostRepresented=mostRepresented,
ratio=ratio,
aggregates=aggregates,
)
return metrics
await backend.set(key, coder.encode(metrics), 60000000)
return metrics

return coder.decode(ret)


@router.get("/{tree_id}", dependencies=[Depends(permissive_authorization("trees:get"))], response_model=schemas.tree.Tree_xy)
Expand Down
13 changes: 12 additions & 1 deletion backend/app/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
from app.core.middleware.channel_event_middleware import ChannelEventMiddleware
from app.api.api_v1.api import api_router

from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache

from starlette.requests import Request
from starlette.responses import Response



app = FastAPI(
title=settings.PROJECT_NAME,
root_path=settings.ROOT_PATH,
exception_handlers={AuthJWTException: authjwt_exception_handler},
)


# Set all CORS enabled origins
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
Expand All @@ -27,3 +34,7 @@

app.add_middleware(ChannelEventMiddleware)
app.include_router(api_router)

@app.on_event("startup")
async def startup():
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
2 changes: 2 additions & 0 deletions backend/app/app/schemas/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TreeStatus(enum.Enum):
FROZEN = "frozen"

class Metrics(BaseModel):
canonicalNameTotalCount: int
mostRepresented: List[Dict]
ratio: Dict
aggregates: Dict

Expand Down
1 change: 1 addition & 0 deletions frontend/abilities/genericOrganizationAbility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Actions =
| "update"
| "delete"
| "manage";

export type Subjects =
| "Members"
| "Teams"
Expand Down
10 changes: 8 additions & 2 deletions frontend/components/AppLayout/Base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ const AppLayoutBase: FC = ({ children }) => {
const { t } = useTranslation(["common", "components"]);
const snackbar = useRef();
const dialog = useRef<ETKDialogActions>(null);
const { organization } = useAppContext();
const ability = buildAbilityFor(organization?.current_user_role); //Just for test
const { organization, user } = useAppContext();
const ability = buildAbilityFor(
user?.is_superuser
? "admin"
: organization?.current_user_role
? organization?.current_user_role
: "guest"
);

return (
<AbilityContext.Provider value={ability}>
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/AppLayout/Subfooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const LayoutSubFooter: FC = () => {
item
xs={12}
spacing={1}
justify="center"
justifyContent="center"
direction="column"
alignItems="center"
>
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/Core/Icons/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const CalendarIcon = (props: SvgIconProps): ReactElement => {
d="M17.1570469,11.7869063 L18.8311406,11.7869063 C19.2096094,11.7869063 19.5175312,11.4789844 19.5175312,11.1005156 L19.5175312,9.42642188 C19.5175312,9.04795313 19.2096094,8.74003125 18.8311406,8.74003125 L17.1570469,8.74003125 C16.7785781,8.74003125 16.4706563,9.04795313 16.4706563,9.42642188 L16.4706563,11.1005156 C16.4706563,11.4789844 16.7785781,11.7869063 17.1570469,11.7869063 Z M17.1737813,9.44315625 L18.8144062,9.44315625 L18.8144062,11.0837813 L17.1737813,11.0837813 L17.1737813,9.44315625 Z M17.1570469,16.2400313 L18.8311406,16.2400313 C19.2096094,16.2400313 19.5175312,15.9321094 19.5175312,15.5536406 L19.5175312,13.8795469 C19.5175312,13.5010781 19.2096094,13.1931563 18.8311406,13.1931563 L17.1570469,13.1931563 C16.7785781,13.1931563 16.4706563,13.5010781 16.4706563,13.8795469 L16.4706563,15.5536406 C16.4706563,15.9321094 16.7785781,16.2400313 17.1570469,16.2400313 Z M17.1737813,13.8962813 L18.8144062,13.8962813 L18.8144062,15.5369063 L17.1737813,15.5369063 L17.1737813,13.8962813 Z M12.3797344,11.7869063 L14.0538281,11.7869063 C14.4322969,11.7869063 14.7402187,11.4789844 14.7402187,11.1005156 L14.7402187,9.42642188 C14.7402187,9.04795313 14.4322969,8.74003125 14.0538281,8.74003125 L12.3797344,8.74003125 C12.0012656,8.74003125 11.6933437,9.04795313 11.6933437,9.42642188 L11.6933437,11.1005156 C11.6933437,11.4789844 12.0012656,11.7869063 12.3797344,11.7869063 Z M12.3964688,9.44315625 L14.0370938,9.44315625 L14.0370938,11.0837813 L12.3964688,11.0837813 L12.3964688,9.44315625 Z M4.49920313,17.4375 L2.82510938,17.4375 C2.44664063,17.4375 2.13871875,17.7454219 2.13871875,18.1238906 L2.13871875,19.7979844 C2.13871875,20.1764531 2.44664063,20.484375 2.82510938,20.484375 L4.49920313,20.484375 C4.87767188,20.484375 5.18559375,20.1764531 5.18559375,19.7979844 L5.18559375,18.1238906 C5.18559375,17.7454219 4.87767188,17.4375 4.49920313,17.4375 Z M4.48246875,19.78125 L2.84184375,19.78125 L2.84184375,18.140625 L4.48246875,18.140625 L4.48246875,19.78125 Z M4.49920313,8.74003125 L2.82510938,8.74003125 C2.44664063,8.74003125 2.13871875,9.04795313 2.13871875,9.42642188 L2.13871875,11.1005156 C2.13871875,11.4789844 2.44664063,11.7869063 2.82510938,11.7869063 L4.49920313,11.7869063 C4.87767188,11.7869063 5.18559375,11.4789844 5.18559375,11.1005156 L5.18559375,9.42642188 C5.18559375,9.04790625 4.87767188,8.74003125 4.49920313,8.74003125 Z M4.48246875,11.0837813 L2.84184375,11.0837813 L2.84184375,9.44315625 L4.48246875,9.44315625 L4.48246875,11.0837813 Z M12.3797344,16.1356406 L14.0538281,16.1356406 C14.4322969,16.1356406 14.7402187,15.8277188 14.7402187,15.44925 L14.7402187,13.7751563 C14.7402187,13.3966875 14.4322969,13.0887656 14.0538281,13.0887656 L12.3797344,13.0887656 C12.0012656,13.0887656 11.6933437,13.3966875 11.6933437,13.7751563 L11.6933437,15.44925 C11.6933437,15.8277188 12.0012656,16.1356406 12.3797344,16.1356406 Z M12.3964688,13.7918906 L14.0370938,13.7918906 L14.0370938,15.4325156 L12.3964688,15.4325156 L12.3964688,13.7918906 Z M19.8667031,1.60659375 L18.3636094,1.60659375 L18.3636094,1.06907812 C18.3636094,0.479578125 17.8840312,0 17.2945312,0 L17.1330937,0 C16.5436406,0 16.0640625,0.479578125 16.0640625,1.06907812 L16.0640625,1.60659375 L5.5921875,1.60659375 L5.5921875,1.06907812 C5.5921875,0.479578125 5.11260938,0 4.52310938,0 L4.36167188,0 C3.77217188,0 3.29259375,0.479578125 3.29259375,1.06907812 L3.29259375,1.60659375 L1.78954688,1.60659375 C0.819,1.60659375 0.029390625,2.39620312 0.029390625,3.36675 L0.029390625,22.2421875 C0.029390625,23.2114688 0.817921875,24 1.78720313,24 L19.8691406,24 C20.8384219,24 21.6269531,23.2114688 21.6269531,22.2421875 L21.6269531,3.36675 C21.6269062,2.39620313 20.8372969,1.60659375 19.8667031,1.60659375 Z M16.7671875,1.06907812 C16.7671875,0.86728125 16.9313438,0.703125 17.1331406,0.703125 L17.2945781,0.703125 C17.496375,0.703125 17.6605313,0.86728125 17.6605313,1.06907812 L17.6605313,1.60659375 L16.7671875,1.60659375 L16.7671875,1.06907812 Z M3.99576563,1.06907812 C3.99576563,0.86728125 4.15992188,0.703125 4.36171875,0.703125 L4.52315625,0.703125 C4.72495313,0.703125 4.88910938,0.86728125 4.88910938,1.06907812 L4.88910938,1.60659375 L3.9958125,1.60659375 L3.9958125,1.06907812 L3.99576563,1.06907812 Z M20.9237812,22.2421875 C20.9237812,22.8237656 20.4506719,23.296875 19.8690937,23.296875 L1.78715625,23.296875 C1.20557813,23.296875 0.73246875,22.8237656 0.73246875,22.2421875 L0.73246875,22.2399844 C1.02703125,22.4619375 1.39317188,22.59375 1.78954688,22.59375 L16.0434375,22.59375 C16.5135937,22.59375 16.955625,22.4106562 17.2880625,22.0781719 L20.9237812,18.4424531 L20.9237812,22.2421875 Z M16.9293281,21.4425469 C16.9568906,21.3287812 16.9718906,21.2101406 16.9718906,21.0880313 L16.9718906,18.7412813 C16.9718906,18.2987813 17.3319375,17.9387344 17.7744844,17.9387344 L20.1211875,17.9387344 C20.2432969,17.9387344 20.3619375,17.9237344 20.4757031,17.8961719 L16.9293281,21.4425469 Z M20.9237813,6.63065625 L5.94375,6.63065625 C5.74959375,6.63065625 5.5921875,6.7880625 5.5921875,6.98221875 C5.5921875,7.176375 5.74959375,7.33378125 5.94375,7.33378125 L20.9237813,7.33378125 L20.9237813,16.4330625 C20.9237813,16.8756094 20.5637344,17.2356562 20.1211875,17.2356562 L17.7744844,17.2356562 C16.9442344,17.2356562 16.2687656,17.911125 16.2687656,18.7413281 L16.2687656,21.0880781 C16.2687656,21.530625 15.9087188,21.8906719 15.4662187,21.8906719 L1.78954688,21.8906719 C1.20670313,21.8906719 0.732515625,21.4164844 0.732515625,20.8335937 L0.732515625,7.33378125 L4.5375,7.33378125 C4.73165625,7.33378125 4.8890625,7.176375 4.8890625,6.98221875 C4.8890625,6.7880625 4.73165625,6.63065625 4.5375,6.63065625 L0.73246875,6.63065625 L0.73246875,3.36675 C0.73246875,2.78390625 1.20665625,2.30971875 1.7895,2.30971875 L3.29259375,2.30971875 L3.29259375,3.34340625 C3.29259375,3.93290625 3.77217188,4.41248438 4.36167188,4.41248438 C4.55582812,4.41248438 4.71323438,4.25507812 4.71323438,4.06092188 C4.71323438,3.86676563 4.55582812,3.70935938 4.36167188,3.70935938 C4.159875,3.70935938 3.99571875,3.54520313 3.99571875,3.34340625 L3.99571875,2.30971875 L16.0640625,2.30971875 L16.0640625,3.34340625 C16.0640625,3.93290625 16.5436406,4.41248438 17.1331406,4.41248438 C17.3272969,4.41248438 17.4847031,4.25507812 17.4847031,4.06092188 C17.4847031,3.86676563 17.3272969,3.70935938 17.1331406,3.70935938 C16.9313437,3.70935938 16.7671875,3.54520313 16.7671875,3.34340625 L16.7671875,2.30971875 L19.86675,2.30971875 C20.4495938,2.30971875 20.9237813,2.78390625 20.9237813,3.36675 L20.9237813,6.63065625 Z M4.49920312,13.0887656 L2.82510937,13.0887656 C2.44664062,13.0887656 2.13871875,13.3966875 2.13871875,13.7751563 L2.13871875,15.44925 C2.13871875,15.8277188 2.44664062,16.1356406 2.82510937,16.1356406 L4.49920312,16.1356406 C4.87767187,16.1356406 5.18559375,15.8277188 5.18559375,15.44925 L5.18559375,13.7751563 C5.18559375,13.3966406 4.87767187,13.0887656 4.49920312,13.0887656 Z M4.48246875,15.4325156 L2.84184375,15.4325156 L2.84184375,13.7918906 L4.48246875,13.7918906 L4.48246875,15.4325156 Z M7.60242187,11.7869063 L9.27651563,11.7869063 C9.65498437,11.7869063 9.96290625,11.4789844 9.96290625,11.1005156 L9.96290625,9.42642188 C9.96290625,9.04795313 9.65498437,8.74003125 9.27651563,8.74003125 L7.60242187,8.74003125 C7.22395312,8.74003125 6.91603125,9.04795313 6.91603125,9.42642188 L6.91603125,11.1005156 C6.91603125,11.4789844 7.22395312,11.7869063 7.60242187,11.7869063 Z M7.61915625,9.44315625 L9.25978125,9.44315625 L9.25978125,11.0837813 L7.61915625,11.0837813 L7.61915625,9.44315625 Z M12.3960469,19.78125 C12.3873281,19.5948281 12.2334375,19.4464219 12.0448594,19.4464219 C11.8507031,19.4464219 11.6932969,19.6038281 11.6932969,19.7979844 C11.6932969,20.1764531 12.0012187,20.484375 12.3796875,20.484375 L14.0537813,20.484375 C14.43225,20.484375 14.7401719,20.1764531 14.7401719,19.7979844 L14.7401719,18.1238906 C14.7401719,17.7454219 14.43225,17.4375 14.0537812,17.4375 L12.3796875,17.4375 C12.0012187,17.4375 11.6932969,17.7454219 11.6932969,18.1238906 L11.6932969,18.6067031 C11.6932969,18.8008594 11.8507031,18.9582656 12.0448594,18.9582656 C12.2390156,18.9582656 12.3964219,18.8008594 12.3964219,18.6067031 L12.3964219,18.140625 L14.0370469,18.140625 L14.0370469,19.78125 L12.3960469,19.78125 Z M7.60242187,16.1356406 L9.27651563,16.1356406 C9.65498437,16.1356406 9.96290625,15.8277188 9.96290625,15.44925 L9.96290625,13.7751563 C9.96290625,13.3966875 9.65498437,13.0887656 9.27651563,13.0887656 L7.60242187,13.0887656 C7.22395312,13.0887656 6.91603125,13.3966875 6.91603125,13.7751563 L6.91603125,15.44925 C6.91603125,15.8277188 7.22395312,16.1356406 7.60242187,16.1356406 Z M7.61915625,13.7918906 L9.25978125,13.7918906 L9.25978125,15.4325156 L7.61915625,15.4325156 L7.61915625,13.7918906 Z M7.60242187,20.484375 L9.27651563,20.484375 C9.65498437,20.484375 9.96290625,20.1764531 9.96290625,19.7979844 L9.96290625,18.1238906 C9.96290625,17.7454219 9.65498437,17.4375 9.27651563,17.4375 L7.60242187,17.4375 C7.22395312,17.4375 6.91603125,17.7454219 6.91603125,18.1238906 L6.91603125,19.7979844 C6.91603125,20.1764531 7.22395312,20.484375 7.60242187,20.484375 Z M7.61915625,18.140625 L9.25978125,18.140625 L9.25978125,19.78125 L7.61915625,19.78125 L7.61915625,18.140625 Z"
id="Shape"
fill="#000000"
fill-rule="nonzero"
fillRule="nonzero"
></path>
</g>
</SvgIcon>
Expand Down
Loading

0 comments on commit fbeab09

Please sign in to comment.