Skip to content

Commit

Permalink
Octopoes origin param endpoint (#731)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Lisser <[email protected]>
  • Loading branch information
noamblitz and Lisser authored Apr 14, 2023
1 parent 19dd152 commit 55feede
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
11 changes: 10 additions & 1 deletion octopoes/octopoes/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from octopoes.models.datetime import TimezoneAwareDatetime
from octopoes.models.exception import ObjectNotFoundException
from octopoes.models.origin import Origin, OriginType
from octopoes.models.origin import Origin, OriginType, OriginParameter
from octopoes.models.pagination import Paginated
from octopoes.models.tree import ReferenceTree
from octopoes.models.types import type_by_name
Expand Down Expand Up @@ -179,6 +179,15 @@ def list_origins(
return octopoes.origin_repository.list_by_result(reference, valid_time)


@router.get("/origin_parameters")
def list_origin_parameters(
octopoes: OctopoesService = Depends(octopoes_service),
valid_time: datetime = Depends(extract_valid_time),
origin_id: Set[str] = Query(default=set()),
) -> List[OriginParameter]:
return octopoes.origin_parameter_repository.list_by_origin(origin_id, valid_time)


@router.post("/observations")
def save_observation(
observation: ValidatedObservation,
Expand Down
7 changes: 6 additions & 1 deletion octopoes/octopoes/connector/octopoes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
DEFAULT_SCAN_PROFILE_TYPE_FILTER,
)
from octopoes.models.exception import ObjectNotFoundException
from octopoes.models.origin import Origin
from octopoes.models.origin import Origin, OriginParameter
from octopoes.models.pagination import Paginated
from octopoes.models.tree import ReferenceTree
from octopoes.models.types import OOIType
Expand Down Expand Up @@ -144,6 +144,11 @@ def delete(self, reference: Reference, valid_time: Optional[datetime] = None) ->
params = {"reference": str(reference), "valid_time": valid_time}
self.session.delete(f"/{self.client}/", params=params)

def list_origin_parameters(self, origin_id: Set[str], valid_time: Optional[datetime] = None) -> List[str]:
params = {"origin_id": origin_id, "valid_time": valid_time}
res = self.session.get(f"/{self.client}/origin_parameters", params=params)
return parse_obj_as(List[OriginParameter], res.json())

def create_node(self):
self.session.post(f"/{self.client}/node")

Expand Down
2 changes: 1 addition & 1 deletion octopoes/octopoes/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _run_inference(self, origin: Origin, valid_time: datetime):

source = self.ooi_repository.get(origin.source, valid_time)

parameters_references = self.origin_parameter_repository.list_by_origin(origin.id, valid_time)
parameters_references = self.origin_parameter_repository.list_by_origin({origin.id}, valid_time)
parameters = self.ooi_repository.get_bulk({x.reference for x in parameters_references}, valid_time)

resulting_oois = []
Expand Down
8 changes: 4 additions & 4 deletions octopoes/octopoes/repositories/origin_parameter_repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from http import HTTPStatus
from logging import getLogger
from typing import Any, Dict, List
from typing import Any, Dict, List, Set

from requests import HTTPError

Expand Down Expand Up @@ -31,7 +31,7 @@ def save(self, origin_parameter: OriginParameter, valid_time: datetime) -> None:
def delete(self, origin_parameter: OriginParameter, valid_time: datetime) -> None:
raise NotImplementedError

def list_by_origin(self, origin_id: str, valid_time: datetime) -> List[OriginParameter]:
def list_by_origin(self, origin_id: Set[str], valid_time: datetime) -> List[OriginParameter]:
raise NotImplementedError

def list_by_reference(self, reference: Reference, valid_time: datetime) -> List[OriginParameter]:
Expand Down Expand Up @@ -70,12 +70,12 @@ def get(self, origin_parameter_id: str, valid_time: datetime) -> OriginParameter
else:
raise e

def list_by_origin(self, origin_id: str, valid_time: datetime) -> List[OriginParameter]:
def list_by_origin(self, origin_id: Set[str], valid_time: datetime) -> List[OriginParameter]:
query = generate_pull_query(
self.xtdb_type,
FieldSet.ALL_FIELDS,
{
"origin_id": str(origin_id),
"origin_id": origin_id,
"type": OriginParameter.__name__,
},
)
Expand Down
4 changes: 2 additions & 2 deletions octopoes/octopoes/xtdb/query_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Set, Optional, Iterator, List
from typing import Set, Optional, Iterator, List, Dict, Union

from octopoes.config.settings import XTDBType
from octopoes.xtdb.related_field_generator import (
Expand All @@ -22,7 +22,7 @@ def str_val(val):
def generate_pull_query(
xtdb_type: XTDBType,
field_set: Optional[FieldSet] = FieldSet.ALL_FIELDS,
where: Optional[dict] = None,
where: Optional[Dict[str, Union[str, int, List[Union[str, int]], Set[Union[str, int]]]]] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
field_node: Optional[RelatedFieldNode] = None,
Expand Down
2 changes: 2 additions & 0 deletions rocky/rocky/templates/oois/ooi_detail_origins_inference.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h2>{% translate "Inferred by" %}</h2>
<tr>
<th>{% translate "Bit" %}</th>
<th>{% translate "Source" %}</th>
<th>{% translate "Parameters" %}</th>
</tr>
</thead>
<tbody>
Expand All @@ -19,6 +20,7 @@ <h2>{% translate "Inferred by" %}</h2>
<td>
<a href="{% ooi_url 'ooi_detail' inference.origin.source organization.code %}">{{ inference.origin.source }}</a>
</td>
<td>{% for param in inference.params %}<a href="{% ooi_url 'ooi_detail' param.reference organization.code %}">{{ param.reference }}</a><br>{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
1 change: 1 addition & 0 deletions rocky/rocky/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class OriginData(BaseModel):
origin: Origin
normalizer: Optional[dict]
boefje: Optional[Plugin]
params: Optional[Dict[str, str]]


class OOIAttributeError(AttributeError):
Expand Down
13 changes: 12 additions & 1 deletion rocky/rocky/views/ooi_detail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from datetime import datetime, timezone
from enum import Enum
from typing import Optional
Expand Down Expand Up @@ -157,6 +158,17 @@ def get_context_data(self, **kwargs):
declarations, observations, inferences = self.get_origins(
self.ooi.reference, self.get_observed_at(), self.organization
)

inference_params = self.octopoes_api_connector.list_origin_parameters(
{inference.origin.id for inference in inferences}
)
inference_params_per_inference = defaultdict(list)
for inference_param in inference_params:
inference_params_per_inference[inference_param.origin_id].append(inference_param)

for inference in inferences:
inference.params = inference_params_per_inference.get(inference.origin.id, [])

context["declarations"] = declarations
context["observations"] = observations
context["inferences"] = inferences
Expand All @@ -180,5 +192,4 @@ def get_context_data(self, **kwargs):
"scan_history_search",
"scan_history_page",
]

return context

0 comments on commit 55feede

Please sign in to comment.