Report.scala 3.15 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
package ch.memobase.models

21
22
import java.text.SimpleDateFormat
import java.util.Date
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
23

24
25
26
27
28
29
import org.apache.kafka.clients.consumer.ConsumerRecord

import scala.util.Try

case class Report(msgKey: String,
                  recordId: String,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
30
                  step: String,
31
                  timestamp: Date,
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
32
                  status: String,
33
34
35
36
                  message: String,
                  recordSetId: String,
                  institutionId: String,
                  sessionId: String)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
37
38

object Report {
39
  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")
40
  private val shortDateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss")
41
42

  def apply(consumerRecord: ConsumerRecord[String, String]): Try[Report] = Try {
43
44
    val json = Try(ujson.read(consumerRecord.value())).getOrElse(throw new ParserException("JSON is not valid!"))
    val step = Try(json.obj("step").str).getOrElse(throw new ParserException("No `step` field in JSON obj"))
45
46
47
48
49
50
51
52
    if (step != "fedora-ingest") {
      throw new ParserIgnore("No fedora-ingest message")
    }
    val status = Try(json.obj("status").str).getOrElse(throw new ParserException("No `status` field in JSON obj"))
    if (status != "SUCCESS") {
      throw new ParserIgnore("No SUCCESS message")
    }
    val id = Try(json.obj("id").str).getOrElse(throw new ParserException("No `id` field in JSON obj"))
53
    val timestampString = Try(json.obj("timestamp").str).getOrElse(throw new ParserException("No `timestamp` field in JSON obj"))
54
55
56
    val timestamp = Try(dateFormatter.parse(timestampString))
      .orElse(Try(shortDateFormatter.parse(timestampString)))
      .getOrElse(throw new ParserException("No valid timestamp"))
57
    val message = Try(json.obj("message").str).getOrElse(throw new ParserException("No `message` field in JSON obj"))
58
    val headers = consumerRecord.headers()
59
60
61
62
63
64
    val recordSet = Try(new String(headers.lastHeader("recordSetId").value()))
      .getOrElse(throw new ParserException("Extraction of `recordSetId` header field failed"))
    val institution = Try(new String(headers.lastHeader("institutionId").value()))
      .getOrElse(throw new ParserException("Extraction of `institutionId` header field failed"))
    val session = Try(new String(headers.lastHeader("sessionId").value()))
      .getOrElse(throw new ParserException("Extraction of `sessionId` header field failed"))
65
    Report(consumerRecord.key, id, step, timestamp, status, message, recordSet, institution, session)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
66
67
68
  }
}