# Path: narrative/batteries/uptime.py
# class UptimeEventTypes:
#     UP = 0
#     DOWN = 1
# def create_heartbeat(get_utc_now=datetime.datetime.utcnow):
# def compute_interval_list(time_list):
# def detect_temporal_clusters(time_list):
# def get_uptime_history(utcnow=datetime.datetime.utcnow):
#     def unix_timestamp(dt):
#     def from_unix_timestamp(timestamp):
# def time_site_came_up(utcnow=datetime.datetime.utcnow):
# 
# Path: narrative/models.py
# class Datum(models.Model):
#     # When was the datum created
#     timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
#     expiration_time = models.DateTimeField(null=True, blank=True, default=None)
#     # Origin of this datum; ie, what piece of software created it
#     origin = models.CharField(max_length=64)
#     datum_name = models.CharField(max_length=64)
# 
#     # Additional information about the datum; this is very datum specific
#     # This approach, storing json in a textfield and having an
#     # explicit accessor and mutator, is very ugly.  Really this is because
#     # we are storing non-structured/sparse data in a relational format.
#     datum_note_json = models.TextField(null=True, blank=True, default=None)
# 
#     # An ID to tie particular datums together
#     thread_id = models.CharField(max_length=36, null=True, blank=True)
# 
#     log_level = models.IntegerField(choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = DatumManager()
# 
#     def __init__(self, *args, **kwargs):
#         if 'ttl' in kwargs:
#             kwargs['expiration_time'] = self.get_utc_now() + kwargs.pop('ttl')
# 
#         super(Datum, self).__init__(*args, **kwargs)
# 
#     def __str__(self):
#         note_snippet = self.datum_note_json[:50] if self.datum_note_json else ''
#         return 'origin:{0} datum_name:{1} note:{2}'.format(
#             self.origin, self.datum_name, note_snippet
#         )
# 
#     def get_note(self):
#         if self.datum_note_json:
#             return json.loads(self.datum_note_json)
#         else:
#             return []
# 
#     def set_note(self, note):
#         self.datum_note_json = json.dumps(note)
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
#     @property
#     def timestamp_with_tz(self):
#         return utc_tz.localize(self.timestamp)
# 
#     def save(self, *args, **kwargs):
#         if None == self.thread_id:
#             self.thread_id = str(uuid.uuid4())
# 
#         super(Datum, self).save(*args, **kwargs)


import datetime
import unittest
from narrative.batteries.uptime import detect_temporal_clusters, heartbeat_details, get_uptime_history, UptimeEventTypes
from narrative.models import Datum



def simulate_uptime(start_time, end_time, interval=None):
    """
    Create the needed heartbeats to simulate uptime during the
    specified time range.
    """
    interval = interval or datetime.timedelta(seconds=20)

    interval_seconds = int(interval.total_seconds())

    for beat_time in range(
            int(start_time.strftime('%s')),
            int(end_time.strftime('%s')) + interval_seconds,
            interval_seconds):
        # Create an datum

# Path: narrative/batteries/uptime.py
# class UptimeEventTypes:
#     UP = 0
#     DOWN = 1
# def create_heartbeat(get_utc_now=datetime.datetime.utcnow):
# def compute_interval_list(time_list):
# def detect_temporal_clusters(time_list):
# def get_uptime_history(utcnow=datetime.datetime.utcnow):
#     def unix_timestamp(dt):
#     def from_unix_timestamp(timestamp):
# def time_site_came_up(utcnow=datetime.datetime.utcnow):
# 
# Path: narrative/models.py
# class Datum(models.Model):
#     # When was the datum created
#     timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
#     expiration_time = models.DateTimeField(null=True, blank=True, default=None)
#     # Origin of this datum; ie, what piece of software created it
#     origin = models.CharField(max_length=64)
#     datum_name = models.CharField(max_length=64)
# 
#     # Additional information about the datum; this is very datum specific
#     # This approach, storing json in a textfield and having an
#     # explicit accessor and mutator, is very ugly.  Really this is because
#     # we are storing non-structured/sparse data in a relational format.
#     datum_note_json = models.TextField(null=True, blank=True, default=None)
# 
#     # An ID to tie particular datums together
#     thread_id = models.CharField(max_length=36, null=True, blank=True)
# 
#     log_level = models.IntegerField(choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = DatumManager()
# 
#     def __init__(self, *args, **kwargs):
#         if 'ttl' in kwargs:
#             kwargs['expiration_time'] = self.get_utc_now() + kwargs.pop('ttl')
# 
#         super(Datum, self).__init__(*args, **kwargs)
# 
#     def __str__(self):
#         note_snippet = self.datum_note_json[:50] if self.datum_note_json else ''
#         return 'origin:{0} datum_name:{1} note:{2}'.format(
#             self.origin, self.datum_name, note_snippet
#         )
# 
#     def get_note(self):
#         if self.datum_note_json:
#             return json.loads(self.datum_note_json)
#         else:
#             return []
# 
#     def set_note(self, note):
#         self.datum_note_json = json.dumps(note)
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
#     @property
#     def timestamp_with_tz(self):
#         return utc_tz.localize(self.timestamp)
# 
#     def save(self, *args, **kwargs):
#         if None == self.thread_id:
#             self.thread_id = str(uuid.uuid4())
# 
#         super(Datum, self).save(*args, **kwargs)


# Path: narrative/batteries/tests/uptime_tests.py
import datetime
import unittest
from narrative.batteries.uptime import detect_temporal_clusters, heartbeat_details, get_uptime_history, UptimeEventTypes
from narrative.models import Datum




def simulate_uptime(start_time, end_time, interval=None):
    """
    Create the needed heartbeats to simulate uptime during the
    specified time range.
    """
    interval = interval or datetime.timedelta(seconds=20)

    interval_seconds = int(interval.total_seconds())

    for beat_time in range(
            int(start_time.strftime('%s')),
            int(end_time.strftime('%s')) + interval_seconds,
            interval_seconds):
        # Create an datum

        evt = Datum.objects.create(