时间:2024-03-09 11:51作者:下载吧人气:24
MongoDB按照天数或小时聚合
需求
最近接到需求,需要对用户账户下的设备状态,分别按照天以及小时进行聚合,以此为基础绘制设备状态趋势图.
实现思路是启动定时任务,对各用户的设备状态数据分别按照小时以及天进行聚合,并存储进数据库中供用户后续查询.
涉及到的技术栈分别为:Spring Boot
,MongoDB,Morphia
.
数据模型
@Data
@Builder
@Entity(value = “rawDevStatus”, noClassnameStored = true)
// 设备状态索引
@Indexes({
// 设置数据超时时间(TTL,MongoDB根据TTL在后台进行数据删除操作)
@Index(fields = @Field(“time”), options = @IndexOptions(expireAfterSeconds = 3600 * 24 * 72)),
@Index(fields = {@Field(“userId”), @Field(value = “time”, type = IndexType.DESC)})
})
public class RawDevStatus {
@Id
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private ObjectId objectId;
private String userId;
private Instant time;
@Embedded(“points”)
List<Point> protocolPoints;
@Data
@AllArgsConstructor
public static class Point {
/**
* 协议类型
*/
private Protocol protocol;
/**
* 设备总数
*/
private Integer total;
/**
* 设备在线数目
*/
private Integer onlineNum;
/**
* 处于启用状态设备数目
*/
private Integer enableNum;
}
}
网友评论