Skip to content

Commit

Permalink
Update reference generator api call
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJKing committed Jan 8, 2025
1 parent f26aec4 commit d05efde
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import uk.gov.nationalarchives.tdr.api.graphql.ConsignmentApiContext
import uk.gov.nationalarchives.tdr.api.graphql.validation.UserOwnsConsignment
import uk.gov.nationalarchives.tdr.api.graphql.fields.FieldTypes.UuidType
import uk.gov.nationalarchives.tdr.api.graphql.fields.ConsignmentFields.FileType
import uk.gov.nationalarchives.tdr.api.graphql.validation.UserOwnsConsignment

import java.util.UUID

Expand All @@ -33,7 +34,7 @@ object FileFields {
"addFilesAndMetadata",
ListType(FileSequenceType),
arguments = List(FileAndMetadataInputArg),
resolve = ctx => ctx.ctx.fileService.addFile(ctx.arg(FileAndMetadataInputArg), ctx.ctx.accessToken.userId),
resolve = ctx => ctx.ctx.fileService.addFile(ctx.arg(FileAndMetadataInputArg), ctx.ctx.accessToken),
tags = List(ValidateUserHasAccessToConsignment(FileAndMetadataInputArg))
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import uk.gov.nationalarchives.tdr.api.utils.NaturalSorting.{ArrayOrdering, natu
import uk.gov.nationalarchives.tdr.api.utils.TimeUtils.LongUtils
import uk.gov.nationalarchives.tdr.api.utils.TreeNodesUtils
import uk.gov.nationalarchives.tdr.api.utils.TreeNodesUtils._
import uk.gov.nationalarchives.tdr.keycloak.Token

import java.sql.Timestamp
import java.util.UUID
Expand All @@ -44,12 +45,13 @@ class FileService(
private val fileUploadBatchSize: Int = config.getInt("fileUpload.batchSize")
private val filePageMaxLimit: Int = config.getInt("pagination.filesMaxLimit")

def addFile(addFileAndMetadataInput: AddFileAndMetadataInput, userId: UUID): Future[List[FileMatches]] = {
def addFile(addFileAndMetadataInput: AddFileAndMetadataInput, token: Token): Future[List[FileMatches]] = {
val now = Timestamp.from(timeSource.now)
val consignmentId = addFileAndMetadataInput.consignmentId
val userId = token.userId
val filePaths = addFileAndMetadataInput.metadataInput.map(_.originalPath).toSet
val allFileNodes: Map[String, TreeNode] = treeNodesUtils.generateNodes(filePaths, fileTypeIdentifier)
val allEmptyDirectoryNodes: Map[String, TreeNode] = treeNodesUtils.generateNodes(addFileAndMetadataInput.emptyDirectories.toSet, directoryTypeIdentifier)
val allFileNodes: Map[String, TreeNode] = treeNodesUtils.generateNodes(filePaths, fileTypeIdentifier, consignmentId, token)
val allEmptyDirectoryNodes: Map[String, TreeNode] = treeNodesUtils.generateNodes(addFileAndMetadataInput.emptyDirectories.toSet, directoryTypeIdentifier, consignmentId, token)

val row: (UUID, String, String) => FilemetadataRow = FilemetadataRow(uuidSource.uuid, _, _, now, userId, _)
val rows: Future[List[Rows]] = customMetadataPropertiesRepository.getCustomMetadataValuesWithDefault.map(filePropertyValue => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package uk.gov.nationalarchives.tdr.api.service

import com.nimbusds.oauth2.sdk.token.BearerAccessToken
import com.typesafe.config.Config
import com.typesafe.scalalogging.Logger
import io.circe.parser._
import org.apache.http.client.HttpResponseException
import sttp.client3.{Response, SimpleHttpClient, UriContext, basicRequest}
import uk.gov.nationalarchives.tdr.api.service.ReferenceGeneratorService.Reference
import uk.gov.nationalarchives.tdr.keycloak.Token

import java.util.UUID
import scala.annotation.tailrec

class ReferenceGeneratorService(config: Config, client: SimpleHttpClient) {
Expand All @@ -15,21 +18,23 @@ class ReferenceGeneratorService(config: Config, client: SimpleHttpClient) {
private val refGeneratorUrl: String = config.getString("referenceGenerator.referenceGeneratorUrl")
private val refGeneratorLimit: Int = config.getInt("referenceGenerator.referenceLimit")

def getReferences(numberOfRefs: Int): List[Reference] = {
def getReferences(numberOfRefs: Int, consignmentId: UUID, token: Token): List[Reference] = {
@tailrec
def fetchReferences(numberOfRefs: Int, acc: List[Reference]): List[Reference] = {
if (numberOfRefs <= 0) acc
else {
val batchSize = Math.min(numberOfRefs, refGeneratorLimit)
fetchReferences(numberOfRefs - batchSize, acc ++ sendRequest(batchSize))
fetchReferences(numberOfRefs - batchSize, acc ++ sendRequest(batchSize, consignmentId, token.bearerAccessToken))
}
}

fetchReferences(numberOfRefs, Nil)
}

private def sendRequest(numberOfRefs: Int): List[Reference] = {
val response: Response[Either[String, Reference]] = client.send(basicRequest.get(uri"$refGeneratorUrl/$environment/counter?numberofrefs=$numberOfRefs"))
private def sendRequest(numberOfRefs: Int, consignmentId: UUID, bearerAccessToken: BearerAccessToken): List[Reference] = {
val response: Response[Either[String, Reference]] = client.send(basicRequest
.auth.bearer(bearerAccessToken.getValue)
.get(uri"$refGeneratorUrl/$environment/counter?numberofrefs=$numberOfRefs"))

try {
response.body match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import uk.gov.nationalarchives.tdr.api.model.file.NodeType.directoryTypeIdentifi
import uk.gov.nationalarchives.tdr.api.service.ReferenceGeneratorService.Reference
import uk.gov.nationalarchives.tdr.api.service.{ReferenceGeneratorService, UUIDSource}
import uk.gov.nationalarchives.tdr.api.utils.TreeNodesUtils.TreeNode
import uk.gov.nationalarchives.tdr.keycloak.Token

import java.io.{File => JIOFile}
import java.util.UUID
Expand All @@ -26,12 +27,12 @@ class TreeNodesUtils(uuidSource: UUIDSource, referenceGeneratorService: Referenc
}
}

def generateNodes(filePaths: Set[String], typeIdentifier: String): Map[String, TreeNode] = {
def generateNodes(filePaths: Set[String], typeIdentifier: String, consignmentId: UUID, token: Token): Map[String, TreeNode] = {
val generatedNodes = filePaths.flatMap { path =>
val pathWithoutInitialSlash: String = if (path.startsWith("/")) path.tail else path
innerFunction(pathWithoutInitialSlash, typeIdentifier, Map())
}.toMap
val generatedReferences = referenceGeneratorService.getReferences(generatedNodes.size)
val generatedReferences = referenceGeneratorService.getReferences(generatedNodes.size, consignmentId, token)
generatedReferences
.zip(generatedNodes.view)
.map { case (reference, (key, treenode)) =>
Expand Down

0 comments on commit d05efde

Please sign in to comment.