use of org.sagebionetworks.bridge.models.Metrics in project BridgeServer2 by Sage-Bionetworks.
the class RequestContextTest method test.
@Test
public void test() {
// An existing metrics object
Metrics metrics = new Metrics(REQUEST_ID);
ClientInfo clientInfo = ClientInfo.fromUserAgentCache("Asthma/26 (Unknown iPhone; iPhone OS/9.1) BridgeSDK/4");
RequestContext context = new RequestContext.Builder().withRequestId(REQUEST_ID).withCallerEnrolledStudies(STUDIES).withCallerAppId(TEST_APP_ID).withMetrics(metrics).withCallerRoles(ROLES).withCallerUserId(TEST_USER_ID).withCallerLanguages(LANGUAGES).withCallerClientInfo(clientInfo).withCallerOrgMembership(TEST_ORG_ID).withOrgSponsoredStudies(USER_STUDY_IDS).build();
assertEquals(context.getId(), REQUEST_ID);
assertEquals(context.getCallerAppId(), TEST_APP_ID);
assertEquals(context.getCallerEnrolledStudies(), STUDIES);
assertEquals(context.getCallerRoles(), ROLES);
assertEquals(context.getCallerUserId(), TEST_USER_ID);
assertEquals(context.getCallerLanguages(), LANGUAGES);
assertEquals(context.getCallerClientInfo(), clientInfo);
assertEquals(context.getMetrics(), metrics);
assertEquals(context.getCallerOrgMembership(), TEST_ORG_ID);
assertEquals(context.getOrgSponsoredStudies(), USER_STUDY_IDS);
}
use of org.sagebionetworks.bridge.models.Metrics in project BridgeServer2 by Sage-Bionetworks.
the class MetricsFilterTest method metricsUsesXForwardedForHeader.
@Test
public void metricsUsesXForwardedForHeader() throws Exception {
when(mockRequest.getHeader(X_REQUEST_ID_HEADER)).thenReturn("request-id");
when(mockRequest.getHeader(X_FORWARDED_FOR_HEADER)).thenReturn("5.6.7.8");
when(mockRequest.getRemoteAddr()).thenReturn(TestConstants.IP_ADDRESS);
filter.doFilter(mockRequest, mockResponse, mockFilterChain);
Metrics metrics = RequestContext.get().getMetrics();
JsonNode node = metrics.getJson();
assertEquals("5.6.7.8", node.get("remote_address").textValue());
}
use of org.sagebionetworks.bridge.models.Metrics in project BridgeServer2 by Sage-Bionetworks.
the class MetricsFilterTest method metricsUserSessionTest.
@Test
public void metricsUserSessionTest() throws Exception {
StudyParticipant participant = new StudyParticipant.Builder().withId("participant").build();
UserSession session = new UserSession(participant);
session.setInternalSessionToken("internal_token");
session.setAppId("app_ID");
when(mockRequest.getAttribute("CreatedUserSession")).thenReturn(session);
filter.doFilter(mockRequest, mockResponse, mockFilterChain);
Metrics metrics = RequestContext.get().getMetrics();
JsonNode node = metrics.getJson();
assertEquals("internal_token", node.get("session_id").textValue());
assertEquals("app_ID", node.get("app_id").textValue());
assertEquals("participant", node.get("user_id").textValue());
}
use of org.sagebionetworks.bridge.models.Metrics in project BridgeServer2 by Sage-Bionetworks.
the class HealthDataController method submitHealthData.
/**
* API to allow consented users to submit health data in a synchronous API, instead of using the asynchronous
* upload API. This is most beneficial for small data sets, like simple surveys. This API returns the health data
* record produced from this submission, which includes the record ID.
*/
@PostMapping(path = "/v3/healthdata", produces = { APPLICATION_JSON_UTF8_VALUE })
@ResponseStatus(HttpStatus.CREATED)
public String submitHealthData() throws IOException, UploadValidationException {
// Submit health data.
UserSession session = getAuthenticatedAndConsentedSession();
HealthDataSubmission healthDataSubmission = parseJson(HealthDataSubmission.class);
HealthDataRecord savedRecord = healthDataService.submitHealthData(session.getAppId(), session.getParticipant(), healthDataSubmission);
// Write record ID into the metrics, for logging and diagnostics.
Metrics metrics = getMetrics();
if (metrics != null) {
metrics.setRecordId(savedRecord.getId());
}
// Record upload time to user's request info. This allows us to track the last time the user submitted.
RequestInfo requestInfo = getRequestInfoBuilder(session).withUploadedOn(DateUtils.getCurrentDateTime()).build();
requestInfoService.updateRequestInfo(requestInfo);
// Return the record produced by this submission. Filter out Health Code, of course.
return HealthDataRecord.PUBLIC_RECORD_WRITER.writeValueAsString(savedRecord);
}
use of org.sagebionetworks.bridge.models.Metrics in project BridgeServer2 by Sage-Bionetworks.
the class HealthDataEx3Controller method createOrUpdateRecord.
/**
* Create or update health data record. Returns the created or updated record.
*/
@PostMapping(path = "/v1/apps/{appId}/exporter3/healthdata")
public HealthDataRecordEx3 createOrUpdateRecord(@PathVariable String appId) {
getAuthenticatedSession(WORKER);
// Verify app exists.
App app = appService.getApp(appId);
if (app == null) {
throw new EntityNotFoundException(App.class);
}
HealthDataRecordEx3 record = parseJson(HealthDataRecordEx3.class);
record.setAppId(appId);
HealthDataRecordEx3 savedRecord = healthDataEx3Service.createOrUpdateRecord(record);
// Write record ID into the metrics, for logging and diagnostics.
Metrics metrics = getMetrics();
if (metrics != null) {
metrics.setRecordId(savedRecord.getId());
}
return savedRecord;
}
Aggregations