Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

41-unique-log-id #47

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion distill/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pydantic import BaseModel
from pydantic.type_adapter import TypeAdapter
from typing import Dict, Union
from pksuid import PKSUID

from distill.core.types import JsonDict, JSONSerializable
from distill.schemas.userale import UserAleSchema
Expand All @@ -39,14 +40,17 @@ def __init__(self, data: Union[str, JsonDict], schema=UserAleSchema):

if isinstance(data, str):
schema.model_validate_json(data, strict=True)
hash_sfx = str(hash(data))
data = json.loads(data)
elif ta.validate_python(data):
hash_sfx = str(hash(json.dumps(data)))
schema.model_validate(data, strict=True)
else:
raise TypeError("ERROR: " + str(type(data)) + " data should be either a string or a JsonDict")
self.data = schema(**data)

# TODO: need to create ID field here on object initialization
self.id = PKSUID("log_" + hash_sfx, schema._timestamp(self.data))


def to_json(self) -> str:
return self.data.model_dump_json(by_alias=True)
Expand Down
31 changes: 31 additions & 0 deletions distill/schemas/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import abc

from enum import Enum
from typing import List, Optional

from pydantic import AliasGenerator, BaseModel, Field
from pydantic.alias_generators import to_camel
from pydantic.config import ConfigDict


class BaseSchema(BaseModel, abc.ABC):
EandrewJones marked this conversation as resolved.
Show resolved Hide resolved
@property
@abc.abstractmethod
def _timestamp(self):
pass
EandrewJones marked this conversation as resolved.
Show resolved Hide resolved

7 changes: 6 additions & 1 deletion distill/schemas/userale.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Optional
from datetime import datetime

from pydantic import AliasGenerator, BaseModel, Field, field_serializer, field_validator
from pydantic.alias_generators import to_camel
from pydantic.config import ConfigDict

from .base import BaseSchema
from datetime import datetime


Expand All @@ -40,7 +43,7 @@ class Details(BaseModel):
window: bool


class UserAleSchema(BaseModel):
class UserAleSchema(BaseSchema):
"""
A raw or custom log produced by UserAle
"""
Expand Down Expand Up @@ -80,3 +83,5 @@ def validate_ct(cls, ct: float):
def serialize_ct(self, ct: datetime):
EandrewJones marked this conversation as resolved.
Show resolved Hide resolved
return int(ct.timestamp() * 1000)

def _timestamp(self):
EandrewJones marked this conversation as resolved.
Show resolved Hide resolved
return self.client_time.timestamp()
97 changes: 95 additions & 2 deletions poetry.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ matplotlib = "3.7.5"
scipy = "1.9.3"
pydantic = "^2.7.4"
dateparser = "^1.2.0"
pksuid = "^1.1.2"
datetime = "^5.5"

[tool.poetry.group.docs]
optional = true
Expand Down
4 changes: 4 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def test_log_constructor():
pageUrl = test_log.data.page_url
assert pageUrl == "https://github.com/apache/flagon/tree/master/docker"

id = test_log.id
EandrewJones marked this conversation as resolved.
Show resolved Hide resolved
assert id.get_timestamp() == 1719530111079 // 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as this passes it is fine, but I do not use floor division when parsing the timestamp. Just a nit pick though

assert id.prefix.startswith("log_")


def test_log_serialize():
data = load_log()
Expand Down