# Path: alertaclient/models/heartbeat.py
# class Heartbeat:
# 
#     def __init__(self, origin=None, tags=None, create_time=None, timeout=None, customer=None, **kwargs):
#         if any(['.' in key for key in kwargs.get('attributes', dict()).keys()])\
#                 or any(['$' in key for key in kwargs.get('attributes', dict()).keys()]):
#             raise ValueError('Attribute keys must not contain "." or "$"')
# 
#         self.id = kwargs.get('id', None)
#         self.origin = origin
#         self.status = kwargs.get('status', None) or 'unknown'
#         self.tags = tags or list()
#         self.attributes = kwargs.get('attributes', None) or dict()
#         self.event_type = kwargs.get('event_type', kwargs.get('type', None)) or 'Heartbeat'
#         self.create_time = create_time
#         self.timeout = timeout
#         self.max_latency = kwargs.get('max_latency', None) or DEFAULT_MAX_LATENCY
#         self.receive_time = kwargs.get('receive_time', None)
#         self.customer = customer
# 
#     @property
#     def latency(self):
#         return int((self.receive_time - self.create_time).total_seconds() * 1000)
# 
#     @property
#     def since(self):
#         since = datetime.utcnow() - self.receive_time
#         return since - timedelta(microseconds=since.microseconds)
# 
#     def __repr__(self):
#         return 'Heartbeat(id={!r}, origin={!r}, create_time={!r}, timeout={!r}, customer={!r})'.format(
#             self.id, self.origin, self.create_time, self.timeout, self.customer)
# 
#     @classmethod
#     def parse(cls, json):
#         if not isinstance(json.get('tags', []), list):
#             raise ValueError('tags must be a list')
#         if not isinstance(json.get('attributes', {}), dict):
#             raise ValueError('attributes must be a JSON object')
#         if not isinstance(json.get('timeout', 0), int):
#             raise ValueError('timeout must be an integer')
# 
#         return Heartbeat(
#             id=json.get('id', None),
#             origin=json.get('origin', None),
#             status=json.get('status', None),
#             tags=json.get('tags', list()),
#             attributes=json.get('attributes', dict()),
#             event_type=json.get('type', None),
#             create_time=DateTime.parse(json.get('createTime')),
#             timeout=json.get('timeout', None),
#             max_latency=json.get('maxLatency', None) or DEFAULT_MAX_LATENCY,
#             receive_time=DateTime.parse(json.get('receiveTime')),
#             customer=json.get('customer', None)
#         )
# 
#     def tabular(self, timezone=None):
#         return {
#             'id': self.id,
#             'origin': self.origin,
#             'customer': self.customer,
#             'tags': ','.join(self.tags),
#             'attributes': self.attributes,
#             'createTime': DateTime.localtime(self.create_time, timezone),
#             'receiveTime': DateTime.localtime(self.receive_time, timezone),
#             'since': self.since,
#             'timeout': f'{self.timeout}s',
#             'latency': f'{self.latency:.0f}ms',
#             'maxLatency': f'{self.max_latency}ms',
#             'status': self.status
#         }
# 
# Path: alertaclient/utils.py
# def origin():
#     prog = os.path.basename(sys.argv[0])
#     return f'{prog}/{platform.uname()[1]}'


import json
import click
from tabulate import tabulate
from alertaclient.models.heartbeat import Heartbeat
from alertaclient.utils import origin
        click.echo(tabulate([h.tabular(timezone) for h in heartbeats], headers=headers, tablefmt=obj['output']))

    not_ok = [hb for hb in heartbeats if hb.status != 'ok']
    if purge:
        with click.progressbar(not_ok, label=f'Purging {len(not_ok)} heartbeats') as bar:
            for b in bar:
                client.delete_heartbeat(b.id)

    if alert:
        with click.progressbar(heartbeats, label=f'Alerting {len(heartbeats)} heartbeats') as bar:
            for b in bar:

                want_environment = b.attributes.pop('environment', 'Production')
                want_severity = b.attributes.pop('severity', severity)
                want_service = b.attributes.pop('service', ['Alerta'])
                want_group = b.attributes.pop('group', 'System')

                if b.status == 'expired':  # aka. "stale"
                    client.send_alert(
                        resource=b.origin,
                        event='HeartbeatFail',
                        environment=want_environment,
                        severity=want_severity,
                        correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
                        service=want_service,
                        group=want_group,
                        value=f'{b.since}',
                        text=f'Heartbeat not received in {b.timeout} seconds',
                        tags=b.tags,
                        attributes=b.attributes,

# Path: alertaclient/models/heartbeat.py
# class Heartbeat:
# 
#     def __init__(self, origin=None, tags=None, create_time=None, timeout=None, customer=None, **kwargs):
#         if any(['.' in key for key in kwargs.get('attributes', dict()).keys()])\
#                 or any(['$' in key for key in kwargs.get('attributes', dict()).keys()]):
#             raise ValueError('Attribute keys must not contain "." or "$"')
# 
#         self.id = kwargs.get('id', None)
#         self.origin = origin
#         self.status = kwargs.get('status', None) or 'unknown'
#         self.tags = tags or list()
#         self.attributes = kwargs.get('attributes', None) or dict()
#         self.event_type = kwargs.get('event_type', kwargs.get('type', None)) or 'Heartbeat'
#         self.create_time = create_time
#         self.timeout = timeout
#         self.max_latency = kwargs.get('max_latency', None) or DEFAULT_MAX_LATENCY
#         self.receive_time = kwargs.get('receive_time', None)
#         self.customer = customer
# 
#     @property
#     def latency(self):
#         return int((self.receive_time - self.create_time).total_seconds() * 1000)
# 
#     @property
#     def since(self):
#         since = datetime.utcnow() - self.receive_time
#         return since - timedelta(microseconds=since.microseconds)
# 
#     def __repr__(self):
#         return 'Heartbeat(id={!r}, origin={!r}, create_time={!r}, timeout={!r}, customer={!r})'.format(
#             self.id, self.origin, self.create_time, self.timeout, self.customer)
# 
#     @classmethod
#     def parse(cls, json):
#         if not isinstance(json.get('tags', []), list):
#             raise ValueError('tags must be a list')
#         if not isinstance(json.get('attributes', {}), dict):
#             raise ValueError('attributes must be a JSON object')
#         if not isinstance(json.get('timeout', 0), int):
#             raise ValueError('timeout must be an integer')
# 
#         return Heartbeat(
#             id=json.get('id', None),
#             origin=json.get('origin', None),
#             status=json.get('status', None),
#             tags=json.get('tags', list()),
#             attributes=json.get('attributes', dict()),
#             event_type=json.get('type', None),
#             create_time=DateTime.parse(json.get('createTime')),
#             timeout=json.get('timeout', None),
#             max_latency=json.get('maxLatency', None) or DEFAULT_MAX_LATENCY,
#             receive_time=DateTime.parse(json.get('receiveTime')),
#             customer=json.get('customer', None)
#         )
# 
#     def tabular(self, timezone=None):
#         return {
#             'id': self.id,
#             'origin': self.origin,
#             'customer': self.customer,
#             'tags': ','.join(self.tags),
#             'attributes': self.attributes,
#             'createTime': DateTime.localtime(self.create_time, timezone),
#             'receiveTime': DateTime.localtime(self.receive_time, timezone),
#             'since': self.since,
#             'timeout': f'{self.timeout}s',
#             'latency': f'{self.latency:.0f}ms',
#             'maxLatency': f'{self.max_latency}ms',
#             'status': self.status
#         }
# 
# Path: alertaclient/utils.py
# def origin():
#     prog = os.path.basename(sys.argv[0])
#     return f'{prog}/{platform.uname()[1]}'


# Path: alertaclient/commands/cmd_heartbeats.py
import json
import click
from tabulate import tabulate
from alertaclient.models.heartbeat import Heartbeat
from alertaclient.utils import origin

        click.echo(tabulate([h.tabular(timezone) for h in heartbeats], headers=headers, tablefmt=obj['output']))

    not_ok = [hb for hb in heartbeats if hb.status != 'ok']
    if purge:
        with click.progressbar(not_ok, label=f'Purging {len(not_ok)} heartbeats') as bar:
            for b in bar:
                client.delete_heartbeat(b.id)

    if alert:
        with click.progressbar(heartbeats, label=f'Alerting {len(heartbeats)} heartbeats') as bar:
            for b in bar:

                want_environment = b.attributes.pop('environment', 'Production')
                want_severity = b.attributes.pop('severity', severity)
                want_service = b.attributes.pop('service', ['Alerta'])
                want_group = b.attributes.pop('group', 'System')

                if b.status == 'expired':  # aka. "stale"
                    client.send_alert(
                        resource=b.origin,
                        event='HeartbeatFail',
                        environment=want_environment,
                        severity=want_severity,
                        correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
                        service=want_service,
                        group=want_group,
                        value=f'{b.since}',
                        text=f'Heartbeat not received in {b.timeout} seconds',
                        tags=b.tags,
                        attributes=b.attributes,

                        origin=origin(),