Cập nhật 29/3/21: https://coronavirusapi.com/ gần đây đóng free access dữ liệu, chỉ dành cho first-responders đăng ký. Tuy nhiên demo visualize coronavirus case không còn hoạt động, nhưng gợi ý vẫn hữu ích cho nhiều implementation khác khi cần hiển thị RedisTimeSeries data trên Grafana Worldmap panel.
Trong bài trước, giới thiệu và chỉ cách dùng Redis Data Source for Grafana mới:
- Introducing the Redis Data Source Plug-in for Grafana
- How to Use the New Redis Data Source for Grafana Plug-in
Bài này khám phá ví dụ thực tế để đưa ý tưởng về nhiều dataset thú vị bạn có thể lưu Redis và visualize Grafana. Cover 3 ứng dụng khác nhau:
- Weather dashboard
- Coronavirus cases visualization
- Pop-up store demo
1. Weather dashboard
Tôi có thực sự nghĩ thế giới cần weather dashboard nữa? Có!
Chắc hầu hết thời gian tôi không phải weather geek. Thường không care thời tiết thế nào. Khi care, best dashboard thường chỉ là cửa sổ nhìn ra ngoài nhà. Nhưng đôi khi, khi tìm thời gian rảnh đi chụp ảnh hoặc bay drone, inner weather geek thức tỉnh.
Câu hỏi lớn: đi đâu? Nhiều location thú vị trong vòng 2 tiếng lái từ nhà New Jersey, thời tiết khác biệt lớn. Tôi muốn dashboard hiển thị thời tiết chục location trên một màn hình, so sánh điều kiện ở một cái nhìn. Quan trọng, không chỉ so sánh location gần nhau.

Quan trọng không kém, tôi thích làm việc với số. Biết landscape photography tốt nhất khi cloud cover 20-50%. Bay drone, wind speed dưới 10 mph ideal, 10-20 tough, trên 20 dangerous. Weather icon cute như này không giúp:
Với đó, đây cách tôi xây weather dashboard dùng Redis.
May mắn raw weather data rộng rãi qua nhiều weather API, nhiều free (với limit). Chọn OpenWeatherMap API, free plan cho tất cả cần: 48h hourly forecast, 7-day daily forecast, current condition.
Viết Python script đơn giản pull data từ API đưa vào Redis database dùng RedisTimeSeries module cho hourly/daily forecast, current condition vào time series khác cho history.
Weather dashboard tuân theo 2 quy tắc bắt buộc dashboard:
- Tất cả dữ liệu hiển thị trên một màn hình, không scroll.
- Người hiểu tình huống dashboard nhanh nhất có thể.

Phần trên dashboard hiển thị current condition một location, có thể primary/favorite/chọn từ list (có Grafana template variable selection list góc trên trái). Cũng hiển thị khung thời gian tốt cho hoạt động cụ thể location đó. Grafana cho highlight low/high zone temperature, dangerous zone wind speed, display độ cloudiness, mark daytime/nighttime.
Phần dưới group location theo activity, dễ so sánh. Circle càng to, giờ tốt cho activity tại location đó càng nhiều.

2. Coronavirus cases visualization
Tôi thích travel, nhưng 2020 khiến hàng triệu người ở nhà tháng (hy vọng không mãi!). Giờ nhiều state dần reopen, rất quan trọng travel smart và hiểu tình hình COVID-19 cả origin và destination. Nghĩa là chú ý không chỉ current picture mà cả longer-term trends.
Đó là gì Grafana U.S. States Coronavirus Dashboard hiển thị. Dựa trên Coronavirus API data, load vào RedisTimeSeries module.

Bên cạnh graph representation tiêu chuẩn, data cũng vẽ trên map. Redis Data Source plug-in không officially supported Grafana Worldmap panel, nhưng làm work trong 2 bước đơn giản:
Bước 1: Apply transformation "Labels to fields" cho time-series output result:

Bước 2: Thay đổi map parameter "Location Data" sang table mode và map field liên quan:

Hai bước này cho phép gán time-series value đến point liên quan trên map, set bởi label "geohash" của RedisTimeSeries. Label "state" dùng hiển thị tên location trên map.
3. Pop-up store demo
Tôi fan lớn Redis Streams, kiểu dữ liệu mới Redis 5.0, tìm fast và simple solution monitor queue cho data processing. Làm việc trên Redis Data Source, team explore RedisGears — framework động cho dev viết và thực thi function implement data flow trong Redis trừu tượng hóa phân phối và triển khai — cho project khác và quyết định dùng chung cho data-pipeline demo pop-up store.
RedisGears hỗ trợ nhiều loại reader operate trên loại input data khác. Để watch streams message, dùng StreamReader ở event mode. Ở mode này, reader thực thi response đến event từ message mới thêm vào stream.
# Add Time-Series def tsAdd(x): xlen = execute('XLEN', x['key']) execute('TS.ADD', 'ts:len:'+x['key'], '*', xlen) execute('TS.ADD', 'ts:enqueue:' + x['key'], '*', x['value']) # Stream Reader for any Queue gb = GearsBuilder('StreamReader') gb.countby(lambda x: x['key']).map(tsAdd) gb.register(prefix='queue:*', duration=5000, batch=10000, trimStream=False)
StreamReader chấp nhận nhiều argument kiểm soát trigger. Trong case này, muốn reader trigger mỗi 5 giây hoặc sau 10.000 message. Option cuối, trimStream, chỉ định không trim stream sau execution.
Dùng RedisTimeSeries lưu số message đến và sample queue-size. Như giải thích bài giới thiệu Redis Data Source Plug-in cho Grafana, time series dễ visualize trong Grafana.

Demo Redis Streams, RedisTimeSeries, RedisGears, Redis Data Source làm việc cùng nhau, tạo Pop-up store demo với dynamic dashboard:

Grafana dashboard hiển thị:
- Product Available: giá trị product key, giảm khi order complete
- Customers Ordering, Orders Processing, Orders Completed: độ dài queue:customers, queue:orders, queue:complete streams
- Customers Overflow: chênh lệch order khách submit và order complete
- Customers Ordering: order tạo trong 5 giây
- Orders In Queue: order chờ xử lý
- Completed Flow: order complete trong 5 giây
Xem GitHub repository project để xem cách generate load, RedisGears scripts, cấu hình Redis Data Source query RedisTimeSeries data dùng TS.RANGE.
Kết luận
Ba kịch bản thực tế này demo cách dùng Redis Data Source for Grafana với các Redis module. Hy vọng chúng inspire bạn dùng technology này và xây dựng ứng dụng riêng.
Bạn sẽ ở good company. Kể từ Redis Data Source plug-in publish trên Grafana repository, đã download >10.000 lần, included popular community plug-ins cải thiện Grafana dashboard, added support Redis cluster, Sentinel, Unix socket, ACL:

Nhưng chưa hết. Stay tuned Redis Tech blog để học về project exciting kết hợp Grafana streaming capabilities với interactivity đưa Grafana beyond observability với redis-cli panel.