async def get_contacts_for_monitor_group( dbcon: DBConnection, id: int) -> Iterable[object_models.Contact]: q = """select contacts.id, contacts.name, contacts.email, contacts.phone, contacts.active from contacts, monitor_group_contacts where contacts.id=monitor_group_contacts.contact_id and monitor_group_contacts.monitor_group_id=%s""" return [ object_models.Contact(*row) for row in await dbcon.fetch_all(q, (id, )) ]
async def get_contacts_for_metadata( dbcon: DBConnection, meta_key: str, meta_value: str) -> Iterable[object_models.Contact]: q = """select c.id, c.name, c.email, c.phone, c.active from contacts as c, object_metadata as meta where meta.key=%s and meta.value=%s and meta.object_type="contact" and meta.object_id=c.id""" q_args = (meta_key, meta_value) return [ object_models.Contact(*row) for row in await dbcon.fetch_all(q, q_args) ]
async def get_contact( dbcon: DBConnection, id: int) -> Any: # Use any because optional returns suck. """Get a single contact if it exists.""" q = """select id, name, email, phone, active from contacts where id=%s""" q_args = (id, ) row = await dbcon.fetch_row(q, q_args) contact = None if row: contact = object_models.Contact(*row) return contact
async def get_contacts_for_contact_group( dbcon: DBConnection, contact_group_id: int) -> Iterable[object_models.Contact]: """Get contacts for a contact group.""" q = """select contacts.id, contacts.name, contacts.email, contacts.phone, contacts.active from contact_group_contacts, contacts where contact_group_contacts.contact_group_id = %s and contact_group_contacts.contact_id = contacts.id""" return [ object_models.Contact(*row) for row in await dbcon.fetch_all(q, (contact_group_id, )) ]
async def _active_monitor_monitor_group_contacts( dbcon: DBConnection, monitor_id: int) -> Set[object_models.Contact]: # Get contacts connected to the monitor via monitor group -> contacts q = """select contacts.id, contacts.name, contacts.email, contacts.phone, contacts.active from monitor_group_active_monitors left join monitor_groups on monitor_group_active_monitors.monitor_group_id=monitor_groups.id left join monitor_group_contacts on monitor_group_contacts.monitor_group_id=monitor_groups.id left join contacts on contacts.id=monitor_group_contacts.contact_id where monitor_group_active_monitors.active_monitor_id=%s and contacts.active = true""" return { object_models.Contact(*row) for row in await dbcon.fetch_all(q, (monitor_id, )) }
async def _active_monitor_contacts( dbcon: DBConnection, monitor_id: int) -> Set[object_models.Contact]: # Get contacts directly connected to the monitor. q = """select contacts.id, contacts.name, contacts.email, contacts.phone, contacts.active from active_monitor_contacts, contacts where active_monitor_contacts.active_monitor_id = %s and active_monitor_contacts.contact_id = contacts.id and contacts.active = true""" return { object_models.Contact(*row) for row in await dbcon.fetch_all(q, (monitor_id, )) }
async def get_contacts_for_active_monitor( dbcon: DBConnection, monitor_id: int) -> Iterable[object_models.Contact]: """Get contacts for an active monitor. Return a list of dicts, one dict describing each contacts information. """ q = """select contacts.id, contacts.name, contacts.email, contacts.phone, contacts.active from active_monitor_contacts, contacts where active_monitor_contacts.active_monitor_id = %s and active_monitor_contacts.contact_id = contacts.id""" contacts = [ object_models.Contact(*row) for row in await dbcon.fetch_all(q, (monitor_id, )) ] return contacts
async def get_all_contacts( dbcon: DBConnection) -> Iterable[object_models.Contact]: """Get all contacts""" q = """select id, name, email, phone, active from contacts""" return [object_models.Contact(*row) for row in await dbcon.fetch_all(q)]