-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save summary at the end of the batch
- Loading branch information
Simon Dumas
committed
Apr 16, 2024
1 parent
9dc4cc1
commit ab2fea5
Showing
12 changed files
with
194 additions
and
30 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
delta/sourcing-psql/src/main/resources/scripts/postgres/drop/drop-tables.ddl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
delta/sourcing-psql/src/main/resources/scripts/postgres/init/V1_10_M09_001__ship_reports.ddl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE IF NOT EXISTS public.ship_runs( | ||
ordering bigserial, | ||
started_at timestamptz NOT NULL, | ||
ended_at timestamptz NOT NULL, | ||
command JSONB NOT NULL, | ||
success boolean NOT NULL, | ||
error text, | ||
report JSONB | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/ImportStatus.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/InitShip.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/ShipSummaryStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ch.epfl.bluebrain.nexus.ship | ||
|
||
import cats.effect.IO | ||
import ch.epfl.bluebrain.nexus.delta.kernel.utils.ThrowableUtils | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors | ||
import ch.epfl.bluebrain.nexus.ship.ShipCommand.RunCommand | ||
import doobie.implicits._ | ||
import doobie.postgres.implicits._ | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.implicits._ | ||
import io.circe.syntax.EncoderOps | ||
|
||
import java.time.Instant | ||
|
||
object ShipSummaryStore { | ||
|
||
def save( | ||
xas: Transactors, | ||
start: Instant, | ||
end: Instant, | ||
command: RunCommand, | ||
reportOrError: Either[Throwable, ImportReport] | ||
): IO[Unit] = { | ||
val success = reportOrError.isRight | ||
val insert = reportOrError match { | ||
case Left(error) => { | ||
val errorMessage = ThrowableUtils.stackTraceAsString(error) | ||
sql""" INSERT INTO ship_runs(started_at, ended_at, command, success, error) VALUES($start, $end, ${command.asJson}, $success, $errorMessage)""" | ||
} | ||
case Right(report) => | ||
sql""" INSERT INTO ship_runs(started_at, ended_at, command , success, report) VALUES($start, $end, ${command.asJson}, $success, ${report.asJson})""" | ||
} | ||
insert.update.run.transact(xas.write).void | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/ShipSummaryStoreSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ch.epfl.bluebrain.nexus.ship | ||
|
||
import cats.effect.IO | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.postgres.Doobie | ||
import ch.epfl.bluebrain.nexus.ship.ShipCommand.RunCommand | ||
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite | ||
import doobie.implicits._ | ||
import doobie.postgres.implicits._ | ||
import munit.AnyFixture | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.offset.Offset | ||
import ch.epfl.bluebrain.nexus.ship.ShipSummaryStoreSuite.ReportRow | ||
import fs2.io.file.Path | ||
|
||
import java.time.Instant | ||
|
||
class ShipSummaryStoreSuite extends NexusSuite with Doobie.Fixture { | ||
|
||
override def munitFixtures: Seq[AnyFixture[_]] = List(doobie) | ||
|
||
private lazy val xas = doobie() | ||
|
||
private def readLast: IO[ReportRow] = | ||
sql"""SELECT started_at, ended_at, success, error is NOT NULL, report is NOT NULL | ||
|FROM public.ship_runs | ||
|ORDER by ordering DESC | ||
|LIMIT 1 | ||
|""".stripMargin | ||
.query[(Instant, Instant, Boolean, Boolean, Boolean)] | ||
.unique | ||
.transact(xas.read) | ||
.map { case (start, end, success, hasError, hasReport) => | ||
ReportRow(start, end, success, hasError, hasReport) | ||
} | ||
|
||
private def assertSave( | ||
start: Instant, | ||
end: Instant, | ||
command: RunCommand, | ||
reportOrError: Either[Throwable, ImportReport] | ||
) = { | ||
for { | ||
_ <- ShipSummaryStore.save(xas, start, end, command, reportOrError) | ||
row <- readLast | ||
} yield { | ||
assertEquals(row.start, start) | ||
assertEquals(row.end, end) | ||
assertEquals(row.success, reportOrError.isRight) | ||
assertEquals(row.hasError, reportOrError.isLeft) | ||
assertEquals(row.hasReport, reportOrError.isRight) | ||
} | ||
} | ||
|
||
private val start = Instant.now().minusSeconds(3600) | ||
private val end = Instant.now() | ||
private val runCommand = RunCommand(Path("/data"), None, Offset.start, RunMode.Local) | ||
|
||
test("Save a failed run") { | ||
val error = new IllegalStateException("BOOM !") | ||
assertSave(start, end, runCommand, Left(error)) | ||
} | ||
|
||
test("Save a successful run") { | ||
val report = ImportReport(Offset.at(5L), Instant.now(), Map.empty) | ||
assertSave(start, end, runCommand, Right(report)) | ||
} | ||
|
||
} | ||
|
||
object ShipSummaryStoreSuite { | ||
|
||
final case class ReportRow(start: Instant, end: Instant, success: Boolean, hasError: Boolean, hasReport: Boolean) | ||
|
||
} |