Report.scala 2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")

  def apply(consumerRecord: ConsumerRecord[String, String]): Try[Report] = Try {
    val json = ujson.read(consumerRecord.value())
    val id = json.obj("id").str
    val step = json.obj("step").str
    val timestamp = dateFormatter.parse(json.obj("timestamp").str)
    val status = json.obj("status").str
    val message = json.obj("message").str
    val headers = consumerRecord.headers()
    val recordSet = new String(headers.lastHeader("recordSetId").value())
    val institution = new String(headers.lastHeader("institutionId").value())
    val session = new String(headers.lastHeader("sessionId").value())
    Report(consumerRecord.key, id, step, timestamp, status, message, recordSet, institution, session)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
53
54
55
  }
}