{
  "$schema": "./.schemas/page.schema.json",
  "title": "Sentry",
  "icon": "ladybug",
  "sources": {
    "org": {
      "url": "https://sentry.io",
      "secret": "sentry",
      "every": "1m",
      "requests": {
        "esc": "/api/0/organizations/tidewater-apps/issues/?query=is:unresolved+is:escalating&statsPeriod=24h&groupStatsPeriod=&sort=freq&collapse=lifetime&collapse=filtered&collapse=unhandled&project=-1&limit=25",
        "reg": "/api/0/organizations/tidewater-apps/issues/?query=is:unresolved+is:regressed&statsPeriod=24h&groupStatsPeriod=&sort=freq&collapse=lifetime&collapse=filtered&collapse=unhandled&project=-1&limit=25",
        "new": "/api/0/organizations/tidewater-apps/issues/?query=is:unresolved+is:new&statsPeriod=24h&groupStatsPeriod=&sort=freq&collapse=lifetime&collapse=filtered&collapse=unhandled&project=-1&limit=25",
        "ongoing": "/api/0/organizations/tidewater-apps/issues/?query=is:unresolved+is:ongoing&statsPeriod=24h&groupStatsPeriod=&sort=freq&collapse=lifetime&collapse=filtered&collapse=unhandled&project=-1&limit=60",
        "hours": "/api/0/organizations/tidewater-apps/stats_v2/?field=sum(quantity)&category=error&outcome=accepted&groupBy=outcome&statsPeriod=24h&interval=1h&project=-1",
        "by_project": "/api/0/organizations/tidewater-apps/stats_v2/?field=sum(quantity)&category=error&outcome=accepted&groupBy=project&statsPeriod=24h&interval=1h&project=-1",
        "list": "/api/0/organizations/tidewater-apps/projects/?per_page=100"
      },
      "derive": [
        {
          "name": "rows",
          "value": {
            "each": [
              "esc",
              "reg",
              "new",
              "ongoing"
            ],
            "limit": 60,
            "fields": {
              "id": "id",
              "shortId": "shortId",
              "title": "title",
              "culprit": "culprit",
              "level": "level",
              "substatus": "substatus",
              "lastSeen": "lastSeen",
              "project": "project.slug",
              "events": {
                "path": "count",
                "match": "^([0-9]+)$"
              }
            }
          }
        },
        {
          "name": "n_esc",
          "value": {
            "count": "esc",
            "default": 0
          }
        },
        {
          "name": "n_reg",
          "value": {
            "count": "reg",
            "default": 0
          }
        },
        {
          "name": "n_new",
          "value": {
            "count": "new",
            "default": 0
          }
        },
        {
          "name": "n_ongoing",
          "value": {
            "count": "ongoing",
            "default": 0
          }
        },
        {
          "name": "n_all",
          "value": {
            "add": [
              "n_esc",
              "n_reg",
              "n_new",
              "n_ongoing"
            ]
          }
        },
        {
          "name": "n_hot",
          "value": {
            "add": [
              "n_esc",
              "n_reg"
            ]
          }
        },
        {
          "name": "cut",
          "value": {
            "if": {
              "any": [
                {
                  "path": "n_esc",
                  "gte": 25
                },
                {
                  "path": "n_reg",
                  "gte": 25
                },
                {
                  "path": "n_new",
                  "gte": 25
                },
                {
                  "path": "n_ongoing",
                  "gte": 60
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "hot_cut",
          "value": {
            "if": {
              "any": [
                {
                  "path": "n_esc",
                  "gte": 25
                },
                {
                  "path": "n_reg",
                  "gte": 25
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "errors_total",
          "value": {
            "path": "hours.groups[0].totals[\"sum(quantity)\"]",
            "default": 0
          }
        },
        {
          "name": "hot_title",
          "value": {
            "if": {
              "path": "n_esc",
              "gt": 0
            },
            "then": "esc[0].title",
            "else": "reg[0].title"
          }
        },
        {
          "name": "hot_words",
          "value": {
            "if": {
              "all": [
                {
                  "path": "n_esc",
                  "gt": 0
                },
                {
                  "path": "n_reg",
                  "gt": 0
                }
              ]
            },
            "then": {
              "template": "{n_esc} escalating · {n_reg} regressed"
            },
            "else": {
              "if": {
                "path": "n_esc",
                "gt": 0
              },
              "then": {
                "template": "{n_esc} escalating"
              },
              "else": {
                "template": "{n_reg} regressed"
              }
            }
          }
        },
        {
          "name": "p1",
          "value": {
            "each": "list",
            "sort": "slug",
            "lookup": {
              "from": "esc",
              "by": "project.id",
              "on": "id",
              "as": "hot"
            },
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "hot.title"
            }
          }
        },
        {
          "name": "p2",
          "value": {
            "each": "p1",
            "lookup": {
              "from": "reg",
              "by": "project.id",
              "on": "id",
              "as": "hot"
            },
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "esc_title",
              "reg_title": "hot.title"
            }
          }
        },
        {
          "name": "errors_by",
          "value": {
            "each": "by_project.groups",
            "fields": {
              "key": {
                "template": "{by.project}"
              },
              "n": "totals[\"sum(quantity)\"]"
            }
          }
        },
        {
          "name": "p3",
          "value": {
            "each": "p2",
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "esc_title",
              "reg_title": "reg_title",
              "id_n": {
                "path": "id",
                "match": "^([0-9]+)$"
              }
            }
          }
        },
        {
          "name": "p4",
          "value": {
            "each": "p3",
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "esc_title",
              "reg_title": "reg_title",
              "key": {
                "template": "{id_n}"
              }
            }
          }
        },
        {
          "name": "projects",
          "value": {
            "each": "p4",
            "lookup": {
              "from": "errors_by",
              "by": "key",
              "on": "key",
              "as": "e"
            },
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "esc_title",
              "reg_title": "reg_title",
              "errors": "e.n",
              "rank": {
                "path": "e.n",
                "default": -1
              }
            }
          }
        },
        {
          "name": "by_errors",
          "value": {
            "each": "projects",
            "sort": {
              "by": "rank",
              "order": "desc"
            },
            "fields": {
              "id": "id",
              "slug": "slug",
              "esc_title": "esc_title",
              "reg_title": "reg_title",
              "errors": "errors"
            }
          }
        },
        {
          "name": "n_projects",
          "value": {
            "count": "list",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "width": "full",
      "placement": "page_info",
      "label": "Escalating and regressed",
      "state": "unknown",
      "source": {
        "ref": "org",
        "failed": {
          "state": "warning",
          "detail": "Sentry isn't answering",
          "after": 3
        },
        "map": {
          "state": {
            "if": {
              "path": "n_hot",
              "gt": 0
            },
            "then": {
              "template": "warning"
            },
            "else": {
              "template": "ok"
            }
          },
          "detail": {
            "if": {
              "path": "n_hot",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "hot_cut",
                "equals": "yes"
              },
              "then": {
                "template": "At least {hot_words} · {hot_title|clip:50}"
              },
              "else": {
                "template": "{hot_words} · {hot_title|clip:60}"
              }
            },
            "else": {
              "if": {
                "path": "n_all",
                "equals": 0
              },
              "then": {
                "template": "No unresolved issues seen in the last 24 hours"
              },
              "else": {
                "if": {
                  "path": "cut",
                  "equals": "yes"
                },
                "then": {
                  "template": "At least {n_all} unresolved issues seen in the last 24 hours"
                },
                "else": {
                  "if": {
                    "path": "n_all",
                    "equals": 1
                  },
                  "then": {
                    "template": "1 unresolved issue seen in the last 24 hours"
                  },
                  "else": {
                    "template": "{n_all} unresolved issues seen in the last 24 hours"
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "day",
      "type": "facts",
      "label": "Last 24 hours",
      "items": [
        {
          "key": "Issues",
          "value": "—"
        },
        {
          "key": "Errors",
          "value": "—"
        }
      ],
      "source": {
        "ref": "org",
        "map": {
          "items[0].value": {
            "if": {
              "path": "cut",
              "equals": "yes"
            },
            "then": {
              "if": {
                "path": "n_new",
                "gt": 0
              },
              "then": {
                "template": "At least {n_all} unresolved · {n_new} new"
              },
              "else": {
                "template": "At least {n_all} unresolved"
              }
            },
            "else": {
              "if": {
                "path": "n_all",
                "equals": 0
              },
              "then": {
                "template": "None unresolved"
              },
              "else": {
                "if": {
                  "path": "n_new",
                  "gt": 0
                },
                "then": {
                  "template": "{n_all} unresolved · {n_new} new"
                },
                "else": {
                  "template": "{n_all} unresolved"
                }
              }
            }
          },
          "items[1].value": {
            "if": {
              "path": "errors_total",
              "equals": 1
            },
            "then": {
              "template": "1"
            },
            "else": {
              "template": "{errors_total}"
            }
          }
        }
      }
    },
    {
      "id": "errors",
      "type": "sparkline",
      "label": "Errors",
      "unit": "an hour",
      "change": false,
      "points": [],
      "source": {
        "ref": "org",
        "when": {
          "value": {
            "count": "hours.groups[0].series[\"sum(quantity)\"]",
            "default": 0
          },
          "gte": 2
        },
        "map": {
          "points": {
            "each": "hours.groups[0].series[\"sum(quantity)\"]",
            "value": "$"
          },
          "times": {
            "each": "hours.intervals",
            "value": "$"
          }
        }
      }
    },
    {
      "id": "issues",
      "type": "list",
      "label": "Issues",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "org",
        "empty": {
          "text": "No unresolved issues seen in the last 24 hours.",
          "when": {
            "path": "n_all",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "n_all",
              "gt": 60
            },
            "then": {
              "if": {
                "path": "cut",
                "equals": "yes"
              },
              "then": {
                "template": "Issues · the first 60 of at least {n_all}"
              },
              "else": {
                "template": "Issues · the first 60 of {n_all}"
              }
            },
            "else": {
              "if": {
                "path": "cut",
                "equals": "yes"
              },
              "then": {
                "template": "Issues · the busiest {n_all}"
              },
              "else": {
                "template": "Issues"
              }
            }
          },
          "items": {
            "each": "rows",
            "fields": {
              "key": "id",
              "text": {
                "template": "{title|clip:160|verbatim}"
              },
              "subtitle": {
                "if": {
                  "all": [
                    {
                      "path": "culprit",
                      "exists": true
                    },
                    {
                      "path": "culprit",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "if": {
                    "path": "events",
                    "equals": 1
                  },
                  "then": {
                    "template": "{project|clip:24} · 1 event · {culprit|clip:30}"
                  },
                  "else": {
                    "template": "{project|clip:24} · {events} events · {culprit|clip:30}"
                  }
                },
                "else": {
                  "if": {
                    "path": "events",
                    "equals": 1
                  },
                  "then": {
                    "template": "{project|clip:24} · 1 event"
                  },
                  "else": {
                    "template": "{project|clip:24} · {events} events"
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "substatus",
                  "in": [
                    "escalating",
                    "regressed",
                    "new"
                  ]
                },
                "then": {
                  "path": "substatus",
                  "values": {
                    "escalating": "Escalating",
                    "regressed": "Regressed",
                    "new": "New"
                  }
                },
                "else": {
                  "template": "{lastSeen|ago}"
                }
              },
              "icon": {
                "path": "level",
                "values": {
                  "fatal": "xmark.octagon.fill",
                  "error": "exclamationmark.circle",
                  "warning": "exclamationmark.triangle",
                  "info": "info.circle",
                  "debug": "info.circle"
                },
                "default": "exclamationmark.circle"
              },
              "color": {
                "path": "level",
                "values": {
                  "fatal": "red"
                }
              },
              "url": {
                "template": "legato://page/sentry-issue?id={id|raw}"
              }
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Resolve",
          "icon": "checkmark.circle",
          "method": "PUT",
          "path": "/api/0/organizations/tidewater-apps/issues/{id}/",
          "body": {
            "status": "resolved"
          },
          "error": "detail",
          "confirm": {
            "message": "Resolve {shortId}? Sentry marks it Regressed if it happens again.",
            "button": "Resolve"
          }
        },
        {
          "label": "Archive",
          "icon": "archivebox",
          "method": "PUT",
          "path": "/api/0/organizations/tidewater-apps/issues/{id}/",
          "body": {
            "status": "ignored",
            "substatus": "archived_until_escalating",
            "statusDetails": {}
          },
          "error": "detail",
          "confirm": {
            "message": "Archive {shortId} until it escalates?",
            "button": "Archive"
          }
        }
      ]
    },
    {
      "id": "projects",
      "type": "list",
      "label": "Projects",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "org",
        "empty": {
          "text": "No projects yet.",
          "when": {
            "value": {
              "count": "list",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "n_projects",
              "gte": 100
            },
            "then": {
              "template": "Projects · busiest 60 of at least 100"
            },
            "else": {
              "if": {
                "path": "n_projects",
                "gt": 60
              },
              "then": {
                "template": "Projects · busiest 60 of {n_projects}"
              },
              "else": {
                "template": "Projects"
              }
            }
          },
          "items": {
            "each": "by_errors",
            "limit": 60,
            "fields": {
              "key": "id",
              "text": {
                "template": "{slug|clip:80|verbatim}"
              },
              "subtitle": {
                "if": {
                  "path": "esc_title",
                  "exists": true
                },
                "then": {
                  "template": "Escalating: {esc_title|clip:60}"
                },
                "else": {
                  "if": {
                    "path": "reg_title",
                    "exists": true
                  },
                  "then": {
                    "template": "Regressed: {reg_title|clip:60}"
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "errors",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "errors",
                    "equals": 1
                  },
                  "then": {
                    "template": "1 error"
                  },
                  "else": {
                    "template": "{errors} errors"
                  }
                }
              },
              "url": {
                "template": "legato://page/sentry-project?id={id|raw}"
              }
            }
          }
        }
      }
    }
  ]
}
