Data Integration
Stats
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.
// 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.
// 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 incom.company.app
format (["com.apple.health"]
)
-
For iOS and Android, stats are returned as an array of objects in SahhaStat
format.
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:
[ { "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.
"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
.