我试图绘制我使用Room存储的数据。为此,我有一个会话实体,然后是会话的1到多个关系,例如会话期间记录的步数,血压和氧气水平。
对于会话我存储开始时间,我希望能够按此日期对会话进行分组(进入日/月/年箱)。然后,对于每个时间间隔,我需要获得图表上显示的步数,血压和氧气水平的平均值。例如,多个会话可能会在4/23/2018发生,但我想获得当天的平均值。
会话
@Entity
public class Session implements Serializable {
public final static String TABLE = "Session";
@PrimaryKey(autoGenerate = true)
private long sid;
@ColumnInfo(name = "startTimestamp")
private Date startTimestamp;
@ColumnInfo(name = "endTimestamp")
private Date endTimestamp;
public long getSid() { return sid; }
public Date getStartTimestamp() { return startTimestamp; }
public Date getEndTimestamp() { return endTimestamp; }
public long getDuration() { return endTimestamp.getTime() - startTimestamp.getTime(); }
public void setSid(long sid) { this.sid = sid; }
public void setStartTimestamp(Date timestamp) { this.startTimestamp = timestamp; }
public void setEndTimestamp(Date timestamp) { this.endTimestamp = timestamp; }
@android.arch.persistence.room.Dao
public interface SessionDao {
@Query("SELECT * FROM " + TABLE)
List<Session> getAll();
@Query("SELECT * FROM " + TABLE)
List<SessionWithRelations> getAllWithRelations();
@Query("SELECT * FROM " + TABLE + " ORDER BY :orderBy")
List<SessionWithRelations> getAllWithRelations(String orderBy);
@Insert
long insert(Session session);
@Update
void update(Session session);
@Delete
void delete(Session session);
}
}
SessionOxygen
@Entity
public class SessionOxygen implements Serializable {
public final static String TABLE = "SessionOxygen";
@PrimaryKey(autoGenerate = true)
private long id;
@ColumnInfo(name = "sid")
private long sid;
@ColumnInfo(name = "timestamp")
private Date timestamp;
@ColumnInfo(name = "oxygen")
private int oxygen;
public long getId() { return id; }
public long getSid() { return sid; }
public Date getTimestamp() { return timestamp; }
public int getOxygen() { return oxygen; }
public void setId(long id) { this.id = id; }
public void setSid(long sid) { this.sid = sid; }
public void setTimestamp(Date timestamp) { this.timestamp = timestamp; }
public void setOxygen(int oxygen) { this.oxygen = oxygen; }
@android.arch.persistence.room.Dao
public interface OxygenDao {
@Query("SELECT * FROM " + TABLE + " WHERE sid=:sid")
List<SessionOxygen> getAllForSession(int sid);
@Insert
long[] insertAll(List<SessionOxygen> oxygens);
@Insert
long insert(SessionOxygen oxygen);
}
}
会话POJO
public class SessionWithRelations {
@Embedded
public Session session;
@Relation(parentColumn = "sid", entityColumn = "sid", entity = SessionBloodPressure.class)
public List<SessionBloodPressure> bloodPressureList = new ArrayList<>();
@Relation(parentColumn = "sid", entityColumn = "sid", entity = SessionWeight.class)
public List<SessionWeight> weightList = new ArrayList<>();
@Relation(parentColumn = "sid", entityColumn = "sid", entity = SessionOxygen.class)
public List<SessionOxygen> oxygenList = new ArrayList<>();
public int averageOxygen() {
int sum = 0;
if (oxygenList.size() > 0) {
for (SessionOxygen oxygen : oxygenList) {
sum += oxygen.getOxygen();
}
return sum / oxygenList.size();
}
return 0;
}
public int averageWeight() {
int sum = 0;
if (oxygenList.size() > 0) {
for (SessionWeight weight : weightList) {
sum += weight.getWeight();
}
return sum / weightList.size();
}
return 0;
}
public int averageDiastolic() {
int sum = 0;
if (oxygenList.size() > 0) {
for (SessionBloodPressure bloodPressure : bloodPressureList) {
sum += bloodPressure.getDiastolic();
}
return sum / weightList.size();
}
return 0;
}
public int averageSystolic() {
int sum = 0;
if (oxygenList.size() > 0) {
for (SessionBloodPressure bloodPressure : bloodPressureList) {
sum += bloodPressure.getSystolic();
}
return sum / weightList.size();
}
return 0;
}
}
我刚将整个应用程序切换到使用Room,但我觉得使用原始查询会非常容易。我不确定这对房间是否可行。我查看了RawQuery文档但看起来只允许在运行时生成查询,但仍然只返回实体,我需要返回自定义列。
// Group by month and get average Oxygen level for each month with data
select s.startTimestamp, AVG(o.oxygen) as oxygenAverage
from Session s
left join SessionOxygen o on s.sid = o.sid
group by DATE_FORMAT(s.startTimestamp, "%m-%Y");
我有一种感觉,我必须对POJO课做一些事情,但我不知道如何去做。
我认为我的另一个选择是在代码中聚合它,但这将非常低效和混乱。