Skip to content

Commit

Permalink
#83: Serde for DTO objects (#93)
Browse files Browse the repository at this point in the history
#83: Serde for DTO objects
  • Loading branch information
salamonpavel authored Oct 26, 2023
1 parent 4ed0d2d commit 6301732
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 20 deletions.
10 changes: 5 additions & 5 deletions agent/src/main/scala/za/co/absa/atum/agent/AtumContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package za.co.absa.atum.agent

import org.apache.spark.sql.DataFrame
import za.co.absa.atum.agent.AtumContext.AtumPartitions
import za.co.absa.atum.agent.model.{Checkpoint, Measure, Measurement, MeasuresMapper}
import AtumContext.AtumPartitions
import za.co.absa.atum.model.dto.{AtumContextDTO, PartitionDTO}

import java.time.ZonedDateTime
import java.time.OffsetDateTime
import scala.collection.immutable.ListMap

/**
Expand Down Expand Up @@ -51,13 +51,13 @@ class AtumContext private[agent] (
author: String,
measurements: Seq[Measurement]
): Checkpoint = {
val zonedDateTimeNow = ZonedDateTime.now()
val offsetDateTimeNow = OffsetDateTime.now()
Checkpoint(
name = checkpointName,
author = author,
atumPartitions = this.atumPartitions,
processStartTime = zonedDateTimeNow,
processEndTime = Some(zonedDateTimeNow),
processStartTime = offsetDateTimeNow,
processEndTime = Some(offsetDateTimeNow),
measurements = measurements
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package za.co.absa.atum.agent.model
import za.co.absa.atum.agent.AtumContext.AtumPartitions
import za.co.absa.atum.model.dto.CheckpointDTO

import java.time.ZonedDateTime
import java.time.OffsetDateTime
import java.util.UUID

case class Checkpoint(
name: String,
author: String,
measuredByAtumAgent: Boolean = false,
atumPartitions: AtumPartitions,
processStartTime: ZonedDateTime,
processEndTime: Option[ZonedDateTime],
processStartTime: OffsetDateTime,
processEndTime: Option[OffsetDateTime],
measurements: Seq[Measurement]
) {
private [agent] def toCheckpointDTO: CheckpointDTO = {
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ lazy val model = (projectMatrix in file("model"))
.settings(
commonSettings ++ Seq(
name := "atum-model",
libraryDependencies ++= Dependencies.modelDependencies,
libraryDependencies ++= Dependencies.modelDependencies(scalaVersion.value),
(Compile / compile) := ((Compile / compile) dependsOn printSparkScalaVersion).value,
): _*
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package za.co.absa.atum.model.dto

import java.time.ZonedDateTime
import java.time.OffsetDateTime
import java.util.UUID

case class CheckpointDTO(
id: UUID,
name: String,
author: String,
measuredByAtumAgent: Boolean = false,
partitioning: Seq[PartitionDTO],
processStartTime: ZonedDateTime,
processEndTime: Option[ZonedDateTime],
measurements: Seq[MeasurementDTO]
id: UUID,
name: String,
author: String,
measuredByAtumAgent: Boolean = false,
partitioning: Seq[PartitionDTO],
processStartTime: OffsetDateTime,
processEndTime: Option[OffsetDateTime],
measurements: Seq[MeasurementDTO]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.utils

import org.json4s.JsonAST.JString
import org.json4s.{CustomSerializer, JNull}

import java.time.OffsetDateTime

case object OffsetDateTimeSerializer extends CustomSerializer[OffsetDateTime](_ => (
{
case JString(s) => OffsetDateTime.parse(s, SerializationUtils.timestampFormat)
case JNull => null
},
{
case d: OffsetDateTime => JString(SerializationUtils.timestampFormat.format(d))
}
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.utils

import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.{write, writePretty}
import org.json4s.{Formats, NoTypeHints, ext}
import za.co.absa.atum.model.dto.MeasureResultDTO.ResultValueType

import java.time.format.DateTimeFormatter

object SerializationUtils {

implicit private val formatsJson: Formats =
Serialization.formats(NoTypeHints).withBigDecimal + new ext.EnumNameSerializer(ResultValueType) +
ext.UUIDSerializer + OffsetDateTimeSerializer

val timestampFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSX")

/**
* The method returns arbitrary object as a Json string.
*
* @return A string representing the object in Json format
*/
def asJson[T <: AnyRef](obj: T): String = {
write[T](obj)
}

/**
* The method returns arbitrary object as a pretty Json string.
*
* @return A string representing the object in Json format
*/
def asJsonPretty[T <: AnyRef](obj: T): String = {
writePretty[T](obj)
}

/**
* The method returns arbitrary object parsed from Json string.
*
* @return An object deserialized from the Json string
*/
def fromJson[T <: AnyRef](jsonStr: String)(implicit m: Manifest[T]): T = {
Serialization.read[T](jsonStr)
}

}
Loading

0 comments on commit 6301732

Please sign in to comment.