MsgFilter.scala 3.8 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Import Process Delete
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
19
20
21
package ch.memobase

import java.text.SimpleDateFormat
22
import java.util.Calendar
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
23
24
25
26
27
28
29
30
31
32
33
34

import ch.memobase.models.Report
import org.apache.kafka.clients.consumer.ConsumerRecord

import scala.collection.JavaConverters._

trait MsgFilter {

  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")

  type FilterFun = ConsumerRecord[String, String] => Boolean

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
  def buildFilters(createdAfter: Calendar,
                   createdBefore: Calendar,
                   institutions: Seq[String],
                   recordSets: Seq[String],
                   records: Seq[String],
                   sessions: Seq[String]): Seq[FilterFun] =
    Seq(buildStepFilter("fedora-ingest")) ++
      Seq(buildStatusFilter("SUCCESS")) ++
      Seq(buildCreatedAfterFilter(standardiseTimestamp(createdAfter))) ++
      Seq(buildCreatedBeforeFilter(standardiseTimestamp(createdBefore))) ++
      institutions.map(v => buildInstitutionIdFilter(v)) ++
      records.map(v => buildRecordIdFilter(v)) ++
      recordSets.map(v => buildRecordSetIdFilter(v)) ++
      sessions.map(v => buildSessionIdFilter(v))


  private def standardiseTimestamp(calendar: Calendar): String =
    f"${calendar.get(Calendar.YEAR)}%04d-" +
      f"${calendar.get(Calendar.MONTH)}%02d-" +
      f"${calendar.get(Calendar.DAY_OF_MONTH)}%02dT" +
      f"${calendar.get(Calendar.HOUR_OF_DAY)}%02d:" +
      f"${calendar.get(Calendar.MINUTE)}%02d:" +
      f"${calendar.get(Calendar.SECOND)}%02d." +
      f"${calendar.get(Calendar.MILLISECOND)}%03d"

  private val buildSessionIdFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
61
62
63
64
65
66
67
    sessionId => rec =>
      rec.headers()
        .headers("sessionId")
        .asScala
        .map(header => new String(header.value()))
        .exists(v => v == sessionId)

68
  private val buildRecordSetIdFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
69
70
71
72
73
74
75
    recordSetId => rec =>
      rec.headers()
        .headers("recordSetId")
        .asScala
        .map(header => new String(header.value()))
        .exists(v => v == recordSetId)

76
  private val buildInstitutionIdFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
77
78
79
80
81
82
83
    institutionId => rec =>
      rec.headers()
        .headers("institutionId")
        .asScala
        .map(header => new String(header.value()))
        .exists(v => v == institutionId)

84
  private val buildRecordIdFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
85
86
87
    recordId => rec =>
      Report(rec.value()).id == recordId

88
  private val buildCreatedAfterFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
89
90
91
92
93
94
    timestamp => rec => {
      val recordTimestamp = Report(rec.value()).timestamp
      recordTimestamp == timestamp ||
        dateFormatter.parse(timestamp).after(dateFormatter.parse(recordTimestamp))
    }

95
  private val buildCreatedBeforeFilter: String => FilterFun =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
96
97
98
99
100
101
    timestamp => rec => {
      val recordTimestamp = Report(rec.value()).timestamp
      recordTimestamp == timestamp ||
        dateFormatter.parse(timestamp).before(dateFormatter.parse(recordTimestamp))
    }

102
103
104
105
106
107
  private val buildStatusFilter: String => FilterFun =
    status => rec => Report(rec.value()).status == status

  private val buildStepFilter: String => FilterFun =
    step => rec => Report(rec.value()).step == step

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
108
}