68758
|
1 |
/* Title: Pure/PIDE/document_status.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Document status based on markup information.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Document_Status
|
|
11 |
{
|
|
12 |
/* command status */
|
|
13 |
|
|
14 |
object Command_Status
|
|
15 |
{
|
|
16 |
val proper_elements: Markup.Elements =
|
|
17 |
Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
|
|
18 |
Markup.FINISHED, Markup.FAILED)
|
|
19 |
|
|
20 |
val liberal_elements: Markup.Elements =
|
|
21 |
proper_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR
|
|
22 |
|
|
23 |
def make(markup_iterator: Iterator[Markup]): Command_Status =
|
|
24 |
{
|
|
25 |
var touched = false
|
|
26 |
var accepted = false
|
|
27 |
var warned = false
|
|
28 |
var failed = false
|
|
29 |
var forks = 0
|
|
30 |
var runs = 0
|
|
31 |
for (markup <- markup_iterator) {
|
|
32 |
markup.name match {
|
|
33 |
case Markup.ACCEPTED => accepted = true
|
|
34 |
case Markup.FORKED => touched = true; forks += 1
|
|
35 |
case Markup.JOINED => forks -= 1
|
|
36 |
case Markup.RUNNING => touched = true; runs += 1
|
|
37 |
case Markup.FINISHED => runs -= 1
|
|
38 |
case Markup.WARNING | Markup.LEGACY => warned = true
|
|
39 |
case Markup.FAILED | Markup.ERROR => failed = true
|
|
40 |
case _ =>
|
|
41 |
}
|
|
42 |
}
|
|
43 |
Command_Status(touched, accepted, warned, failed, forks, runs)
|
|
44 |
}
|
|
45 |
|
|
46 |
val empty = make(Iterator.empty)
|
|
47 |
|
|
48 |
def merge(status_iterator: Iterator[Command_Status]): Command_Status =
|
|
49 |
if (status_iterator.hasNext) {
|
|
50 |
val status0 = status_iterator.next
|
|
51 |
(status0 /: status_iterator)(_ + _)
|
|
52 |
}
|
|
53 |
else empty
|
|
54 |
}
|
|
55 |
|
|
56 |
sealed case class Command_Status(
|
|
57 |
private val touched: Boolean,
|
|
58 |
private val accepted: Boolean,
|
|
59 |
private val warned: Boolean,
|
|
60 |
private val failed: Boolean,
|
|
61 |
forks: Int,
|
|
62 |
runs: Int)
|
|
63 |
{
|
|
64 |
def + (that: Command_Status): Command_Status =
|
|
65 |
Command_Status(
|
|
66 |
touched || that.touched,
|
|
67 |
accepted || that.accepted,
|
|
68 |
warned || that.warned,
|
|
69 |
failed || that.failed,
|
|
70 |
forks + that.forks,
|
|
71 |
runs + that.runs)
|
|
72 |
|
|
73 |
def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
|
|
74 |
def is_running: Boolean = runs != 0
|
|
75 |
def is_warned: Boolean = warned
|
|
76 |
def is_failed: Boolean = failed
|
|
77 |
def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
|
|
78 |
}
|
|
79 |
|
|
80 |
|
|
81 |
/* node status */
|
|
82 |
|
|
83 |
object Node_Status
|
|
84 |
{
|
|
85 |
def make(
|
|
86 |
state: Document.State,
|
|
87 |
version: Document.Version,
|
|
88 |
name: Document.Node.Name): Node_Status =
|
|
89 |
{
|
|
90 |
val node = version.nodes(name)
|
|
91 |
|
|
92 |
var unprocessed = 0
|
|
93 |
var running = 0
|
|
94 |
var warned = 0
|
|
95 |
var failed = 0
|
|
96 |
var finished = 0
|
|
97 |
for (command <- node.commands.iterator) {
|
|
98 |
val states = state.command_states(version, command)
|
|
99 |
val status = Command_Status.merge(states.iterator.map(_.document_status))
|
|
100 |
|
|
101 |
if (status.is_running) running += 1
|
|
102 |
else if (status.is_failed) failed += 1
|
|
103 |
else if (status.is_warned) warned += 1
|
|
104 |
else if (status.is_finished) finished += 1
|
|
105 |
else unprocessed += 1
|
|
106 |
}
|
|
107 |
val initialized = state.node_initialized(version, name)
|
|
108 |
val consolidated = state.node_consolidated(version, name)
|
|
109 |
|
|
110 |
Node_Status(unprocessed, running, warned, failed, finished, initialized, consolidated)
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
sealed case class Node_Status(
|
|
115 |
unprocessed: Int, running: Int, warned: Int, failed: Int, finished: Int,
|
|
116 |
initialized: Boolean, consolidated: Boolean)
|
|
117 |
{
|
|
118 |
def ok: Boolean = failed == 0
|
|
119 |
def total: Int = unprocessed + running + warned + failed + finished
|
|
120 |
|
|
121 |
def json: JSON.Object.T =
|
|
122 |
JSON.Object("ok" -> ok, "total" -> total, "unprocessed" -> unprocessed,
|
|
123 |
"running" -> running, "warned" -> warned, "failed" -> failed, "finished" -> finished,
|
|
124 |
"initialized" -> initialized, "consolidated" -> consolidated)
|
|
125 |
}
|
|
126 |
|
|
127 |
|
|
128 |
/* node timing */
|
|
129 |
|
|
130 |
object Node_Timing
|
|
131 |
{
|
|
132 |
val empty: Node_Timing = Node_Timing(0.0, Map.empty)
|
|
133 |
|
|
134 |
def make(
|
|
135 |
state: Document.State,
|
|
136 |
version: Document.Version,
|
|
137 |
node: Document.Node,
|
|
138 |
threshold: Double): Node_Timing =
|
|
139 |
{
|
|
140 |
var total = 0.0
|
|
141 |
var commands = Map.empty[Command, Double]
|
|
142 |
for {
|
|
143 |
command <- node.commands.iterator
|
|
144 |
st <- state.command_states(version, command)
|
|
145 |
} {
|
|
146 |
val command_timing =
|
|
147 |
(0.0 /: st.status)({
|
|
148 |
case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
|
|
149 |
case (timing, _) => timing
|
|
150 |
})
|
|
151 |
total += command_timing
|
|
152 |
if (command_timing >= threshold) commands += (command -> command_timing)
|
|
153 |
}
|
|
154 |
Node_Timing(total, commands)
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
|
68759
|
159 |
|
|
160 |
|
|
161 |
/* nodes status */
|
|
162 |
|
|
163 |
object Overall_Node_Status extends Enumeration
|
|
164 |
{
|
|
165 |
val ok, failed, pending = Value
|
|
166 |
}
|
|
167 |
|
|
168 |
object Nodes_Status
|
|
169 |
{
|
|
170 |
val empty: Nodes_Status = new Nodes_Status(Map.empty)
|
|
171 |
}
|
|
172 |
|
|
173 |
final class Nodes_Status private(private val rep: Map[Document.Node.Name, Node_Status])
|
|
174 |
{
|
|
175 |
def get(name: Document.Node.Name): Option[Node_Status] = rep.get(name)
|
|
176 |
|
|
177 |
def overall_node_status(name: Document.Node.Name): Overall_Node_Status.Value =
|
|
178 |
rep.get(name) match {
|
|
179 |
case Some(st) if st.consolidated =>
|
|
180 |
if (st.ok) Overall_Node_Status.ok else Overall_Node_Status.failed
|
|
181 |
case _ => Overall_Node_Status.pending
|
|
182 |
}
|
|
183 |
|
68763
|
184 |
def update(
|
|
185 |
session_base: Sessions.Base,
|
|
186 |
state: Document.State,
|
|
187 |
version: Document.Version,
|
68766
|
188 |
domain: Option[Set[Document.Node.Name]],
|
68764
|
189 |
trim: Boolean): Option[(Nodes_Status, List[Document.Node.Name])] =
|
68763
|
190 |
{
|
|
191 |
val nodes = version.nodes
|
|
192 |
val update_iterator =
|
|
193 |
for {
|
68766
|
194 |
name <- domain.getOrElse(nodes.domain).iterator
|
68763
|
195 |
if !Sessions.is_hidden(name) &&
|
|
196 |
!session_base.loaded_theory(name) &&
|
|
197 |
!nodes.is_suppressed(name) &&
|
|
198 |
!nodes(name).is_empty
|
|
199 |
st = Document_Status.Node_Status.make(state, version, name)
|
|
200 |
if !rep.isDefinedAt(name) || rep(name) != st
|
|
201 |
} yield (name -> st)
|
|
202 |
val rep1 = rep ++ update_iterator
|
|
203 |
val rep2 = if (trim) rep1 -- rep1.keysIterator.filterNot(nodes.domain) else rep1
|
68764
|
204 |
|
|
205 |
if (rep == rep2) None
|
|
206 |
else Some(new Nodes_Status(rep2), version.nodes.topological_order.filter(rep2.keySet))
|
68763
|
207 |
}
|
68761
|
208 |
|
68759
|
209 |
override def hashCode: Int = rep.hashCode
|
|
210 |
override def equals(that: Any): Boolean =
|
|
211 |
that match {
|
|
212 |
case other: Nodes_Status => rep == other.rep
|
|
213 |
case _ => false
|
|
214 |
}
|
68765
|
215 |
|
|
216 |
override def toString: String =
|
|
217 |
{
|
|
218 |
var ok = 0
|
|
219 |
var failed = 0
|
|
220 |
var pending = 0
|
|
221 |
for (name <- rep.keysIterator) {
|
|
222 |
overall_node_status(name) match {
|
|
223 |
case Overall_Node_Status.ok => ok += 1
|
|
224 |
case Overall_Node_Status.failed => failed += 1
|
|
225 |
case Overall_Node_Status.pending => pending += 1
|
|
226 |
}
|
|
227 |
}
|
|
228 |
"Nodes_Status(ok = " + ok + ", failed = " + failed + ", pending = " + pending + ")"
|
|
229 |
}
|
68759
|
230 |
}
|
68758
|
231 |
}
|