Skip to content

Commit

Permalink
Change Timestamp to datetime
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Wang <[email protected]>
  • Loading branch information
felixwang9817 committed Jul 27, 2021
1 parent 4e39bad commit 681b126
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
31 changes: 18 additions & 13 deletions sdk/python/feast/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
from typing import Dict, Optional

import yaml
from google.protobuf import json_format
from google.protobuf.json_format import MessageToDict, MessageToJson
from google.protobuf.timestamp_pb2 import Timestamp

from feast.loaders import yaml as feast_yaml
from feast.protos.feast.core.Entity_pb2 import Entity as EntityV2Proto
Expand Down Expand Up @@ -46,8 +46,8 @@ class Entity:
_description: str
_join_key: str
_labels: Dict[str, str]
_created_timestamp: Optional[Timestamp]
_last_updated_timestamp: Optional[Timestamp]
_created_timestamp: Optional[datetime]
_last_updated_timestamp: Optional[datetime]

@log_exceptions
def __init__(
Expand All @@ -72,8 +72,8 @@ def __init__(
else:
self._labels = labels

self._created_timestamp: Optional[Timestamp] = None
self._last_updated_timestamp: Optional[Timestamp] = None
self._created_timestamp: Optional[datetime] = None
self._last_updated_timestamp: Optional[datetime] = None

def __eq__(self, other):
if not isinstance(other, Entity):
Expand Down Expand Up @@ -164,14 +164,14 @@ def labels(self, labels: Dict[str, str]):
self._labels = labels

@property
def created_timestamp(self) -> Optional[Timestamp]:
def created_timestamp(self) -> Optional[datetime]:
"""
Gets the created_timestamp of this entity.
"""
return self._created_timestamp

@property
def last_updated_timestamp(self) -> Optional[Timestamp]:
def last_updated_timestamp(self) -> Optional[datetime]:
"""
Gets the last_updated_timestamp of this entity.
"""
Expand Down Expand Up @@ -238,8 +238,12 @@ def from_proto(cls, entity_proto: EntityV2Proto):
join_key=entity_proto.spec.join_key,
)

entity._created_timestamp = entity_proto.meta.created_timestamp
entity._last_updated_timestamp = entity_proto.meta.last_updated_timestamp
if entity_proto.meta.HasField("created_timestamp"):
entity._created_timestamp = entity_proto.meta.created_timestamp.ToDatetime()
if entity_proto.meta.HasField("last_updated_timestamp"):
entity._last_updated_timestamp = (
entity_proto.meta.last_updated_timestamp.ToDatetime()
)

return entity

Expand All @@ -250,10 +254,11 @@ def to_proto(self) -> EntityV2Proto:
Returns:
An EntityV2Proto protobuf.
"""
meta = EntityMetaProto(
created_timestamp=self.created_timestamp,
last_updated_timestamp=self.last_updated_timestamp,
)
meta = EntityMetaProto()
if self._created_timestamp:
meta.created_timestamp.FromDatetime(self._created_timestamp)
if self._last_updated_timestamp:
meta.last_updated_timestamp.FromDatetime(self._last_updated_timestamp)

spec = EntitySpecProto(
name=self.name,
Expand Down
27 changes: 18 additions & 9 deletions sdk/python/feast/feature_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from google.protobuf.duration_pb2 import Duration
from google.protobuf.json_format import MessageToJson
from google.protobuf.timestamp_pb2 import Timestamp

from feast import utils
from feast.data_source import DataSource
Expand Down Expand Up @@ -71,8 +70,8 @@ class FeatureView:
input: DataSource
batch_source: DataSource
stream_source: Optional[DataSource] = None
created_timestamp: Optional[Timestamp] = None
last_updated_timestamp: Optional[Timestamp] = None
created_timestamp: Optional[datetime] = None
last_updated_timestamp: Optional[datetime] = None
materialization_intervals: List[Tuple[datetime, datetime]]

@log_exceptions
Expand Down Expand Up @@ -133,6 +132,9 @@ def __init__(

self.materialization_intervals = []

self.created_timestamp: Optional[datetime] = None
self.last_updated_timestamp: Optional[datetime] = None

def __repr__(self):
items = (f"{k} = {v}" for k, v in self.__dict__.items())
return f"<{self.__class__.__name__}({', '.join(items)})>"
Expand Down Expand Up @@ -198,11 +200,11 @@ def to_proto(self) -> FeatureViewProto:
Returns:
A FeatureViewProto protobuf.
"""
meta = FeatureViewMetaProto(
created_timestamp=self.created_timestamp,
last_updated_timestamp=self.last_updated_timestamp,
materialization_intervals=[],
)
meta = FeatureViewMetaProto(materialization_intervals=[])
if self.created_timestamp:
meta.created_timestamp.FromDatetime(self.created_timestamp)
if self.last_updated_timestamp:
meta.last_updated_timestamp.FromDatetime(self.last_updated_timestamp)
for interval in self.materialization_intervals:
interval_proto = MaterializationIntervalProto()
interval_proto.start_time.FromDatetime(interval[0])
Expand Down Expand Up @@ -276,7 +278,14 @@ def from_proto(cls, feature_view_proto: FeatureViewProto):
stream_source=stream_source,
)

feature_view.created_timestamp = feature_view_proto.meta.created_timestamp
if feature_view_proto.meta.HasField("created_timestamp"):
feature_view.created_timestamp = (
feature_view_proto.meta.created_timestamp.ToDatetime()
)
if feature_view_proto.meta.HasField("last_updated_timestamp"):
feature_view.last_updated_timestamp = (
feature_view_proto.meta.last_updated_timestamp.ToDatetime()
)

for interval in feature_view_proto.meta.materialization_intervals:
feature_view.materialization_intervals.append(
Expand Down

0 comments on commit 681b126

Please sign in to comment.