Fix dashboards

This commit is contained in:
phil 2024-03-24 11:21:11 +05:30
parent d539a72e6a
commit 5434c7d6ef
6 changed files with 330 additions and 74 deletions

View file

@ -1,6 +1,7 @@
import logging
from pathlib import Path
from json import dumps
from typing import Annotated
from fastapi import Depends, APIRouter, HTTPException, status, responses
from sqlalchemy.orm import selectinload
@ -14,8 +15,8 @@ from gisaf.database import fastapi_db_session as db_session
from gisaf.models.authentication import User
from gisaf.models.dashboard import (
DashboardPage, DashboardPageSection,
DashboadPageSectionType, DashboardPage_,
DashboardGroup, DashboardHome,
DashboardPageMetaData,
DashboardGroup, DashboardHome, Dashboard, DashboardSection
)
from gisaf.models.misc import NotADataframeError
from gisaf.security import get_current_active_user
@ -46,10 +47,12 @@ async def get_groups(
) -> list[DashboardGroup]:
query = select(DashboardPage)
data = await db_session.exec(query)
groups: dict[str, DashboardPage_] = {}
groups: dict[str, DashboardPageMetaData] = {}
for page in data.all():
page_field = DashboardPage_(name=page.name, group=page.group,
description=page.description)
page_field = DashboardPageMetaData(name=page.name, group=page.group,
description=page.description,
viewable_role=page.viewable_role
)
group = groups.get(page.group)
if group is None:
group = DashboardGroup(name=page.group, pages=[page_field])
@ -80,9 +83,10 @@ async def get_home() -> DashboardHome:
async def get_dashboard_page(group: str, name: str,
db_session: db_session,
user: User = Depends(get_current_active_user),
) -> DashboardPage_:
query1 = select(DashboardPage).where((DashboardPage.name==name)
& (DashboardPage.group==group))
) -> Dashboard:
query1 = select(DashboardPage).\
options(selectinload(DashboardPage.sections)).\
where((DashboardPage.name==name) & (DashboardPage.group==group))
data1 = await db_session.exec(query1)
page = data1.one_or_none()
if not page:
@ -98,7 +102,7 @@ async def get_dashboard_page(group: str, name: str,
username = user.username if user is not None else "Anonymous"
logger.info(f'{username} tried to access dashboard page {name}')
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
dp = DashboardPage_(
dp = Dashboard(
name=page.name,
group=page.group,
description=page.description,
@ -111,7 +115,7 @@ async def get_dashboard_page(group: str, name: str,
for p in page.expanded_panes.split(',')
] if page.expanded_panes else [],
sections=[
DashboadPageSectionType(
DashboardSection(
name=dps.name,
plot=dps.get_plot_url()
)
@ -126,7 +130,7 @@ async def get_dashboard_page(group: str, name: str,
if isinstance(df, gpd.GeoDataFrame):
gdf = pd.DataFrame(df.drop(columns=['geometry']))
df = gdf
dp.dfData = df.to_json(orient='table', double_precision=2)
dp.dfData = df.reset_index().to_dict(orient='records')
except NotADataframeError:
logger.warning(f'Dashboard: cannot read dataframe for page {page.name}')
except Exception as err: