{
  "$schema": "./.schemas/page.schema.json",
  "title": "Speedtest Tracker",
  "icon": "speedometer",
  "sources": {
    "now": {
      "url": "http://speedtest.local:8080",
      "secret": "speedtest-tracker",
      "auth": {
        "type": "bearer"
      },
      "every": "10m",
      "requests": {
        "last": "/api/v1/results?sort=-created_at&page%5Bsize%5D=4&filter%5Bstatus%5D=completed,failed",
        "good": "/api/v1/results?sort=-created_at&page%5Bsize%5D=1&filter%5Bstatus%5D=completed",
        "stats": "/api/v1/stats?filter%5Bstart_at%5D={date.days_ago_6}",
        "week": "/api/v1/results?sort=-created_at&page%5Bsize%5D=200&filter%5Bstatus%5D=completed&filter%5Bstart_at%5D={date.days_ago_6}"
      },
      "derive": [
        {
          "name": "any",
          "value": {
            "count": "last.data"
          }
        },
        {
          "name": "tests",
          "value": {
            "each": "last.data",
            "fields": {
              "id": "id",
              "data": "data",
              "created_at": "created_at",
              "status": "status",
              "healthy": "healthy",
              "download_bits": "download_bits",
              "upload_bits": "upload_bits",
              "ping": "ping",
              "utc_day": {
                "path": "created_at",
                "match": "^([0-9-]+) "
              },
              "utc_time": {
                "path": "created_at",
                "match": " ([0-9:]+)$"
              }
            }
          }
        },
        {
          "name": "last_at",
          "value": {
            "path": "tests[0].data.timestamp",
            "default": {
              "template": "{tests[0].utc_day}T{tests[0].utc_time}Z"
            }
          }
        },
        {
          "name": "last_age",
          "value": {
            "since": "last_at"
          }
        },
        {
          "name": "last_down",
          "value": {
            "path": "last.data[0].download_bits",
            "scale": 1e-06,
            "round": 0
          }
        },
        {
          "name": "last_up",
          "value": {
            "path": "last.data[0].upload_bits",
            "scale": 1e-06,
            "round": 0
          }
        },
        {
          "name": "good_day",
          "value": {
            "path": "good.data[0].created_at",
            "match": "^([0-9-]+) "
          }
        },
        {
          "name": "good_time",
          "value": {
            "path": "good.data[0].created_at",
            "match": " ([0-9:]+)$"
          }
        },
        {
          "name": "good_at",
          "value": {
            "path": "good.data[0].data.timestamp",
            "default": {
              "template": "{good_day}T{good_time}Z"
            }
          }
        },
        {
          "name": "avg_down",
          "value": {
            "path": "stats.data.download.avg_bits",
            "scale": 1e-06,
            "round": 0
          }
        },
        {
          "name": "avg_up",
          "value": {
            "path": "stats.data.upload.avg_bits",
            "scale": 1e-06,
            "round": 0
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "tests",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "at": {
                "path": "data.timestamp",
                "default": {
                  "template": "{utc_day}T{utc_time}Z"
                }
              },
              "failed": {
                "path": "status",
                "values": {
                  "failed": 1
                },
                "default": 0
              },
              "slow": {
                "path": "healthy",
                "values": {
                  "false": 1
                },
                "default": 0
              },
              "down": {
                "path": "download_bits",
                "scale": 1e-06,
                "round": 0
              },
              "up": {
                "path": "upload_bits",
                "scale": 1e-06,
                "round": 0
              },
              "ping": {
                "path": "ping",
                "round": 0
              },
              "message": {
                "path": "data.message",
                "default": "Speedtest Tracker gave no reason"
              },
              "url": "data.result.url"
            }
          }
        },
        {
          "name": "week_count",
          "value": {
            "count": "week.data"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "Speedtest Tracker",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "now",
        "failed": {
          "state": "critical",
          "detail": "Speedtest Tracker isn't answering",
          "after": 3
        },
        "map": {
          "state": {
            "if": {
              "path": "any",
              "equals": 0
            },
            "then": {
              "template": "unknown"
            },
            "else": {
              "if": {
                "path": "last.data[0].status",
                "equals": "failed"
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "last.data[0].healthy",
                  "equals": false
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "if": {
                    "path": "last_age",
                    "gt": 93600
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "template": "ok"
                  }
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "any",
              "equals": 0
            },
            "then": {
              "template": "No finished tests yet"
            },
            "else": {
              "if": {
                "path": "last.data[0].status",
                "equals": "failed"
              },
              "then": {
                "template": "Last test failed {last_at|ago}: {last.data[0].data.message|clip:70}"
              },
              "else": {
                "if": {
                  "path": "last.data[0].healthy",
                  "equals": false
                },
                "then": {
                  "template": "Below your thresholds {last_at|ago} · {last_down} ↓ · {last_up} ↑ Mbps"
                },
                "else": {
                  "if": {
                    "path": "last_age",
                    "gt": 93600
                  },
                  "then": {
                    "template": "Last test {last_at|ago}"
                  },
                  "else": {
                    "template": "Last test {last_at|ago} · {last_down} ↓ · {last_up} ↑ Mbps"
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "download",
      "type": "sparkline",
      "width": "half",
      "label": "Download",
      "unit": "Mbps",
      "points": [
        0,
        0
      ],
      "change": false,
      "source": {
        "ref": "now",
        "when": {
          "path": "week_count",
          "gte": 2
        },
        "map": {
          "label": {
            "if": {
              "path": "week.meta.total",
              "gt": 200
            },
            "then": {
              "template": "Download · newest 200"
            },
            "else": {
              "template": "Download"
            }
          },
          "points": {
            "each": "week.data",
            "sort": "id",
            "value": {
              "path": "download_bits",
              "scale": 1e-06,
              "round": 0
            }
          },
          "times": {
            "each": "week.data",
            "sort": "id",
            "value": "data.timestamp"
          },
          "labels": {
            "each": "week.data",
            "sort": "id",
            "template": "{data.timestamp|weekday} {data.timestamp|clock}"
          },
          "axis_labels": {
            "each": "week.data",
            "sort": "id",
            "template": "{data.timestamp|weekday}"
          },
          "target": "week.data[0].benchmarks.download.benchmark_value"
        }
      },
      "target_label": "Threshold"
    },
    {
      "id": "upload",
      "type": "sparkline",
      "width": "half",
      "label": "Upload",
      "unit": "Mbps",
      "points": [
        0,
        0
      ],
      "change": false,
      "source": {
        "ref": "now",
        "when": {
          "path": "week_count",
          "gte": 2
        },
        "map": {
          "label": {
            "if": {
              "path": "week.meta.total",
              "gt": 200
            },
            "then": {
              "template": "Upload · newest 200"
            },
            "else": {
              "template": "Upload"
            }
          },
          "points": {
            "each": "week.data",
            "sort": "id",
            "value": {
              "path": "upload_bits",
              "scale": 1e-06,
              "round": 0
            }
          },
          "times": {
            "each": "week.data",
            "sort": "id",
            "value": "data.timestamp"
          },
          "labels": {
            "each": "week.data",
            "sort": "id",
            "template": "{data.timestamp|weekday} {data.timestamp|clock}"
          },
          "axis_labels": {
            "each": "week.data",
            "sort": "id",
            "template": "{data.timestamp|weekday}"
          },
          "target": "week.data[0].benchmarks.upload.benchmark_value"
        }
      },
      "target_label": "Threshold"
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Last test",
      "items": [
        {
          "key": "Ping",
          "value": "—"
        },
        {
          "key": "Average, 7 days",
          "value": "—"
        },
        {
          "key": "Server",
          "value": "—"
        },
        {
          "key": "Provider",
          "value": "—"
        },
        {
          "key": "Packet loss",
          "value": "—"
        }
      ],
      "actions": [
        {
          "label": "Run a test now",
          "icon": "play.fill",
          "method": "POST",
          "path": "/api/v1/speedtests/run",
          "completion": {
            "path": "/api/v1/results/{data.id|raw}",
            "done": {
              "path": "data.status",
              "in": [
                "completed",
                "failed",
                "skipped"
              ]
            },
            "success": {
              "path": "data.status",
              "equals": "completed"
            },
            "interval": 5,
            "timeout": 300
          }
        }
      ],
      "actions_placement": "corner",
      "source": {
        "ref": "now",
        "map": {
          "label": {
            "if": {
              "path": "good.data[0].id",
              "exists": false
            },
            "then": {
              "template": "No completed tests yet"
            },
            "else": {
              "if": {
                "path": "last.data[0].status",
                "equals": "failed"
              },
              "then": {
                "template": "Last completed test · {good_at|ago}"
              },
              "else": {
                "template": "Last test"
              }
            }
          },
          "items[0].value": {
            "if": {
              "path": "good.data[0].id",
              "exists": true
            },
            "then": {
              "if": {
                "path": "good.data[0].data.ping.jitter",
                "exists": true
              },
              "then": {
                "template": "{good.data[0].ping|round} ms · jitter {good.data[0].data.ping.jitter|round:1} ms"
              },
              "else": {
                "template": "{good.data[0].ping|round} ms"
              }
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "if": {
              "path": "good.data[0].id",
              "exists": true
            },
            "then": {
              "template": "{avg_down} ↓ · {avg_up} ↑ Mbps · {stats.data.ping.avg|round} ms"
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "if": {
              "path": "good.data[0].id",
              "exists": true
            },
            "then": {
              "if": {
                "path": "good.data[0].data.server.location",
                "exists": true
              },
              "then": {
                "template": "{good.data[0].data.server.name} · {good.data[0].data.server.location}"
              },
              "else": {
                "template": "{good.data[0].data.server.name}"
              }
            },
            "else": {
              "template": "—"
            }
          },
          "items[3].value": {
            "if": {
              "path": "good.data[0].id",
              "exists": true
            },
            "then": {
              "template": "{good.data[0].data.isp}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[4].value": {
            "if": {
              "path": "good.data[0].id",
              "exists": true
            },
            "then": {
              "if": {
                "path": "good.data[0].data.packetLoss",
                "exists": true
              },
              "then": {
                "if": {
                  "path": "good.data[0].data.packetLoss",
                  "equals": 0
                },
                "then": {
                  "template": "0%"
                },
                "else": {
                  "template": "{good.data[0].data.packetLoss|round:1}%"
                }
              },
              "else": {
                "template": "Not reported"
              }
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recent tests",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "now",
        "empty": {
          "text": "No finished tests yet",
          "when": {
            "path": "any",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "rows",
            "fields": {
              "key": "key",
              "text": {
                "if": {
                  "path": "failed",
                  "equals": 1
                },
                "then": {
                  "template": "Failed"
                },
                "else": {
                  "template": "{down} ↓ · {up} ↑ Mbps · {ping} ms"
                }
              },
              "subtitle": {
                "if": {
                  "path": "failed",
                  "equals": 1
                },
                "then": {
                  "template": "{message|clip:80}"
                },
                "else": {
                  "if": {
                    "path": "slow",
                    "equals": 1
                  },
                  "then": {
                    "template": "Below your thresholds"
                  }
                }
              },
              "detail": {
                "template": "{at|ago}"
              },
              "url": "url"
            }
          }
        }
      }
    }
  ]
}
