Skip to content

Commit

Permalink
246 implement measuremaatregel modal to change state (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensWe authored Oct 16, 2024
2 parents e26ed98 + 21dcdc1 commit 9df2c7e
Show file tree
Hide file tree
Showing 108 changed files with 2,879 additions and 374 deletions.
121 changes: 119 additions & 2 deletions amt/api/routes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field

from amt.api.deps import templates
from amt.api.lifecycles import get_lifecycle
Expand All @@ -18,8 +19,11 @@
from amt.core.exceptions import AMTNotFound, AMTRepositoryError
from amt.enums.status import Status
from amt.models import Project
from amt.schema.measure import ExtendedMeasureTask, MeasureTask
from amt.schema.requirement import RequirementTask
from amt.schema.system_card import SystemCard
from amt.schema.task import MovedTask
from amt.services import task_registry
from amt.services.instruments_and_requirements_state import InstrumentStateService, RequirementsStateService
from amt.services.projects import ProjectsService
from amt.services.storage import StorageFactory
Expand Down Expand Up @@ -311,8 +315,27 @@ async def get_system_card_requirements(
system_card = project.system_card
requirements = fetch_requirements([requirement.urn for requirement in system_card.requirements])

# Get measures that correspond to the requirements.
requirements_and_measures = [(requirement, fetch_measures(requirement.links)) for requirement in requirements]
# Get measures that correspond to the requirements and merge them with the measuretasks
requirements_and_measures = []
for requirement in requirements:
completed_measures_count = 0
linked_measures = fetch_measures(requirement.links)
extended_linked_measures: list[ExtendedMeasureTask] = []
for measure in linked_measures:
measure_task = find_measure_task(system_card, measure.urn)
if measure_task:
ext_measure_task = ExtendedMeasureTask(
name=measure.name,
description=measure.description,
urn=measure.urn,
state=measure_task.state,
value=measure_task.value,
version=measure_task.version,
)
if ext_measure_task.state == "done":
completed_measures_count += 1
extended_linked_measures.append(ext_measure_task)
requirements_and_measures.append((requirement, completed_measures_count, extended_linked_measures)) # pyright: ignore [reportUnknownMemberType]

context = {
"instrument_state": instrument_state,
Expand All @@ -327,6 +350,100 @@ async def get_system_card_requirements(
return templates.TemplateResponse(request, "projects/details_requirements.html.j2", context)


def find_measure_task(system_card: SystemCard, urn: str) -> MeasureTask | None:
for measure in system_card.measures:
if measure.urn == urn:
return measure
return None


def find_requirement_task(system_card: SystemCard, requirement_urn: str) -> RequirementTask | None:
for requirement in system_card.requirements:
if requirement.urn == requirement_urn:
return requirement
return None


def find_requirement_tasks_by_measure_urn(system_card: SystemCard, measure_urn: str) -> list[RequirementTask]:
requirement_mapper: dict[str, RequirementTask] = {}
for requirement_task in system_card.requirements:
requirement_mapper[requirement_task.urn] = requirement_task

requirement_tasks: list[RequirementTask] = []
measure = fetch_measures([measure_urn])
for requirement_urn in measure[0].links:
# TODO: This is because measure are linked to too many requirement not applicable in our use case
if len(fetch_requirements([requirement_urn])) > 0:
requirement_tasks.append(requirement_mapper[requirement_urn])

return requirement_tasks


@router.get("/{project_id}/measure/{measure_urn}")
async def get_measure(
request: Request,
project_id: int,
measure_urn: str,
projects_service: Annotated[ProjectsService, Depends(ProjectsService)],
) -> HTMLResponse:
project = get_project_or_error(project_id, projects_service, request)
measure = task_registry.fetch_measures([measure_urn])
measure_task = find_measure_task(project.system_card, measure_urn)

context = {
"measure": measure[0],
"measure_state": measure_task.state, # pyright: ignore [reportOptionalMemberAccess]
"measure_value": measure_task.value, # pyright: ignore [reportOptionalMemberAccess]
"project_id": project_id,
}

return templates.TemplateResponse(request, "projects/details_measure_modal.html.j2", context)


class MeasureUpdate(BaseModel):
measure_state: str = Field(default=None)
measure_value: str = Field(default=None)


@router.post("/{project_id}/measure/{measure_urn}")
async def update_measure_value(
request: Request,
project_id: int,
measure_urn: str,
measure_update: MeasureUpdate,
projects_service: Annotated[ProjectsService, Depends(ProjectsService)],
) -> HTMLResponse:
project = get_project_or_error(project_id, projects_service, request)

measure_task = find_measure_task(project.system_card, measure_urn)
measure_task.state = measure_update.measure_state # pyright: ignore [reportOptionalMemberAccess]
measure_task.value = measure_update.measure_value # pyright: ignore [reportOptionalMemberAccess]

# update for the linked requirements the state based on all it's measures
requirement_tasks = find_requirement_tasks_by_measure_urn(project.system_card, measure_urn)
requirement_urns = [requirement_task.urn for requirement_task in requirement_tasks]
requirements = fetch_requirements(requirement_urns)

for requirement in requirements:
count_completed = 0
for link_measure_urn in requirement.links:
link_measure_task = find_measure_task(project.system_card, link_measure_urn)
if link_measure_task: # noqa: SIM102
if link_measure_task.state == "done":
count_completed += 1
requirement_task = find_requirement_task(project.system_card, requirement.urn)
if count_completed == len(requirement.links):
requirement_task.state = "done" # pyright: ignore [reportOptionalMemberAccess]
elif count_completed == 0 and len(requirement.links) > 0:
requirement_task.state = "to do" # pyright: ignore [reportOptionalMemberAccess]
else:
requirement_task.state = "in progress" # pyright: ignore [reportOptionalMemberAccess]

projects_service.update(project)
# TODO: FIX THIS!! The page now reloads at the top, which is annoying
return templates.Redirect(request, f"/project/{project_id}/details/system_card/requirements")


# !!!
# Implementation of this endpoint is for now independent of the project ID, meaning
# that the same system card is rendered for all project ID's. This is due to the fact
Expand Down
73 changes: 60 additions & 13 deletions amt/locale/base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-10-16 08:58+0200\n"
"POT-Creation-Date: 2024-10-16 09:55+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -66,6 +66,7 @@ msgid "Verification and Validation"
msgstr ""

#: amt/api/lifecycles.py:45
#: amt/site/templates/projects/details_measure_modal.html.j2:66
msgid "Implementation"
msgstr ""

Expand Down Expand Up @@ -255,10 +256,10 @@ msgid "Reviewing"
msgstr ""

#: amt/site/templates/macros/tasks.html.j2:32
#: amt/site/templates/projects/details_base.html.j2:20
#: amt/site/templates/projects/details_base.html.j2:41
#: amt/site/templates/projects/details_base.html.j2:67
#: amt/site/templates/projects/details_base.html.j2:89
#: amt/site/templates/projects/details_base.html.j2:27
#: amt/site/templates/projects/details_base.html.j2:52
#: amt/site/templates/projects/details_base.html.j2:78
#: amt/site/templates/projects/details_base.html.j2:100
msgid "Done"
msgstr ""

Expand Down Expand Up @@ -337,24 +338,28 @@ msgstr ""
msgid "Everyone is welcome to make comments and suggestions."
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:18
#: amt/site/templates/projects/details_base.html.j2:24
msgid "Does the algorithm meet the requirements?"
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:39
#: amt/site/templates/projects/details_base.html.j2:87
#: amt/site/templates/projects/details_base.html.j2:48
#: amt/site/templates/projects/details_base.html.j2:98
msgid "To do"
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:53
msgid "Go to all requirements"
#: amt/site/templates/projects/details_base.html.j2:50
msgid "In progress"
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:64
msgid "Go to all requirements"
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:75
msgid "Which instruments are executed?"
msgstr ""

#: amt/site/templates/projects/details_base.html.j2:101
#: amt/site/templates/projects/details_base.html.j2:112
msgid "Go to all instruments"
msgstr ""

Expand Down Expand Up @@ -420,6 +425,7 @@ msgid "Failed to estimate WOZ value: "
msgstr ""

#: amt/site/templates/projects/details_info.html.j2:7
#: amt/site/templates/projects/details_measure_modal.html.j2:19
msgid "Description"
msgstr ""

Expand All @@ -439,10 +445,51 @@ msgstr ""
msgid "Labels"
msgstr ""

#: amt/site/templates/projects/details_requirements.html.j2:19
#: amt/site/templates/projects/details_measure_modal.html.j2:28
msgid "Read more on the algoritmekader"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:37
msgid "Status"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:44
#: amt/site/templates/projects/details_measure_modal.html.j2:46
msgid "to do"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:49
#: amt/site/templates/projects/details_measure_modal.html.j2:51
msgid "in progress"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:54
#: amt/site/templates/projects/details_measure_modal.html.j2:56
msgid "done"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:69
msgid ""
"Describe how the measure has been implemented, including challenges and "
"solutions."
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:80
msgid "Save"
msgstr ""

#: amt/site/templates/projects/details_measure_modal.html.j2:84
msgid "Cancel"
msgstr ""

#: amt/site/templates/projects/details_requirements.html.j2:25
msgid "measures executed"
msgstr ""

#: amt/site/templates/projects/details_requirements.html.j2:54
msgid "Edit measure"
msgstr ""

#: amt/site/templates/projects/index.html.j2:12
msgid "New project"
msgstr ""
Expand Down Expand Up @@ -518,7 +565,7 @@ msgstr ""
msgid "Create Project"
msgstr ""

#: amt/site/templates/projects/new.html.j2:191
#: amt/site/templates/projects/new.html.j2:193
msgid "Copy results and close"
msgstr ""

Binary file modified amt/locale/en_US/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 9df2c7e

Please sign in to comment.