{
  "$schema": "./.schemas/page.schema.json",
  "title": "Tautulli",
  "icon": "chart.bar.xaxis",
  "period": {
    "choices": [
      {
        "id": "30",
        "label": "30 days",
        "last_days": 30
      },
      {
        "id": "7",
        "label": "7 days",
        "last_days": 7
      },
      {
        "id": "90",
        "label": "90 days",
        "last_days": 90
      }
    ]
  },
  "sources": {
    "now": {
      "url": "http://tautulli.local:8181",
      "secret": "tautulli",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "1m",
      "requests": {
        "activity": "/api/v2?cmd=get_activity",
        "status": "/api/v2?cmd=server_status",
        "server": "/api/v2?cmd=get_server_info",
        "history": "/api/v2?cmd=get_history&length=4&include_activity=0"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "activity.response.result",
              "equals": "error"
            },
            {
              "path": "status.response.result",
              "equals": "error"
            },
            {
              "path": "server.response.result",
              "equals": "error"
            },
            {
              "path": "history.response.result",
              "equals": "error"
            }
          ]
        },
        "message": {
          "if": {
            "path": "activity.response.message",
            "exists": true
          },
          "then": {
            "template": "{activity.response.message}"
          },
          "else": {
            "template": "Tautulli answered with an error"
          }
        }
      },
      "derive": [
        {
          "name": "streams",
          "value": {
            "each": "activity.response.data.sessions",
            "fields": {
              "id": "session_key",
              "title": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": "grandparent_title",
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": "parent_title",
                  "else": "title"
                }
              },
              "subtitle": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "template": "S{parent_media_index|pad:2}E{media_index|pad:2} · {title}"
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "template": "{grandparent_title} · {title}"
                  },
                  "else": {
                    "if": {
                      "path": "year",
                      "not": ""
                    },
                    "then": {
                      "template": "{year|raw}"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "who": {
                "template": "{friendly_name} · {player}"
              },
              "method": {
                "path": "transcode_decision",
                "values": {
                  "transcode": "Transcoding",
                  "copy": "Direct stream",
                  "direct play": "Direct play"
                },
                "default": ""
              },
              "state": {
                "path": "state",
                "values": {
                  "paused": "Paused",
                  "buffering": "Buffering"
                },
                "default": ""
              },
              "pos": {
                "divide": [
                  {
                    "path": "view_offset",
                    "match": "^[0-9]+"
                  },
                  60000
                ],
                "round": 0
              },
              "run": {
                "divide": [
                  {
                    "path": "duration",
                    "match": "^[0-9]+"
                  },
                  60000
                ],
                "round": 0
              },
              "image": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "grandparent_thumb",
                        "exists": true
                      },
                      {
                        "path": "grandparent_thumb",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "/api/v2?cmd=pms_image_proxy&img={grandparent_thumb|raw}&width=300&height=450"
                  },
                  "else": {
                    "template": ""
                  }
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "if": {
                      "all": [
                        {
                          "path": "parent_thumb",
                          "exists": true
                        },
                        {
                          "path": "parent_thumb",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "template": "/api/v2?cmd=pms_image_proxy&img={parent_thumb|raw}&width=300&height=300"
                    },
                    "else": {
                      "template": ""
                    }
                  },
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "thumb",
                          "exists": true
                        },
                        {
                          "path": "thumb",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "shape": {
                "if": {
                  "path": "media_type",
                  "equals": "track"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "icon": {
                "path": "media_type",
                "values": {
                  "movie": "film",
                  "episode": "tv",
                  "show": "tv",
                  "season": "tv",
                  "track": "music.note",
                  "album": "music.note",
                  "artist": "music.note",
                  "live": "antenna.radiowaves.left.and.right"
                },
                "default": "play.rectangle"
              },
              "plex_pass": {
                "template": "yes"
              }
            }
          }
        },
        {
          "name": "playing",
          "value": {
            "count": "streams",
            "default": 0
          }
        },
        {
          "name": "idle",
          "value": {
            "if": {
              "path": "status.response.data.connected",
              "equals": false
            },
            "then": {
              "template": "Tautulli can’t reach Plex right now"
            },
            "else": {
              "template": "Nothing playing"
            }
          }
        },
        {
          "name": "plex_pass",
          "value": {
            "if": {
              "path": "server.response.data.pms_plexpass",
              "equals": 1
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ]
    },
    "stats": {
      "url": "http://tautulli.local:8181",
      "secret": "tautulli",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "15m",
      "requests": {
        "plays": "/api/v2?cmd=get_plays_by_date&time_range={period.id}",
        "time": "/api/v2?cmd=get_plays_by_date&time_range={period.id}&y_axis=duration",
        "concurrent": "/api/v2?cmd=get_home_stats&stat_id=most_concurrent&time_range={period.id}",
        "info": "/api/v2?cmd=get_tautulli_info",
        "update": "/api/v2?cmd=update_check",
        "server": "/api/v2?cmd=get_server_info"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "plays.response.result",
              "equals": "error"
            },
            {
              "path": "time.response.result",
              "equals": "error"
            },
            {
              "path": "concurrent.response.result",
              "equals": "error"
            },
            {
              "path": "info.response.result",
              "equals": "error"
            },
            {
              "path": "update.response.result",
              "equals": "error"
            },
            {
              "path": "server.response.result",
              "equals": "error"
            }
          ]
        },
        "message": {
          "if": {
            "path": "plays.response.message",
            "exists": true
          },
          "then": {
            "template": "{plays.response.message}"
          },
          "else": {
            "template": "Tautulli answered with an error"
          }
        }
      },
      "derive": [
        {
          "name": "days",
          "value": "plays.response.data.categories"
        },
        {
          "name": "per_day",
          "value": {
            "first": "data",
            "of": "plays.response.data.series",
            "where": {
              "path": "name",
              "equals": "Total"
            }
          }
        },
        {
          "name": "secs_per_day",
          "value": {
            "first": "data",
            "of": "time.response.data.series",
            "where": {
              "path": "name",
              "equals": "Total"
            }
          }
        },
        {
          "name": "plays",
          "value": {
            "sum": "$",
            "of": "per_day",
            "default": 0
          }
        },
        {
          "name": "watched",
          "value": {
            "sum": "$",
            "of": "secs_per_day",
            "default": 0
          }
        },
        {
          "name": "most",
          "value": {
            "first": "$",
            "of": "concurrent.response.data.rows",
            "where": {
              "path": "title",
              "equals": "Concurrent Streams"
            }
          }
        },
        {
          "name": "most_at",
          "value": {
            "path": "most.started",
            "match": "^[0-9]+$"
          }
        },
        {
          "name": "plex",
          "value": {
            "path": "server.response.data.pms_version",
            "match": "^([0-9]+\\.[0-9]+\\.[0-9]+)"
          }
        },
        {
          "name": "version",
          "value": {
            "path": "info.response.data.tautulli_version",
            "match": "^v?([0-9][0-9.]*)"
          }
        }
      ]
    },
    "top": {
      "url": "http://tautulli.local:8181",
      "secret": "tautulli",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "15m",
      "requests": {
        "top_tv": "/api/v2?cmd=get_home_stats&stat_id=top_tv&stats_type=plays&stats_count=5&time_range={period.id}",
        "top_movies": "/api/v2?cmd=get_home_stats&stat_id=top_movies&stats_type=plays&stats_count=5&time_range={period.id}",
        "top_music": "/api/v2?cmd=get_home_stats&stat_id=top_music&stats_type=plays&stats_count=5&time_range={period.id}",
        "top_users": "/api/v2?cmd=get_home_stats&stat_id=top_users&stats_type=plays&stats_count=5&time_range={period.id}"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "top_tv.response.result",
              "equals": "error"
            },
            {
              "path": "top_movies.response.result",
              "equals": "error"
            },
            {
              "path": "top_music.response.result",
              "equals": "error"
            },
            {
              "path": "top_users.response.result",
              "equals": "error"
            }
          ]
        },
        "message": {
          "if": {
            "path": "top_tv.response.message",
            "exists": true
          },
          "then": {
            "template": "{top_tv.response.message}"
          },
          "else": {
            "template": "Tautulli answered with an error"
          }
        }
      }
    }
  },
  "cards": [
    {
      "id": "tautulli",
      "type": "status",
      "placement": "page_info",
      "label": "Tautulli",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "now",
        "map": {
          "label": {
            "if": {
              "path": "server.response.data.pms_name",
              "exists": true
            },
            "then": {
              "template": "{server.response.data.pms_name}"
            },
            "else": {
              "template": "Tautulli"
            }
          },
          "state": {
            "path": "status.response.data.connected",
            "values": {
              "true": "ok",
              "false": "warning"
            },
            "default": "unknown"
          },
          "detail": {
            "path": "status.response.data.connected",
            "values": {
              "true": "Connected to Plex",
              "false": "Not connected to Plex"
            },
            "default": "Tautulli hasn't said whether it can reach Plex"
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Tautulli isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "now",
      "type": "pacing",
      "label": "Now playing",
      "unit": "min",
      "items": [],
      "source": {
        "ref": "now",
        "empty": {
          "text": "{idle}",
          "when": {
            "path": "playing",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "streams",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "label": "title",
              "subtitle": "subtitle",
              "state": "state",
              "detail": {
                "if": {
                  "path": "method",
                  "equals": ""
                },
                "then": {
                  "template": "{who}"
                },
                "else": {
                  "template": "{who} · {method}"
                }
              },
              "value": "pos",
              "target": "run",
              "image": "image",
              "image_shape": "shape",
              "icon": "icon"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Stop",
          "icon": "stop.fill",
          "method": "POST",
          "path": "/api/v2?cmd=terminate_session&session_key={id|raw}&message=The%20server%20owner%20has%20ended%20the%20stream.",
          "success": {
            "path": "response.result",
            "equals": "success"
          },
          "error": "response.message",
          "confirm": {
            "message": "Stop {title} for {who}?",
            "button": "Stop"
          },
          "when": {
            "all": [
              {
                "path": "id",
                "exists": true
              },
              {
                "path": "id",
                "not": ""
              },
              {
                "path": "plex_pass",
                "equals": {
                  "answer": "plex_pass"
                }
              }
            ]
          }
        }
      ]
    },
    {
      "id": "plays",
      "type": "sparkline",
      "label": "Plays per day",
      "unit": "plays",
      "points": [
        0,
        0
      ],
      "change": false,
      "source": {
        "ref": "stats",
        "empty": {
          "text": "No plays in this period",
          "when": {
            "path": "plays",
            "equals": 0
          }
        },
        "map": {
          "points": "per_day",
          "labels": {
            "each": "days",
            "template": "{$|date}"
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Tautulli",
      "items": [
        {
          "key": "Plays",
          "value": "—"
        },
        {
          "key": "Most at once",
          "value": "—"
        },
        {
          "key": "Tautulli",
          "value": "—"
        },
        {
          "key": "Plex",
          "value": "—"
        }
      ],
      "source": {
        "ref": "stats",
        "map": {
          "items[0].value": {
            "if": {
              "path": "plays",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "path": "plays",
                "equals": 1
              },
              "then": {
                "template": "1 · {watched|duration} watched"
              },
              "else": {
                "template": "{plays} · {watched|duration} watched"
              }
            }
          },
          "items[1].value": {
            "if": {
              "path": "most.count",
              "exists": true
            },
            "then": {
              "if": {
                "path": "most.count",
                "equals": 1
              },
              "then": {
                "template": "1 stream · {most_at|date}"
              },
              "else": {
                "template": "{most.count} streams · {most_at|date}"
              }
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "if": {
              "path": "update.response.data.update",
              "equals": true
            },
            "then": {
              "template": "{version|raw} · Update ready"
            },
            "else": {
              "template": "{version|raw}"
            }
          },
          "items[3].value": {
            "if": {
              "path": "plex",
              "exists": true
            },
            "then": {
              "template": "{plex|raw}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "top",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Top shows",
          "cards": [
            {
              "id": "top-tv",
              "type": "list",
              "image_shape": "poster",
              "max_items": 5,
              "items": [],
              "source": {
                "ref": "top",
                "empty": {
                  "text": "No shows watched in this period",
                  "when": {
                    "value": {
                      "count": "top_tv.response.data.rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "top_tv.response.data.rows",
                    "limit": 5,
                    "fields": {
                      "key": {
                        "template": "{rating_key|raw}"
                      },
                      "text": "title",
                      "detail": {
                        "if": {
                          "path": "total_plays",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 play · {total_duration|duration}"
                        },
                        "else": {
                          "template": "{total_plays} plays · {total_duration|duration}"
                        }
                      },
                      "url": {
                        "template": "http://tautulli.local:8181/info?rating_key={rating_key|raw}"
                      },
                      "image": {
                        "if": {
                          "all": [
                            {
                              "path": "grandparent_thumb",
                              "exists": true
                            },
                            {
                              "path": "grandparent_thumb",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "/api/v2?cmd=pms_image_proxy&img={grandparent_thumb|raw}&width=300&height=450"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "thumb",
                                "exists": true
                              },
                              {
                                "path": "thumb",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "icon": {
                        "template": "tv"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Top movies",
          "cards": [
            {
              "id": "top-movies",
              "type": "list",
              "image_shape": "poster",
              "max_items": 5,
              "items": [],
              "source": {
                "ref": "top",
                "empty": {
                  "text": "No movies watched in this period",
                  "when": {
                    "value": {
                      "count": "top_movies.response.data.rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "top_movies.response.data.rows",
                    "limit": 5,
                    "fields": {
                      "key": {
                        "template": "{rating_key|raw}"
                      },
                      "text": "title",
                      "detail": {
                        "if": {
                          "path": "total_plays",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 play · {total_duration|duration}"
                        },
                        "else": {
                          "template": "{total_plays} plays · {total_duration|duration}"
                        }
                      },
                      "url": {
                        "template": "http://tautulli.local:8181/info?rating_key={rating_key|raw}"
                      },
                      "image": {
                        "if": {
                          "all": [
                            {
                              "path": "grandparent_thumb",
                              "exists": true
                            },
                            {
                              "path": "grandparent_thumb",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "/api/v2?cmd=pms_image_proxy&img={grandparent_thumb|raw}&width=300&height=450"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "thumb",
                                "exists": true
                              },
                              {
                                "path": "thumb",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "icon": {
                        "template": "film"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Top artists",
          "cards": [
            {
              "id": "top-music",
              "type": "list",
              "image_shape": "square",
              "max_items": 5,
              "items": [],
              "source": {
                "ref": "top",
                "empty": {
                  "text": "No music played in this period",
                  "when": {
                    "value": {
                      "count": "top_music.response.data.rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "top_music.response.data.rows",
                    "limit": 5,
                    "fields": {
                      "key": {
                        "template": "{rating_key|raw}"
                      },
                      "text": "title",
                      "detail": {
                        "if": {
                          "path": "total_plays",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 play · {total_duration|duration}"
                        },
                        "else": {
                          "template": "{total_plays} plays · {total_duration|duration}"
                        }
                      },
                      "url": {
                        "template": "http://tautulli.local:8181/info?rating_key={rating_key|raw}"
                      },
                      "image": {
                        "if": {
                          "all": [
                            {
                              "path": "grandparent_thumb",
                              "exists": true
                            },
                            {
                              "path": "grandparent_thumb",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "/api/v2?cmd=pms_image_proxy&img={grandparent_thumb|raw}&width=300&height=450"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "thumb",
                                "exists": true
                              },
                              {
                                "path": "thumb",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "icon": {
                        "template": "music.note"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "viewers",
      "type": "list",
      "label": "Top viewers",
      "max_items": 5,
      "items": [],
      "source": {
        "ref": "top",
        "empty": {
          "text": "No one watched in this period",
          "when": {
            "value": {
              "count": "top_users.response.data.rows",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "top_users.response.data.rows",
            "limit": 5,
            "fields": {
              "key": {
                "template": "{user_id|raw}"
              },
              "text": "friendly_name",
              "subtitle": {
                "template": "Last played {last_play|ago}"
              },
              "detail": {
                "if": {
                  "path": "total_plays",
                  "equals": 1
                },
                "then": {
                  "template": "1 play · {total_duration|duration}"
                },
                "else": {
                  "template": "{total_plays} plays · {total_duration|duration}"
                }
              },
              "url": {
                "template": "legato://page/tautulli-user?user={user_id|raw}"
              }
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "list",
      "label": "Recently watched",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "now",
        "empty": {
          "text": "Nothing watched yet",
          "when": {
            "value": {
              "count": "history.response.data.data",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "history.response.data.data",
            "limit": 4,
            "fields": {
              "key": {
                "template": "{row_id|raw}"
              },
              "text": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "template": "{grandparent_title}"
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "template": "{parent_title}"
                  },
                  "else": {
                    "template": "{title}"
                  }
                }
              },
              "subtitle": {
                "if": {
                  "path": "media_type",
                  "equals": "episode"
                },
                "then": {
                  "template": "S{parent_media_index|pad:2}E{media_index|pad:2} · {title}"
                },
                "else": {
                  "if": {
                    "path": "media_type",
                    "equals": "track"
                  },
                  "then": {
                    "template": "{grandparent_title} · {title}"
                  },
                  "else": {
                    "if": {
                      "path": "percent_complete",
                      "lt": 85
                    },
                    "then": {
                      "if": {
                        "all": [
                          {
                            "path": "year",
                            "exists": true
                          },
                          {
                            "path": "year",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "{year|raw} · stopped at {percent_complete}%"
                      },
                      "else": {
                        "template": "Stopped at {percent_complete}%"
                      }
                    },
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "year",
                            "exists": true
                          },
                          {
                            "path": "year",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "{year|raw}"
                      },
                      "else": {
                        "template": ""
                      }
                    }
                  }
                }
              },
              "detail": {
                "template": "{friendly_name} · {date|ago}"
              },
              "url": {
                "template": "http://tautulli.local:8181/info?rating_key={rating_key|raw}"
              },
              "image": {
                "if": {
                  "all": [
                    {
                      "path": "thumb",
                      "exists": true
                    },
                    {
                      "path": "thumb",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "/api/v2?cmd=pms_image_proxy&img={thumb|raw}&width=300&height=450"
                },
                "else": {
                  "template": ""
                }
              },
              "image_shape": {
                "if": {
                  "path": "media_type",
                  "equals": "track"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "icon": {
                "path": "media_type",
                "values": {
                  "movie": "film",
                  "episode": "tv",
                  "show": "tv",
                  "season": "tv",
                  "track": "music.note",
                  "album": "music.note",
                  "artist": "music.note",
                  "live": "antenna.radiowaves.left.and.right"
                },
                "default": "play.rectangle"
              }
            }
          }
        }
      }
    }
  ]
}
