Skip to content

Commit

Permalink
Timezone aware datetime (#1)
Browse files Browse the repository at this point in the history
* Add timezone to all datetime objects

Issue was occurring where the timestamps in the database
were not compatible with python datetime objects because
they were timezone unaware. UTC was given as the default
timezone to any calls to datetime.now().

* Fix bug where collector skipped very first event

Subtracted the first datetime object by one second to
compensate.

* Update values in preparation for 0.1.1
  • Loading branch information
chasebrewsky authored Dec 11, 2020
1 parent 84ff2f5 commit c6a5a81
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ScryWarden

ScryWarden is a framework for detecting anomalies in general datasets through the use of behavioral profiling. The library provides simple interfaces to design behavioral profiles, to pull messages from datasets, and to report on found anomalies. Its also engineered to horizontally scale as needed for larger datasets.
ScryWarden is a framework for detecting anomalies in general datasets through the use of behavioral models. The library provides simple interfaces to design behavioral profiles, to pull messages from datasets, and to report on found anomalies. Its also engineered to horizontally scale as needed for larger datasets.

It aims to be an accessible anomaly detection method that doesn't require any advanced knowledge on the topic while also allowing those with relevant knowledge to extend it for their needs.

Expand All @@ -20,7 +20,7 @@ This will install a CLI command in your path called `scrywarden`. Try it out by
$ scrywarden --help
Usage: scrywarden [OPTIONS] COMMAND [ARGS]...
Detects anomalies in datasets using behavioral profiling.
Detects anomalies in datasets using behavioral modeling.
Options:
-c, --config TEXT Path to the config file to use. [default:
Expand Down
2 changes: 1 addition & 1 deletion scrywarden/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
@click.pass_context
def main(ctx: Context, **kwargs):
"""Detects anomalies in datasets using behavioral profiling."""
"""Detects anomalies in datasets using behavioral modeling."""
ctx.ensure_object(dict)
ctx.obj['config_file'] = kwargs['config']

Expand Down
4 changes: 2 additions & 2 deletions scrywarden/investigator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import threading
import typing as t
import uuid
from datetime import datetime
from datetime import datetime, timezone
from queue import Queue, Full

import pandas as pa
Expand Down Expand Up @@ -167,7 +167,7 @@ def _investigate(self) -> t.Optional[
logger.debug("\n%s", malicious_anomalies)
with self._session(expire_on_commit=False) as session:
session.add(investigation)
investigation.completed_at = datetime.now()
investigation.completed_at = datetime.now(timezone.utc)
session.flush()
return investigation, malicious_anomalies

Expand Down
8 changes: 5 additions & 3 deletions scrywarden/profile/collectors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import threading
import typing as t
from datetime import timedelta, datetime
from datetime import timedelta, datetime, timezone

import pandas as pa
from sqlalchemy.orm import sessionmaker, Session
Expand Down Expand Up @@ -247,7 +247,7 @@ def _loop_until_anomalies(
def _wait(self, target: datetime) -> bool:
timeout: float = 0.0
while not self.shutdown.wait(timeout):
now = datetime.now()
now = datetime.now(timezone.utc)
logger.debug("Target start time %s current time %s", target, now)
if target + timedelta(seconds=self.delay) <= now:
break
Expand Down Expand Up @@ -297,7 +297,9 @@ def _create_initial_investigation(
first_event = self._get_first_event(profile)
if not first_event:
return pa.DataFrame()
return self._loop_until_anomalies(profile, first_event.created_at)
return self._loop_until_anomalies(
profile, first_event.created_at - timedelta(seconds=1),
)


PARSER = parsers.Options({
Expand Down
6 changes: 4 additions & 2 deletions scrywarden/transport/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing as t
from collections import KeysView, ItemsView, ValuesView
from datetime import datetime
from datetime import datetime, timezone
from uuid import uuid4, UUID

from scrywarden.typing import JSONValue
Expand Down Expand Up @@ -133,7 +133,9 @@ def create(
Message
Created message object.
"""
return cls(id or uuid4(), timestamp or datetime.now(), data)
return cls(
id or uuid4(), timestamp or datetime.now(timezone.utc), data,
)

def __getitem__(self, item: t.Sequence[str]) -> JSONValue:
if isinstance(item, str):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

setup(
name='scrywarden',
version='0.1.0',
version='0.1.1',
author='Chase Brewer',
author_email='chasebrewsky@gmail.com',
description="Detect anomalies in datasets using behavioral profiling",
description="Detect anomalies in datasets using behavioral modeling",
long_description=README,
long_description_content_type='text/markdown',
url='https://github.com/chasebrewsky/scrywarden',
Expand Down

0 comments on commit c6a5a81

Please sign in to comment.