Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,50 @@
'module: database':
- src/app/db/*

'module: schemas':
- src/app/schemas/*

'module: models':
- src/app/models/*

'module: services':
- src/app/services/*

'route: accesses':
- src/app/api/routes/accesses.py
- src/app/api/endpoints/accesses.py
- src/app/api/crud/accesses.py

'route: alerts':
- src/app/api/routes/alerts.py
- src/app/api/endpoints/alerts.py
- src/app/api/crud/alerts.py

'route: devices':
- src/app/api/routes/devices.py
- src/app/api/endpoints/devices.py

'route: events':
- src/app/api/routes/events.py
- src/app/api/endpoints/events.py

'route: groups':
- src/app/api/routes/groups.py
- src/app/api/endpoints/groups.py
- src/app/api/crud/groups.py

'route: installations':
- src/app/api/routes/installations.py
- src/app/api/endpoints/installations.py

'route: login':
- src/app/api/routes/login.py
- src/app/api/endpoints/login.py

'route: media':
- src/app/api/routes/media.py
- src/app/api/endpoints/media.py

'route: sites':
- src/app/api/routes/sites.py
- src/app/api/endpoints/sites.py

'route: users':
- src/app/api/routes/users.py
- src/app/api/endpoints/users.py

'route: webhooks':
- src/app/api/routes/webhooks.py
- src/app/api/endpoints/webhooks.py
- src/app/api/crud/webhooks.py

'topic: build':
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

<p align="center">
<a href="https://github.com/pyronear/pyro-api/actions?query=workflow%3Abuilds">
<img alt="CI Status" src="https://img.shields.io/github/workflow/status/pyronear/pyro-api/builds?label=CI&logo=github&style=flat-square">
<img alt="CI Status" src="https://img.shields.io/github/actions/workflow/status/pyronear/pyro-api/builds.yml?branch=main&label=CI&logo=github&style=flat-square">
</a>
<a href="http://pyronear-api.herokuapp.com/redoc">
<img src="https://img.shields.io/github/workflow/status/pyronear/pyro-api/builds?label=docs&logo=read-the-docs&style=flat-square" alt="Documentation Status">
<img src="https://img.shields.io/github/actions/workflow/status/pyronear/pyro-api/builds.yml?brain=main&label=docs&logo=read-the-docs&style=flat-square" alt="Documentation Status">
</a>
<a href="https://codecov.io/gh/pyronear/pyro-api">
<img src="https://img.shields.io/codecov/c/github/pyronear/pyro-api.svg?logo=codecov&style=flat-square" alt="Test coverage percentage">
Expand Down
7 changes: 0 additions & 7 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ services:
- S3_REGION=${S3_REGION}
depends_on:
- db
proxy:
build: nginx
ports:
- 80:80
- 443:443
depends_on:
- backend
db:
image: postgres:12.1-alpine
volumes:
Expand Down
7 changes: 0 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ services:
- POSTGRES_USER=dummy_pg_user
- POSTGRES_PASSWORD=dummy_pg_pwd
- POSTGRES_DB=dummy_pg_db
proxy:
build: nginx
ports:
- 80:80
- 443:443
depends_on:
- backend

volumes:
postgres_data:
3 changes: 0 additions & 3 deletions nginx/Dockerfile

This file was deleted.

55 changes: 0 additions & 55 deletions nginx/nginx.conf

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/api/crud/accesses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from app.api import security
from app.api.crud import base
from app.api.schemas import (
from app.schemas import (
AccessCreation,
AccessRead,
Cred,
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/crud/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import app.config as cfg
from app.api import crud
from app.api.routes.events import create_event
from app.api.schemas import AlertIn, AlertOut, EventIn
from app.api.endpoints.events import create_event
from app.db import alerts
from app.schemas import AlertIn, AlertOut, EventIn


async def resolve_previous_alert(device_id: int) -> Optional[AlertOut]:
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/crud/authorizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from app.api import crud
from app.db import accesses
from app.db.models import AccessType
from app.models import AccessType


async def is_in_same_group(table: Table, entry_id: int, group_id: int) -> bool:
Expand Down
13 changes: 11 additions & 2 deletions src/app/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

import app.config as cfg
from app.api import crud
from app.api.schemas import AccessRead, DeviceOut, TokenPayload, UserRead
from app.db import accesses, devices, users
from app.db.models import AccessType
from app.db.session import SessionLocal
from app.models import AccessType
from app.schemas import AccessRead, DeviceOut, TokenPayload, UserRead

# Scope definition
oauth2_scheme = OAuth2PasswordBearer(
Expand All @@ -25,6 +26,14 @@
)


def get_db():
db = SessionLocal() # noqa: F405
try:
yield db
finally:
db.close()


async def get_current_access(security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme)) -> AccessRead:
"""Dependency to use as fastapi.security.Security with scopes.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from app.api import crud
from app.api.deps import get_current_access
from app.api.schemas.accesses import AccessRead
from app.db import accesses
from app.db.models import AccessType
from app.models import AccessType
from app.schemas.accesses import AccessRead

router = APIRouter()

Expand Down
30 changes: 13 additions & 17 deletions src/app/api/routes/alerts.py → src/app/api/endpoints/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from app.api import crud
from app.api.crud.authorizations import check_group_read, is_admin_access
from app.api.crud.groups import get_entity_group_id
from app.api.deps import get_current_access, get_current_device
from app.api.deps import get_current_access, get_current_device, get_db
from app.api.external import post_request
from app.api.schemas import AlertBase, AlertIn, AlertOut, DeviceOut
from app.db import alerts, events, get_session, media, models
from app.db.models import AccessType
from app.db import alerts, events, media
from app.models import Access, AccessType, Alert, Device, Event
from app.schemas import AlertBase, AlertIn, AlertOut, DeviceOut

router = APIRouter()

Expand Down Expand Up @@ -95,7 +95,7 @@ async def get_alert(

@router.get("/", response_model=List[AlertOut], summary="Get the list of all alerts")
async def fetch_alerts(
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_session)
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_db)
):
"""
Retrieves the list of all alerts and their information
Expand All @@ -104,11 +104,7 @@ async def fetch_alerts(
return await crud.fetch_all(alerts)
else:
retrieved_alerts = (
session.query(models.Alerts)
.join(models.Devices)
.join(models.Accesses)
.filter(models.Accesses.group_id == requester.group_id)
.all()
session.query(Alert).join(Device).join(Access).filter(Access.group_id == requester.group_id).all()
)
retrieved_alerts = [x.__dict__ for x in retrieved_alerts]
return retrieved_alerts
Expand All @@ -124,7 +120,7 @@ async def delete_alert(alert_id: int = Path(..., gt=0), _=Security(get_current_a

@router.get("/ongoing", response_model=List[AlertOut], summary="Get the list of ongoing alerts")
async def fetch_ongoing_alerts(
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_session)
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_db)
):
"""
Retrieves the list of ongoing alerts and their information
Expand All @@ -138,12 +134,12 @@ async def fetch_ongoing_alerts(
return (await crud.base.database.fetch_all(query=query.limit(50)))[::-1]
else:
retrieved_alerts = (
session.query(models.Alerts)
.join(models.Events)
.filter(models.Events.end_ts.is_(None))
.join(models.Devices)
.join(models.Accesses)
.filter(models.Accesses.group_id == requester.group_id)
session.query(Alert)
.join(Event)
.filter(Event.end_ts.is_(None))
.join(Device)
.join(Access)
.filter(Access.group_id == requester.group_id)
)
retrieved_alerts = [x.__dict__ for x in retrieved_alerts.all()]
return retrieved_alerts
17 changes: 6 additions & 11 deletions src/app/api/routes/devices.py → src/app/api/endpoints/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from app.api import crud
from app.api.crud.authorizations import is_admin_access
from app.api.crud.groups import get_entity_group_id
from app.api.deps import get_current_access, get_current_device, get_current_user
from app.api.schemas import (
from app.api.deps import get_current_access, get_current_device, get_current_user, get_db
from app.db import accesses, devices, users
from app.models import Access, AccessType, Device
from app.schemas import (
AdminDeviceAuth,
Cred,
DeviceAuth,
Expand All @@ -24,8 +26,6 @@
SoftwareHash,
UserRead,
)
from app.db import accesses, devices, get_session, models, users
from app.db.models import AccessType

router = APIRouter()

Expand Down Expand Up @@ -77,20 +77,15 @@ async def get_my_device(me: DeviceOut = Security(get_current_device, scopes=["de

@router.get("/", response_model=List[DeviceOut], summary="Get the list of all devices")
async def fetch_devices(
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_session)
requester=Security(get_current_access, scopes=[AccessType.admin, AccessType.user]), session=Depends(get_db)
):
"""
Retrieves the list of all devices and their information
"""
if await is_admin_access(requester.id):
return await crud.fetch_all(devices)
else:
retrieved_devices = (
session.query(models.Devices)
.join(models.Accesses)
.filter(models.Accesses.group_id == requester.group_id)
.all()
)
retrieved_devices = session.query(Device).join(Access).filter(Access.group_id == requester.group_id).all()
retrieved_devices = [x.__dict__ for x in retrieved_devices]

return retrieved_devices
Expand Down
Loading