> ## Documentation Index
> Fetch the complete documentation index at: https://docs.junction.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Examples

> Explore practical code examples of Junction Sense Continuous Query use cases including daily sleep analysis, in both Python and JSON DSL.

A couple of examples to get you started with [Continuous Query](/continuous-query-overview):

## Daily Sleep Analysis

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"), 
      va.Sleep.col("efficiency").mean(),
      va.Sleep.score().mean(),
      va.Sleep.chronotype().newest()
  ).where(
      "type = 'long_sleep'"
  ).group_by(
      va.date_trunc(va.Sleep.index(), 1, "day")
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "where": "type = 'long_sleep'",
    "select": [
      {
        "group_key": "*"
      },
      {
        "arg": {
          "sleep": "efficiency"
        },
        "func": "mean"
      },
      {
        "arg": {
          "value_macro": "sleep_score",
          "version": "automatic"
        },
        "func": "mean"
      },
      {
        "arg": {
          "value_macro": "chronotype",
          "version": "automatic"
        },
        "func": "newest"
      }
    ],
    "group_by": [
      {
        "date_trunc": {
          "value": 1,
          "unit": "day"
        },
        "arg": {
          "index": "sleep"
        }
      }
    ]
  }
  ```
</CodeGroup>

## Weekly Insights Into Users' Activity

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"), 
      va.Activity.col("heart_rate_resting").mean(),
      va.Activity.col("calories_total").max(),
      va.Activity.col("steps").min(),
      va.Activity.col("duration_active_second").mean()
  ).group_by(
      va.date_trunc(va.Activity.index(), 1, "week")
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "select": [
      {
        "group_key": "*"
      },
      {
        "arg": {
          "activity": "heart_rate_resting"
        },
        "func": "mean"
      },
      {
        "arg": {
          "activity": "calories_total"
        },
        "func": "max"
      },
      {
        "arg": {
          "activity": "steps"
        },
        "func": "min"
      },
      {
        "arg": {
          "activity": "duration_active_second"
        },
        "func": "mean"
      }
    ],
    "group_by": [
      {
        "date_trunc": {
          "value": 1,
          "unit": "week"
        },
        "arg": {
          "index": "activity"
        }
      }
    ]
  }
  ```
</CodeGroup>

## First Glucose Measurement Of The Day Grouped By Source Type and Provider

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"),
      va.Timeseries.col("glucose").field("value").oldest()
  ).group_by(
      va.date_trunc(va.Timeseries.index(), 1, "day"),
      va.Source.col("source_provider"),
      va.Source.col("source_type")
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "select": [
        {
          "group_key": "*"
        },
        {
          "arg": {
            "field": "value",
            "timeseries": "glucose"
          },
          "func": "oldest"
        }
      ],
      "group_by": [
        {
          "arg": {
            "index": "timeseries"
          },
          "date_trunc": {"unit": "day", "value": 1}
        },
        {"source": "source_provider"},
        {"source": "source_type"}
      ]
  }
  ```
</CodeGroup>

## Daily Summaries of Metabolic Biomarkers Grouped By Source Type and Provider

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"),
      va.Timeseries.col("glucose").field("value").mean(),
      va.Timeseries.col("heartrate").field("value").mean(),
      va.Timeseries.col("steps").field("value").sum(),
      va.Timeseries.col("hrv").field("value").mean(),
      va.Timeseries.col("calories_active").field("value").sum(),
      va.Timeseries.col("body_temperature").field("value").mean(),
      va.Timeseries.col("body_temperature").field("value").min(),
      va.Timeseries.col("body_temperature").field("value").max(),
  ).group_by(
      va.date_trunc(va.Timeseries.index(), 1, "day"),
      va.Source.col("source_provider"),
      va.Source.col("source_type"),
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "select": [
        {
          "group_key": "*"
        },
        {
          "arg": {
            "timeseries": "glucose",
            "field": "value"
          },
          "func": "mean"
        },
        {
          "arg": {
            "timeseries": "heartrate",
            "field": "value"
          },
          "func": "mean"
        },
        {
          "arg": {
            "timeseries": "steps",
            "field": "value"
          },
          "func": "sum"
        },
        {
          "arg": {
            "timeseries": "hrv",
            "field": "value"
          },
          "func": "mean"
        },
        {
          "arg": {
            "timeseries": "calories_active",
            "field": "value"
          },
          "func": "sum"
        },
        {
          "arg": {
            "timeseries": "body_temperature",
            "field": "value"
          },
          "func": "mean"
        },
        {
          "arg": {
            "timeseries": "body_temperature",
            "field": "value"
          },
          "func": "min"
        },
        {
          "arg": {
            "timeseries": "body_temperature",
            "field": "value"
          },
          "func": "max"
        }
      ],
      "group_by": [
        {
          "arg": {
            "index": "timeseries"
          },
          "date_trunc": {"unit": "day", "value": 1}
        },
        {"source": "source_provider"},
        {"source": "source_type"}
      ]
  }
  ```
</CodeGroup>

## Weekly Exercise Summary

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"),
      va.Workout.col("calories").max(),
      va.Workout.col("calories").min(),
      va.Workout.col("heart_rate_zone_1").mean(),
      va.Workout.col("heart_rate_zone_2").mean(),
      va.Workout.col("heart_rate_zone_3").mean(),
      va.Workout.col("heart_rate_zone_4").mean(),
      va.Workout.col("heart_rate_zone_5").mean(),
      va.Workout.col("heart_rate_zone_6").mean(),
      va.Workout.col("distance_meter").max(),
      va.Workout.col("duration_active_second").mean()
  ).group_by(
      va.date_trunc(va.Workout.index(), 1, "week")
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "select": [
      {
        "group_key": "*"
      },
      {
        "arg": {
          "workout": "calories"
        },
        "func": "max"
      },
      {
        "arg": {
          "workout": "calories"
        },
        "func": "min"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_1"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_2"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_3"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_4"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_5"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "heart_rate_zone_6"
        },
        "func": "mean"
      },
      {
        "arg": {
          "workout": "distance_meter"
        },
        "func": "max"
      },
      {
        "arg": {
          "workout": "duration_active_second"
        },
        "func": "mean"
      }
    ],
    "group_by": [
      {
        "date_trunc": {
          "value": 1,
          "unit": "week"
        },
        "arg": {
          "index": "workout"
        }
      }
    ]
  }
  ```
</CodeGroup>

## Menstrual Cycle Summary

Period end, cycle end, mean basal body temperature, and number of meaningful flow days — one row per cycle.

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.group_key("*"),
      va.MenstrualCycle.col("period_end").newest(),
      va.MenstrualCycle.col("cycle_end").newest(),
      va.MenstrualCycle.col("basal_body_temperature")
          .unnest_and_select(lambda col: col.field("value").mean())
          .mean(),
      va.MenstrualCycle.col("menstrual_flow")
          .unnest_and_select(lambda col: col.count())
          .where("flow != 'none'")
          .mean(),
  ).group_by(
      va.date_trunc(va.MenstrualCycle.index(), 1, "day")
  ).finalize()
  ```

  ```jsonc JSON DSL theme={null}
  {
    "select": [
      { "group_key": "*" },
      { "func": "newest", "arg": { "menstrual_cycle": "period_end" } },
      { "func": "newest", "arg": { "menstrual_cycle": "cycle_end" } },
      {
        "func": "mean",
        "arg": {
          "select": {
            "func": "mean",
            "arg": { "field_for": "menstrual_cycle", "basal_body_temperature": "value" }
          },
          "from": { "unnest": { "menstrual_cycle": "basal_body_temperature" } }
        }
      },
      {
        "func": "mean",
        "arg": {
          "select": { "func": "count", "arg": null },
          "from": { "unnest": { "menstrual_cycle": "menstrual_flow" } },
          "where": "flow != 'none'"
        }
      }
    ],
    "group_by": [
      {
        "date_trunc": { "value": 1, "unit": "day" },
        "arg": { "index": "menstrual_cycle" }
      }
    ]
  }
  ```
</CodeGroup>
