/
DynamoReportDataDao.java
167 lines (136 loc) · 7.11 KB
/
DynamoReportDataDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package org.sagebionetworks.bridge.dynamodb;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.BETWEEN;
import static org.sagebionetworks.bridge.models.ResourceList.PAGE_SIZE;
import static org.sagebionetworks.bridge.models.ResourceList.OFFSET_KEY;
import static org.sagebionetworks.bridge.models.ResourceList.START_DATE;
import static org.sagebionetworks.bridge.models.ResourceList.END_DATE;
import static org.sagebionetworks.bridge.models.ResourceList.START_TIME;
import static org.sagebionetworks.bridge.models.ResourceList.END_TIME;
import java.util.List;
import javax.annotation.Resource;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Component;
import org.sagebionetworks.bridge.BridgeUtils;
import org.sagebionetworks.bridge.dao.ReportDataDao;
import org.sagebionetworks.bridge.models.DateRangeResourceList;
import org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList;
import org.sagebionetworks.bridge.models.reports.ReportData;
import org.sagebionetworks.bridge.models.reports.ReportDataKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@Component
public class DynamoReportDataDao implements ReportDataDao {
private DynamoDBMapper mapper;
@Resource(name = "reportDataMapper")
final void setReportDataMapper(DynamoDBMapper reportDataMapper) {
this.mapper = reportDataMapper;
}
@Override
public DateRangeResourceList<? extends ReportData> getReportData(ReportDataKey key, LocalDate startDate, LocalDate endDate) {
checkNotNull(key);
checkNotNull(startDate);
checkNotNull(endDate);
DynamoReportData hashKey = new DynamoReportData();
hashKey.setKey(key.getKeyString());
// range key is between start date and end date
Condition dateCondition = new Condition().withComparisonOperator(BETWEEN)
.withAttributeValueList(new AttributeValue().withS(startDate.toString()),
new AttributeValue().withS(endDate.toString()));
DynamoDBQueryExpression<DynamoReportData> query =
new DynamoDBQueryExpression<DynamoReportData>().withHashKeyValues(hashKey)
.withRangeKeyCondition("date", dateCondition);
List<DynamoReportData> results = mapper.query(DynamoReportData.class, query);
return new DateRangeResourceList<DynamoReportData>(results)
.withRequestParam(START_DATE, startDate)
.withRequestParam(END_DATE, endDate);
}
/**
* Query for report records within a range of DateTime values, using the indicated page size and offset key.
* The report's date field will be returned using the timezone provided in the startTime/endTime parameters,
* but all searches are done against the values in UTC time so they remain accurate when a user switches
* time zones.
*/
@Override
public ForwardCursorPagedResourceList<ReportData> getReportDataV4(final ReportDataKey key, final DateTime startTime,
final DateTime endTime, final String offsetKey, final int pageSize) {
checkNotNull(key);
checkNotNull(startTime);
checkNotNull(endTime);
int pageSizeWithIndicatorRecord = pageSize+1;
DynamoReportData hashKey = new DynamoReportData();
hashKey.setKey(key.getKeyString());
AttributeValue start = new AttributeValue().withS(startTime.withZone(DateTimeZone.UTC).toString());
AttributeValue end = new AttributeValue().withS(endTime.withZone(DateTimeZone.UTC).toString());
// The offsetKey should be in UTC
if (offsetKey != null) {
start = new AttributeValue().withS(offsetKey);
}
Condition dateCondition = new Condition().withComparisonOperator(BETWEEN)
.withAttributeValueList(start, end);
DynamoDBQueryExpression<DynamoReportData> query = new DynamoDBQueryExpression<DynamoReportData>()
.withHashKeyValues(hashKey)
.withRangeKeyCondition("date", dateCondition)
.withLimit(pageSizeWithIndicatorRecord);
QueryResultPage<DynamoReportData> page = mapper.queryPage(DynamoReportData.class, query);
List<ReportData> list = Lists.newArrayListWithCapacity(pageSizeWithIndicatorRecord);
for (int i = 0, len = page.getResults().size(); i < len; i++) {
ReportData oneReport = page.getResults().get(i);
DateTime dateTime = oneReport.getDateTime();
if (dateTime != null) {
oneReport.setDateTime(dateTime.withZone(startTime.getZone()));
}
list.add(i, oneReport);
}
String nextPageOffsetKey = null;
if (list.size() == pageSizeWithIndicatorRecord) {
nextPageOffsetKey = Iterables.getLast(list).getDate();
}
int limit = Math.min(list.size(), pageSize);
return new ForwardCursorPagedResourceList<ReportData>(list.subList(0, limit), nextPageOffsetKey, true)
.withRequestParam(PAGE_SIZE, pageSize)
.withRequestParam(OFFSET_KEY, offsetKey)
.withRequestParam(START_TIME, startTime)
.withRequestParam(END_TIME, endTime);
}
@Override
public void saveReportData(ReportData reportData) {
checkNotNull(reportData);
DateTime dateTime = reportData.getDateTime();
if (dateTime != null) {
reportData.setDateTime(dateTime.withZone(DateTimeZone.UTC));
}
mapper.save(reportData);
}
@Override
public void deleteReportData(ReportDataKey key) {
checkNotNull(key);
DynamoReportData hashKey = new DynamoReportData();
hashKey.setKey(key.getKeyString());
DynamoDBQueryExpression<DynamoReportData> query =
new DynamoDBQueryExpression<DynamoReportData>().withHashKeyValues(hashKey);
List<DynamoReportData> objectsToDelete = mapper.query(DynamoReportData.class, query);
if (!objectsToDelete.isEmpty()) {
List<FailedBatch> failures = mapper.batchDelete(objectsToDelete);
BridgeUtils.ifFailuresThrowException(failures);
}
}
@Override
public void deleteReportDataRecord(ReportDataKey key, String date) {
DynamoReportData hashKey = new DynamoReportData();
hashKey.setKey(key.getKeyString());
hashKey.setDate(date);
DynamoReportData reportDataRecord = mapper.load(hashKey);
if (reportDataRecord != null) {
mapper.delete(reportDataRecord);
}
}
}