Overview

Sahha provides your app with daily stats directly from Apple Health and Google Health Connect.

Stats are daily summaries of user activity.

For example:

  • the total number of steps taken in a day
  • the average heart rate for a day
  • the total minutes slept at night

Stats are calculated in realtime for a 24 hour period

Using the getStats method will give you the most up-to-date calculation for a SahhaSensor for the current day.

For example, if you'd like to monitor a user's daily steps, you can call getStats(.steps) multiple times during the day - we suggest using it everytime the user opens your app.

The total number of steps for the current day will continue to increase until midnight in the user's local timezone.

This is very useful for monitoring daily changes in user activity.

After midnight, a new day will begin and a new stat will start to be calculated.

Using getStats for previous days will always return the total value that was calculated at midnight for that day.


Get stats for today

Get the most recent stats for the current day in the user's local timezone.

MyApp.swift
Swift
// Get stats for today

Sahha.getStats(
    sensor: .steps,
    startDateTime: .now,
    endDateTime: .now)
{ error, stats in
    if let error = error {
        print(error)
    } else if stats.isEmpty == false {
        print(stats)
    }
}

Get stats for the last week

You can provide a data range if you would like to receive multiple stats over a specific time period. The response will include an array of stats for each 24 hour segment in that time period.

MyApp.swift
Swift
// Get stats for last 7 days
// Add a date range

let today = Date()
let sevenDaysAgo = Calendar.current.date(byAdding: .day, value: -7, to: today) ?? Date()

Sahha.getStats(
    sensor: .steps,
    startDateTime: sevenDaysAgo,
    endDateTime: today)
{ error, stats in
    if let error = error {
        print(error)
    } else if stats.isEmpty == false {
        print(stats)
    }
}

Stat response

Stats are returned as an array of objects in JSON format.

  • Parameters:
    • id: UUID for the stat
    • type: The type of the stat (steps, sleep, etc.)
    • startDateTime: Start date-time in "yyyy-MM-dd'T'HH:mm:ss.SSZZZZZ" format ("2021-10-27T16:34:06-06:00")
    • endDateTime: End date-time in "yyyy-MM-dd'T'HH:mm:ss.SSZZZZZ" format ("2021-10-27T16:44:06-06:00")
    • value: Numerical value of the stat (100, 200, etc.)
    • unit: The unit for measuring the value (count, minute, kg, bpm, etc.)
    • sources: An array of sources which recorded the stat in com.company.app format (["com.apple.health"])

For iOS and Android, stats are returned as an array of objects in SahhaStat format.

SahhaStat.swift
Swift
public struct SahhaStat: Comparable, Codable {
    public var id: String
    public var type: String
    public var value: Double
    public var unit: String
    public var startDateTime: Date
    public var endDateTime: Date
    public var sources: [String]
}

Array response

Stats are returned as an array of objects in JSON format.

Older stats will be at the start of the array.

Newer stats will be at the end of the array.

An example response includes these fields:

RESPONSE
JSON
[
	{
		"value": 5616,
		"id": "464A029C-D2B6-4A49-A94F-01BFEC642CBA",
		"sources": ["com.apple.health.0C1972B1-DBA3-4B25-9FE7-E9C00DF1DCE6"],
		"unit": "count",
		"startDateTime": "2024-12-13T00:00:00.00+09:00",
		"endDateTime": "2024-12-14T00:00:00.00+09:00",
		"type": "steps"
	},
	{
		"value": 7576,
		"sources": ["com.apple.health.0C1972B1-DBA3-4B25-9FE7-E9C00DF1DCE6"],
		"endDateTime": "2024-12-15T00:00:00.00+09:00",
		"unit": "count",
		"id": "A3313BE9-AD16-4C1B-B8DE-0D1916A517F1",
		"type": "steps",
		"startDateTime": "2024-12-14T00:00:00.00+09:00"
	},
	{
		"startDateTime": "2024-12-15T00:00:00.00+09:00",
		"type": "steps",
		"sources": ["com.apple.health.0C1972B1-DBA3-4B25-9FE7-E9C00DF1DCE6"],
		"unit": "count",
		"id": "24CB1DE7-7B3B-40CE-BF51-1584364991C3",
		"value": 227,
		"endDateTime": "2024-12-16T00:00:00.00+09:00"
	}
]

Empty response

If no stats are found for the SahhaSensor and time period you specify, you will receive an error message.

RESPONSE
JSON
"No stats found"

Using SahhaSensor

Stats are grouped by SahhaSensor type.

For example, use getStats(.heartRate) to get stats of a user's heart rate.

Choose a SahhaSensor

You must specify which SahhaSensor to use for getStats.

SahhaSensorAndroidiOS
genderSensor Not AvailableSensor Not Available
date_of_birthSensor Not AvailableSensor Not Available
sleep✔✔
steps✔✔
floors_climbed✔✔
heart_rate✔✔
resting_heart_rate✔✔
walking_heart_rate_averageSensor Not Available✔
heart_rate_variability_rmssd✔Sensor Not Available
heart_rate_variability_sdnnSensor Not Available✔
blood_pressure_systolicSensor Not Available✔
blood_pressure_diastolicSensor Not Available✔
blood_glucoseSensor Not Available✔
vo2_maxSensor Not Available✔
oxygen_saturationSensor Not Available✔
respiratory_rateSensor Not Available✔
active_energy_burned✔✔
basal_energy_burnedSensor Not Available✔
total_energy_burned✔Sensor Not Available
basal_metabolic_rate✔Sensor Not Available
time_in_daylightSensor Not Available✔
body_temperatureSensor Not Available✔
basal_body_temperatureSensor Not Available✔
sleeping_wrist_temperatureSensor Not Available✔
height✔✔
weight✔✔
lean_body_massSensor Not Available✔
body_mass_indexSensor Not Available✔
body_fatSensor Not Available✔
body_water_massSensor Not AvailableSensor Not Available
bone_massSensor Not AvailableSensor Not Available
waist_circumferenceSensor Not Available✔
stand_timeSensor Not Available✔
move_timeSensor Not Available✔
exercise_timeSensor Not Available✔
activity_summarySensor Not AvailableSensor Not Available
device_lockSensor Not AvailableSensor Not Available
exercise✔Sensor Not Available
Docs