Skip to content

Commit

Permalink
feat: Store users for reports + realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Oct 8, 2024
1 parent f7a254a commit efcbecd
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 51 deletions.
5 changes: 4 additions & 1 deletion backend/models/report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import Boolean, Column, DateTime, String, text
from sqlalchemy import UUID, Boolean, Column, DateTime, ForeignKey, String, text
from sqlalchemy.orm import relationship

from backend.database import Base

Expand All @@ -7,6 +8,8 @@ class Report(Base):
__tablename__ = "reports"

scene_id = Column(String, primary_key=True)
user_id = Column(UUID, ForeignKey("auth.users.id", onupdate="CASCADE", ondelete="CASCADE"), nullable=False)
user = relationship("User")
is_processed = Column(Boolean, nullable=False, default=False)
created_at = Column(DateTime, nullable=False, server_default=text("now()"))
raw_data = Column(String, nullable=True)
18 changes: 12 additions & 6 deletions backend/routers/report_router.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import datetime
from typing import List

import structlog
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
Expand All @@ -22,25 +23,31 @@ def get_report_by_scene_id(scene_id: str, db: Session):
return query.first()


def add_new_processing_report(scene_id: str, db: Session):
def add_new_processing_report(scene_id: str, db: Session, user: UserResponse):
new_report = models.Report(
scene_id=scene_id,
created_at=datetime.now(),
user_id=user.user.id,
)
db.add(new_report)
db.commit()


@report_router.post("/generate_report", status_code=202)
async def generate_report(scene_id: str, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
async def generate_report(
scene_id: str,
background_tasks: BackgroundTasks,
user: UserResponse = Depends(get_current_user),
db: Session = Depends(get_db),
):
report = get_report_by_scene_id(scene_id, db)
if report:
if report.is_processed:
raise HTTPException(status_code=409, detail="Report already processed")
else:
raise HTTPException(status_code=402, detail="Report is being processed")

add_new_processing_report(scene_id, db)
add_new_processing_report(scene_id, db, user)
background_tasks.add_task(write_report_to_db, scene_id, db)

return {"message": "Report added to processing queue"}
Expand All @@ -59,9 +66,8 @@ async def get_report(scene_id: str, db: Session = Depends(get_db)) -> LandsatAdv
return LandsatAdvancedItem.model_validate(json.loads(str(report.raw_data)))


@report_router.get("/get_reports", response_model=list[schemas.Report])
@report_router.get("/get_reports", response_model=List[schemas.Report])
async def get_reports(user: UserResponse = Depends(get_current_user), db: Session = Depends(get_db)):
# todo: return only user reports
reports = db.query(models.Report).all()
reports = db.query(models.Report).join(models.User).filter(models.Report.user_id == user.user.id).all()

return reports
3 changes: 3 additions & 0 deletions backend/schemas/structures/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

from pydantic import BaseModel

from backend.schemas.structures.user import UserBase


class Report(BaseModel):
scene_id: str
user: UserBase
is_processed: bool
created_at: datetime.datetime
raw_data: Optional[str]
Expand Down
3 changes: 3 additions & 0 deletions frontend/components/GenerateRaportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const props = defineProps<{
async function generateReport() {
await useApi("/report/generate_report", {
method: "POST",
headers: {
Authorization: `Bearer ${useSupabaseSession().value?.access_token}`
},
query: {
scene_id: props.sceneId
}
Expand Down
24 changes: 0 additions & 24 deletions frontend/components/Report.vue

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/openapi/api/openapi.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frontend/pages/how_does_it_work.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<p text-4xl color="#a3a3a3">
Landsat Data
</p>
<p mt-4>
<div mt-4>
Landsat provides two main types of data products:<br><br>
<ul>
<li>
Expand All @@ -43,7 +43,7 @@
Level 2 products: These are preprocessed data, corrected and enhanced for easier analysis.
</li>
</ul>
</p>
</div>
</div>
<div mt-8>
<p text-4xl color="#a3a3a3">
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/panel/report/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (error.value) {
const metadata = {
"Scene ID": reportData.value.id,
Satellite: reportData.value.platform,
Satellite: reportData.value.platform.replace("-", " ").replace("landsat", "Landsat"),
"Cloud Coverage": reportData.value.eo_cloud_cover,
Path: reportData.value.wrs_coordinates.wrs_path,
Row: reportData.value.wrs_coordinates.wrs_row
Expand Down
41 changes: 25 additions & 16 deletions frontend/pages/panel/reports.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
<Table>
<TableHeader>
<TableRow>
<TableHead>Is Processed</TableHead>
<TableHead>Created at</TableHead>
<TableHead>Status</TableHead>
<TableHead>Created at</TableHead>
<TableHead>Scene</TableHead>
<TableHead><!-- extends table header to full width --></TableHead>
<TableHead><!-- extends table header to full width --></TableHead>
<TableHead>Action</TableHead>
<TableHead />
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="(report, index) in reports" :key="index">
<TableCell>{{ report.is_processed }}</TableCell>
<TableRow v-for="(report, index) in data" :key="index">
<TableCell>{{ report.is_processed ? "Processed" : "Processing..." }}</TableCell>
<TableCell>{{ report.created_at }}</TableCell>
<TableCell>{{ report.scene_id }}</TableCell>
<TableCell>
Expand All @@ -40,20 +39,30 @@
</template>

<script setup lang="ts">
const reports = ref<{
scene_id: string
is_processed: boolean
created_at: string
raw_data: string | null
}[]>();
import type { RealtimeChannel } from "@supabase/supabase-js";
const { data } = await useApi("/report/get_reports", {
const supabase = useSupabaseClient();
const { data, refresh } = await useApi("/report/get_reports", {
headers: {
Authorization: `Bearer ${useSupabaseSession().value?.access_token}`
}
});
if (data.value) {
reports.value = data.value;
}
let realtimeChannel: RealtimeChannel;
onMounted(() => {
realtimeChannel = supabase.channel("public:reports").on(
"postgres_changes",
{ event: "*", schema: "public", table: "reports" },
() => {
refresh();
}
);
realtimeChannel.subscribe();
});
onUnmounted(() => {
supabase.removeChannel(realtimeChannel);
});
</script>

0 comments on commit efcbecd

Please sign in to comment.