ProcessingReport.scala 3.64 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
19
20
21
/*
 * Media Converter
 * Extracts media files from Fedora repository
 * 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/>.
 */

package ch.memobase.models

22
23
import java.text.SimpleDateFormat
import java.util.Calendar
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
24
25


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
sealed trait ProcessingStatus {
  val value: String
}

case object ProcessingSuccess extends ProcessingStatus {
  override val value: String = "SUCCESS"
}

case object ProcessingWarning extends ProcessingStatus {
  override val value: String = "WARNING"
}

case object ProcessingIgnore extends ProcessingStatus {
  override val value: String = "IGNORE"
}

case object ProcessingFatal extends ProcessingStatus {
  override val value: String = "FATAL"
}
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
45
46


Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
47
48
49
50
case class ReportingObject(id: String,
                           digitalObject: Option[(ProcessingStatus, String)] = None,
                           poster: Option[(ProcessingStatus, String)] = None,
                           audioSnippet: Option[(ProcessingStatus, String)] = None) {
51

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
52
  import ReportingObject._
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
53
54

  override def toString: String =
55
56
57
58
59
    ujson.write(
      ujson.Obj(
        ("step", "media-metadata-extractor"),
        ("timestamp", createTimestamp),
        ("id", id),
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
60
61
        ("status", mergeStatus(digitalObject, poster, audioSnippet)),
        ("message", mergeMessages(digitalObject, poster, audioSnippet))
62
63
      )
    )
64
65
66
}

object ReportingObject {
67
  private val dateFormatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS")
68
69

  def createTimestamp: String = dateFormatter.format(Calendar.getInstance().getTime)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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

  def mergeStatus(digitalObject: Option[(ProcessingStatus, String)],
                  poster: Option[(ProcessingStatus, String)],
                  audioSnippet: Option[(ProcessingStatus, String)]): String = {
    val processStatus = List(digitalObject, poster, audioSnippet)
      .flatMap(obj => obj.flatMap(o => Some(o._1)))
    if (processStatus.contains(ProcessingFatal)) {
      ProcessingFatal.value
    } else if (processStatus.contains(ProcessingWarning)) {
      ProcessingWarning.value
    } else if (processStatus.contains(ProcessingSuccess)) {
      ProcessingSuccess.value
    } else {
      ProcessingIgnore.value
    }
  }

  def mergeMessages(digitalObject: Option[(ProcessingStatus, String)],
                    poster: Option[(ProcessingStatus, String)],
                    audioSnippet: Option[(ProcessingStatus, String)]): String =
    s"""DIGITAL OBJECT: ${digitalObject.flatMap(dO => Some(dO._2)).getOrElse("not available")}
       | -- POSTER: ${poster.flatMap(p => Some(p._2)).getOrElse("not available")}
       | -- AUDIO SNIPPET:${audioSnippet.flatMap(aS => Some(aS._2)).getOrElse("not available")}""".stripMargin

  def addResourceOutcome(report: ReportingObject, resource: (MemobaseResource, String), status: ProcessingStatus): ReportingObject =
    resource._1 match {
      case DigitalObject => report.copy(digitalObject = Some(status, resource._2))
      case Thumbnail => report.copy(poster = Some(status, resource._2))
      case AudioSnippet => report.copy(audioSnippet = Some(status, resource._2))
      case _ => report
    }
}