Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b77a775

Browse files
committedNov 27, 2023
Add DateTimeField to cache DB models; Add create_instance and adjust create_program methods on LightNode
1 parent 6f286fc commit b77a775

File tree

7 files changed

+75
-12
lines changed

7 files changed

+75
-12
lines changed
 

‎src/aleph/sdk/client/light_node.py

+57
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ async def create_program(
319319
vcpus: Optional[int] = None,
320320
timeout_seconds: Optional[float] = None,
321321
persistent: bool = False,
322+
allow_amend: bool = False,
323+
internet: bool = True,
324+
aleph_api: bool = True,
322325
encoding: Encoding = Encoding.zip,
323326
volumes: Optional[List[Mapping]] = None,
324327
subscriptions: Optional[List[Mapping]] = None,
@@ -340,6 +343,9 @@ async def create_program(
340343
vcpus=vcpus,
341344
timeout_seconds=timeout_seconds,
342345
persistent=persistent,
346+
allow_amend=allow_amend,
347+
internet=internet,
348+
aleph_api=aleph_api,
343349
encoding=encoding,
344350
volumes=volumes,
345351
subscriptions=subscriptions,
@@ -350,6 +356,57 @@ async def create_program(
350356
asyncio.create_task(self.delete_if_rejected(resp.item_hash))
351357
return resp, status
352358

359+
async def create_instance(
360+
self,
361+
rootfs: str,
362+
rootfs_size: int,
363+
rootfs_name: str,
364+
environment_variables: Optional[Mapping[str, str]] = None,
365+
storage_engine: StorageEnum = StorageEnum.storage,
366+
channel: Optional[str] = None,
367+
address: Optional[str] = None,
368+
sync: bool = False,
369+
memory: Optional[int] = None,
370+
vcpus: Optional[int] = None,
371+
timeout_seconds: Optional[float] = None,
372+
allow_amend: bool = False,
373+
internet: bool = True,
374+
aleph_api: bool = True,
375+
encoding: Encoding = Encoding.zip,
376+
volumes: Optional[List[Mapping]] = None,
377+
volume_persistence: str = "host",
378+
ssh_keys: Optional[List[str]] = None,
379+
metadata: Optional[Mapping[str, Any]] = None,
380+
) -> Tuple[AlephMessage, MessageStatus]:
381+
self.check_validity(
382+
MessageType.instance, address, channel, dict(metadata) if metadata else None
383+
)
384+
resp, status = await self.session.create_instance(
385+
rootfs=rootfs,
386+
rootfs_size=rootfs_size,
387+
rootfs_name=rootfs_name,
388+
environment_variables=environment_variables,
389+
storage_engine=storage_engine,
390+
channel=channel,
391+
address=address,
392+
sync=sync,
393+
memory=memory,
394+
vcpus=vcpus,
395+
timeout_seconds=timeout_seconds,
396+
allow_amend=allow_amend,
397+
internet=internet,
398+
aleph_api=aleph_api,
399+
encoding=encoding,
400+
volumes=volumes,
401+
volume_persistence=volume_persistence,
402+
ssh_keys=ssh_keys,
403+
metadata=metadata,
404+
)
405+
if status in [MessageStatus.PENDING, MessageStatus.PROCESSED]:
406+
self.add(resp)
407+
asyncio.create_task(self.delete_if_rejected(resp.item_hash))
408+
return resp, status
409+
353410
async def forget(
354411
self,
355412
hashes: List[str],

‎src/aleph/sdk/client/message_cache.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import datetime
12
import logging
23
import typing
3-
from datetime import datetime
44
from pathlib import Path
55
from typing import (
66
AsyncIterable,
@@ -230,7 +230,7 @@ def _handle_amends(self, amend_messages: List[PostMessage]):
230230
self.missing_posts[ItemHash(amend.content.ref)] = amend
231231
continue
232232

233-
if datetime.fromtimestamp(amend.time) < original_post.last_updated:
233+
if amend.time < original_post.last_updated:
234234
continue
235235

236236
original_post.item_hash = amend.item_hash
@@ -239,7 +239,7 @@ def _handle_amends(self, amend_messages: List[PostMessage]):
239239
original_post.original_type = amend.content.type
240240
original_post.address = amend.sender
241241
original_post.channel = amend.channel
242-
original_post.last_updated = datetime.fromtimestamp(amend.time)
242+
original_post.last_updated = amend.time
243243
post_data.append(model_to_dict(original_post))
244244
with self.db.atomic():
245245
PostDBModel.insert_many(post_data).on_conflict_replace().execute()
@@ -254,7 +254,9 @@ def _handle_aggregates(self, aggregate_messages):
254254
if not existing_aggregate:
255255
aggregate_data.append(aggregate_to_model(aggregate))
256256
continue
257-
data = model_to_dict(existing_aggregate)
257+
existing_aggregate.time = datetime.datetime.fromisoformat(
258+
existing_aggregate.time
259+
)
258260
if aggregate.time > existing_aggregate.time:
259261
existing_aggregate.content.update(aggregate.content.content)
260262
existing_aggregate.time = aggregate.time

‎src/aleph/sdk/db/aggregate.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict
22

33
from aleph_message.models import AggregateMessage
4-
from peewee import CharField, FloatField, Model
4+
from peewee import CharField, DateTimeField, Model
55
from playhouse.sqlite_ext import JSONField
66

77
from .common import pydantic_json_dumps
@@ -17,7 +17,7 @@ class AggregateDBModel(Model):
1717
key = CharField()
1818
channel = CharField(null=True)
1919
content = JSONField(json_dumps=pydantic_json_dumps, null=True)
20-
time = FloatField()
20+
time = DateTimeField()
2121

2222

2323
def aggregate_to_model(message: AggregateMessage) -> Dict:

‎src/aleph/sdk/db/message.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from aleph_message import parse_message
44
from aleph_message.models import AlephMessage, MessageConfirmation
5-
from peewee import BooleanField, CharField, FloatField, IntegerField, Model
5+
from peewee import BooleanField, CharField, DateTimeField, IntegerField, Model
66
from playhouse.shortcuts import model_to_dict
77
from playhouse.sqlite_ext import JSONField
88

@@ -26,7 +26,7 @@ class MessageDBModel(Model):
2626
confirmed = BooleanField(null=True)
2727
signature = CharField(null=True)
2828
size = IntegerField(null=True)
29-
time = FloatField()
29+
time = DateTimeField()
3030
item_type = CharField(7)
3131
item_content = CharField(null=True)
3232
hash_type = CharField(6, null=True)

‎src/aleph/sdk/db/post.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, Iterable
22

33
from aleph_message.models import MessageConfirmation, PostMessage
4-
from peewee import BooleanField, CharField, FloatField, IntegerField, Model
4+
from peewee import BooleanField, CharField, DateTimeField, IntegerField, Model
55
from playhouse.shortcuts import model_to_dict
66
from playhouse.sqlite_ext import JSONField
77

@@ -29,7 +29,7 @@ class PostDBModel(Model):
2929
confirmed = BooleanField()
3030
signature = CharField()
3131
size = IntegerField(null=True)
32-
time = FloatField()
32+
time = DateTimeField()
3333
item_type = CharField(7)
3434
item_content = CharField(null=True)
3535
content = JSONField(json_dumps=pydantic_json_dumps)

‎src/aleph/sdk/query/responses.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from datetime import datetime
34
from typing import Any, Dict, List, Optional, Union
45

56
from aleph_message.models import (
@@ -35,7 +36,7 @@ class Post(BaseModel):
3536
description="Cryptographic signature of the message by the sender"
3637
)
3738
size: int = Field(description="Size of the post")
38-
time: float = Field(description="Timestamp of the post")
39+
time: datetime = Field(description="Timestamp of the post")
3940
confirmations: List[MessageConfirmation] = Field(
4041
description="Number of confirmations"
4142
)

‎tests/unit/conftest.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ async def __aenter__(self):
154154
async def __aexit__(self, exc_type, exc_val, exc_tb):
155155
...
156156

157-
async def raise_for_status(self):
157+
def raise_for_status(self):
158+
...
159+
160+
async def close(self):
158161
...
159162

160163
@property

0 commit comments

Comments
 (0)
Please sign in to comment.