{
  "$schema": "./.schemas/page.schema.json",
  "title": "Watch",
  "icon": "eye",
  "sidebar": false,
  "params": {
    "uuid": {
      "example": "6df8c849-8b37-40f3-877a-82db2adc131f",
      "pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
    }
  },
  "title_from": {
    "ref": "watch",
    "value": "name"
  },
  "sources": {
    "watch": {
      "url": "http://192.0.2.30:5000",
      "secret": "changedetection",
      "every": "5m",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "requests": {
        "watch": "/api/v1/watch/{param.uuid}",
        "tags": "/api/v1/tags",
        "info": "/api/v1/systeminfo"
      },
      "derive": [
        {
          "name": "tag_rows",
          "value": {
            "each": "tags",
            "entries": true,
            "fields": {
              "id": "key",
              "title": "value.title"
            }
          }
        },
        {
          "name": "name",
          "value": {
            "if": {
              "all": [
                {
                  "path": "watch.title",
                  "exists": true
                },
                {
                  "path": "watch.title",
                  "not": ""
                }
              ]
            },
            "then": "watch.title",
            "else": {
              "if": {
                "all": [
                  {
                    "path": "watch.page_title",
                    "exists": true
                  },
                  {
                    "path": "watch.page_title",
                    "not": ""
                  }
                ]
              },
              "then": "watch.page_title",
              "else": "watch.url"
            }
          }
        },
        {
          "name": "mine",
          "value": {
            "each": "watch.tags",
            "limit": 1,
            "fields": {
              "id": {
                "template": "{$}"
              }
            }
          }
        },
        {
          "name": "groups",
          "value": {
            "each": "mine",
            "lookup": {
              "from": "tag_rows",
              "by": "id",
              "on": "id",
              "as": "tag"
            },
            "fields": {
              "title": "tag.title"
            }
          }
        },
        {
          "name": "group",
          "value": {
            "first": "title",
            "of": "groups",
            "default": "No group"
          }
        },
        {
          "name": "error",
          "value": {
            "if": {
              "all": [
                {
                  "path": "watch.last_error",
                  "exists": true
                },
                {
                  "path": "watch.last_error",
                  "not": false
                },
                {
                  "path": "watch.last_error",
                  "not": ""
                },
                {
                  "path": "watch.paused",
                  "not": true
                }
              ]
            },
            "then": "watch.last_error"
          }
        },
        {
          "name": "every_s",
          "value": {
            "add": [
              {
                "multiply": [
                  {
                    "path": "watch.time_between_check.weeks",
                    "default": 0
                  },
                  604800
                ]
              },
              {
                "multiply": [
                  {
                    "path": "watch.time_between_check.days",
                    "default": 0
                  },
                  86400
                ]
              },
              {
                "multiply": [
                  {
                    "path": "watch.time_between_check.hours",
                    "default": 0
                  },
                  3600
                ]
              },
              {
                "multiply": [
                  {
                    "path": "watch.time_between_check.minutes",
                    "default": 0
                  },
                  60
                ]
              },
              {
                "path": "watch.time_between_check.seconds",
                "default": 0
              }
            ]
          }
        },
        {
          "name": "host",
          "value": {
            "path": "watch.url",
            "match": "^[A-Za-z][A-Za-z0-9+.-]*://(?:[^@/?#]*@)?(\\[[^\\]/]+\\]|[^/:?#]+)"
          }
        }
      ]
    },
    "change": {
      "url": "http://192.0.2.30:5000",
      "secret": "changedetection",
      "every": "5m",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "requests": {
        "watch": "/api/v1/watch/{param.uuid}",
        "diff": {
          "path": "/api/v1/watch/{param.uuid}/difference/previous/{latest}?format=text&changesOnly=true",
          "format": "text",
          "with": {
            "latest": "watch.last_changed"
          },
          "missing": [
            404
          ]
        }
      }
    },
    "history": {
      "url": "http://192.0.2.30:5000",
      "secret": "changedetection",
      "every": "5m",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "requests": {
        "history": "/api/v1/watch/{param.uuid}/history"
      },
      "derive": [
        {
          "name": "snapshots",
          "value": {
            "each": "history",
            "entries": true,
            "fields": {
              "at": {
                "path": "key",
                "match": "^([0-9]+)$"
              }
            }
          }
        },
        {
          "name": "kept",
          "value": {
            "count": "snapshots"
          }
        },
        {
          "name": "first_at",
          "value": {
            "first": "at",
            "of": "snapshots"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "error",
      "type": "text",
      "tone": "warning",
      "body": "",
      "source": {
        "ref": "watch",
        "when": {
          "path": "error",
          "exists": true
        },
        "map": {
          "body": {
            "template": "Can't check this page: {error|clip:160|verbatim}"
          }
        }
      }
    },
    {
      "id": "latest",
      "type": "text",
      "label": "Latest change",
      "body": "",
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Mark as seen",
          "icon": "checkmark.circle",
          "method": "PUT",
          "path": "/api/v1/watch/{param.uuid}",
          "body": {
            "last_viewed": "{answer.watch.last_changed}"
          },
          "when": {
            "all": [
              {
                "path": "watch.viewed",
                "equals": false
              },
              {
                "path": "watch.last_changed",
                "gt": 0
              },
              {
                "path": "diff",
                "exists": true
              },
              {
                "path": "diff",
                "not": ""
              }
            ]
          }
        }
      ],
      "source": {
        "ref": "change",
        "empty": {
          "text": "No changes yet. The first check is kept as the page to compare with.",
          "when": {
            "all": [
              {
                "path": "diff",
                "exists": false
              },
              {
                "path": "watch.last_changed",
                "equals": 0
              }
            ]
          }
        },
        "map": {
          "label": {
            "if": {
              "all": [
                {
                  "path": "watch.viewed",
                  "equals": false
                },
                {
                  "path": "watch.last_changed",
                  "gt": 0
                }
              ]
            },
            "then": {
              "template": "Latest change · {watch.last_changed|ago} · new"
            },
            "else": {
              "template": "Latest change · {watch.last_changed|ago}"
            }
          },
          "body": {
            "if": {
              "path": "diff",
              "equals": ""
            },
            "then": {
              "template": "No changed lines to show."
            },
            "else": {
              "if": {
                "path": "diff",
                "exists": true
              },
              "then": {
                "template": "{diff|clip:400|verbatim}"
              },
              "else": {
                "template": "changedetection.io no longer keeps the snapshot before this one, so there's nothing to compare it with."
              }
            }
          }
        }
      }
    },
    {
      "id": "week",
      "type": "columns",
      "label": "Snapshots, 7 days",
      "series": [
        "Snapshots"
      ],
      "items": [],
      "source": {
        "ref": "history",
        "map": {
          "detail": {
            "if": {
              "path": "kept",
              "equals": 0
            },
            "then": {
              "template": "None kept yet"
            },
            "else": {
              "if": {
                "path": "kept",
                "equals": 1
              },
              "then": {
                "template": "1 kept, on {first_at|date}"
              },
              "else": {
                "template": "{kept} kept since {first_at|date}"
              }
            }
          },
          "items": {
            "each": "snapshots",
            "by_day": {
              "at": "at",
              "from": "{date.days_ago_6}",
              "through": "{date.today}"
            },
            "fields": {
              "label": {
                "template": "{from|weekday}"
              },
              "values[0]": "count"
            }
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Watch",
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Check now",
          "icon": "doc.text.magnifyingglass",
          "method": "GET",
          "path": "/api/v1/watch/{param.uuid}?recheck=1",
          "when": {
            "all": [
              {
                "path": "watch.paused",
                "not": true
              },
              {
                "path": "info.all_paused",
                "not": true
              }
            ]
          }
        },
        "|",
        {
          "label": "Pause",
          "icon": "pause.fill",
          "method": "PUT",
          "path": "/api/v1/watch/{param.uuid}",
          "body": {
            "paused": true
          },
          "when": {
            "path": "watch.paused",
            "not": true
          }
        },
        {
          "label": "Resume",
          "icon": "play.fill",
          "method": "PUT",
          "path": "/api/v1/watch/{param.uuid}",
          "body": {
            "paused": false
          },
          "when": {
            "path": "watch.paused",
            "equals": true
          }
        },
        {
          "label": "Mute alerts",
          "icon": "bell.slash",
          "method": "PUT",
          "path": "/api/v1/watch/{param.uuid}",
          "body": {
            "notification_muted": true
          },
          "when": {
            "path": "watch.notification_muted",
            "not": true
          }
        },
        {
          "label": "Unmute alerts",
          "icon": "bell",
          "method": "PUT",
          "path": "/api/v1/watch/{param.uuid}",
          "body": {
            "notification_muted": false
          },
          "when": {
            "path": "watch.notification_muted",
            "equals": true
          }
        }
      ],
      "items": [
        {
          "key": "Checked",
          "value": "—"
        },
        {
          "key": "Address",
          "value": "—"
        },
        {
          "key": "Group",
          "value": "—"
        },
        {
          "key": "Alerts",
          "value": "—"
        }
      ],
      "source": {
        "ref": "watch",
        "map": {
          "items[0].value": {
            "if": {
              "path": "watch.paused",
              "equals": true
            },
            "then": {
              "template": "Paused"
            },
            "else": {
              "if": {
                "path": "info.all_paused",
                "equals": true
              },
              "then": {
                "template": "Paused, with every watch"
              },
              "else": {
                "if": {
                  "path": "watch.last_checked",
                  "gt": 0
                },
                "then": {
                  "if": {
                    "path": "watch.time_between_check_use_default",
                    "equals": true
                  },
                  "then": {
                    "template": "{watch.last_checked|ago} · on the default schedule"
                  },
                  "else": {
                    "template": "{watch.last_checked|ago} · every {every_s|duration}"
                  }
                },
                "else": {
                  "template": "Not yet"
                }
              }
            }
          },
          "items[1].value": {
            "template": "{host}"
          },
          "items[1].url": {
            "if": {
              "all": [
                {
                  "path": "watch.open_link",
                  "exists": true
                },
                {
                  "path": "watch.open_link",
                  "not": ""
                }
              ]
            },
            "then": "watch.open_link",
            "else": "watch.link"
          },
          "items[2].value": {
            "template": "{group}"
          },
          "items[3].value": {
            "if": {
              "path": "info.all_muted",
              "equals": true
            },
            "then": {
              "template": "Muted for every watch"
            },
            "else": {
              "if": {
                "path": "watch.notification_muted",
                "equals": true
              },
              "then": {
                "template": "Muted"
              },
              "else": {
                "template": "Not muted"
              }
            }
          }
        }
      }
    }
  ]
}
