-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetFlowCheckpoints.scala
74 lines (64 loc) · 2.69 KB
/
GetFlowCheckpoints.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* 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.server.api.database.flows.functions
import doobie.Fragment
import doobie.implicits.toSqlInterpolator
import doobie.util.Read
import za.co.absa.atum.model.dto.CheckpointQueryDTO
import za.co.absa.atum.server.api.database.PostgresDatabaseProvider
import za.co.absa.atum.server.api.database.flows.Flows
import za.co.absa.atum.server.model.{CheckpointFromDB, PartitioningForDB}
import za.co.absa.fadb.DBSchema
import za.co.absa.fadb.doobie.DoobieEngine
import za.co.absa.fadb.doobie.DoobieFunction.DoobieMultipleResultFunction
import zio._
import zio.interop.catz._
import za.co.absa.atum.server.api.database.DoobieImplicits.Sequence.get
import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits._
import io.circe.syntax.EncoderOps
import io.circe.generic.auto._
class GetFlowCheckpoints(implicit schema: DBSchema, dbEngine: DoobieEngine[Task])
extends DoobieMultipleResultFunction[CheckpointQueryDTO, CheckpointFromDB, Task] {
override val fieldsToSelect: Seq[String] = Seq(
"id_checkpoint",
"checkpoint_name",
"author",
"measured_by_atum_agent",
"measure_name", "measured_columns", "measurement_value",
"checkpoint_start_time", "checkpoint_end_time",
)
override def sql(values: CheckpointQueryDTO)(implicit read: Read[CheckpointFromDB]): Fragment = {
val partitioning = PartitioningForDB.fromSeqPartitionDTO(values.partitioning)
val partitioningNormalized = partitioning.asJson.noSpaces
sql"""SELECT ${Fragment.const(selectEntry)}
FROM ${Fragment.const(functionName)}(
${
import za.co.absa.atum.server.api.database.DoobieImplicits.Jsonb.jsonbPutUsingString
partitioningNormalized
},
${values.limit},
${values.checkpointName}
) AS ${Fragment.const(alias)};"""
}
}
object GetFlowCheckpoints {
val layer: URLayer[PostgresDatabaseProvider, GetFlowCheckpoints] = ZLayer {
for {
dbProvider <- ZIO.service[PostgresDatabaseProvider]
} yield new GetFlowCheckpoints()(Flows, dbProvider.dbEngine)
}
}