<commit_before>package com.nowellpoint.sforce;

import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Date;
import java.time.Instant;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.util.IOUtils;
import com.nowellpoint.sforce.model.Notification;
import com.nowellpoint.sforce.model.OutboundMessage;

public class OutboundMessageHandler {
	
	private static DynamoDBMapper mapper = new DynamoDBMapper(new AmazonDynamoDBClient());

	public String handleEvent(DynamodbEvent event, Context context) {
		
		LambdaLogger logger = context.getLogger();
		
		Long startTime = System.currentTimeMillis();
		
		event.getRecords().stream().filter(record -> "INSERT".equals(record.getEventName())).forEach(record -> {
			
			logger.log("DynamodbEvent received...Event Id: "
					.concat(record.getEventID())
					.concat(" Event Name: " + record.getEventName()));
			
			String organizationId = record.getDynamodb().getKeys().get("OrganizationId").getS();
			String key = record.getDynamodb().getKeys().get("Key").getS();
			
			OutboundMessage outboundMessage = mapper.load(OutboundMessage.class, organizationId, key);
			
			try {
				process(outboundMessage);
				outboundMessage.setStatus("PROCESSED");
			} catch (Exception e) {
				outboundMessage.setStatus("FAILED");
				outboundMessage.setErrorMessage(e.getMessage());
			} finally {
				outboundMessage.setProcessedDate(Date.from(Instant.now()));
				outboundMessage.setDuration(System.currentTimeMillis() - startTime);
				outboundMessage.setSessionId(null);
				mapper.save(outboundMessage);
			}
		});
		
		return "ok";
	}
	
	private void process(OutboundMessage outboundMessage) throws ClientProtocolException, IOException, URISyntaxException {
		HttpClient client = HttpClientBuilder.create().build();
		
		String url = outboundMessage
				.getPartnerUrl()
				.substring(0, outboundMessage
						.getPartnerUrl()
						.lastIndexOf("/") + 1)
				.replace("/Soap/u/", "/data/v")
				.concat("query");<commit_msg>made an adjustment on calculating run time of an OutboundMessage to use the message received date as the start time<commit_after>package com.nowellpoint.sforce;

import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Date;
import java.time.Instant;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.util.IOUtils;
import com.nowellpoint.sforce.model.Notification;
import com.nowellpoint.sforce.model.OutboundMessage;

public class OutboundMessageHandler {
	
	private static DynamoDBMapper mapper = new DynamoDBMapper(new AmazonDynamoDBClient());

	public String handleEvent(DynamodbEvent event, Context context) {
		
		LambdaLogger logger = context.getLogger();
		
		event.getRecords().stream().filter(record -> "INSERT".equals(record.getEventName())).forEach(record -> {
			
			logger.log("DynamodbEvent received...Event Id: "
					.concat(record.getEventID())
					.concat(" Event Name: " + record.getEventName()));
			
			String organizationId = record.getDynamodb().getKeys().get("OrganizationId").getS();
			String key = record.getDynamodb().getKeys().get("Key").getS();
			
			OutboundMessage outboundMessage = mapper.load(OutboundMessage.class, organizationId, key);
			
			try {
				process(outboundMessage);
				outboundMessage.setStatus("PROCESSED");
			} catch (Exception e) {
				outboundMessage.setStatus("FAILED");
				outboundMessage.setErrorMessage(e.getMessage());
			} finally {
				outboundMessage.setProcessedDate(Date.from(Instant.now()));
				outboundMessage.setDuration(System.currentTimeMillis() - outboundMessage.getReceivedDate().getTime());
				outboundMessage.setSessionId(null);
				mapper.save(outboundMessage);
			}
		});
		
		return "ok";
	}
	
	private void process(OutboundMessage outboundMessage) throws ClientProtocolException, IOException, URISyntaxException {
		HttpClient client = HttpClientBuilder.create().build();
		
		String url = outboundMessage
				.getPartnerUrl()
				.substring(0, outboundMessage
						.getPartnerUrl()
						.lastIndexOf("/") + 1)
				.replace("/Soap/u/", "/data/v")
				.concat("query");
{'fraction_non_alphanumeric': 0.12210172026925954, 'fraction_numerical': 0.0014958863126402393, 'mean_word_length': 5.078409090909091, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065348', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1596, 'n_words': 263}