• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Build a Real-Time Fitness Wearable Data Analysis API with Tinybird

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
In the realm of health and fitness technology, the ability to analyze wearable device data in real-time can offer users immediate insights into their physical activities, heart rate patterns, and overall health metrics. Crafting an API to process this data efficiently requires a robust backend capable of handling high-velocity data ingestion and complex analytical queries. This tutorial guides you through building an API using Tinybird, which ingests raw fitness metrics like steps, heart rate, and calories burned, and provides

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

for retrieving daily summaries, recent activity, and heart rate analysis. Tinybird is a data analytics backend for software developers. You use Tinybird to build real-time analytics APIs without needing to set up or manage the underlying infrastructure. It leverages data sources and pipes to ingest, transform, and serve large volumes of data through high-performance APIs. In this tutorial, we'll use Tinybird to implement a solution that allows for real-time analysis of fitness wearable data, providing valuable insights into individual user activities and health trends.

Understanding the data


Imagine your data looks like this:


{"user_id": "user_23", "device_id": "device_3", "timestamp": "2025-04-27 15:52:24", "steps": 522, "heart_rate": 82, "calories_burned": 172.2, "distance_meters": 552.2, "sleep_minutes": 122, "active_minutes": 2}
{"user_id": "user_51", "device_id": "device_11", "timestamp": "2025-04-28 03:52:24", "steps": 1150, "heart_rate": 110, "calories_burned": 135, "distance_meters": 1115, "sleep_minutes": 110, "active_minutes": 110}

This data represents metrics collected from fitness wearables, such as steps walked, heart rate, calories burned, and distance covered. To store this data, we create a Tinybird data source with a schema designed to optimize query performance.


DESCRIPTION >
Raw fitness wearable data containing metrics like steps, heart rate, and calories burned

SCHEMA >
`user_id` String `json:$.user_id`,
`device_id` String `json:$.device_id`,
`timestamp` DateTime `json:$.timestamp`,
`steps` Int32 `json:$.steps`,
`heart_rate` Int16 `json:$.heart_rate`,
`calories_burned` Float32 `json:$.calories_burned`,
`distance_meters` Float32 `json:$.distance_meters`,
`sleep_minutes` Int16 `json:$.sleep_minutes`,
`active_minutes` Int16 `json:$.active_minutes`

ENGINE "MergeTree"
ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
ENGINE_SORTING_KEY "user_id, timestamp"

This schema includes types and keys that ensure efficient data storage and querying. For instance, sorting by user_id and timestamp improves the performance of time-series queries. Tinybird's

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

allows you to stream JSON/NDJSON events from your application frontend or backend with a simple HTTP request:


curl -X POST "

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

" \
-H "Authorization: Bearer $TB_ADMIN_TOKEN" \
-d '{
"user_id": "user123",
"device_id": "device456",
"timestamp": "2023-03-15 14:30:00",
"steps": 8542,
"heart_rate": 72,
"calories_burned": 325.5,
"distance_meters": 6250.0,
"sleep_minutes": 0,
"active_minutes": 45
}'

This ingestion method is particularly suited for real-time data with low latency. Other ingestion options include a Kafka connector for event/streaming data, which allows for reliable, scalable ingestion, and the

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

and S3 connector for batch/file data.

Transforming data and publishing APIs


Tinybird's

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

enable batch and real-time data transformations and the creation of API endpoints. Let's explore how to use pipes to build our fitness data analysis API.

Daily User Summary


The user_daily_summary pipe summarizes fitness metrics by user and day:


DESCRIPTION >
Summarizes fitness metrics by user and day

NODE user_daily_metrics
SQL >
SELECT
user_id,
toDate(timestamp) as date,
sum(steps) as total_steps,
avg(heart_rate) as avg_heart_rate,
sum(calories_burned) as total_calories,
sum(distance_meters) as total_distance,
sum(sleep_minutes) as total_sleep,
sum(active_minutes) as total_active
FROM fitness_data
WHERE 1=1
AND user_id = '{{String(user_id)}}'
AND date >= '{{Date(start_date, "2023-01-01")}}'
AND date <= '{{Date(end_date, "2023-12-31")}}'
GROUP BY user_id, date
ORDER BY user_id, date DESC

TYPE endpoint

This pipe creates an endpoint that aggregates daily fitness data for a user, allowing for flexible date range queries. The SQL logic demonstrates how to efficiently summarize time-series data.

Recent Activity


The recent_activity pipe retrieves the most recent fitness data entries for a specific user:


DESCRIPTION >
Retrieves the most recent fitness data for a specific user

NODE recent_user_activity
SQL >
SELECT
user_id,
device_id,
timestamp,
steps,
heart_rate,
calories_burned,
distance_meters,
sleep_minutes,
active_minutes
FROM fitness_data
WHERE 1=1
AND user_id = '{{String(user_id)}}'
ORDER BY timestamp DESC
LIMIT {{Int32(limit, 100)}}

TYPE endpoint

This endpoint enables querying of the latest activities by user, showcasing the real-time capabilities of Tinybird APIs.

Heart Rate Analysis


The heart_rate_analysis pipe provides heart rate statistics for users over a specified period:


DESCRIPTION >
Provides heart rate statistics for users

NODE heart_rate_stats
SQL >
SELECT
user_id,
toDate(timestamp) as date,
min(heart_rate) as min_heart_rate,
max(heart_rate) as max_heart_rate,
avg(heart_rate) as avg_heart_rate,
count() as readings_count
FROM fitness_data
WHERE heart_rate > 0
AND user_id = '{{String(user_id)}}'
AND date >= '{{Date(start_date, "2023-01-01")}}'
AND date <= '{{Date(end_date, "2023-12-31")}}'
GROUP BY user_id, date
ORDER BY date DESC

TYPE endpoint

This analysis is critical for monitoring cardiovascular health and exercise intensity over time.

Deploying to production


Deploy your Tinybird project to the cloud with the following command:


tb --cloud deploy

This command makes your data APIs production-ready and scalable. Tinybird manages resources as code, facilitating integration with CI/CD pipelines and ensuring that your APIs are secure and performant. Here's an example of how to call one of the deployed endpoints:


curl -X GET "

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

"

Token-based authentication secures your endpoints, ensuring data privacy and integrity.

Conclusion


In this tutorial, we've built a real-time API for analyzing fitness wearable data using Tinybird. We've covered data ingestion, transformation, and the creation of API endpoints tailored for real-time fitness data analysis, including daily summaries, recent activity, and heart rate analysis. The benefits of using Tinybird for this use case include efficient data management, real-time analytics, and scalable, secure API endpoints.

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

to build and deploy your first real-time data APIs in a few minutes. Start for free, with no time limit and no credit card required, and leverage the power of real-time data analytics for your projects.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу