時間:2024-03-09 11:51作者:下載吧人氣:20
MongoDB按照天數(shù)或小時聚合
需求
最近接到需求,需要對用戶賬戶下的設備狀態(tài),分別按照天以及小時進行聚合,以此為基礎繪制設備狀態(tài)趨勢圖.
實現(xiàn)思路是啟動定時任務,對各用戶的設備狀態(tài)數(shù)據(jù)分別按照小時以及天進行聚合,并存儲進數(shù)據(jù)庫中供用戶后續(xù)查詢.
涉及到的技術棧分別為:Spring Boot
,MongoDB,Morphia
.
數(shù)據(jù)模型
@Data
@Builder
@Entity(value = “rawDevStatus”, noClassnameStored = true)
// 設備狀態(tài)索引
@Indexes({
// 設置數(shù)據(jù)超時時間(TTL,MongoDB根據(jù)TTL在后臺進行數(shù)據(jù)刪除操作)
@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 {
/**
* 協(xié)議類型
*/
private Protocol protocol;
/**
* 設備總數(shù)
*/
private Integer total;
/**
* 設備在線數(shù)目
*/
private Integer onlineNum;
/**
* 處于啟用狀態(tài)設備數(shù)目
*/
private Integer enableNum;
}
}
網友評論