-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScheduleStrategyIncremental.scala
124 lines (108 loc) · 5.04 KB
/
ScheduleStrategyIncremental.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2022 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.pramen.core.runner.splitter
import za.co.absa.pramen.api.jobdef.Schedule
import za.co.absa.pramen.api.status.{MetastoreDependency, TaskRunReason}
import za.co.absa.pramen.core.bookkeeper.Bookkeeper
import za.co.absa.pramen.core.pipeline
import za.co.absa.pramen.core.pipeline.TaskPreDef
import za.co.absa.pramen.core.runner.splitter.ScheduleStrategyUtils._
import java.time.LocalDate
class ScheduleStrategyIncremental(lastInfoDateProcessedOpt: Option[LocalDate], hasInfoDateColumn: Boolean) extends ScheduleStrategy {
private val log = org.slf4j.LoggerFactory.getLogger(this.getClass)
override def getDaysToRun(
outputTable: String,
dependencies: Seq[MetastoreDependency],
bookkeeper: Bookkeeper,
infoDateExpression: String,
schedule: Schedule,
params: ScheduleParams,
initialSourcingDateExpr: String,
minimumDate: LocalDate
): Seq[TaskPreDef] = {
val dates = params match {
case ScheduleParams.Normal(runDate, trackDays, _, newOnly, lateOnly) =>
val infoDate = evaluateRunDate(runDate, infoDateExpression)
log.info(s"Normal run strategy: runDate=$runDate, infoDate=$infoDate")
val runInfoDays = if (hasInfoDateColumn) {
lastInfoDateProcessedOpt match {
case Some(lastInfoDate) =>
val newDays = if (lastInfoDate.isBefore(infoDate))
Seq(TaskPreDef(infoDate.minusDays(1), TaskRunReason.New), TaskPreDef(infoDate, TaskRunReason.New))
else
Seq(TaskPreDef(infoDate, TaskRunReason.New))
val lateDays = getLateDays(infoDate, lastInfoDate, trackDays)
if (newOnly) {
newDays
} else if (lateOnly) {
lateDays
} else {
lateDays ++ newDays
}
case None => Seq(TaskPreDef(infoDate, TaskRunReason.New))
}
} else {
lastInfoDateProcessedOpt match {
case Some(lastInfoDate) if lastInfoDate.isAfter(infoDate) => Seq.empty
case _ => Seq(TaskPreDef(infoDate, TaskRunReason.New))
}
}
if (runInfoDays.nonEmpty) {
log.info(s"Days to run: ${runInfoDays.map(_.infoDate).mkString(", ")}")
} else {
log.info(s"Days to run: no days have been selected to run by the scheduler.")
}
runInfoDays.toList
case ScheduleParams.Rerun(runDate) =>
log.info(s"Rerun strategy for a single day: $runDate")
val infoDate = evaluateRunDate(runDate, infoDateExpression)
log.info(s"Rerunning '$outputTable' for date $runDate. Info date = '$infoDateExpression' = $infoDate.")
List(pipeline.TaskPreDef(infoDate, TaskRunReason.Rerun))
case ScheduleParams.Historical(dateFrom, dateTo, inverseDateOrder, mode) =>
log.info(s"Ranged strategy: from $dateFrom to $dateTo, mode = '${mode.toString}', minimumDate = $minimumDate")
getHistorical(outputTable, dateFrom, dateTo, schedule, mode, infoDateExpression, minimumDate, inverseDateOrder, bookkeeper)
}
filterOutPastMinimumDates(dates, minimumDate)
}
private[core] def getLateDays(infoDate: LocalDate, lastInfoDate: LocalDate, trackDays: Int): Seq[TaskPreDef] = {
val lastNewDate = infoDate.minusDays(1)
if (lastInfoDate.isBefore(lastNewDate)) {
val startDate = if (trackDays > 1) {
val trackDate = lastNewDate.minusDays(trackDays - 1)
val date = if (trackDate.isAfter(lastInfoDate))
trackDate
else
lastInfoDate
log.warn(s"Last ran day: $lastInfoDate. Tracking days = '$trackDate'. Catching up from '$date' until '$infoDate'.")
date
} else {
if (trackDays < 0) {
log.warn(s"Last ran day: $lastInfoDate. Catching up data until '$infoDate'.")
lastInfoDate
} else {
log.warn(s"Last ran day: $lastInfoDate. Not catching up since 'track.days=$trackDays'. Set it to the number of days or '-1' for infinite catching up.")
lastNewDate
}
}
val potentialDates = getInfoDateRange(startDate, lastNewDate.minusDays(1), "@runDate", Schedule.Incremental)
potentialDates.map(date => {
TaskPreDef(date, TaskRunReason.Late)
})
} else {
Seq.empty
}
}
}