時間:2024-03-09 11:51作者:下載吧人氣:18
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;
}
}
網友評論