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

Dev feature reporting poc #4

Merged
merged 18 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion processing-status-api-function-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ configurations {

dependencies {
implementation "com.microsoft.azure.functions:azure-functions-java-library:3.0.0"
implementation 'com.azure:azure-cosmos:4.42.0'
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.14.2"
implementation 'io.opentelemetry:opentelemetry-api:1.29.0'
implementation 'io.opentelemetry:opentelemetry-sdk:1.29.0'
implementation 'io.opentelemetry:opentelemetry-exporter-logging:1.29.0'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp:1.29.0'

implementation 'io.opentelemetry:opentelemetry-semconv:1.29.0-alpha'
implementation 'com.google.code.gson:gson:2.10.1'

implementation 'com.azure:azure-messaging-servicebus:7.13.3'
implementation 'com.azure:azure-identity:1.8.0'

implementation 'org.danilopianini:khttp:1.3.1'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.7'
Expand Down
3 changes: 3 additions & 0 deletions processing-status-api-function-app/src/main/.azure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
connections.resources.xml
deployment.targets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<connections />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<profiles version="1" defaultProfile="default">
<profile name="default" />
</profiles>
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,122 @@ public HttpResponseMessage getTraceByUploadId(
return new GetTraceByUploadIdFunction().run(request, uploadId, context);
}

@FunctionName("CreateReport")
public HttpResponseMessage createReport(
@HttpTrigger(
name = "req",
methods = {HttpMethod.POST},
route = "report",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
return new CreateReportFunction(context).withHttpRequest(request);
}

/***
* Process a message from the service bus queue. The same queue is used for all
* messages to ensure sequential processing. For example, we need to ensure if a
* report is created first that it can be amended. With separate queues for creating
* and amending there would always be a possibility that the amend message is processed
* before the create message.
*
* @param message JSON message content
* @param context Execution context of the service bus message
*/
@FunctionName("ServiceBusProcessor")
public void serviceBusProcessor(
@ServiceBusQueueTrigger(
name = "msg",
queueName = "%ServiceBusQueueName%",
connection = "ServiceBusConnectionString"
) String message,
final ExecutionContext context
) {
try {
context.getLogger().info("Received message: " + message);
new ServiceBusProcessor(context).withMessage(message);
} catch (Exception e) {
context.getLogger().warning("Failed to process service bus message: " + e.getLocalizedMessage());
}
}

@FunctionName("AmendReportByUploadId")
public HttpResponseMessage amendReportByUploadId(
@HttpTrigger(
name = "req",
methods = {HttpMethod.PUT},
route = "report/json/uploadId/{uploadId}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("uploadId") String uploadId,
final ExecutionContext context) {
return new AmendReportFunction(request, context).jsonWithUploadId(uploadId);
}

@FunctionName("AmendReportByReportId")
public HttpResponseMessage amendReportByReportId(
@HttpTrigger(
name = "req",
methods = {HttpMethod.PUT},
route = "report/json/reportId/{reportId}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("reportId") String reportId,
final ExecutionContext context) {
return new AmendReportFunction(request, context).jsonWithReportId(reportId);
}

@FunctionName("GetReportByUploadId")
public HttpResponseMessage getReportByUploadId(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
route = "report/uploadId/{uploadId}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("uploadId") String uploadId,
final ExecutionContext context) {
return new GetReportFunction(request, context).withUploadId(uploadId);
}

@FunctionName("GetReportByReportId")
public HttpResponseMessage getReportByReportId(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
route = "report/reportId/{reportId}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("reportId") String reportId,
final ExecutionContext context) {
return new GetReportFunction(request, context).withReportId(reportId);
}

@FunctionName("GetUploadStatus")
public HttpResponseMessage getUploadStatus(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
route = "upload/{destinationId}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("destinationId") String destinationId,
final ExecutionContext context) {
return new GetUploadStatusFunction(request, context).uploadStatus(destinationId, "dex-upload");
}

@FunctionName("GetReportForStage")
public HttpResponseMessage getReportByStage(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
route = "report/{destinationId}/{stageName}",
authLevel = AuthorizationLevel.ANONYMOUS
) HttpRequestMessage<Optional<String>> request,
@BindingName("destinationId") String destinationId,
@BindingName("stageName") String stageName,
final ExecutionContext context) {
context.getLogger().info("getReportByStage: destinationId=" + destinationId + ", stageName=" + stageName);
return new GetReportFunction(request, context).withDestinationId(destinationId, stageName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gov.cdc.ocio.cosmossync.cosmos

import com.azure.cosmos.ConsistencyLevel
import com.azure.cosmos.CosmosClient
import com.azure.cosmos.CosmosClientBuilder

class CosmosClientManager {
companion object {

private var client: CosmosClient? = null

fun getCosmosClient(): CosmosClient {
// Initialize a connection to cosmos that will persist across HTTP triggers
val uri = System.getenv("CosmosDbEndpoint")
val authKey = System.getenv("CosmosDbKey")

if (client == null) {
client = CosmosClientBuilder()
.endpoint(uri)
.key(authKey)
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(true)
.buildClient()
}
return client!!
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gov.cdc.ocio.processingstatusapi.cosmos

import com.azure.cosmos.CosmosClient
import com.azure.cosmos.CosmosContainer
import com.azure.cosmos.CosmosDatabase
import com.azure.cosmos.CosmosException
import com.azure.cosmos.models.CosmosContainerProperties
import com.azure.cosmos.models.ThroughputProperties
import com.microsoft.azure.functions.ExecutionContext
import gov.cdc.ocio.cosmossync.cosmos.CosmosClientManager

class CosmosContainerManager {

companion object {

@Throws(Exception::class)
fun createDatabaseIfNotExists(context: ExecutionContext, cosmosClient: CosmosClient, databaseName: String): CosmosDatabase? {
context.logger.info("Create database $databaseName if not exists...")

// Create database if not exists
val databaseResponse = cosmosClient.createDatabaseIfNotExists(databaseName)
return cosmosClient.getDatabase(databaseResponse.properties.id)
}

fun initDatabaseContainer(context: ExecutionContext, containerName: String): CosmosContainer? {
try {
val logger = context.logger

logger.info("calling getCosmosClient...")
val cosmosClient = CosmosClientManager.getCosmosClient()

// setup database
logger.info("calling createDatabaseIfNotExists...")
val db = createDatabaseIfNotExists(context, cosmosClient, "ProcessingStatus")!!

val containerProperties = CosmosContainerProperties(containerName, "/partitionKey")

// Provision throughput
val throughputProperties = ThroughputProperties.createManualThroughput(400)

// Create container with 400 RU/s
logger.info("calling createContainerIfNotExists...")
val databaseResponse = db.createContainerIfNotExists(containerProperties, throughputProperties)

return db.getContainer(databaseResponse.properties.id)

} catch (ex: CosmosException) {
context.logger.info("exception: ${ex.localizedMessage}")
}
return null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package gov.cdc.ocio.processingstatusapi.exceptions

class BadRequestException(message: String): Exception(message) {
}
/**
* Intended use of this exception is for bad requests, such as a record could not be located because an invalid
* identifier was provided. Or, if a required parameter is missing for a request.
*
* @constructor
*/
class BadRequestException(message: String): Exception(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gov.cdc.ocio.processingstatusapi.exceptions

/**
* Intended use of this exception is for internal server issues where we expect to be in a certain state or have
* internal state information that is missing or invalid.
*
* @constructor
*/
class BadStateException(message: String): Exception(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gov.cdc.ocio.processingstatusapi.exceptions

/**
* Intended use of this exception is for content issues.
*
* @constructor
*/
class ContentException(message: String): Exception(message)
Loading
Loading