Skip to content

Commit

Permalink
Upgrade Pygen to 0.99.14 for GraphQL feature (#337)
Browse files Browse the repository at this point in the history
* build; pygen with fix

* build; pygen to 99.14

* refactor: regen Pygen

* changelog and version

---------

Co-authored-by: Juliamg <[email protected]>
  • Loading branch information
doctrino and Juliamg authored Mar 21, 2024
1 parent 6d82616 commit 2f308d0
Show file tree
Hide file tree
Showing 101 changed files with 9,180 additions and 731 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [0.91.3] - 2024-03-21
### Added
* Regenerated the SDK with the latest version of `pygen` to get GraphQL feature for querying and parsing responses from cdf

## [0.91.2] - 2024-03-15
### Changed
* Changed property in `MultiScenarioMatrixRaw` for `shopResults` to `SHOPResultPriceProd` and added the `MultiScenarioMatrixRaw` to the `TotalBidCalculation` data model.
Expand Down
2 changes: 1 addition & 1 deletion cognite/powerops/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.91.2"
__version__ = "0.91.3"
41 changes: 41 additions & 0 deletions cognite/powerops/client/_generated/afrr_bid/_api/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
DomainModelCore,
DomainModelWrite,
DomainRelationWrite,
GraphQLList,
ResourcesWriteResult,
T_DomainModel,
T_DomainModelWrite,
Expand All @@ -32,6 +33,8 @@
DomainModelCore,
DomainRelation,
)
from cognite.powerops.client._generated.afrr_bid import data_classes


DEFAULT_LIMIT_READ = 25
DEFAULT_QUERY_LIMIT = 3
Expand Down Expand Up @@ -751,3 +754,41 @@ def _create_edge_filter(
if filter:
filters.append(filter)
return dm.filters.And(*filters)


class GraphQLQueryResponse:
def __init__(self, data_model_id: dm.DataModelId):
self._output = GraphQLList([])
self._data_class_by_type = _GRAPHQL_DATA_CLASS_BY_DATA_MODEL_BY_TYPE[data_model_id]

def parse(self, response: dict[str, Any]) -> GraphQLList:
if "errors" in response:
raise RuntimeError(response["errors"])
_, data = list(response.items())[0]
self._parse_item(data)
return self._output

def _parse_item(self, data: dict[str, Any]) -> None:
if "items" in data:
for item in data["items"]:
self._parse_item(item)
elif "__typename" in data:
try:
item = self._data_class_by_type[data["__typename"]].model_validate(data)
except KeyError:
raise ValueError(f"Could not find class for type {data['__typename']}")
else:
self._output.append(item)
else:
raise RuntimeError("Missing '__typename' in GraphQL response. Cannot determine the type of the response.")


_GRAPHQL_DATA_CLASS_BY_DATA_MODEL_BY_TYPE = {
dm.DataModelId("power-ops-afrr-bid", "AFRRBid", "1"): {
"BidDocument": data_classes.BidDocumentGraphQL,
"BidRow": data_classes.BidRowGraphQL,
"PriceArea": data_classes.PriceAreaGraphQL,
"BidMethod": data_classes.BidMethodGraphQL,
"Alert": data_classes.AlertGraphQL,
},
}
40 changes: 31 additions & 9 deletions cognite/powerops/client/_generated/afrr_bid/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from pathlib import Path
from typing import Sequence
from typing import Any, Sequence

from cognite.client import ClientConfig, CogniteClient, data_modeling as dm
from cognite.client.data_classes import TimeSeriesList
Expand All @@ -13,8 +13,8 @@
from ._api.bid_method import BidMethodAPI
from ._api.bid_row import BidRowAPI
from ._api.price_area import PriceAreaAPI
from ._api._core import SequenceNotStr
from .data_classes._core import DEFAULT_INSTANCE_SPACE
from ._api._core import SequenceNotStr, GraphQLQueryResponse
from .data_classes._core import DEFAULT_INSTANCE_SPACE, GraphQLList
from . import data_classes


Expand All @@ -23,9 +23,9 @@ class AFRRBidAPI:
AFRRBidAPI
Generated with:
pygen = 0.99.11
cognite-sdk = 7.26.0
pydantic = 2.6.3
pygen = 0.99.14
cognite-sdk = 7.26.2
pydantic = 2.6.4
Data Model:
space: power-ops-afrr-bid
Expand All @@ -41,7 +41,7 @@ def __init__(self, config_or_client: CogniteClient | ClientConfig):
else:
raise ValueError(f"Expected CogniteClient or ClientConfig, got {type(config_or_client)}")
# The client name is used for aggregated logging of Pygen Usage
client.config.client_name = "CognitePygen:0.99.11"
client.config.client_name = "CognitePygen:0.99.14"

view_by_read_class = {
data_classes.Alert: dm.ViewId("power-ops-shared", "Alert", "1"),
Expand All @@ -64,6 +64,7 @@ def upsert(
items: data_classes.DomainModelWrite | Sequence[data_classes.DomainModelWrite],
replace: bool = False,
write_none: bool = False,
allow_version_increase: bool = False,
) -> data_classes.ResourcesWriteResult:
"""Add or update (upsert) items.
Expand All @@ -73,17 +74,27 @@ def upsert(
Or should we merge in new values for properties together with the existing values (false)? Note: This setting applies for all nodes or edges specified in the ingestion call.
write_none (bool): This method will, by default, skip properties that are set to None. However, if you want to set properties to None,
you can set this parameter to True. Note this only applies to properties that are nullable.
allow_version_increase (bool): If set to true, the version of the instance will be increased if the instance already exists.
If you get an error: 'A version conflict caused the ingest to fail', you can set this to true to allow
the version to increase.
Returns:
Created instance(s), i.e., nodes, edges, and time series.
"""
if isinstance(items, data_classes.DomainModelWrite):
instances = items.to_instances_write(self._view_by_read_class, write_none)
instances = items.to_instances_write(self._view_by_read_class, write_none, allow_version_increase)
else:
instances = data_classes.ResourcesWrite()
cache: set[tuple[str, str]] = set()
for item in items:
instances.extend(item._to_instances_write(cache, self._view_by_read_class, write_none))
instances.extend(
item._to_instances_write(
cache,
self._view_by_read_class,
write_none,
allow_version_increase,
)
)
result = self._client.data_modeling.instances.apply(
nodes=instances.nodes,
edges=instances.edges,
Expand Down Expand Up @@ -151,6 +162,17 @@ def delete(
nodes=[(space, id) for id in external_id],
)

def graphql_query(self, query: str, variables: dict[str, Any] | None = None) -> GraphQLList:
"""Execute a GraphQl query against the AFRRBid data model.
Args:
query (str): The GraphQL query to issue.
variables (dict[str, Any] | None): An optional dict of variables to pass to the query.
"""
data_model_id = dm.DataModelId("power-ops-afrr-bid", "AFRRBid", "1")
result = self._client.data_modeling.graphql.query(data_model_id, query, variables)
return GraphQLQueryResponse(data_model_id).parse(result)

@classmethod
def azure_project(
cls, tenant_id: str, client_id: str, client_secret: str, cdf_cluster: str, project: str
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from ._core import (
DataRecord,
DataRecordGraphQL,
DataRecordWrite,
DomainModel,
DomainModelCore,
DomainModelWrite,
DomainModelList,
DomainRelationWrite,
GraphQLCore,
GraphQLList,
ResourcesWrite,
ResourcesWriteResult,
)
Expand All @@ -14,6 +17,7 @@
AlertApply,
AlertApplyList,
AlertFields,
AlertGraphQL,
AlertList,
AlertTextFields,
AlertWrite,
Expand All @@ -24,6 +28,7 @@
BidDocumentApply,
BidDocumentApplyList,
BidDocumentFields,
BidDocumentGraphQL,
BidDocumentList,
BidDocumentTextFields,
BidDocumentWrite,
Expand All @@ -34,6 +39,7 @@
BidMethodApply,
BidMethodApplyList,
BidMethodFields,
BidMethodGraphQL,
BidMethodList,
BidMethodTextFields,
BidMethodWrite,
Expand All @@ -44,31 +50,39 @@
BidRowApply,
BidRowApplyList,
BidRowFields,
BidRowGraphQL,
BidRowList,
BidRowTextFields,
BidRowWrite,
BidRowWriteList,
)
from ._price_area import PriceArea, PriceAreaFields, PriceAreaList, PriceAreaTextFields
from ._price_area import PriceArea, PriceAreaFields, PriceAreaGraphQL, PriceAreaList, PriceAreaTextFields

BidDocument.model_rebuild()
BidDocumentGraphQL.model_rebuild()
BidDocumentWrite.model_rebuild()
BidDocumentApply.model_rebuild()
BidRow.model_rebuild()
BidRowGraphQL.model_rebuild()
BidRowWrite.model_rebuild()
BidRowApply.model_rebuild()


__all__ = [
"DataRecord",
"DataRecordGraphQL",
"DataRecordWrite",
"ResourcesWrite",
"DomainModel",
"DomainModelCore",
"DomainModelWrite",
"DomainModelList",
"DomainRelationWrite",
"GraphQLCore",
"GraphQLList",
"ResourcesWriteResult",
"Alert",
"AlertGraphQL",
"AlertWrite",
"AlertApply",
"AlertList",
Expand All @@ -77,6 +91,7 @@
"AlertFields",
"AlertTextFields",
"BidDocument",
"BidDocumentGraphQL",
"BidDocumentWrite",
"BidDocumentApply",
"BidDocumentList",
Expand All @@ -85,6 +100,7 @@
"BidDocumentFields",
"BidDocumentTextFields",
"BidMethod",
"BidMethodGraphQL",
"BidMethodWrite",
"BidMethodApply",
"BidMethodList",
Expand All @@ -93,6 +109,7 @@
"BidMethodFields",
"BidMethodTextFields",
"BidRow",
"BidRowGraphQL",
"BidRowWrite",
"BidRowApply",
"BidRowList",
Expand All @@ -101,6 +118,7 @@
"BidRowFields",
"BidRowTextFields",
"PriceArea",
"PriceAreaGraphQL",
"PriceAreaList",
"PriceAreaFields",
"PriceAreaTextFields",
Expand Down
Loading

0 comments on commit 2f308d0

Please sign in to comment.