src/Pure/PIDE/document_status.scala
author wenzelm
Sun Mar 10 14:19:30 2019 +0100 (4 months ago ago)
changeset 70070 be04e9a053a7
parent 70045 d594997cdcf4
permissions -rw-r--r--
markup and document markers for some meta data from "Dublin Core Metadata Element Set";
wenzelm@68758
     1
/*  Title:      Pure/PIDE/document_status.scala
wenzelm@68758
     2
    Author:     Makarius
wenzelm@68758
     3
wenzelm@68758
     4
Document status based on markup information.
wenzelm@68758
     5
*/
wenzelm@68758
     6
wenzelm@68758
     7
package isabelle
wenzelm@68758
     8
wenzelm@68758
     9
wenzelm@68758
    10
object Document_Status
wenzelm@68758
    11
{
wenzelm@68758
    12
  /* command status */
wenzelm@68758
    13
wenzelm@68758
    14
  object Command_Status
wenzelm@68758
    15
  {
wenzelm@68758
    16
    val proper_elements: Markup.Elements =
wenzelm@68758
    17
      Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@68871
    18
        Markup.FINISHED, Markup.FAILED, Markup.CANCELED)
wenzelm@68758
    19
wenzelm@68758
    20
    val liberal_elements: Markup.Elements =
wenzelm@68758
    21
      proper_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR
wenzelm@68758
    22
wenzelm@68758
    23
    def make(markup_iterator: Iterator[Markup]): Command_Status =
wenzelm@68758
    24
    {
wenzelm@68758
    25
      var touched = false
wenzelm@68758
    26
      var accepted = false
wenzelm@68758
    27
      var warned = false
wenzelm@68758
    28
      var failed = false
wenzelm@68871
    29
      var canceled = false
wenzelm@68884
    30
      var finalized = false
wenzelm@68758
    31
      var forks = 0
wenzelm@68758
    32
      var runs = 0
wenzelm@68758
    33
      for (markup <- markup_iterator) {
wenzelm@68758
    34
        markup.name match {
wenzelm@68758
    35
          case Markup.ACCEPTED => accepted = true
wenzelm@68758
    36
          case Markup.FORKED => touched = true; forks += 1
wenzelm@68758
    37
          case Markup.JOINED => forks -= 1
wenzelm@68758
    38
          case Markup.RUNNING => touched = true; runs += 1
wenzelm@68758
    39
          case Markup.FINISHED => runs -= 1
wenzelm@68758
    40
          case Markup.WARNING | Markup.LEGACY => warned = true
wenzelm@68758
    41
          case Markup.FAILED | Markup.ERROR => failed = true
wenzelm@68871
    42
          case Markup.CANCELED => canceled = true
wenzelm@68884
    43
          case Markup.FINALIZED => finalized = true
wenzelm@68758
    44
          case _ =>
wenzelm@68758
    45
        }
wenzelm@68758
    46
      }
wenzelm@68887
    47
      Command_Status(
wenzelm@68887
    48
        touched = touched,
wenzelm@68887
    49
        accepted = accepted,
wenzelm@68887
    50
        warned = warned,
wenzelm@68887
    51
        failed = failed,
wenzelm@68887
    52
        canceled = canceled,
wenzelm@68887
    53
        finalized = finalized,
wenzelm@68887
    54
        forks = forks,
wenzelm@68887
    55
        runs = runs)
wenzelm@68758
    56
    }
wenzelm@68758
    57
wenzelm@68758
    58
    val empty = make(Iterator.empty)
wenzelm@68758
    59
wenzelm@68758
    60
    def merge(status_iterator: Iterator[Command_Status]): Command_Status =
wenzelm@68758
    61
      if (status_iterator.hasNext) {
wenzelm@68758
    62
        val status0 = status_iterator.next
wenzelm@68758
    63
        (status0 /: status_iterator)(_ + _)
wenzelm@68758
    64
      }
wenzelm@68758
    65
      else empty
wenzelm@68758
    66
  }
wenzelm@68758
    67
wenzelm@68758
    68
  sealed case class Command_Status(
wenzelm@68758
    69
    private val touched: Boolean,
wenzelm@68758
    70
    private val accepted: Boolean,
wenzelm@68758
    71
    private val warned: Boolean,
wenzelm@68758
    72
    private val failed: Boolean,
wenzelm@68871
    73
    private val canceled: Boolean,
wenzelm@68884
    74
    private val finalized: Boolean,
wenzelm@68758
    75
    forks: Int,
wenzelm@68758
    76
    runs: Int)
wenzelm@68758
    77
  {
wenzelm@68758
    78
    def + (that: Command_Status): Command_Status =
wenzelm@68758
    79
      Command_Status(
wenzelm@68887
    80
        touched = touched || that.touched,
wenzelm@68887
    81
        accepted = accepted || that.accepted,
wenzelm@68887
    82
        warned = warned || that.warned,
wenzelm@68887
    83
        failed = failed || that.failed,
wenzelm@68887
    84
        canceled = canceled || that.canceled,
wenzelm@68887
    85
        finalized = finalized || that.finalized,
wenzelm@68887
    86
        forks = forks + that.forks,
wenzelm@68887
    87
        runs = runs + that.runs)
wenzelm@68758
    88
wenzelm@68758
    89
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@68758
    90
    def is_running: Boolean = runs != 0
wenzelm@68758
    91
    def is_warned: Boolean = warned
wenzelm@68758
    92
    def is_failed: Boolean = failed
wenzelm@68758
    93
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@68871
    94
    def is_canceled: Boolean = canceled
wenzelm@68884
    95
    def is_finalized: Boolean = finalized
wenzelm@68881
    96
    def is_terminated: Boolean = canceled || touched && forks == 0 && runs == 0
wenzelm@68758
    97
  }
wenzelm@68758
    98
wenzelm@68758
    99
wenzelm@68758
   100
  /* node status */
wenzelm@68758
   101
wenzelm@68758
   102
  object Node_Status
wenzelm@68758
   103
  {
wenzelm@68758
   104
    def make(
wenzelm@68758
   105
      state: Document.State,
wenzelm@68758
   106
      version: Document.Version,
wenzelm@68758
   107
      name: Document.Node.Name): Node_Status =
wenzelm@68758
   108
    {
wenzelm@68758
   109
      val node = version.nodes(name)
wenzelm@68758
   110
wenzelm@68758
   111
      var unprocessed = 0
wenzelm@68758
   112
      var running = 0
wenzelm@68758
   113
      var warned = 0
wenzelm@68758
   114
      var failed = 0
wenzelm@68758
   115
      var finished = 0
wenzelm@68871
   116
      var canceled = false
wenzelm@68892
   117
      var terminated = true
wenzelm@68884
   118
      var finalized = false
wenzelm@68758
   119
      for (command <- node.commands.iterator) {
wenzelm@68758
   120
        val states = state.command_states(version, command)
wenzelm@68758
   121
        val status = Command_Status.merge(states.iterator.map(_.document_status))
wenzelm@68758
   122
wenzelm@68758
   123
        if (status.is_running) running += 1
wenzelm@68758
   124
        else if (status.is_failed) failed += 1
wenzelm@68758
   125
        else if (status.is_warned) warned += 1
wenzelm@68758
   126
        else if (status.is_finished) finished += 1
wenzelm@68758
   127
        else unprocessed += 1
wenzelm@68871
   128
wenzelm@68871
   129
        if (status.is_canceled) canceled = true
wenzelm@68892
   130
        if (!status.is_terminated) terminated = false
wenzelm@68884
   131
        if (status.is_finalized) finalized = true
wenzelm@68758
   132
      }
wenzelm@68758
   133
      val initialized = state.node_initialized(version, name)
wenzelm@68758
   134
      val consolidated = state.node_consolidated(version, name)
wenzelm@68758
   135
wenzelm@68887
   136
      Node_Status(
wenzelm@69998
   137
        is_suppressed = version.nodes.is_suppressed(name),
wenzelm@68887
   138
        unprocessed = unprocessed,
wenzelm@68887
   139
        running = running,
wenzelm@68887
   140
        warned = warned,
wenzelm@68887
   141
        failed = failed,
wenzelm@68887
   142
        finished = finished,
wenzelm@68887
   143
        canceled = canceled,
wenzelm@68887
   144
        terminated = terminated,
wenzelm@68889
   145
        initialized = initialized,
wenzelm@68887
   146
        finalized = finalized,
wenzelm@68887
   147
        consolidated = consolidated)
wenzelm@68758
   148
    }
wenzelm@68758
   149
  }
wenzelm@68758
   150
wenzelm@68758
   151
  sealed case class Node_Status(
wenzelm@69998
   152
    is_suppressed: Boolean,
wenzelm@68887
   153
    unprocessed: Int,
wenzelm@68887
   154
    running: Int,
wenzelm@68887
   155
    warned: Int,
wenzelm@68887
   156
    failed: Int,
wenzelm@68887
   157
    finished: Int,
wenzelm@68887
   158
    canceled: Boolean,
wenzelm@68887
   159
    terminated: Boolean,
wenzelm@68889
   160
    initialized: Boolean,
wenzelm@68887
   161
    finalized: Boolean,
wenzelm@68884
   162
    consolidated: Boolean)
wenzelm@68758
   163
  {
wenzelm@68758
   164
    def ok: Boolean = failed == 0
wenzelm@68758
   165
    def total: Int = unprocessed + running + warned + failed + finished
wenzelm@68758
   166
wenzelm@70025
   167
    def quasi_consolidated: Boolean = !is_suppressed && !finalized && terminated
wenzelm@68897
   168
wenzelm@68898
   169
    def percentage: Int =
wenzelm@68898
   170
      if (consolidated) 100
wenzelm@68898
   171
      else if (total == 0) 0
wenzelm@68898
   172
      else (((total - unprocessed).toDouble / total) * 100).toInt min 99
wenzelm@68898
   173
wenzelm@68758
   174
    def json: JSON.Object.T =
wenzelm@68758
   175
      JSON.Object("ok" -> ok, "total" -> total, "unprocessed" -> unprocessed,
wenzelm@68758
   176
        "running" -> running, "warned" -> warned, "failed" -> failed, "finished" -> finished,
wenzelm@68898
   177
        "canceled" -> canceled, "consolidated" -> consolidated,
wenzelm@68898
   178
        "percentage" -> percentage)
wenzelm@68758
   179
  }
wenzelm@68758
   180
wenzelm@68758
   181
wenzelm@70044
   182
  /* overall timing */
wenzelm@68758
   183
wenzelm@70044
   184
  object Overall_Timing
wenzelm@68758
   185
  {
wenzelm@70044
   186
    val empty: Overall_Timing = Overall_Timing(0.0, Map.empty)
wenzelm@68758
   187
wenzelm@68758
   188
    def make(
wenzelm@68758
   189
      state: Document.State,
wenzelm@68758
   190
      version: Document.Version,
wenzelm@70044
   191
      commands: Iterable[Command],
wenzelm@70045
   192
      threshold: Double = 0.0): Overall_Timing =
wenzelm@68758
   193
    {
wenzelm@68758
   194
      var total = 0.0
wenzelm@70044
   195
      var command_timings = Map.empty[Command, Double]
wenzelm@68758
   196
      for {
wenzelm@70044
   197
        command <- commands.iterator
wenzelm@68758
   198
        st <- state.command_states(version, command)
wenzelm@68758
   199
      } {
wenzelm@68758
   200
        val command_timing =
wenzelm@68758
   201
          (0.0 /: st.status)({
wenzelm@68758
   202
            case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@68758
   203
            case (timing, _) => timing
wenzelm@68758
   204
          })
wenzelm@68758
   205
        total += command_timing
wenzelm@70045
   206
        if (command_timing > 0.0 && command_timing >= threshold) {
wenzelm@70045
   207
          command_timings += (command -> command_timing)
wenzelm@70045
   208
        }
wenzelm@68758
   209
      }
wenzelm@70044
   210
      Overall_Timing(total, command_timings)
wenzelm@68758
   211
    }
wenzelm@68758
   212
  }
wenzelm@68758
   213
wenzelm@70044
   214
  sealed case class Overall_Timing(total: Double, command_timings: Map[Command, Double])
wenzelm@70045
   215
  {
wenzelm@70045
   216
    def command_timing(command: Command): Double =
wenzelm@70045
   217
      command_timings.getOrElse(command, 0.0)
wenzelm@70045
   218
  }
wenzelm@68759
   219
wenzelm@68759
   220
wenzelm@68759
   221
  /* nodes status */
wenzelm@68759
   222
wenzelm@68759
   223
  object Overall_Node_Status extends Enumeration
wenzelm@68759
   224
  {
wenzelm@68759
   225
    val ok, failed, pending = Value
wenzelm@68759
   226
  }
wenzelm@68759
   227
wenzelm@68759
   228
  object Nodes_Status
wenzelm@68759
   229
  {
wenzelm@68904
   230
    val empty: Nodes_Status = new Nodes_Status(Map.empty, Document.Nodes.empty)
wenzelm@68759
   231
  }
wenzelm@68759
   232
wenzelm@68904
   233
  final class Nodes_Status private(
wenzelm@68904
   234
    private val rep: Map[Document.Node.Name, Node_Status],
wenzelm@68904
   235
    nodes: Document.Nodes)
wenzelm@68759
   236
  {
wenzelm@68770
   237
    def is_empty: Boolean = rep.isEmpty
wenzelm@68770
   238
    def apply(name: Document.Node.Name): Node_Status = rep(name)
wenzelm@68759
   239
    def get(name: Document.Node.Name): Option[Node_Status] = rep.get(name)
wenzelm@68759
   240
wenzelm@69998
   241
    def present: List[(Document.Node.Name, Node_Status)] =
wenzelm@69997
   242
      (for {
wenzelm@69997
   243
        name <- nodes.topological_order.iterator
wenzelm@69997
   244
        node_status <- get(name)
wenzelm@69997
   245
      } yield (name, node_status)).toList
wenzelm@68904
   246
wenzelm@68884
   247
    def quasi_consolidated(name: Document.Node.Name): Boolean =
wenzelm@68883
   248
      rep.get(name) match {
wenzelm@68897
   249
        case Some(st) => st.quasi_consolidated
wenzelm@68883
   250
        case None => false
wenzelm@68883
   251
      }
wenzelm@68883
   252
wenzelm@68759
   253
    def overall_node_status(name: Document.Node.Name): Overall_Node_Status.Value =
wenzelm@68759
   254
      rep.get(name) match {
wenzelm@68759
   255
        case Some(st) if st.consolidated =>
wenzelm@68759
   256
          if (st.ok) Overall_Node_Status.ok else Overall_Node_Status.failed
wenzelm@68759
   257
        case _ => Overall_Node_Status.pending
wenzelm@68759
   258
      }
wenzelm@68759
   259
wenzelm@68763
   260
    def update(
wenzelm@69260
   261
      resources: Resources,
wenzelm@68763
   262
      state: Document.State,
wenzelm@68763
   263
      version: Document.Version,
wenzelm@68769
   264
      domain: Option[Set[Document.Node.Name]] = None,
wenzelm@68904
   265
      trim: Boolean = false): (Boolean, Nodes_Status) =
wenzelm@68763
   266
    {
wenzelm@68904
   267
      val nodes1 = version.nodes
wenzelm@68763
   268
      val update_iterator =
wenzelm@68763
   269
        for {
wenzelm@68904
   270
          name <- domain.getOrElse(nodes1.domain).iterator
wenzelm@69997
   271
          if !resources.is_hidden(name) && !resources.session_base.loaded_theory(name)
wenzelm@68763
   272
          st = Document_Status.Node_Status.make(state, version, name)
wenzelm@68763
   273
          if !rep.isDefinedAt(name) || rep(name) != st
wenzelm@68763
   274
        } yield (name -> st)
wenzelm@68763
   275
      val rep1 = rep ++ update_iterator
wenzelm@68904
   276
      val rep2 = if (trim) rep1 -- rep1.keysIterator.filterNot(nodes1.domain) else rep1
wenzelm@68764
   277
wenzelm@68904
   278
      (rep != rep2, new Nodes_Status(rep2, nodes1))
wenzelm@68763
   279
    }
wenzelm@68761
   280
wenzelm@68759
   281
    override def hashCode: Int = rep.hashCode
wenzelm@68759
   282
    override def equals(that: Any): Boolean =
wenzelm@68759
   283
      that match {
wenzelm@68759
   284
        case other: Nodes_Status => rep == other.rep
wenzelm@68759
   285
        case _ => false
wenzelm@68759
   286
      }
wenzelm@68765
   287
wenzelm@68765
   288
    override def toString: String =
wenzelm@68765
   289
    {
wenzelm@68765
   290
      var ok = 0
wenzelm@68765
   291
      var failed = 0
wenzelm@68765
   292
      var pending = 0
wenzelm@68873
   293
      var canceled = 0
wenzelm@68765
   294
      for (name <- rep.keysIterator) {
wenzelm@68765
   295
        overall_node_status(name) match {
wenzelm@68765
   296
          case Overall_Node_Status.ok => ok += 1
wenzelm@68765
   297
          case Overall_Node_Status.failed => failed += 1
wenzelm@68765
   298
          case Overall_Node_Status.pending => pending += 1
wenzelm@68765
   299
        }
wenzelm@68873
   300
        if (apply(name).canceled) canceled += 1
wenzelm@68765
   301
      }
wenzelm@68873
   302
      "Nodes_Status(ok = " + ok + ", failed = " + failed + ", pending = " + pending +
wenzelm@68873
   303
        ", canceled = " + canceled + ")"
wenzelm@68765
   304
    }
wenzelm@68759
   305
  }
wenzelm@68758
   306
}