{
  "id": null,
  "uid": "puls-yesterday",
  "title": "Yesterday in Apple Health",
  "description": "Recorded steps, asleep interval coverage and workouts. Set the dashboard timezone to the user's reporting zone.",
  "schemaVersion": 39,
  "version": 1,
  "editable": true,
  "timezone": "browser",
  "refresh": "",
  "time": {
    "from": "now-1d/d",
    "to": "now-1d/d"
  },
  "tags": [
    "pulshealth",
    "tutorial"
  ],
  "panels": [
    {
      "id": 1,
      "title": "Daily steps by provenance",
      "type": "timeseries",
      "description": "Daily values displayed in the selected account reporting zone; phone aggregates retain their recorded calendar day. Aggregate and fallback series stay distinct. Edge days are whole-day totals.",
      "datasource": {
        "type": "postgres",
        "uid": "puls-tsdb"
      },
      "gridPos": {
        "x": 0,
        "y": 0,
        "w": 12,
        "h": 8
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "postgres",
            "uid": "puls-tsdb"
          },
          "rawQuery": true,
          "rawSql": "SELECT day::timestamp AT TIME ZONE puls_user_time_zone(${user:sqlstring}::uuid) AS time,\n       value, source AS metric\nFROM metric_daily\nWHERE identifier = 'HKQuantityTypeIdentifierStepCount'\n  AND user_id = ${user:sqlstring}::uuid\n  AND day >= ($__timeFrom()::timestamptz AT TIME ZONE puls_user_time_zone(${user:sqlstring}::uuid))::date\n  AND day <= (($__timeTo()::timestamptz - interval '1 microsecond') AT TIME ZONE puls_user_time_zone(${user:sqlstring}::uuid))::date\nORDER BY time",
          "format": "time_series"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "short",
          "noValue": "No data",
          "custom": {
            "spanNulls": false,
            "showPoints": "always",
            "lineWidth": 0
          }
        },
        "overrides": []
      },
      "options": {}
    },
    {
      "id": 2,
      "title": "Time recorded asleep in range",
      "type": "stat",
      "datasource": {
        "type": "postgres",
        "uid": "puls-tsdb"
      },
      "gridPos": {
        "x": 12,
        "y": 0,
        "w": 12,
        "h": 8
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "postgres",
            "uid": "puls-tsdb"
          },
          "rawQuery": true,
          "rawSql": "WITH asleep AS (\n  SELECT range_agg(tstzrange(greatest(c.start_ts, $__timeFrom()::timestamptz),\n                             least(c.end_ts, $__timeTo()::timestamptz), '[)')) AS spans\n  FROM category_samples c\n  JOIN sample_types st ON st.type_id = c.type_id\n  WHERE st.identifier = 'HKCategoryTypeIdentifierSleepAnalysis'\n    AND c.user_id = ${user:sqlstring}::uuid\n    AND c.value IN (1, 3, 4, 5)\n    AND c.start_ts < $__timeTo() AND c.end_ts > $__timeFrom()\n    AND c.end_ts > c.start_ts\n)\nSELECT sum(extract(epoch FROM upper(span) - lower(span))) / 3600 AS hours\nFROM asleep CROSS JOIN LATERAL unnest(spans) AS span",
          "format": "table"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "h",
          "noValue": "No data"
        },
        "overrides": []
      },
      "options": {}
    },
    {
      "id": 3,
      "title": "Recorded workout duration",
      "type": "timeseries",
      "description": "One point per workout; records, not proof of complete activity coverage.",
      "datasource": {
        "type": "postgres",
        "uid": "puls-tsdb"
      },
      "gridPos": {
        "x": 0,
        "y": 8,
        "w": 24,
        "h": 8
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "postgres",
            "uid": "puls-tsdb"
          },
          "rawQuery": true,
          "rawSql": "SELECT start_ts AS time, duration_s / 60.0 AS minutes\nFROM workouts\nWHERE user_id = ${user:sqlstring}::uuid\n  AND start_ts >= $__timeFrom() AND start_ts < $__timeTo()\nORDER BY time",
          "format": "time_series"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "m",
          "noValue": "No data",
          "custom": {
            "spanNulls": false,
            "showPoints": "always",
            "lineWidth": 0
          }
        },
        "overrides": []
      },
      "options": {}
    }
  ],
  "templating": {
    "list": [
      {
        "name": "user",
        "label": "User UUID",
        "type": "textbox",
        "current": {
          "text": "00000000-0000-0000-0000-000000000000",
          "value": "00000000-0000-0000-0000-000000000000"
        },
        "options": [],
        "query": "00000000-0000-0000-0000-000000000000",
        "hide": 0,
        "skipUrlSync": true
      }
    ]
  },
  "annotations": {
    "list": []
  }
}
