Skip to content

Commit

Permalink
Add file extension to event metrics (#5105)
Browse files Browse the repository at this point in the history
* Add file extension to event metrics

* Fix tests

---------

Co-authored-by: Simon Dumas <[email protected]>
  • Loading branch information
imsdu and Simon Dumas committed Aug 15, 2024
1 parent 237e396 commit ef07991
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model

import akka.http.scaladsl.model.ContentType
import ch.epfl.bluebrain.nexus.delta.kernel.utils.FileUtils
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileAttributes.FileAttributesOrigin
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.{contexts, nxvFile, Files}
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.StoragesConfig.ShowFileLocation
Expand Down Expand Up @@ -477,6 +478,7 @@ object FileEvent {
newFileWritten: Option[Int],
bytes: Option[Long],
mediaType: Option[ContentType],
extension: Option[String],
origin: Option[FileAttributesOrigin]
)

Expand All @@ -490,6 +492,7 @@ object FileEvent {
Some(1),
Some(c.attributes.bytes),
c.attributes.mediaType,
FileUtils.extension(c.attributes.filename),
Some(c.attributes.origin)
)
case c: FileCreated =>
Expand All @@ -499,6 +502,7 @@ object FileEvent {
Some(1),
None,
None,
None,
Some(c.attributes.origin)
)
case u: FileUpdated if u.attributes.digest.computed =>
Expand All @@ -508,6 +512,7 @@ object FileEvent {
Some(1),
Some(u.attributes.bytes),
u.attributes.mediaType,
FileUtils.extension(u.attributes.filename),
Some(u.attributes.origin)
)
case u: FileUpdated =>
Expand All @@ -517,6 +522,7 @@ object FileEvent {
Some(1),
None,
None,
None,
Some(u.attributes.origin)
)
case fau: FileAttributesUpdated =>
Expand All @@ -526,23 +532,23 @@ object FileEvent {
None,
Some(fau.bytes),
fau.mediaType,
None,
Some(FileAttributesOrigin.Storage)
)
case fcmu: FileCustomMetadataUpdated =>
FileExtraFields(fcmu.storage.iri, fcmu.storageType, None, None, None, None)
FileExtraFields(fcmu.storage.iri, fcmu.storageType, None, None, None, None, None)
case fta: FileTagAdded =>
FileExtraFields(fta.storage.iri, fta.storageType, None, None, None, None)
FileExtraFields(fta.storage.iri, fta.storageType, None, None, None, None, None)
case ftd: FileTagDeleted =>
FileExtraFields(ftd.storage.iri, ftd.storageType, None, None, None, None)
FileExtraFields(ftd.storage.iri, ftd.storageType, None, None, None, None, None)
case fd: FileDeprecated =>
FileExtraFields(fd.storage.iri, fd.storageType, None, None, None, None)
FileExtraFields(fd.storage.iri, fd.storageType, None, None, None, None, None)
case fud: FileUndeprecated =>
FileExtraFields(fud.storage.iri, fud.storageType, None, None, None, None)
FileExtraFields(fud.storage.iri, fud.storageType, None, None, None, None, None)
case fce: FileCancelledEvent =>
FileExtraFields(fce.storage.iri, fce.storageType, None, None, None, None)
FileExtraFields(fce.storage.iri, fce.storageType, None, None, None, None, None)
}

implicit val fileExtraFieldsEncoder: Encoder.AsObject[FileExtraFields] =
deriveEncoder[FileExtraFields]
implicit val fileExtraFieldsEncoder: Encoder.AsObject[FileExtraFields] = deriveEncoder[FileExtraFields]
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model

import akka.http.scaladsl.model.ContentTypes.`text/plain(UTF-8)`
import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.model.{ContentType, ContentTypes, Uri}
import ch.epfl.bluebrain.nexus.delta.kernel.utils.ClassUtils
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.Digest.{ComputedDigest, NotComputedDigest}
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileAttributes.FileAttributesOrigin
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileAttributes.FileAttributesOrigin.Client
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.FileEvent._
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.nxvFile
Expand All @@ -12,14 +13,16 @@ import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.DigestAlgori
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.StorageType.{DiskStorage => DiskStorageType}
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.nxv
import ch.epfl.bluebrain.nexus.delta.sdk.SerializationSuite
import ch.epfl.bluebrain.nexus.delta.sdk.instances._
import ch.epfl.bluebrain.nexus.delta.sdk.model.Tags
import ch.epfl.bluebrain.nexus.delta.sdk.model.metrics.EventMetric._
import ch.epfl.bluebrain.nexus.delta.sdk.sse.SseEncoder.SseData
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.{Subject, User}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Label, ProjectRef, ResourceRef}
import io.circe.{Json, JsonObject}
import io.circe.JsonObject
import io.circe.syntax.KeyOps

import java.time.Instant
import java.util.UUID
Expand Down Expand Up @@ -72,14 +75,26 @@ class FileSerializationSuite extends SerializationSuite with StorageFixtures {
private val undeprecated = FileUndeprecated(fileId, projectRef, storageRef, DiskStorageType, 6, instant, subject)
// format: on

private def expected(event: FileEvent, newFileWritten: Json, bytes: Json, mediaType: Json, origin: Json) =
private val clientOrigin = Some(FileAttributesOrigin.Client)
private val storageOrigin = Some(FileAttributesOrigin.Storage)
private val textContent = Some(ContentTypes.`text/plain(UTF-8)`)

private def expectedExtraFields(
event: FileEvent,
newFileWritten: Option[Int],
bytes: Option[Long],
mediaType: Option[ContentType],
extension: Option[String],
origin: Option[FileAttributesOrigin]
) =
JsonObject(
"storage" -> Json.fromString(event.storage.iri.toString),
"storageType" -> Json.fromString(event.storageType.toString),
"newFileWritten" -> newFileWritten,
"bytes" -> bytes,
"mediaType" -> mediaType,
"origin" -> origin
"storage" := event.storage.iri,
"storageType" := event.storageType,
"newFileWritten" := newFileWritten,
"bytes" := bytes,
"mediaType" := mediaType,
"extension" := extension,
"origin" := origin
)

private val filesMapping = List(
Expand All @@ -88,95 +103,77 @@ class FileSerializationSuite extends SerializationSuite with StorageFixtures {
created,
loadEvents("files", "file-created.json"),
Created,
expected(created, Json.fromInt(1), Json.Null, Json.Null, Json.fromString("Client"))
expectedExtraFields(created, Some(1), None, None, None, clientOrigin)
),
(
"FileCreated with metadata",
createdWithMetadata,
loadEvents("files", "file-created-with-metadata.json"),
Created,
expected(created, Json.fromInt(1), Json.Null, Json.Null, Json.fromString("Client"))
expectedExtraFields(created, Some(1), None, None, None, clientOrigin)
),
(
"FileCreated with tags",
createdTagged,
loadEvents("files", "file-created-tagged.json"),
Created,
expected(createdTagged, Json.fromInt(1), Json.Null, Json.Null, Json.fromString("Client"))
expectedExtraFields(createdTagged, Some(1), None, None, None, clientOrigin)
),
(
"FileCreated with tags and keywords",
createdTaggedWithMetadata,
loadEvents("files", "file-created-tagged-with-metadata.json"),
Created,
expected(createdTaggedWithMetadata, Json.fromInt(1), Json.Null, Json.Null, Json.fromString("Client"))
expectedExtraFields(createdTaggedWithMetadata, Some(1), None, None, None, clientOrigin)
),
(
"FileUpdated",
updated,
loadEvents("files", "file-updated.json"),
Updated,
expected(
updated,
Json.fromInt(1),
Json.fromInt(12),
Json.fromString("text/plain; charset=UTF-8"),
Json.fromString("Client")
)
expectedExtraFields(updated, Some(1), Some(12), textContent, Some("txt"), clientOrigin)
),
(
"FileAttributesUpdated",
updatedAttr,
loadEvents("files", "file-attributes-created-updated.json"),
Updated,
expected(
updatedAttr,
Json.Null,
Json.fromInt(12),
Json.fromString("text/plain; charset=UTF-8"),
Json.fromString("Storage")
)
expectedExtraFields(updatedAttr, None, Some(12), textContent, None, storageOrigin)
),
(
"FileCustomMetadataUpdated",
updatedMetadata,
loadEvents("files", "file-custom-metadata-updated.json"),
Updated,
expected(
updatedMetadata,
Json.Null,
Json.Null,
Json.Null,
Json.Null
)
expectedExtraFields(updatedMetadata, None, None, None, None, None)
),
(
"FileTagAdded",
tagged,
loadEvents("files", "file-tag-added.json"),
Tagged,
expected(tagged, Json.Null, Json.Null, Json.Null, Json.Null)
expectedExtraFields(tagged, None, None, None, None, None)
),
(
"FileTagDeleted",
tagDeleted,
loadEvents("files", "file-tag-deleted.json"),
TagDeleted,
expected(tagDeleted, Json.Null, Json.Null, Json.Null, Json.Null)
expectedExtraFields(tagDeleted, None, None, None, None, None)
),
(
"FileDeprecated",
deprecated,
loadEvents("files", "file-deprecated.json"),
Deprecated,
expected(deprecated, Json.Null, Json.Null, Json.Null, Json.Null)
expectedExtraFields(deprecated, None, None, None, None, None)
),
(
"FileUndeprecated",
undeprecated,
loadEvents("files", "file-undeprecated.json"),
Undeprecated,
expected(undeprecated, Json.Null, Json.Null, Json.Null, Json.Null)
expectedExtraFields(undeprecated, None, None, None, None, None)
)
)

Expand Down

0 comments on commit ef07991

Please sign in to comment.