def format_job(job): """Turns job (from `format_scheduler`) -> slack attachment.""" attachment_fields = [ { 'title': "Entrypoint", "value": job.name, 'short': utils.is_short(job.name), }, { "title": "State", "value": job.state, "short": True, }, { "title": "Trigger", "short": utils.is_short(job.trigger), "value": job.trigger, }, ] if job.runs_in is not None: job_runs_in = utils.format_seconds(job.runs_in) attachment_fields.append({ "title": "Runs in", "value": job_runs_in, "short": utils.is_short(job_runs_in), }) return { "pretext": u"• Job `%s`" % (job.id), 'mrkdwn_in': ['pretext'], 'text': job.description, 'fields': attachment_fields, }
def _format_handler(handler): h_message_kind = reflection.get_class_name(handler.message) h_state = handler.state h_fields = [ { 'title': 'State', 'value': h_state, 'short': utils.is_short(h_state), }, { 'title': 'Message type', 'value': h_message_kind, 'short': utils.is_short(h_message_kind), }, ] h_elapsed = None try: h_elapsed = handler.watch.elapsed() except RuntimeError: pass if h_elapsed is not None: h_elapsed = utils.format_seconds(h_elapsed) h_fields.append({ 'title': 'Elapsed', 'value': h_elapsed, 'short': utils.is_short(h_elapsed), }) try: h_started_user = handler.message.body.user_name except AttributeError: pass else: if h_started_user: h_fields.append({ 'title': 'Started by', 'value': h_started_user, 'short': utils.is_short(h_started_user), }) try: h_quick_link = handler.message.body.quick_link except AttributeError: pass else: if h_quick_link: h_fields.append({ 'title': 'Link', 'value': h_quick_link, 'short': utils.is_short(h_quick_link), }) return { "pretext": u"• Handler `%s`" % reflection.get_class_name(handler), 'mrkdwn_in': ['pretext'], 'fields': h_fields, }
def _build_attachment(self, event, event_color, message_text): maybe_fields = [] maybe_attrs = [ ("action", ""), ("check.name", "Event name"), ("check.type", "Event type"), ("silenced", ""), ] for attr_name, pretty_name in maybe_attrs: value_found, value = _get_deep_attr(event, attr_name) if value_found: if not pretty_name: pretty_name = attr_name.title() value = str(value) maybe_fields.append({ "title": pretty_name, "value": value, "short": utils.is_short(value), }) attachment = { "footer": "Sensu", "footer_icon": ("https://raw.githubusercontent.com/sensu/" "sensu-logo/master/" "sensu1_flat%20white%20bg_png.png"), "color": event_color, "text": message_text, 'mrkdwn_in': ["text"], } if maybe_fields: attachment['fields'] = maybe_fields return attachment
def _format_child(self, child): try: child_pretext = "%s/%s" % (child.parent.name, child.path.name) except AttributeError: child_pretext = "%s" % child.path.name attachment = { 'pretext': child_pretext, 'mrkdwn_in': [], 'footer': "Artifactory", 'footer_icon': ART_ICON, } tot_size = utils.format_bytes(child.total_size) attachment['fields'] = [ { 'title': 'Size', 'value': tot_size, 'short': utils.is_short(tot_size), }, { "title": "Created on", "value": _format_dt(child.ctime), "short": True, }, ] return attachment
def _build_attachments(cls, mem, memory_low=True): attachments = [] if memory_low: fields = [] for k in ('total', 'available', 'free', 'used', 'active', 'inactive'): try: tmp_v = utils.format_bytes(getattr(mem, k)) except AttributeError: pass else: fields.append({ 'title': k.title(), 'value': tmp_v, 'short': utils.is_short(tmp_v), }) attachment = { 'pretext': ':dangeeerrrzonnneee: Memory low!', 'mrkdwn_in': ["pretext"], 'fields': fields, } if mem.available <= cls.REALLY_LOW_MEMORY: attachment['color'] = su.COLORS.red else: attachment['color'] = su.COLORS.orange attachments.append(attachment) return attachments
def _format_pr_fields(gh_pr, now=None): fields = [ { "title": "State", "value": str(gh_pr.state.title()), "short": True, }, { "title": "Mergeable", "value": str(gh_pr.mergeable), "short": True, }, ] if gh_pr.additions > 0: add_val = "+" + str(gh_pr.additions) else: add_val = "0" if gh_pr.deletions > 0: del_val = "-" + str(gh_pr.deletions) else: del_val = "0" fields.extend([ { "title": "Additions", "value": add_val, "short": True, }, { "title": "Deletions", "value": del_val, "short": True, }, ]) if now is not None: created_at_diff = now - gh_pr.created_at created_at_diff_secs = created_at_diff.total_seconds() created_at = utils.format_seconds(created_at_diff_secs) fields.append({ 'title': 'Age', 'value': created_at, "short": utils.is_short(created_at), }) return sorted(fields, key=lambda v: v['title'].lower())