{
  "$schema": "./.schemas/page.schema.json",
  "title": "changedetection.io",
  "icon": "eye",
  "sources": {
    "health": {
      "url": "http://192.0.2.30:5000",
      "secret": "changedetection",
      "every": "10m",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "requests": {
        "info": "/api/v1/systeminfo"
      }
    },
    "cd": {
      "url": "http://192.0.2.30:5000",
      "secret": "changedetection",
      "every": "10m",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "requests": {
        "watch": "/api/v1/watch",
        "tags": "/api/v1/tags",
        "info": "/api/v1/systeminfo",
        "changes": {
          "path": "/api/v1/changes?from={date.hours_ago_168_unix}&format=json&context=0&max_lines_per_watch=20&limit=60",
          "optional": true
        }
      },
      "derive": [
        {
          "name": "tag_rows",
          "value": {
            "each": "tags",
            "entries": true,
            "fields": {
              "id": "key",
              "title": "value.title"
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "watch",
            "entries": true,
            "lookup": {
              "from": "tag_rows",
              "by": "id",
              "on": "value.tags[0]",
              "as": "tag"
            },
            "fields": {
              "uuid": "key",
              "name": {
                "if": {
                  "all": [
                    {
                      "path": "value.title",
                      "exists": true
                    },
                    {
                      "path": "value.title",
                      "not": ""
                    }
                  ]
                },
                "then": "value.title",
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "value.page_title",
                        "exists": true
                      },
                      {
                        "path": "value.page_title",
                        "not": ""
                      }
                    ]
                  },
                  "then": "value.page_title",
                  "else": "value.url"
                }
              },
              "group": {
                "path": "tag.title",
                "default": "No group"
              },
              "changed": "value.last_changed",
              "error": {
                "if": {
                  "all": [
                    {
                      "path": "value.last_error",
                      "exists": true
                    },
                    {
                      "path": "value.last_error",
                      "not": false
                    },
                    {
                      "path": "value.last_error",
                      "not": ""
                    },
                    {
                      "path": "value.paused",
                      "not": true
                    }
                  ]
                },
                "then": "value.last_error"
              },
              "paused": {
                "if": {
                  "path": "value.paused",
                  "equals": true
                },
                "then": {
                  "template": "yes"
                }
              },
              "rank": {
                "if": {
                  "path": "value.paused",
                  "equals": true
                },
                "then": {
                  "template": "0"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "value.last_error",
                        "exists": true
                      },
                      {
                        "path": "value.last_error",
                        "not": false
                      },
                      {
                        "path": "value.last_error",
                        "not": ""
                      },
                      {
                        "path": "value.paused",
                        "not": true
                      }
                    ]
                  },
                  "then": {
                    "template": "3"
                  },
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "value.viewed",
                          "equals": false
                        },
                        {
                          "path": "value.last_changed",
                          "gt": 0
                        }
                      ]
                    },
                    "then": {
                      "template": "2"
                    },
                    "else": {
                      "template": "1"
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "each": "rows",
            "sort": {
              "by": [
                "rank",
                "changed"
              ],
              "order": "desc"
            },
            "limit": 60,
            "lookup": {
              "from": "changes.watches",
              "by": "uuid",
              "on": "uuid",
              "as": "change"
            },
            "fields": {
              "uuid": "uuid",
              "name": "name",
              "group": "group",
              "changed": "changed",
              "error": "error",
              "paused": "paused",
              "rank": "rank",
              "added": {
                "path": "change.diff",
                "match": "\\n\\+(?!\\+\\+ )[ \\t]*(?:[*•·-][ \\t]+)?((?=[^\\n]*[^\\d\\s.,+-])[^\\n]*[^\\s][^\\n]*)"
              },
              "removed": {
                "path": "change.diff",
                "match": "\\n-(?!-- )[ \\t]*(?:[*•·-][ \\t]+)?((?=[^\\n]*[^\\d\\s.,+-])[^\\n]*[^\\s][^\\n]*)"
              },
              "back": {
                "if": {
                  "path": "change.no_net_difference",
                  "equals": true
                },
                "then": {
                  "template": "yes"
                }
              }
            }
          }
        },
        {
          "name": "total",
          "value": {
            "count": "rows"
          }
        },
        {
          "name": "n_new",
          "value": {
            "count": "rows",
            "where": {
              "path": "rank",
              "equals": "2"
            }
          }
        },
        {
          "name": "n_paused",
          "value": {
            "count": "rows",
            "where": {
              "path": "paused",
              "exists": true
            }
          }
        },
        {
          "name": "n_overdue",
          "value": {
            "count": "info.overdue_watches"
          }
        },
        {
          "name": "n_checking",
          "value": {
            "count": "info.checking_now"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "changedetection.io",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "health",
        "map": {
          "state": {
            "template": "ok"
          },
          "detail": {
            "if": {
              "path": "info.all_paused",
              "equals": true
            },
            "then": {
              "template": "All paused"
            },
            "else": {
              "if": {
                "path": "info.watch_count",
                "equals": 1
              },
              "then": {
                "template": "1 watch"
              },
              "else": {
                "template": "{info.watch_count} watches"
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "changedetection.io isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "watches",
      "type": "list",
      "label": "Watches",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "cd",
        "empty": {
          "text": "No watches in changedetection.io yet.",
          "when": {
            "path": "total",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "total",
              "gt": 60
            },
            "then": {
              "template": "Watches · 60 of {total}"
            },
            "else": {
              "if": {
                "path": "n_new",
                "gt": 0
              },
              "then": {
                "template": "Watches · {n_new} new"
              },
              "else": {
                "template": "Watches"
              }
            }
          },
          "items": {
            "each": "shown",
            "fields": {
              "key": {
                "template": "{uuid}"
              },
              "text": {
                "template": "{name|clip:48|verbatim}"
              },
              "subtitle": {
                "if": {
                  "path": "error",
                  "exists": true
                },
                "then": {
                  "template": "Can't check · {error|clip:70}"
                },
                "else": {
                  "if": {
                    "path": "paused",
                    "exists": true
                  },
                  "then": {
                    "template": "{group}"
                  },
                  "else": {
                    "if": {
                      "path": "added",
                      "exists": true
                    },
                    "then": {
                      "template": "{added|clip:70}"
                    },
                    "else": {
                      "if": {
                        "path": "removed",
                        "exists": true
                      },
                      "then": {
                        "template": "Removed: {removed|clip:60}"
                      },
                      "else": {
                        "if": {
                          "path": "back",
                          "exists": true
                        },
                        "then": {
                          "template": "Changed, then back as it was"
                        },
                        "else": {
                          "template": "{group}"
                        }
                      }
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "paused",
                  "exists": true
                },
                "then": {
                  "template": "Paused"
                },
                "else": {
                  "if": {
                    "path": "changed",
                    "gt": 0
                  },
                  "then": {
                    "template": "{changed|ago}"
                  },
                  "else": {
                    "template": "No changes yet"
                  }
                }
              },
              "icon": {
                "path": "rank",
                "values": {
                  "3": {
                    "template": "exclamationmark.triangle.fill"
                  },
                  "2": {
                    "template": "circle.fill"
                  },
                  "1": {
                    "template": "circle"
                  },
                  "0": {
                    "template": "pause.circle"
                  }
                },
                "default": {
                  "template": "circle"
                }
              },
              "color": {
                "path": "rank",
                "values": {
                  "3": {
                    "template": "orange"
                  },
                  "2": {
                    "template": "blue"
                  }
                }
              },
              "url": {
                "template": "legato://page/changedetection-watch?uuid={uuid|raw}"
              }
            }
          }
        }
      }
    },
    {
      "id": "server",
      "type": "facts",
      "label": "Server",
      "items": [
        {
          "key": "Last 7 days",
          "value": "—"
        },
        {
          "key": "Checking",
          "value": "—"
        },
        {
          "key": "Watches",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "cd",
        "map": {
          "items[0].value": {
            "if": {
              "path": "changes",
              "exists": false
            },
            "then": {
              "template": "—"
            },
            "else": {
              "if": {
                "path": "changes.incomplete",
                "equals": true
              },
              "then": {
                "template": "At least {changes.changes_total} changes"
              },
              "else": {
                "if": {
                  "path": "changes.total",
                  "equals": 0
                },
                "then": {
                  "template": "No changes"
                },
                "else": {
                  "if": {
                    "path": "changes.changes_total",
                    "equals": 1
                  },
                  "then": {
                    "template": "1 change on 1 page"
                  },
                  "else": {
                    "if": {
                      "path": "changes.total",
                      "equals": 1
                    },
                    "then": {
                      "template": "{changes.changes_total} changes on 1 page"
                    },
                    "else": {
                      "template": "{changes.changes_total} changes on {changes.total} pages"
                    }
                  }
                }
              }
            }
          },
          "items[1].value": {
            "if": {
              "path": "info.all_paused",
              "equals": true
            },
            "then": {
              "template": "All paused"
            },
            "else": {
              "if": {
                "all": [
                  {
                    "path": "n_overdue",
                    "gt": 0
                  },
                  {
                    "path": "n_checking",
                    "gt": 0
                  }
                ]
              },
              "then": {
                "template": "{n_checking} now · {n_overdue} overdue"
              },
              "else": {
                "if": {
                  "path": "n_overdue",
                  "gt": 0
                },
                "then": {
                  "template": "{n_overdue} overdue"
                },
                "else": {
                  "if": {
                    "path": "n_checking",
                    "gt": 0
                  },
                  "then": {
                    "template": "{n_checking} now"
                  },
                  "else": {
                    "template": "On time"
                  }
                }
              }
            }
          },
          "items[2].value": {
            "if": {
              "path": "n_paused",
              "gt": 0
            },
            "then": {
              "template": "{info.watch_count} · {n_paused} paused"
            },
            "else": {
              "template": "{info.watch_count}"
            }
          },
          "items[3].value": {
            "if": {
              "all": [
                {
                  "path": "info.new_version_available",
                  "exists": true
                },
                {
                  "path": "info.new_version_available",
                  "not": false
                }
              ]
            },
            "then": {
              "template": "{info.version|raw} · update available"
            },
            "else": {
              "template": "{info.version|raw}"
            }
          }
        }
      }
    }
  ]
}
