Search in sources :

Example 1 with Metrics

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);
}
Also used : Metrics(org.sagebionetworks.bridge.models.Metrics) ClientInfo(org.sagebionetworks.bridge.models.ClientInfo) Test(org.testng.annotations.Test)

Example 2 with Metrics

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());
}
Also used : Metrics(org.sagebionetworks.bridge.models.Metrics) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.testng.annotations.Test)

Example 3 with Metrics

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());
}
Also used : Metrics(org.sagebionetworks.bridge.models.Metrics) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) JsonNode(com.fasterxml.jackson.databind.JsonNode) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 4 with Metrics

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);
}
Also used : Metrics(org.sagebionetworks.bridge.models.Metrics) HealthDataSubmission(org.sagebionetworks.bridge.models.healthdata.HealthDataSubmission) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) RequestInfo(org.sagebionetworks.bridge.models.RequestInfo) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 5 with Metrics

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;
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Metrics(org.sagebionetworks.bridge.models.Metrics) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) HealthDataRecordEx3(org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Metrics (org.sagebionetworks.bridge.models.Metrics)14 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)7 Test (org.testng.annotations.Test)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)3 App (org.sagebionetworks.bridge.models.apps.App)3 HealthDataRecordEx3 (org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3)3 ClientInfo (org.sagebionetworks.bridge.models.ClientInfo)2 RequestInfo (org.sagebionetworks.bridge.models.RequestInfo)2 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)2 HealthDataRecord (org.sagebionetworks.bridge.models.healthdata.HealthDataRecord)2 HealthDataSubmission (org.sagebionetworks.bridge.models.healthdata.HealthDataSubmission)2 Multimap (com.google.common.collect.Multimap)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1