Skip to content

Commit

Permalink
updated to latest version of OSBot-Utils
Browse files Browse the repository at this point in the history
started adding implementation of MGraph__Time_Point__Create
  • Loading branch information
DinisCruz committed Feb 10, 2025
1 parent 70a27ee commit 07d3fb0
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 14 deletions.
105 changes: 105 additions & 0 deletions mgraph_db/providers/time_series/actions/MGraph__Time_Point__Create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from typing import Type, Dict, Tuple
from mgraph_db.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_db.mgraph.actions.MGraph__Index import MGraph__Index
from mgraph_db.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node
from mgraph_db.mgraph.schemas.Schema__MGraph__Edge import Schema__MGraph__Edge
from mgraph_db.providers.time_series.schemas.Schema__MGraph__Time_Point__Create__Data import Schema__MGraph__Time_Point__Create__Data
from mgraph_db.providers.time_series.schemas.Schema__MGraph__Time_Point__Created__Objects import Schema__MGraph__Time_Point__Created__Objects
from osbot_utils.helpers.Obj_Id import Obj_Id
from osbot_utils.type_safe.Type_Safe import Type_Safe
from osbot_utils.utils.Dev import pprint


class MGraph__Time_Point__Create(Type_Safe):
mgraph_edit : MGraph__Edit
mgraph_index: MGraph__Index

def execute(self, create_data: Schema__MGraph__Time_Point__Create__Data) -> Schema__MGraph__Time_Point__Created__Objects:
time_point = self.mgraph_edit.new_node(node_type=create_data.node_type__time_point)

if create_data.datetime_str: # Set the display value if available
time_point.node_data.value = create_data.datetime_str

value_nodes = {} # Initialize tracking dictionaries
component_edges = {}

time_components = [(create_data.year , create_data.edge_type__year ), # Define time components to process
(create_data.month , create_data.edge_type__month ),
(create_data.day , create_data.edge_type__day ),
(create_data.hour , create_data.edge_type__hour ),
(create_data.minute, create_data.edge_type__minute),
(create_data.second, create_data.edge_type__second)]

for value, edge_type in time_components: # Process each time component
if value is not None:
node_id, edge_id = self.add_value_component(value = value ,
node_type = create_data.node_type__value,
edge_type = edge_type ,
from_node = time_point )
value_nodes[edge_type] = node_id
component_edges[edge_type] = edge_id

tz_id, tz_edge = None, None
if create_data.timezone: # Handle timezone if present
tz_id, tz_edge = self.add_timezone_component(timezone = create_data.timezone ,
utc_offset = create_data.utc_offset ,
create_data = create_data ,
time_point = time_point )

pprint(value_nodes)
return Schema__MGraph__Time_Point__Created__Objects(time_point_id = time_point.node_id ,
value_nodes = value_nodes ,
component_edges = component_edges ,
timezone_id = tz_id ,
timezone_edge = tz_edge ,
utc_offset_id = None , # todo: add support for this
offset_edge = None )

def add_timezone_component(self, timezone : str ,
utc_offset : int ,
create_data : Schema__MGraph__Time_Point__Create__Data ,
time_point : Domain__MGraph__Node
) -> Tuple[Obj_Id, Obj_Id]: # Returns (timezone_node_id, timezone_edge_id)
tz_node = self.mgraph_edit.new_node(node_type = create_data.node_type__timezone, # Create timezone node
value = timezone)

tz_edge = self.mgraph_edit.new_edge(edge_type = create_data.edge_type__tz, # Create edge to timezone
from_node_id = time_point.node_id ,
to_node_id = tz_node.node_id)

if utc_offset is not None: # Add UTC offset if available
offset_node = self.mgraph_edit.new_node(node_type = create_data.node_type__utc_offset,
value = utc_offset)

self.mgraph_edit.new_edge(edge_type = create_data.edge_type__offset, # Link timezone to offset
from_node_id = tz_node.node_id ,
to_node_id = offset_node.node_id)

return tz_node.node_id, tz_edge.edge_id

def add_value_component(self, value : int ,
node_type : Type[Domain__MGraph__Node] ,
edge_type : Type[Schema__MGraph__Edge] ,
from_node : Domain__MGraph__Node
) -> Tuple[Obj_Id, Obj_Id]: # Returns (value_node_id, edge_id)
value_node = self.get_or_create_value_node(value, node_type) # Get or create value node

edge = self.mgraph_edit.new_edge(edge_type = edge_type , # Create edge to value node
from_node_id = from_node.node_id ,
to_node_id = value_node.node_id )

return value_node.node_id, edge.edge_id

def get_or_create_value_node(self, value: int, node_type: Type[Domain__MGraph__Node]) -> Domain__MGraph__Node:
matching_nodes = self.mgraph_index.get_nodes_by_field('value', value) # Check if value node exists
type_nodes = self.mgraph_index.get_nodes_by_type(node_type)
result_nodes = matching_nodes & type_nodes # Find intersection

if result_nodes: # Return existing node if found
return self.mgraph_edit.data().node(next(iter(result_nodes)))

value_node = self.mgraph_edit.new_node(node_type = node_type, # Create new value node if needed
value = value)

self.mgraph_index.add_node(value_node.node.data) # Add to index for future reuse
return value_node
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from mgraph_db.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_db.mgraph.actions.MGraph__Index import MGraph__Index
from mgraph_db.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node
from mgraph_db.providers.time_series.actions.MGraph__Time_Point__Builder import MGraph__Time_Point__Builder
from mgraph_db.providers.time_series.actions.MGraph__Time_Point__Create import MGraph__Time_Point__Create
from mgraph_db.providers.time_series.actions.MGraph__Time_Series__Node__Create import MGraph__Time_Series__Node__Create
from mgraph_db.providers.time_series.actions.MGraph__Time_Series__Node__Find import MGraph__Time_Series__Node__Find
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
Expand All @@ -18,6 +20,13 @@ def create_time_point__with_tz(self, timezone: str = 'UTC', **kwargs ) -> Domain
def create_time_point__from_datetime(self, dt: datetime) -> Domain__MGraph__Node: # Create time point from datetime object
return self.node_create().create_time_point__from_datetime(dt)

def create_time_point__from_datetime_2(self, dt: datetime) -> Domain__MGraph__Node:
time_point_builder = MGraph__Time_Point__Builder()
create_data = time_point_builder.from_datetime(dt)
time_point_create = MGraph__Time_Point__Create(mgraph_edit=self,mgraph_index=self.index())
created_objects = time_point_create.execute(create_data)
return self.data().node(created_objects.time_point_id)

@cache_on_self
def index(self):
return MGraph__Index.from_graph(self.graph)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import datetime, UTC

from mgraph_db.mgraph.domain.Domain__MGraph__Node import Domain__MGraph__Node
from mgraph_db.providers.time_series.MGraph__Time_Series import MGraph__Time_Series
from mgraph_db.providers.time_series.actions.MGraph__Time_Point__Builder import MGraph__Time_Point__Builder
from mgraph_db.providers.time_series.schemas.Schema__MGraph__Time_Point__Create__Data import Schema__MGraph__Time_Point__Create__Data
Expand All @@ -13,6 +15,8 @@ class Flow__Create__MGraph__Time_Point(Flow):
source_id : Obj_Id = None
create_data : Schema__MGraph__Time_Point__Create__Data = None
mgraph_time_series: MGraph__Time_Series = None
time_point : Domain__MGraph__Node = None
png_create : bool = False
png_file_name : str = './flow-time-point.png'

@task()
Expand All @@ -31,18 +35,20 @@ def setup__point__create_data(self):
@task()
def create_points(self):
with self.mgraph_time_series.edit() as _:
_.create_time_point__from_datetime(self.date_time)
#_.create_time_point__from_datetime(self.date_time)
self.time_point = _.create_time_point__from_datetime_2(self.date_time)

# with self.mgraph_time_series.index() as _:
# _.print__stats()

@task()
def create_png(self):
with self.mgraph_time_series.screenshot() as _:
(_.export().export_dot().show_node__value()
.set_edge_to_node__type_fill_color(Schema__MGraph__Time_Series__Edge__Second, 'azure'))
_.save_to(self.png_file_name)
_.dot()
if self.png_create:
with self.mgraph_time_series.screenshot() as _:
(_.export().export_dot().show_node__value()
.set_edge_to_node__type_fill_color(Schema__MGraph__Time_Series__Edge__Second, 'azure'))
_.save_to(self.png_file_name)
_.dot()


def main(self, date_time: datetime=None, source_id: Obj_Id=None):
Expand All @@ -51,6 +57,6 @@ def main(self, date_time: datetime=None, source_id: Obj_Id=None):

self.setup__point__create_data()
self.create_points()
#self.create_png()
self.create_png()


Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Schema__MGraph__Time_Point__Created__Objects(Type_Safe): # The objects that were created

time_point_id : Obj_Id # Node IDs created
value_nodes : Dict[Type[Schema__MGraph__Node__Value__Int], Obj_Id]
value_nodes : Dict[Type[Schema__MGraph__Edge], Obj_Id]
timezone_id : Obj_Id
utc_offset_id : Obj_Id

Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def add_property__add_node_and_edge(self, name:str, value:Any):
_.add_property__update_existing ('a', 42)
total_duration__duration__with__node_and_edge__update_existing += duration__duration__with__node_and_edge__update_existing.seconds
if previous__duration__duration__with__node_and_edge__update_existing:
assert duration__duration__with__node_and_edge__update_existing.seconds + 0.005 > previous__duration__duration__with__node_and_edge__update_existing
assert duration__duration__with__node_and_edge__update_existing.seconds + 0.006 > previous__duration__duration__with__node_and_edge__update_existing

previous__duration__duration__with__node_and_edge__update_existing = duration__duration__with__node_and_edge__update_existing.seconds

Expand Down
Loading

0 comments on commit 07d3fb0

Please sign in to comment.