当前位置:网站首页>Using redis bitmap to realize personnel online monitoring
Using redis bitmap to realize personnel online monitoring
2022-06-28 01:58:00 【OUO~】
One : Business scenario
It is necessary to display and count the online status of some people ( The same day or a specified number of days )
Two : Code
Here I am using websocket Heartbeat mechanism redis bitmap Data insertion
/** * receive userId */
private String userId = "";
// Omit here - Successfully set up websocket Assign values when connecting userId
/** * The heartbeat detection * * @param message The message sent by the client */
@OnMessage
public void onMessage(String message, Session session) throws IOException {
// The online status is mainly set here - Instance independent code
redisTemplate.opsForValue().set(Constant.CASE_ONLINE+this.userId
,System.currentTimeMillis(),Constant.ONLINE_MAX_ALIVE_SECOND, TimeUnit.SECONDS);
// The same day of the current month --( Since there is error in directly calculating the number of milliseconds in a day, the following stupid method is used to get the current number of steps )
String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
int m = DateUtil.thisMinute();
int hour = DateUtil.thisHour(true);
// Get the number of steps -- In minutes per day 00:00-24:00-- The most steps a day 1,440 position
long index = hour* 60L +m;
// To set up KEY value
String key="ONLINE:"+this.userId+":"+yyyyMMdd;
// Deposit in redis in ,index Represents the current number of steps
redisTemplate.opsForValue().setBit(key, index, true);
// Detect whether there is an expiration time , Set if none 7 Days overdue
Long expire = redisTemplate.getExpire(key);
if (expire==null || expire < 1L){
redisTemplate.expire(key,7,TimeUnit.DAYS);
}
}
The effect is as follows , Note that the place marked is set to binary To see 01

Let's write an interface to query the online situation
/** * @Author: OUO * @DateTime: 2022/6/1 11:17 * @Description: Get the online status of users -- On the same day or on a specified day , The format is YYYYMMDD */
@RequestMapping("/queryOnlineInfo")
public Result queryOnlineInfo(String userId,String times){
Map<String,Object> map=new HashMap<>();
List<String> dateList=new ArrayList<>();
String yyyyMMdd = times;
int m = DateUtil.thisMinute();
int hour = DateUtil.thisHour(true);
// Get the number of steps so far ,-- In minutes
long index = hour* 60L +m;
// Set up KEY value
String key="ONLINE:"+userId+":"+yyyyMMdd;
// Query the total number of valid steps inserted by yourself , For example, you successfully insert 10 Time , The result here is 10-- This is not used yet , Generally used for statistics
Long execute = jsonRedisTemplate.execute((RedisCallback<Long>) conn -> conn.bitCount(key.getBytes()));
List<String> list = new ArrayList<>();
for (int i = 0; i < index; i++) {
// According to the number of steps Get all the values step by step and assign values
Boolean bit = jsonRedisTemplate.opsForValue().getBit("ONLINE:" + userId + ":" + yyyyMMdd, i);
// establish Y Axis data source
list.add((Boolean.TRUE.equals(bit) ?" On-line ":" offline "));
// establish X Axis data source
Date dateTime = DateUtil.offsetMinute(DateUtil.beginOfDay(new Date()),i);
String format = DateUtil.format(dateTime, "HH:mm");
dateList.add(format);
}
// In order to meet Echarts Figure so set XY
map.put("x",dateList);
map.put("y",list);
return Result.build(true,"200",map," The query is successful ");
}
}
design sketch

边栏推荐
猜你喜欢

Data analysts too hot? Monthly income 3W? Tell you the true situation of this industry with data

How to build dual channel memory for Lenovo Savior r720
![完全二叉树的节点个数[非O(n)求法 -> 抽象二分]](/img/56/768f8be9f70bf751f176e40cbb1df2.png)
完全二叉树的节点个数[非O(n)求法 -> 抽象二分]

The practice of dual process guard and keeping alive in IM instant messaging development

Web3 技术初体验以及相关学习资料

TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language

Cloud assisted privacy collection intersection (server assisted psi) protocol introduction: Learning

Ten thousand words long article understanding business intelligence (BI) | recommended collection

frp实现内网穿透

万字长文看懂商业智能(BI)|推荐收藏
随机推荐
Want to open an account to buy stock, is it safe to open an account on the Internet?
766. toplitz matrix
fiddle如何使用代理
评价——灰色关联分析
要搞清楚什么是同步,异步,串行,并行,并发,进程,线程,协程
Intensive reading of transformer thesis paragraph by paragraph
Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa
style中的scoped属性和lang属性
MySQL十种锁,一篇文章带你全解析
零基礎多圖詳解圖神經網絡
[Yocto RM] 4 - Source Directory Structure
【开源】开源系统整理-考试问卷等
pytorch_lightning.utilities.exceptions.MisconfigurationException: You requested GPUs: [1] But...
Ten MySQL locks, one article will give you full analysis
混合app的介绍
国外LEAD赚钱的一些习惯
Centos8 operation record command version Yum redis MySQL Nacos JDK
Ai+ clinical trial patient recruitment | massive bio completed round a financing of $9million
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
Lmsoc: a socially sensitive pre training method