Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AB2D-6124] Add Opt Out Metrics to Slack Message #105

Merged
merged 25 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public class OptOutConstants {
public static final String UPDATE_STATEMENT = "UPDATE public.current_mbi\n" +
"SET opt_out_flag = ?, effective_date = current_date\n" +
"WHERE mbi = ?";
public static final String COUNT_STATEMENT = "SELECT \n"+
"COUNT(CASE WHEN opt_out_flag = 'true' THEN 1 END) AS optin, \n"+
"COUNT(CASE WHEN opt_out_flag = 'false' THEN 1 END) AS optout \n"+
"FROM current_mbi WHERE opt_out_flag IS NOT NULL";

private OptOutConstants() {
}
private OptOutConstants() {}
}
12 changes: 10 additions & 2 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public Void handleRequest(SQSEvent sqsEvent, Context context) {
for (SQSEvent.SQSMessage msg : sqsEvent.getRecords()) {
processSQSMessage(msg, context);
}
context.getLogger().log("OptOut Lambda completed");
return null;
}

Expand All @@ -32,7 +31,16 @@ public void processSQSMessage(SQSEvent.SQSMessage msg, Context context) {
var notification = S3EventNotification.parseJson(s3EventMessage.toString()).getRecords().get(0);

var optOutProcessing = processorInit(logger);
optOutProcessing.process(getFileName(notification), getBucketName(notification), ENDPOINT);
var optOutResults = optOutProcessing.process(getFileName(notification), getBucketName(notification), ENDPOINT);
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
if (optOutResults != null) {
logger.log("OptOut Lambda completed. Total records processed today is: totaltoday=" + optOutResults.getTotalToday()
+ " Number of opt in today is: todayin=" + optOutResults.getOptInToday()
+ " Number of opt out today is: todayout=" + optOutResults.getOptOutToday()
+ " Total records processed to date is: totaltodate=" + optOutResults.getTotalAllTime()
+ " Total number of opt in is: totalin=" + optOutResults.getOptInTotal()
+ " Total number of opt out is: totalout=" + optOutResults.getOptOutTotal()
);
}
} catch (Exception ex) {
logger.log("An error occurred");
throw new OptOutException("An error occurred", ex);
Expand Down
34 changes: 33 additions & 1 deletion optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -34,14 +35,15 @@ public OptOutProcessor(LambdaLogger logger) {
parameterStore = OptOutParameterStore.getParameterStore();
}

public void process(String fileName, String bfdBucket, String endpoint) throws URISyntaxException {
public OptOutResults process(String fileName, String bfdBucket, String endpoint) throws URISyntaxException {
var optOutS3 = new OptOutS3(getS3Client(endpoint), fileName, bfdBucket, logger);
processFileFromS3(optOutS3.openFileS3());
updateOptOut();
var name = optOutS3.createResponseOptOutFile(createResponseContent());
logger.log("File with name " + name + " was uploaded to bucket: " + bfdBucket);
if (!isRejected)
optOutS3.deleteFileFromS3();
return getOptOutResults();
}

public S3Client getS3Client(String endpoint) throws URISyntaxException {
Expand Down Expand Up @@ -132,6 +134,36 @@ public String createResponseContent() {
return responseContent.toString();
}

public OptOutResults getOptOutResults() {
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
int totalOptedIn = 0;
int totalOptedOut = 0;

try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword());
var statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery(COUNT_STATEMENT)
) {
while (rs.next()) {
totalOptedIn = rs.getInt("optin");
totalOptedOut = rs.getInt("optout");
}

int numberOptedIn = 0;
int numberOptedOut = 0;

for (OptOutInformation optOut : optOutInformationList) {
if (Boolean.TRUE.equals(optOut.getOptOutFlag())) {
smirnovaae marked this conversation as resolved.
Show resolved Hide resolved
numberOptedIn++;
} else {
numberOptedOut++;
}
}
return new OptOutResults(numberOptedIn, numberOptedOut, totalOptedIn, totalOptedOut);
} catch (SQLException ex) {
logger.log("There is an error " + ex.getMessage());
}
return null;
}

public String getRecordStatus() {
return (isRejected) ? RecordStatus.REJECTED.toString() : RecordStatus.ACCEPTED.toString();
}
Expand Down
41 changes: 41 additions & 0 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gov.cms.ab2d.optout;

public class OptOutResults {

private final int optInToday;
private final int optOutToday;
private final int optInTotal;
private final int optOutTotal;

public OptOutResults(int optInToday, int optOutToday, int optInTotal, int optOutTotal) {
this.optInToday = optInToday;
this.optOutToday = optOutToday;
this.optInTotal = optInTotal;
this.optOutTotal = optOutTotal;
}

public int getOptInToday() {
return optInToday;
}

public int getOptOutToday() {
return optOutToday;
}

public int getTotalToday() {
return optInToday + optOutToday;
}

public int getOptInTotal() {
return optInTotal;
}

public int getOptOutTotal() {
return optOutTotal;
}

public int getTotalAllTime() {
return optInTotal + optOutTotal;
}

}
39 changes: 36 additions & 3 deletions optout/src/test/java/gov/cms/ab2d/optout/OptOutProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -28,6 +29,8 @@

@ExtendWith({S3MockAPIExtension.class})
class OptOutProcessorTest {
private static final ResultSet resultSet = mock(ResultSet.class);
private static final PreparedStatement preparedStatement = mock(PreparedStatement.class);
private static final Connection dbConnection = mock(Connection.class);
private static final MockedStatic<OptOutParameterStore> parameterStore = mockStatic(OptOutParameterStore.class);
private static final String DATE = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date());
Expand All @@ -44,7 +47,9 @@ static void beforeAll() throws SQLException {

mockStatic(DriverManager.class)
.when(() -> DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(dbConnection);
when(dbConnection.prepareStatement(anyString())).thenReturn(mock(PreparedStatement.class));
when(dbConnection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(dbConnection.createStatement()).thenReturn(preparedStatement);
when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet);
}

@BeforeEach
Expand All @@ -63,16 +68,21 @@ void afterEach() {
@Test
void processTest() throws URISyntaxException {
optOutProcessing.isRejected = false;
optOutProcessing.process(TEST_FILE_NAME, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
OptOutResults results = optOutProcessing.process(TEST_FILE_NAME, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(7, optOutProcessing.optOutInformationList.size());

assertEquals(3, results.getOptInToday());
assertEquals(4, results.getOptOutToday());
assertEquals(optOutProcessing.optOutInformationList.size(), results.getTotalToday());
}

@Test
void processEmptyFileTest() throws IOException, URISyntaxException {
var emptyFileName = "emptyDummy.txt";
S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + emptyFileName), StandardCharsets.UTF_8), emptyFileName);
optOutProcessing.process(emptyFileName, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
OptOutResults results = optOutProcessing.process(emptyFileName, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(0, optOutProcessing.optOutInformationList.size());
assertEquals(optOutProcessing.optOutInformationList.size(), results.getTotalToday());
S3MockAPIExtension.deleteFile(emptyFileName);
}

Expand Down Expand Up @@ -142,4 +152,27 @@ void getRecordStatusTest() {
assertEquals(RecordStatus.REJECTED.toString(), optOutProcessing.getRecordStatus());
}

@Test
void getOptOutResultsTest() throws SQLException {
final String optInResultSetString = "optin";
final String optOutResultSetString = "optout";

final int optInTotalCount = 9;
final int optOutTotalCount = 7;

when(resultSet.next()).thenReturn(true).thenReturn(false);
when(resultSet.getInt(optInResultSetString)).thenReturn(optInTotalCount);
when(resultSet.getInt(optOutResultSetString)).thenReturn(optOutTotalCount);

optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, true));
optOutProcessing.optOutInformationList.add(new OptOutInformation("DUMMY000002", false));

OptOutResults results = optOutProcessing.getOptOutResults();
assertNotNull(results);
assertEquals(1, results.getOptInToday());
assertEquals(1, results.getOptOutToday());
assertEquals(optInTotalCount, results.getOptInTotal());
assertEquals(optOutTotalCount, results.getOptOutTotal());
}

}
26 changes: 26 additions & 0 deletions optout/src/test/java/gov/cms/ab2d/optout/OptOutResultsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gov.cms.ab2d.optout;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class OptOutResultsTest {

@Test
public void itStoresValuesCorrectly() {
int todayOptIn = 1;
int todayOptOut = 1;

int totalOptedIn = 1;
int totalOptedOut = 1;
OptOutResults optOutResults = new OptOutResults(todayOptIn, todayOptOut, totalOptedIn, totalOptedOut);

assertEquals(todayOptIn, optOutResults.getOptInToday());
assertEquals(todayOptOut, optOutResults.getOptOutToday());
assertEquals(totalOptedIn, optOutResults.getOptInTotal());
assertEquals(totalOptedOut, optOutResults.getOptOutTotal());
assertEquals(todayOptIn + todayOptOut, optOutResults.getTotalToday());
assertEquals(totalOptedIn + totalOptedOut, optOutResults.getTotalAllTime());
}

}
Loading