{
  "$schema": "./.schemas/page.schema.json",
  "title": "Audiobookshelf",
  "icon": "headphones",
  "sources": {
    "now": {
      "url": "http://audiobookshelf.local:13378",
      "secret": "audiobookshelf",
      "auth": {
        "type": "bearer"
      },
      "every": "1m",
      "requests": {
        "open": "/api/sessions/open"
      },
      "derive": [
        {
          "name": "streams",
          "value": {
            "each": "open.sessions",
            "where": {
              "all": [
                {
                  "value": {
                    "since": "updatedAt"
                  },
                  "lt": 960
                },
                {
                  "path": "duration",
                  "gt": 0
                }
              ]
            },
            "sort": {
              "by": "updatedAt",
              "order": "desc"
            },
            "fields": {
              "id": "id",
              "title": "displayTitle",
              "subtitle": {
                "if": {
                  "path": "mediaType",
                  "equals": "podcast"
                },
                "then": "mediaMetadata.title",
                "else": "displayAuthor"
              },
              "who": {
                "if": {
                  "path": "user.username",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "deviceInfo.deviceName",
                    "exists": true
                  },
                  "then": {
                    "template": "{user.username} · {deviceInfo.deviceName}"
                  },
                  "else": {
                    "template": "{user.username} · {deviceInfo.clientName}"
                  }
                },
                "else": {
                  "if": {
                    "path": "deviceInfo.deviceName",
                    "exists": true
                  },
                  "then": "deviceInfo.deviceName",
                  "else": "deviceInfo.clientName"
                }
              },
              "state": {
                "if": {
                  "value": {
                    "since": "updatedAt"
                  },
                  "gte": 120
                },
                "then": {
                  "template": "Paused"
                },
                "else": {
                  "template": ""
                }
              },
              "unit": {
                "if": {
                  "path": "duration",
                  "gte": 3600
                },
                "then": {
                  "template": "h"
                },
                "else": {
                  "template": "min"
                }
              },
              "pos": {
                "if": {
                  "path": "duration",
                  "gte": 3600
                },
                "then": {
                  "divide": [
                    "currentTime",
                    3600
                  ],
                  "round": 1
                },
                "else": {
                  "divide": [
                    "currentTime",
                    60
                  ],
                  "round": 0
                }
              },
              "run": {
                "if": {
                  "path": "duration",
                  "gte": 3600
                },
                "then": {
                  "divide": [
                    "duration",
                    3600
                  ],
                  "round": 1
                },
                "else": {
                  "if": {
                    "path": "duration",
                    "lt": 60
                  },
                  "then": {
                    "divide": [
                      60,
                      60
                    ]
                  },
                  "else": {
                    "divide": [
                      "duration",
                      60
                    ],
                    "round": 0
                  }
                }
              },
              "image": {
                "if": {
                  "path": "coverPath",
                  "exists": true
                },
                "then": {
                  "template": "/api/items/{libraryItemId}/cover?width=144&format=jpeg"
                },
                "else": {
                  "template": ""
                }
              },
              "icon": {
                "path": "mediaType",
                "values": {
                  "book": "headphones",
                  "podcast": "antenna.radiowaves.left.and.right"
                },
                "default": "headphones"
              }
            }
          }
        },
        {
          "name": "playing",
          "value": {
            "count": "streams",
            "default": 0
          }
        }
      ]
    },
    "server": {
      "url": "http://audiobookshelf.local:13378",
      "secret": "audiobookshelf",
      "auth": {
        "type": "bearer"
      },
      "every": "5m",
      "requests": {
        "status": "/status",
        "backups": "/api/backups"
      }
    },
    "history": {
      "url": "http://audiobookshelf.local:13378",
      "secret": "audiobookshelf",
      "auth": {
        "type": "bearer"
      },
      "every": "15m",
      "requests": {
        "sessions": "/api/sessions?itemsPerPage=100&sort=updatedAt&desc=1",
        "open": "/api/sessions/open",
        "totals": "/api/stats/server",
        "backups": "/api/backups",
        "status": "/status"
      },
      "derive": [
        {
          "name": "days",
          "value": {
            "each": "sessions.sessions",
            "by_day": {
              "at": "date",
              "from": "{date.days_ago_6}",
              "through": "{date.today}"
            },
            "fields": {
              "day": "from",
              "secs": {
                "sum": "timeListening",
                "of": "rows"
              },
              "rows": "rows"
            }
          }
        },
        {
          "name": "week_secs",
          "value": {
            "sum": "secs",
            "of": "days",
            "default": 0
          }
        },
        {
          "name": "week_hours",
          "value": {
            "divide": [
              "week_secs",
              3600
            ],
            "round": 1,
            "default": 0
          }
        },
        {
          "name": "week_users",
          "value": {
            "each": "days",
            "flatten": "rows",
            "group": "userId",
            "fields": {
              "user": "key"
            }
          }
        },
        {
          "name": "listeners",
          "value": {
            "count": "week_users",
            "default": 0
          }
        },
        {
          "name": "window_age",
          "value": {
            "since": "days[0].day"
          }
        },
        {
          "name": "oldest_age",
          "value": {
            "since": "sessions.sessions[99].updatedAt"
          }
        },
        {
          "name": "complete",
          "value": {
            "if": {
              "any": [
                {
                  "path": "sessions.total",
                  "lte": 100
                },
                {
                  "path": "oldest_age",
                  "gt": {
                    "path": "window_age"
                  }
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "last_backup",
          "value": {
            "max": "createdAt",
            "of": "backups.backups"
          }
        },
        {
          "name": "version",
          "value": "status.serverVersion"
        },
        {
          "name": "recent",
          "value": {
            "each": "sessions.sessions",
            "lookup": {
              "from": "open.sessions",
              "by": "id",
              "on": "id",
              "as": "o"
            },
            "where": {
              "all": [
                {
                  "path": "timeListening",
                  "gte": 60
                },
                {
                  "any": [
                    {
                      "path": "o.id",
                      "exists": false
                    },
                    {
                      "value": {
                        "since": "o.updatedAt"
                      },
                      "gte": 960
                    }
                  ]
                }
              ]
            },
            "limit": 4,
            "fields": {
              "id": "id",
              "title": "displayTitle",
              "at": "updatedAt",
              "mins": {
                "divide": [
                  "timeListening",
                  60
                ],
                "round": 0
              },
              "who": {
                "if": {
                  "path": "user.username",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "deviceInfo.deviceName",
                    "exists": true
                  },
                  "then": {
                    "template": "{user.username} · {deviceInfo.deviceName}"
                  },
                  "else": {
                    "template": "{user.username} · {deviceInfo.clientName}"
                  }
                },
                "else": {
                  "if": {
                    "path": "deviceInfo.deviceName",
                    "exists": true
                  },
                  "then": "deviceInfo.deviceName",
                  "else": "deviceInfo.clientName"
                }
              }
            }
          }
        }
      ]
    },
    "library": {
      "url": "http://audiobookshelf.local:13378",
      "secret": "audiobookshelf",
      "auth": {
        "type": "bearer"
      },
      "every": "15m",
      "requests": {
        "libraries": "/api/libraries",
        "stats": {
          "path": "/api/libraries/{id}/stats",
          "each_row": {
            "from": "libraries.libraries",
            "key": "id",
            "max": 10,
            "pick": [
              "totalItems",
              "totalDuration",
              "numAudioTracks",
              "totalSize"
            ]
          }
        },
        "items": {
          "path": "/api/libraries/{id}/items?sort=addedAt&desc=1&limit=8&minified=1",
          "each_row": {
            "from": "libraries.libraries",
            "key": "id",
            "max": 10,
            "pick": [
              "results"
            ]
          }
        },
        "episodes": {
          "path": "/api/libraries/{id}/recent-episodes?limit=8",
          "each_row": {
            "from": "libraries.libraries",
            "key": "id",
            "max": 10,
            "pick": [
              "episodes"
            ]
          }
        }
      },
      "derive": [
        {
          "name": "added_items",
          "value": {
            "each": "items",
            "flatten": "answer.results",
            "fields": {
              "id": "id",
              "item": "id",
              "kind": "mediaType",
              "at": "addedAt",
              "title": "media.metadata.title",
              "by": "media.metadata.authorName",
              "cover": "media.coverPath"
            }
          }
        },
        {
          "name": "added_books",
          "value": {
            "each": "added_items",
            "where": {
              "path": "kind",
              "equals": "book"
            },
            "fields": {
              "id": "id",
              "item": "item",
              "kind": "kind",
              "at": "at",
              "title": "title",
              "by": "by",
              "cover": "cover"
            }
          }
        },
        {
          "name": "added_episodes",
          "value": {
            "each": "episodes",
            "flatten": "answer.episodes",
            "fields": {
              "id": "id",
              "item": "libraryItemId",
              "kind": {
                "template": "podcast"
              },
              "at": "addedAt",
              "title": "title",
              "by": "podcast.metadata.title",
              "cover": "podcast.coverPath"
            }
          }
        },
        {
          "name": "added",
          "value": {
            "each": [
              "added_books",
              "added_episodes"
            ],
            "sort": {
              "by": "at",
              "order": "desc"
            },
            "limit": 8,
            "fields": {
              "id": "id",
              "item": "item",
              "kind": "kind",
              "at": "at",
              "title": "title",
              "by": "by",
              "cover": "cover"
            }
          }
        },
        {
          "name": "library_totals",
          "value": {
            "each": "libraries.libraries",
            "lookup": {
              "from": "stats",
              "by": "key",
              "on": "id",
              "as": "s"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "kind": "mediaType",
              "n": "s.answer.totalItems",
              "eps": "s.answer.numAudioTracks",
              "hours": {
                "divide": [
                  "s.answer.totalDuration",
                  3600
                ],
                "round": 0
              }
            }
          }
        },
        {
          "name": "library_rows",
          "value": {
            "each": "library_totals",
            "fields": {
              "id": "id",
              "name": "name",
              "kind": "kind",
              "count": {
                "if": {
                  "path": "n",
                  "exists": false
                },
                "then": {
                  "template": ""
                },
                "else": {
                  "if": {
                    "path": "kind",
                    "equals": "podcast"
                  },
                  "then": {
                    "if": {
                      "path": "n",
                      "equals": 1
                    },
                    "then": {
                      "template": "1 podcast · {eps} episodes"
                    },
                    "else": {
                      "template": "{n} podcasts · {eps} episodes"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "n",
                      "equals": 1
                    },
                    "then": {
                      "template": "1 book · {hours} h"
                    },
                    "else": {
                      "template": "{n} books · {hours} h"
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "server",
      "type": "status",
      "placement": "page_info",
      "label": "Audiobookshelf",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "server",
        "map": {
          "state": {
            "template": "ok"
          }
        },
        "failed": {
          "state": "critical",
          "detail": "The server isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "listening",
      "type": "columns",
      "label": "Listening",
      "series": [
        "Hours"
      ],
      "unit": "h",
      "highlight": "Today",
      "items": [],
      "source": {
        "ref": "history",
        "map": {
          "label": {
            "if": {
              "path": "complete",
              "equals": "yes"
            },
            "then": {
              "template": "Listening"
            },
            "else": {
              "template": "Listening, newest 100 sessions"
            }
          },
          "items": {
            "each": "days",
            "fields": {
              "label": {
                "template": "{day|weekday}"
              },
              "values[0]": {
                "divide": [
                  "secs",
                  3600
                ],
                "round": 1
              }
            }
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Server",
      "items": [
        {
          "key": "This week",
          "value": "—"
        },
        {
          "key": "Last backup",
          "value": "—"
        },
        {
          "key": "Library size",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "history",
        "map": {
          "items[0].value": {
            "if": {
              "path": "complete",
              "equals": "yes"
            },
            "then": {
              "if": {
                "path": "listeners",
                "equals": 1
              },
              "then": {
                "template": "{week_hours} h · 1 listener"
              },
              "else": {
                "template": "{week_hours} h · {listeners} listeners"
              }
            },
            "else": {
              "if": {
                "path": "listeners",
                "equals": 1
              },
              "then": {
                "template": "At least {week_hours} h · 1 listener"
              },
              "else": {
                "template": "At least {week_hours} h · {listeners} listeners"
              }
            }
          },
          "items[1].value": {
            "if": {
              "path": "last_backup",
              "exists": true
            },
            "then": {
              "template": "{last_backup|ago}"
            },
            "else": {
              "template": "No backups"
            }
          },
          "items[2].value": {
            "template": "{totals.total.totalSize|bytes}"
          },
          "items[3].value": "version"
        }
      }
    },
    {
      "id": "now",
      "type": "pacing",
      "label": "Now listening",
      "unit": "h",
      "items": [],
      "source": {
        "ref": "now",
        "empty": {
          "text": "Nothing playing",
          "when": {
            "path": "playing",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "streams",
            "fields": {
              "key": "id",
              "label": "title",
              "subtitle": "subtitle",
              "state": "state",
              "detail": "who",
              "value": "pos",
              "target": "run",
              "unit": "unit",
              "image": "image",
              "image_shape": {
                "template": "square"
              },
              "icon": "icon"
            }
          }
        }
      }
    },
    {
      "id": "added",
      "type": "list",
      "label": "Recently added",
      "layout": "shelf",
      "image_shape": "square",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "library",
        "map": {
          "items": {
            "each": "added",
            "fields": {
              "key": "id",
              "text": "title",
              "detail": {
                "template": "{by} · {at|ago}"
              },
              "image": {
                "if": {
                  "path": "cover",
                  "exists": true
                },
                "then": {
                  "template": "/api/items/{item}/cover?width=144&format=jpeg"
                },
                "else": {
                  "template": ""
                }
              },
              "icon": {
                "path": "kind",
                "values": {
                  "book": "headphones",
                  "podcast": "antenna.radiowaves.left.and.right"
                },
                "default": "headphones"
              },
              "url": {
                "template": "http://audiobookshelf.local:13378/item/{item}"
              }
            }
          }
        }
      }
    },
    {
      "id": "libraries",
      "type": "list",
      "label": "Libraries",
      "items": [],
      "source": {
        "ref": "library",
        "map": {
          "items": {
            "each": "library_rows",
            "fields": {
              "text": "name",
              "detail": "count",
              "icon": {
                "path": "kind",
                "values": {
                  "book": "headphones",
                  "podcast": "antenna.radiowaves.left.and.right"
                },
                "default": "headphones"
              },
              "url": {
                "template": "legato://page/audiobookshelf-library?id={id}"
              }
            }
          }
        }
      }
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recent listening",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "history",
        "empty": {
          "text": "No listening yet",
          "when": {
            "value": {
              "count": "recent",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "recent",
            "fields": {
              "key": "id",
              "text": "title",
              "subtitle": "who",
              "detail": {
                "template": "{mins|duration:min} · {at|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
