{
  "$schema": "./.schemas/page.schema.json",
  "title": "Nextcloud",
  "icon": "cloud",
  "sources": {
    "server": {
      "url": "https://nextcloud.example.com",
      "every": "5m",
      "requests": {
        "status": "/status.php"
      }
    },
    "info": {
      "url": "https://nextcloud.example.com",
      "secret": "nextcloud",
      "auth": {
        "type": "header",
        "name": "NC-Token"
      },
      "every": "5m",
      "requests": {
        "info": "/ocs/v2.php/apps/serverinfo/api/v1/info?format=json&skipApps=false&skipUpdate=false"
      },
      "derive": [
        {
          "name": "free",
          "value": {
            "path": "info.ocs.data.nextcloud.system.freespace"
          }
        },
        {
          "name": "free_gb",
          "value": {
            "path": "info.ocs.data.nextcloud.system.freespace",
            "scale": 9.313225746154785e-10,
            "round": 1
          }
        },
        {
          "name": "core",
          "value": {
            "path": "info.ocs.data.nextcloud.system.update.available"
          }
        },
        {
          "name": "core_version",
          "value": {
            "path": "info.ocs.data.nextcloud.system.update.available_version",
            "match": "^([0-9]+\\.[0-9]+\\.[0-9]+)"
          }
        },
        {
          "name": "core_words",
          "value": {
            "if": {
              "path": "core_version",
              "exists": true
            },
            "then": {
              "template": "Nextcloud {core_version}"
            },
            "else": {
              "template": "Nextcloud"
            }
          }
        },
        {
          "name": "apps",
          "value": {
            "each": "info.ocs.data.nextcloud.system.apps.app_updates",
            "entries": true,
            "sort": "key",
            "fields": {
              "name": {
                "path": "key",
                "values": {
                  "calendar": "Calendar",
                  "contacts": "Contacts",
                  "mail": "Mail",
                  "spreed": "Talk",
                  "deck": "Deck",
                  "notes": "Notes",
                  "tasks": "Tasks",
                  "richdocuments": "Nextcloud Office",
                  "richdocumentscode": "Collabora Online",
                  "onlyoffice": "ONLYOFFICE",
                  "photos": "Photos",
                  "memories": "Memories",
                  "recognize": "Recognize",
                  "forms": "Forms",
                  "polls": "Polls",
                  "groupfolders": "Team folders",
                  "files_pdfviewer": "PDF viewer",
                  "text": "Text",
                  "whiteboard": "Whiteboard",
                  "news": "News",
                  "bookmarks": "Bookmarks",
                  "cookbook": "Cookbook",
                  "music": "Music",
                  "maps": "Maps",
                  "twofactor_totp": "Two-Factor TOTP",
                  "twofactor_webauthn": "Two-Factor WebAuthn",
                  "user_ldap": "LDAP user and group backend",
                  "user_oidc": "OpenID Connect user backend",
                  "integration_openai": "OpenAI integration",
                  "assistant": "Assistant",
                  "collectives": "Collectives",
                  "tables": "Tables",
                  "end_to_end_encryption": "End-to-end encryption",
                  "suspicious_login": "Suspicious login",
                  "previewgenerator": "Preview Generator",
                  "files_retention": "Retention",
                  "app_api": "AppAPI",
                  "serverinfo": "Monitoring",
                  "activity": "Activity",
                  "notifications": "Notifications"
                },
                "default": {
                  "template": "{key|title}"
                }
              }
            }
          }
        },
        {
          "name": "app_count",
          "value": {
            "count": "apps",
            "default": 0
          }
        },
        {
          "name": "first_apps",
          "value": {
            "each": "apps",
            "limit": 2,
            "fields": {
              "name": "name"
            }
          }
        },
        {
          "name": "app_words",
          "value": {
            "join": "name",
            "of": "first_apps",
            "separator": ", "
          }
        },
        {
          "name": "apps_more",
          "value": {
            "subtract": [
              "app_count",
              2
            ]
          }
        },
        {
          "name": "app_text",
          "value": {
            "if": {
              "path": "app_count",
              "gt": 2
            },
            "then": {
              "template": "{app_words} and {apps_more} more"
            },
            "else": {
              "template": "{app_words}"
            }
          }
        },
        {
          "name": "users",
          "value": {
            "path": "info.ocs.data.nextcloud.storage.num_users"
          }
        },
        {
          "name": "disabled",
          "value": {
            "path": "info.ocs.data.nextcloud.storage.num_disabled_users",
            "default": 0
          }
        },
        {
          "name": "shares",
          "value": {
            "path": "info.ocs.data.nextcloud.shares.num_shares",
            "default": 0
          }
        },
        {
          "name": "links",
          "value": {
            "path": "info.ocs.data.nextcloud.shares.num_shares_link",
            "default": 0
          }
        },
        {
          "name": "open_links",
          "value": {
            "path": "info.ocs.data.nextcloud.shares.num_shares_link_no_password",
            "default": 0
          }
        },
        {
          "name": "version",
          "value": {
            "path": "info.ocs.data.nextcloud.system.version",
            "match": "^([0-9]+\\.[0-9]+\\.[0-9]+)"
          }
        },
        {
          "name": "mem_total",
          "value": {
            "path": "info.ocs.data.nextcloud.system.mem_total",
            "scale": 1024
          }
        },
        {
          "name": "mem_used",
          "value": {
            "subtract": [
              "info.ocs.data.nextcloud.system.mem_total",
              "info.ocs.data.nextcloud.system.mem_free"
            ],
            "scale": 1024
          }
        },
        {
          "name": "load",
          "value": {
            "path": "info.ocs.data.nextcloud.system.cpuload[0]",
            "round": 2
          }
        },
        {
          "name": "cores",
          "value": {
            "path": "info.ocs.data.nextcloud.system.cpunum"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "server",
      "type": "status",
      "placement": "page_info",
      "label": "Nextcloud",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "server",
        "map": {
          "state": {
            "if": {
              "path": "status.maintenance",
              "equals": true
            },
            "then": {
              "template": "warning"
            },
            "else": {
              "if": {
                "path": "status.needsDbUpgrade",
                "equals": true
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "template": "ok"
              }
            }
          },
          "detail": {
            "if": {
              "path": "status.maintenance",
              "equals": true
            },
            "then": {
              "template": "In maintenance mode"
            },
            "else": {
              "if": {
                "path": "status.needsDbUpgrade",
                "equals": true
              },
              "then": {
                "template": "An upgrade is waiting to finish"
              },
              "else": {
                "template": "Nextcloud {status.versionstring} · No problems"
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "The server isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "storage",
      "type": "status",
      "placement": "page_info",
      "label": "Storage",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "info",
        "map": {
          "state": {
            "if": {
              "path": "free",
              "exists": false
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "path": "free",
                "lt": 2147483648
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "free",
                  "lt": 10737418240
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "free",
              "exists": false
            },
            "then": {
              "template": "Nextcloud doesn't report free space for this storage"
            },
            "else": {
              "if": {
                "path": "free",
                "lt": 10737418240
              },
              "then": {
                "template": "Only {free|bytes} free"
              },
              "else": {
                "template": "{free|bytes} free"
              }
            }
          }
        },
        "failed": {
          "state": "unknown",
          "detail": "Can't read Nextcloud's server info",
          "after": 3
        }
      }
    },
    {
      "id": "free",
      "type": "sparkline",
      "label": "Free space",
      "unit": "GB",
      "points": [],
      "source": {
        "ref": "info",
        "map": {
          "points": {
            "history": "free_gb",
            "every": "3h",
            "keep": 56
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "items": [
        {
          "key": "Updates",
          "value": "—"
        },
        {
          "key": "Active users",
          "value": "—"
        },
        {
          "key": "Shares",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        },
        {
          "key": "Memory",
          "value": "—"
        },
        {
          "key": "Load",
          "value": "—"
        },
        {
          "key": "Users",
          "value": "—"
        },
        {
          "key": "Files",
          "value": "—"
        }
      ],
      "source": {
        "ref": "info",
        "map": {
          "items[0].value": {
            "if": {
              "path": "core",
              "equals": true
            },
            "then": {
              "if": {
                "path": "app_count",
                "gt": 0
              },
              "then": {
                "template": "{core_words} · {app_text}"
              },
              "else": {
                "template": "{core_words}"
              }
            },
            "else": {
              "if": {
                "path": "app_count",
                "gt": 0
              },
              "then": {
                "template": "{app_text}"
              },
              "else": {
                "if": {
                  "path": "core",
                  "exists": true
                },
                "then": {
                  "template": "None waiting"
                },
                "else": {
                  "template": "Not reported"
                }
              }
            }
          },
          "items[1].value": {
            "template": "{info.ocs.data.activeUsers.last5minutes} in 5 minutes · {info.ocs.data.activeUsers.last24hours} in 24 hours · {info.ocs.data.activeUsers.last7days} in 7 days"
          },
          "items[2].value": {
            "if": {
              "path": "links",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "open_links",
                "gt": 0
              },
              "then": {
                "if": {
                  "path": "links",
                  "equals": 1
                },
                "then": {
                  "template": "{shares} · 1 link, with no password"
                },
                "else": {
                  "template": "{shares} · {links} links, {open_links} with no password"
                }
              },
              "else": {
                "if": {
                  "path": "links",
                  "equals": 1
                },
                "then": {
                  "template": "{shares} · 1 link"
                },
                "else": {
                  "template": "{shares} · {links} links"
                }
              }
            },
            "else": {
              "template": "{shares}"
            }
          },
          "items[3].value": {
            "if": {
              "path": "info.ocs.data.server.php.version",
              "exists": true
            },
            "then": {
              "template": "Nextcloud {version} · PHP {info.ocs.data.server.php.version}"
            },
            "else": {
              "template": "Nextcloud {version}"
            }
          },
          "items[4].value": {
            "if": {
              "all": [
                {
                  "path": "info.ocs.data.nextcloud.system.mem_total",
                  "gt": 0
                },
                {
                  "path": "info.ocs.data.nextcloud.system.mem_free",
                  "gte": 0
                }
              ]
            },
            "then": {
              "template": "{mem_used|bytes} used of {mem_total|bytes}"
            },
            "else": {
              "template": "Not reported"
            }
          },
          "items[5].value": {
            "if": {
              "path": "load",
              "gte": 0
            },
            "then": {
              "if": {
                "path": "cores",
                "gt": 0
              },
              "then": {
                "if": {
                  "path": "cores",
                  "equals": 1
                },
                "then": {
                  "template": "{load} on 1 thread"
                },
                "else": {
                  "template": "{load} on {cores} threads"
                }
              },
              "else": {
                "template": "{load}"
              }
            },
            "else": {
              "template": "Not reported"
            }
          },
          "items[6].value": {
            "if": {
              "path": "disabled",
              "gt": 0
            },
            "then": {
              "template": "{users} active, {disabled} disabled"
            },
            "else": {
              "template": "{users} active"
            }
          },
          "items[7].value": {
            "template": "{info.ocs.data.nextcloud.storage.num_files}"
          }
        }
      }
    }
  ]
}
