src/Pure/Admin/build_log.scala
author wenzelm
Sun May 13 16:37:36 2018 +0200 (13 months ago)
changeset 68169 395432e7516e
parent 68018 3747fe57eb67
child 69299 2fd070377c99
permissions -rw-r--r--
tuned signature;
wenzelm@64160
     1
/*  Title:      Pure/Admin/build_log.scala
wenzelm@64045
     2
    Author:     Makarius
wenzelm@64045
     3
wenzelm@65608
     4
Management of build log files and database storage.
wenzelm@64045
     5
*/
wenzelm@64045
     6
wenzelm@64045
     7
package isabelle
wenzelm@64045
     8
wenzelm@64045
     9
wenzelm@64100
    10
import java.io.{File => JFile}
wenzelm@64100
    11
import java.time.ZoneId
wenzelm@64110
    12
import java.time.format.{DateTimeFormatter, DateTimeParseException}
wenzelm@64096
    13
import java.util.Locale
wenzelm@64061
    14
wenzelm@65600
    15
import scala.collection.immutable.SortedMap
wenzelm@64054
    16
import scala.collection.mutable
wenzelm@64054
    17
import scala.util.matching.Regex
wenzelm@64054
    18
wenzelm@64054
    19
wenzelm@64045
    20
object Build_Log
wenzelm@64045
    21
{
wenzelm@64298
    22
  /** content **/
wenzelm@64101
    23
wenzelm@64298
    24
  /* properties */
wenzelm@64150
    25
wenzelm@64298
    26
  object Prop
wenzelm@64298
    27
  {
wenzelm@65624
    28
    val build_tags = SQL.Column.string("build_tags")  // lines
wenzelm@65624
    29
    val build_args = SQL.Column.string("build_args")  // lines
wenzelm@65591
    30
    val build_group_id = SQL.Column.string("build_group_id")
wenzelm@65591
    31
    val build_id = SQL.Column.string("build_id")
wenzelm@65591
    32
    val build_engine = SQL.Column.string("build_engine")
wenzelm@65591
    33
    val build_host = SQL.Column.string("build_host")
wenzelm@65591
    34
    val build_start = SQL.Column.date("build_start")
wenzelm@65591
    35
    val build_end = SQL.Column.date("build_end")
wenzelm@65591
    36
    val isabelle_version = SQL.Column.string("isabelle_version")
wenzelm@65591
    37
    val afp_version = SQL.Column.string("afp_version")
wenzelm@65591
    38
wenzelm@65611
    39
    val all_props: List[SQL.Column] =
wenzelm@65591
    40
      List(build_tags, build_args, build_group_id, build_id, build_engine,
wenzelm@65591
    41
        build_host, build_start, build_end, isabelle_version, afp_version)
wenzelm@64298
    42
  }
wenzelm@64150
    43
wenzelm@64150
    44
wenzelm@64298
    45
  /* settings */
wenzelm@64080
    46
wenzelm@64081
    47
  object Settings
wenzelm@64081
    48
  {
wenzelm@65611
    49
    val ISABELLE_BUILD_OPTIONS = SQL.Column.string("ISABELLE_BUILD_OPTIONS")
wenzelm@65611
    50
    val ML_PLATFORM = SQL.Column.string("ML_PLATFORM")
wenzelm@65611
    51
    val ML_HOME = SQL.Column.string("ML_HOME")
wenzelm@65611
    52
    val ML_SYSTEM = SQL.Column.string("ML_SYSTEM")
wenzelm@65611
    53
    val ML_OPTIONS = SQL.Column.string("ML_OPTIONS")
wenzelm@65611
    54
wenzelm@65611
    55
    val ml_settings = List(ML_PLATFORM, ML_HOME, ML_SYSTEM, ML_OPTIONS)
wenzelm@65611
    56
    val all_settings = ISABELLE_BUILD_OPTIONS :: ml_settings
wenzelm@64081
    57
wenzelm@64081
    58
    type Entry = (String, String)
wenzelm@64081
    59
    type T = List[Entry]
wenzelm@64080
    60
wenzelm@64081
    61
    object Entry
wenzelm@64081
    62
    {
wenzelm@64081
    63
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    64
        s.indexOf('=') match {
wenzelm@64081
    65
          case -1 => None
wenzelm@64081
    66
          case i =>
wenzelm@64081
    67
            val a = s.substring(0, i)
wenzelm@64081
    68
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    69
            Some((a, b))
wenzelm@64081
    70
        }
wenzelm@64081
    71
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    72
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    73
    }
wenzelm@64080
    74
wenzelm@64081
    75
    def show(): String =
wenzelm@64081
    76
      cat_lines(
wenzelm@65611
    77
        List(Entry.getenv(ISABELLE_BUILD_OPTIONS.name), "") :::
wenzelm@65611
    78
        ml_settings.map(c => Entry.getenv(c.name)))
wenzelm@64080
    79
  }
wenzelm@64080
    80
wenzelm@64080
    81
wenzelm@64298
    82
  /* file names */
wenzelm@64298
    83
wenzelm@64298
    84
  def log_date(date: Date): String =
wenzelm@64298
    85
    String.format(Locale.ROOT, "%s.%05d",
wenzelm@64298
    86
      DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date.rep),
wenzelm@64298
    87
      new java.lang.Long((date.time - date.midnight.time).ms / 1000))
wenzelm@64298
    88
wenzelm@64298
    89
  def log_subdir(date: Date): Path =
wenzelm@64298
    90
    Path.explode("log") + Path.explode(date.rep.getYear.toString)
wenzelm@64298
    91
wenzelm@64298
    92
  def log_filename(engine: String, date: Date, more: List[String] = Nil): Path =
wenzelm@64298
    93
    Path.explode((engine :: log_date(date) :: more).mkString("", "_", ".log"))
wenzelm@64298
    94
wenzelm@64298
    95
wenzelm@64100
    96
wenzelm@64062
    97
  /** log file **/
wenzelm@64045
    98
wenzelm@64155
    99
  def print_date(date: Date): String = Log_File.Date_Format(date)
wenzelm@64155
   100
wenzelm@64062
   101
  object Log_File
wenzelm@64062
   102
  {
wenzelm@65607
   103
    /* log file */
wenzelm@65607
   104
wenzelm@65609
   105
    def plain_name(name: String): String =
wenzelm@65609
   106
    {
wenzelm@65609
   107
      List(".log", ".log.gz", ".log.xz", ".gz", ".xz").find(name.endsWith(_)) match {
wenzelm@65609
   108
        case Some(s) => Library.try_unsuffix(s, name).get
wenzelm@65609
   109
        case None => name
wenzelm@65609
   110
      }
wenzelm@65609
   111
    }
wenzelm@65609
   112
wenzelm@64062
   113
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@65609
   114
      new Log_File(plain_name(name), lines)
wenzelm@64062
   115
wenzelm@64062
   116
    def apply(name: String, text: String): Log_File =
wenzelm@64063
   117
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64090
   118
wenzelm@64090
   119
    def apply(file: JFile): Log_File =
wenzelm@64090
   120
    {
wenzelm@64090
   121
      val name = file.getName
wenzelm@65609
   122
      val text =
wenzelm@65609
   123
        if (name.endsWith(".gz")) File.read_gzip(file)
wenzelm@65609
   124
        else if (name.endsWith(".xz")) File.read_xz(file)
wenzelm@65609
   125
        else File.read(file)
wenzelm@65609
   126
      apply(name, text)
wenzelm@64090
   127
    }
wenzelm@64090
   128
wenzelm@64090
   129
    def apply(path: Path): Log_File = apply(path.file)
wenzelm@64101
   130
wenzelm@64110
   131
wenzelm@65607
   132
    /* log file collections */
wenzelm@65607
   133
wenzelm@65607
   134
    def is_log(file: JFile,
wenzelm@65607
   135
      prefixes: List[String] =
wenzelm@66995
   136
        List(Build_History.log_prefix, Identify.log_prefix, Identify.log_prefix2,
wenzelm@66995
   137
          Isatest.log_prefix, AFP_Test.log_prefix, Jenkins.log_prefix),
wenzelm@65609
   138
      suffixes: List[String] = List(".log", ".log.gz", ".log.xz")): Boolean =
wenzelm@65607
   139
    {
wenzelm@65607
   140
      val name = file.getName
wenzelm@65639
   141
wenzelm@65607
   142
      prefixes.exists(name.startsWith(_)) &&
wenzelm@65639
   143
      suffixes.exists(name.endsWith(_)) &&
wenzelm@65639
   144
      name != "isatest.log" &&
wenzelm@65639
   145
      name != "afp-test.log" &&
wenzelm@65639
   146
      name != "main.log"
wenzelm@65607
   147
    }
wenzelm@65607
   148
wenzelm@65607
   149
    def find_files(dirs: Iterable[Path]): List[JFile] =
wenzelm@65607
   150
      dirs.iterator.flatMap(dir => File.find_files(dir.file, is_log(_))).toList
wenzelm@65607
   151
wenzelm@65607
   152
wenzelm@64110
   153
    /* date format */
wenzelm@64110
   154
wenzelm@64101
   155
    val Date_Format =
wenzelm@64101
   156
    {
wenzelm@64101
   157
      val fmts =
wenzelm@64101
   158
        Date.Formatter.variants(
wenzelm@64116
   159
          List("EEE MMM d HH:mm:ss O yyyy", "EEE MMM d HH:mm:ss VV yyyy"),
wenzelm@64104
   160
          List(Locale.ENGLISH, Locale.GERMAN)) :::
wenzelm@64110
   161
        List(
wenzelm@64110
   162
          DateTimeFormatter.RFC_1123_DATE_TIME,
wenzelm@64110
   163
          Date.Formatter.pattern("EEE MMM d HH:mm:ss yyyy").withZone(ZoneId.of("Europe/Berlin")))
wenzelm@64101
   164
wenzelm@64104
   165
      def tune_timezone(s: String): String =
wenzelm@64104
   166
        s match {
wenzelm@64101
   167
          case "CET" | "MET" => "GMT+1"
wenzelm@64101
   168
          case "CEST" | "MEST" => "GMT+2"
wenzelm@64104
   169
          case "EST" => "Europe/Berlin"
wenzelm@64104
   170
          case _ => s
wenzelm@64104
   171
        }
wenzelm@64104
   172
      def tune_weekday(s: String): String =
wenzelm@64104
   173
        s match {
wenzelm@64104
   174
          case "Die" => "Di"
wenzelm@64104
   175
          case "Mit" => "Mi"
wenzelm@64104
   176
          case "Don" => "Do"
wenzelm@64104
   177
          case "Fre" => "Fr"
wenzelm@64104
   178
          case "Sam" => "Sa"
wenzelm@64104
   179
          case "Son" => "So"
wenzelm@64104
   180
          case _ => s
wenzelm@64104
   181
        }
wenzelm@64104
   182
wenzelm@64104
   183
      def tune(s: String): String =
wenzelm@64104
   184
        Word.implode(
wenzelm@64104
   185
          Word.explode(s) match {
wenzelm@64104
   186
            case a :: "M\uFFFDr" :: bs => tune_weekday(a) :: "Mär" :: bs.map(tune_timezone(_))
wenzelm@64104
   187
            case a :: bs => tune_weekday(a) :: bs.map(tune_timezone(_))
wenzelm@64104
   188
            case Nil => Nil
wenzelm@64104
   189
          }
wenzelm@64104
   190
        )
wenzelm@64101
   191
wenzelm@64101
   192
      Date.Format.make(fmts, tune)
wenzelm@64101
   193
    }
wenzelm@64117
   194
wenzelm@64117
   195
wenzelm@64117
   196
    /* inlined content */
wenzelm@64117
   197
wenzelm@64119
   198
    def print_props(marker: String, props: Properties.T): String =
wenzelm@65624
   199
      marker + YXML.string_of_body(XML.Encode.properties(Properties.encode_lines(props)))
wenzelm@64102
   200
  }
wenzelm@64102
   201
wenzelm@64102
   202
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64102
   203
  {
wenzelm@64102
   204
    log_file =>
wenzelm@64102
   205
wenzelm@64102
   206
    override def toString: String = name
wenzelm@64102
   207
wenzelm@64102
   208
    def text: String = cat_lines(lines)
wenzelm@64102
   209
wenzelm@64102
   210
    def err(msg: String): Nothing =
wenzelm@64102
   211
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64102
   212
wenzelm@64102
   213
wenzelm@64102
   214
    /* date format */
wenzelm@64101
   215
wenzelm@64101
   216
    object Strict_Date
wenzelm@64101
   217
    {
wenzelm@64101
   218
      def unapply(s: String): Some[Date] =
wenzelm@64102
   219
        try { Some(Log_File.Date_Format.parse(s)) }
wenzelm@64101
   220
        catch { case exn: DateTimeParseException => log_file.err(exn.getMessage) }
wenzelm@64101
   221
    }
wenzelm@64101
   222
wenzelm@64101
   223
wenzelm@64062
   224
    /* inlined content */
wenzelm@64062
   225
wenzelm@64062
   226
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
   227
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
   228
wenzelm@64196
   229
    def find_line(marker: String): Option[String] =
wenzelm@64196
   230
      find(Library.try_unprefix(marker, _))
wenzelm@64196
   231
wenzelm@65684
   232
    def find_match(regexes: List[Regex]): Option[String] =
wenzelm@65684
   233
      regexes match {
wenzelm@65684
   234
        case Nil => None
wenzelm@65684
   235
        case regex :: rest =>
wenzelm@65684
   236
          lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@65684
   237
            map(res => res.get.head) orElse find_match(rest)
wenzelm@65684
   238
      }
wenzelm@64062
   239
wenzelm@64062
   240
wenzelm@64062
   241
    /* settings */
wenzelm@64062
   242
wenzelm@64091
   243
    def get_setting(a: String): Option[Settings.Entry] =
wenzelm@64091
   244
      lines.find(_.startsWith(a + "=")) match {
wenzelm@64091
   245
        case Some(line) => Settings.Entry.unapply(line)
wenzelm@64091
   246
        case None => None
wenzelm@64091
   247
      }
wenzelm@64045
   248
wenzelm@65611
   249
    def get_all_settings: Settings.T =
wenzelm@65611
   250
      for { c <- Settings.all_settings; entry <- get_setting(c.name) }
wenzelm@65611
   251
      yield entry
wenzelm@64062
   252
wenzelm@64062
   253
wenzelm@64062
   254
    /* properties (YXML) */
wenzelm@64062
   255
wenzelm@68169
   256
    val xml_cache = XML.make_cache()
wenzelm@64062
   257
wenzelm@64062
   258
    def parse_props(text: String): Properties.T =
wenzelm@66046
   259
      try {
wenzelm@66046
   260
        xml_cache.props(Properties.decode_lines(XML.Decode.properties(YXML.parse_body(text))))
wenzelm@66046
   261
      }
wenzelm@66046
   262
      catch { case _: XML.Error => log_file.err("malformed properties") }
wenzelm@64062
   263
wenzelm@65934
   264
    def filter_lines(marker: String): List[String] =
wenzelm@65934
   265
      for (line <- lines; s <- Library.try_unprefix(marker, line)) yield s
wenzelm@65934
   266
wenzelm@64119
   267
    def filter_props(marker: String): List[Properties.T] =
wenzelm@65934
   268
      for (s <- filter_lines(marker) if YXML.detect(s)) yield parse_props(s)
wenzelm@64062
   269
wenzelm@64119
   270
    def find_props(marker: String): Option[Properties.T] =
wenzelm@64196
   271
      find_line(marker) match {
wenzelm@64196
   272
        case Some(text) if YXML.detect(text) => Some(parse_props(text))
wenzelm@64196
   273
        case _ => None
wenzelm@64196
   274
      }
wenzelm@64062
   275
wenzelm@64062
   276
wenzelm@64062
   277
    /* parse various formats */
wenzelm@64062
   278
wenzelm@64105
   279
    def parse_meta_info(): Meta_Info = Build_Log.parse_meta_info(log_file)
wenzelm@64105
   280
wenzelm@65646
   281
    def parse_build_info(ml_statistics: Boolean = false): Build_Info =
wenzelm@65646
   282
      Build_Log.parse_build_info(log_file, ml_statistics)
wenzelm@64105
   283
wenzelm@64082
   284
    def parse_session_info(
wenzelm@64082
   285
        command_timings: Boolean = false,
wenzelm@66873
   286
        theory_timings: Boolean = false,
wenzelm@64082
   287
        ml_statistics: Boolean = false,
wenzelm@64082
   288
        task_statistics: Boolean = false): Session_Info =
wenzelm@66873
   289
      Build_Log.parse_session_info(
wenzelm@66873
   290
        log_file, command_timings, theory_timings, ml_statistics, task_statistics)
wenzelm@64045
   291
  }
wenzelm@64045
   292
wenzelm@64045
   293
wenzelm@64098
   294
wenzelm@65276
   295
  /** digested meta info: produced by Admin/build_history in log.xz file **/
wenzelm@64045
   296
wenzelm@64108
   297
  object Meta_Info
wenzelm@64099
   298
  {
wenzelm@64108
   299
    val empty: Meta_Info = Meta_Info(Nil, Nil)
wenzelm@64099
   300
  }
wenzelm@64098
   301
wenzelm@65611
   302
  sealed case class Meta_Info(props: Properties.T, settings: Settings.T)
wenzelm@64103
   303
  {
wenzelm@64103
   304
    def is_empty: Boolean = props.isEmpty && settings.isEmpty
wenzelm@65599
   305
wenzelm@65611
   306
    def get(c: SQL.Column): Option[String] =
wenzelm@65611
   307
      Properties.get(props, c.name) orElse
wenzelm@65611
   308
      Properties.get(settings, c.name)
wenzelm@65611
   309
wenzelm@65611
   310
    def get_date(c: SQL.Column): Option[Date] =
wenzelm@65611
   311
      get(c).map(Log_File.Date_Format.parse(_))
wenzelm@64103
   312
  }
wenzelm@64061
   313
wenzelm@65625
   314
  object Identify
wenzelm@65625
   315
  {
wenzelm@65625
   316
    val log_prefix = "isabelle_identify_"
wenzelm@66995
   317
    val log_prefix2 = "plain_identify_"
wenzelm@65674
   318
wenzelm@65674
   319
    def engine(log_file: Log_File): String =
wenzelm@65674
   320
      if (log_file.name.startsWith(Jenkins.log_prefix)) "jenkins_identify"
wenzelm@66995
   321
      else if (log_file.name.startsWith(log_prefix2)) "plain_identify"
wenzelm@65674
   322
      else "identify"
wenzelm@65674
   323
wenzelm@65674
   324
    def content(date: Date, isabelle_version: Option[String], afp_version: Option[String]): String =
wenzelm@65674
   325
      terminate_lines(
wenzelm@65674
   326
        List("isabelle_identify: " + Build_Log.print_date(date), "") :::
wenzelm@65674
   327
        isabelle_version.map("Isabelle version: " + _).toList :::
wenzelm@65674
   328
        afp_version.map("AFP version: " + _).toList)
wenzelm@65674
   329
wenzelm@65625
   330
    val Start = new Regex("""^isabelle_identify: (.+)$""")
wenzelm@65625
   331
    val No_End = new Regex("""$.""")
wenzelm@65684
   332
    val Isabelle_Version = List(new Regex("""^Isabelle version: (\S+)$"""))
wenzelm@65684
   333
    val AFP_Version = List(new Regex("""^AFP version: (\S+)$"""))
wenzelm@65625
   334
  }
wenzelm@65625
   335
wenzelm@64095
   336
  object Isatest
wenzelm@64095
   337
  {
wenzelm@65588
   338
    val log_prefix = "isatest-makeall-"
wenzelm@64108
   339
    val engine = "isatest"
wenzelm@64109
   340
    val Start = new Regex("""^------------------- starting test --- (.+) --- (.+)$""")
wenzelm@64109
   341
    val End = new Regex("""^------------------- test (?:successful|FAILED) --- (.+) --- .*$""")
wenzelm@65684
   342
    val Isabelle_Version = List(new Regex("""^Isabelle version: (\S+)$"""))
wenzelm@64095
   343
  }
wenzelm@64095
   344
wenzelm@64109
   345
  object AFP_Test
wenzelm@64061
   346
  {
wenzelm@65588
   347
    val log_prefix = "afp-test-devel-"
wenzelm@64108
   348
    val engine = "afp-test"
wenzelm@64109
   349
    val Start = new Regex("""^Start test(?: for .+)? at ([^,]+), (.*)$""")
wenzelm@64109
   350
    val Start_Old = new Regex("""^Start test(?: for .+)? at ([^,]+)$""")
wenzelm@64109
   351
    val End = new Regex("""^End test on (.+), .+, elapsed time:.*$""")
wenzelm@65684
   352
    val Isabelle_Version = List(new Regex("""^Isabelle version: .* -- hg id (\S+)$"""))
wenzelm@65684
   353
    val AFP_Version = List(new Regex("""^AFP version: .* -- hg id (\S+)$"""))
wenzelm@64104
   354
    val Bad_Init = new Regex("""^cp:.*: Disc quota exceeded$""")
wenzelm@64061
   355
  }
wenzelm@64061
   356
wenzelm@64110
   357
  object Jenkins
wenzelm@64110
   358
  {
wenzelm@65664
   359
    val log_prefix = "jenkins_"
wenzelm@64110
   360
    val engine = "jenkins"
wenzelm@65663
   361
    val Host = new Regex("""^Building remotely on (\S+) \((\S+)\).*$""")
wenzelm@65665
   362
    val Start = new Regex("""^(?:Started by an SCM change|Started from command line by admin|).*$""")
wenzelm@64110
   363
    val Start_Date = new Regex("""^Build started at (.+)$""")
wenzelm@64110
   364
    val No_End = new Regex("""$.""")
wenzelm@65674
   365
    val Isabelle_Version =
wenzelm@65684
   366
      List(new Regex("""^(?:Build for Isabelle id|Isabelle id) (\w+).*$"""),
wenzelm@65685
   367
        new Regex("""^ISABELLE_CI_REPO_ID="(\w+)".*$"""),
wenzelm@65685
   368
        new Regex("""^(\w{12}) tip.*$"""))
wenzelm@65674
   369
    val AFP_Version =
wenzelm@65684
   370
      List(new Regex("""^(?:Build for AFP id|AFP id) (\w+).*$"""),
wenzelm@65684
   371
        new Regex("""^ISABELLE_CI_AFP_ID="(\w+)".*$"""))
wenzelm@64110
   372
    val CONFIGURATION = "=== CONFIGURATION ==="
wenzelm@64110
   373
    val BUILD = "=== BUILD ==="
wenzelm@64110
   374
  }
wenzelm@64110
   375
wenzelm@64105
   376
  private def parse_meta_info(log_file: Log_File): Meta_Info =
wenzelm@64061
   377
  {
wenzelm@64108
   378
    def parse(engine: String, host: String, start: Date,
wenzelm@65684
   379
      End: Regex, Isabelle_Version: List[Regex], AFP_Version: List[Regex]): Meta_Info =
wenzelm@64091
   380
    {
wenzelm@64296
   381
      val build_id =
wenzelm@64296
   382
      {
wenzelm@65714
   383
        val prefix = proper_string(host) orElse proper_string(engine) getOrElse "build"
wenzelm@65714
   384
        prefix + ":" + start.time.ms
wenzelm@64296
   385
      }
wenzelm@65591
   386
      val build_engine = if (engine == "") Nil else List(Prop.build_engine.name -> engine)
wenzelm@65591
   387
      val build_host = if (host == "") Nil else List(Prop.build_host.name -> host)
wenzelm@64108
   388
wenzelm@65599
   389
      val start_date = List(Prop.build_start.name -> print_date(start))
wenzelm@64091
   390
      val end_date =
wenzelm@64091
   391
        log_file.lines.last match {
wenzelm@64109
   392
          case End(log_file.Strict_Date(end_date)) =>
wenzelm@65599
   393
            List(Prop.build_end.name -> print_date(end_date))
wenzelm@64091
   394
          case _ => Nil
wenzelm@64091
   395
        }
wenzelm@64091
   396
wenzelm@64091
   397
      val isabelle_version =
wenzelm@65591
   398
        log_file.find_match(Isabelle_Version).map(Prop.isabelle_version.name -> _)
wenzelm@64091
   399
      val afp_version =
wenzelm@65591
   400
        log_file.find_match(AFP_Version).map(Prop.afp_version.name -> _)
wenzelm@64062
   401
wenzelm@65591
   402
      Meta_Info((Prop.build_id.name -> build_id) :: build_engine ::: build_host :::
wenzelm@64108
   403
          start_date ::: end_date ::: isabelle_version.toList ::: afp_version.toList,
wenzelm@65611
   404
        log_file.get_all_settings)
wenzelm@64091
   405
    }
wenzelm@64091
   406
wenzelm@64091
   407
    log_file.lines match {
wenzelm@64119
   408
      case line :: _ if line.startsWith(Build_History.META_INFO_MARKER) =>
wenzelm@64119
   409
        Meta_Info(log_file.find_props(Build_History.META_INFO_MARKER).get,
wenzelm@65611
   410
          log_file.get_all_settings)
wenzelm@64117
   411
wenzelm@65625
   412
      case Identify.Start(log_file.Strict_Date(start)) :: _ =>
wenzelm@65674
   413
        parse(Identify.engine(log_file), "", start, Identify.No_End,
wenzelm@65625
   414
          Identify.Isabelle_Version, Identify.AFP_Version)
wenzelm@65625
   415
wenzelm@64109
   416
      case Isatest.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   417
        parse(Isatest.engine, host, start, Isatest.End,
wenzelm@65684
   418
          Isatest.Isabelle_Version, Nil)
wenzelm@64099
   419
wenzelm@64109
   420
      case AFP_Test.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   421
        parse(AFP_Test.engine, host, start, AFP_Test.End,
wenzelm@64109
   422
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   423
wenzelm@64109
   424
      case AFP_Test.Start_Old(log_file.Strict_Date(start)) :: _ =>
wenzelm@64109
   425
        parse(AFP_Test.engine, "", start, AFP_Test.End,
wenzelm@64109
   426
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   427
wenzelm@65665
   428
      case Jenkins.Start() :: _ =>
wenzelm@64110
   429
        log_file.lines.dropWhile(_ != Jenkins.BUILD) match {
wenzelm@64110
   430
          case Jenkins.BUILD :: _ :: Jenkins.Start_Date(log_file.Strict_Date(start)) :: _ =>
wenzelm@65663
   431
            val host =
wenzelm@65663
   432
              log_file.lines.takeWhile(_ != Jenkins.CONFIGURATION).collectFirst({
wenzelm@65663
   433
                case Jenkins.Host(a, b) => a + "." + b
wenzelm@65663
   434
              }).getOrElse("")
wenzelm@65663
   435
            parse(Jenkins.engine, host, start.to(ZoneId.of("Europe/Berlin")), Jenkins.No_End,
wenzelm@64110
   436
              Jenkins.Isabelle_Version, Jenkins.AFP_Version)
wenzelm@64110
   437
          case _ => Meta_Info.empty
wenzelm@64110
   438
        }
wenzelm@64110
   439
wenzelm@64341
   440
      case line :: _ if line.startsWith("\u0000") => Meta_Info.empty
wenzelm@64109
   441
      case List(Isatest.End(_)) => Meta_Info.empty
wenzelm@64109
   442
      case _ :: AFP_Test.Bad_Init() :: _ => Meta_Info.empty
wenzelm@64105
   443
      case Nil => Meta_Info.empty
wenzelm@64104
   444
wenzelm@64110
   445
      case _ => log_file.err("cannot detect log file format")
wenzelm@64061
   446
    }
wenzelm@64061
   447
  }
wenzelm@64061
   448
wenzelm@64085
   449
wenzelm@64098
   450
wenzelm@65276
   451
  /** build info: toplevel output of isabelle build or Admin/build_history **/
wenzelm@64119
   452
wenzelm@66873
   453
  val THEORY_TIMING_MARKER = "\ftheory_timing = "
wenzelm@64119
   454
  val ML_STATISTICS_MARKER = "\fML_statistics = "
wenzelm@65937
   455
  val ERROR_MESSAGE_MARKER = "\ferror_message = "
wenzelm@64119
   456
  val SESSION_NAME = "session_name"
wenzelm@64085
   457
wenzelm@64061
   458
  object Session_Status extends Enumeration
wenzelm@64061
   459
  {
wenzelm@65633
   460
    val existing, finished, failed, cancelled = Value
wenzelm@64061
   461
  }
wenzelm@64061
   462
wenzelm@64085
   463
  sealed case class Session_Entry(
wenzelm@65643
   464
    chapter: String = "",
wenzelm@65643
   465
    groups: List[String] = Nil,
wenzelm@65643
   466
    threads: Option[Int] = None,
wenzelm@65643
   467
    timing: Timing = Timing.zero,
wenzelm@65643
   468
    ml_timing: Timing = Timing.zero,
wenzelm@66913
   469
    sources: Option[String] = None,
wenzelm@65643
   470
    heap_size: Option[Long] = None,
wenzelm@65643
   471
    status: Option[Session_Status.Value] = None,
wenzelm@65937
   472
    errors: List[String] = Nil,
wenzelm@66874
   473
    theory_timings: Map[String, Timing] = Map.empty,
wenzelm@65643
   474
    ml_statistics: List[Properties.T] = Nil)
wenzelm@64085
   475
  {
wenzelm@65631
   476
    def proper_groups: Option[String] = if (groups.isEmpty) None else Some(cat_lines(groups))
wenzelm@65643
   477
    def finished: Boolean = status == Some(Session_Status.finished)
wenzelm@65937
   478
    def failed: Boolean = status == Some(Session_Status.failed)
wenzelm@64085
   479
  }
wenzelm@64054
   480
wenzelm@66874
   481
  object Build_Info
wenzelm@66874
   482
  {
wenzelm@66874
   483
    val sessions_dummy: Map[String, Session_Entry] =
wenzelm@66874
   484
      Map("" -> Session_Entry(theory_timings = Map("" -> Timing.zero)))
wenzelm@66874
   485
  }
wenzelm@66874
   486
wenzelm@64085
   487
  sealed case class Build_Info(sessions: Map[String, Session_Entry])
wenzelm@64054
   488
  {
wenzelm@65937
   489
    def finished_sessions: List[String] = for ((a, b) <- sessions.toList if b.finished) yield a
wenzelm@65937
   490
    def failed_sessions: List[String] = for ((a, b) <- sessions.toList if b.failed) yield a
wenzelm@64054
   491
  }
wenzelm@64054
   492
wenzelm@65646
   493
  private def parse_build_info(log_file: Log_File, parse_ml_statistics: Boolean): Build_Info =
wenzelm@64085
   494
  {
wenzelm@64085
   495
    object Chapter_Name
wenzelm@64085
   496
    {
wenzelm@64085
   497
      def unapply(s: String): Some[(String, String)] =
wenzelm@64085
   498
        space_explode('/', s) match {
wenzelm@64085
   499
          case List(chapter, name) => Some((chapter, name))
wenzelm@64085
   500
          case _ => Some(("", s))
wenzelm@64085
   501
        }
wenzelm@64085
   502
    }
wenzelm@64054
   503
wenzelm@64085
   504
    val Session_No_Groups = new Regex("""^Session (\S+)$""")
wenzelm@64085
   505
    val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
wenzelm@64085
   506
    val Session_Finished1 =
wenzelm@64085
   507
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64085
   508
    val Session_Finished2 =
wenzelm@64085
   509
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64085
   510
    val Session_Timing =
wenzelm@65679
   511
      new Regex("""^Timing (\S+) \((\d+) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64086
   512
    val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
wenzelm@64085
   513
    val Session_Failed = new Regex("""^(\S+) FAILED""")
wenzelm@64085
   514
    val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
wenzelm@66913
   515
    val Sources = new Regex("""^Sources (\S+) (\S{""" + SHA1.digest_length + """})$""")
wenzelm@64120
   516
    val Heap = new Regex("""^Heap (\S+) \((\d+) bytes\)$""")
wenzelm@64085
   517
wenzelm@66874
   518
    object Theory_Timing
wenzelm@66874
   519
    {
wenzelm@66874
   520
      def unapply(line: String): Option[(String, (String, Timing))] =
wenzelm@67007
   521
      {
wenzelm@67007
   522
        val line1 = line.replace('~', '-')
wenzelm@67007
   523
        Library.try_unprefix(THEORY_TIMING_MARKER, line1).map(log_file.parse_props(_)) match {
wenzelm@66874
   524
          case Some((SESSION_NAME, name) :: props) =>
wenzelm@66874
   525
            (props, props) match {
wenzelm@66874
   526
              case (Markup.Name(thy), Markup.Timing_Properties(t)) => Some((name, thy -> t))
wenzelm@66874
   527
              case _ => None
wenzelm@66874
   528
            }
wenzelm@66874
   529
          case _ => None
wenzelm@66874
   530
        }
wenzelm@67007
   531
      }
wenzelm@66874
   532
    }
wenzelm@66874
   533
wenzelm@64085
   534
    var chapter = Map.empty[String, String]
wenzelm@64085
   535
    var groups = Map.empty[String, List[String]]
wenzelm@64085
   536
    var threads = Map.empty[String, Int]
wenzelm@64054
   537
    var timing = Map.empty[String, Timing]
wenzelm@64085
   538
    var ml_timing = Map.empty[String, Timing]
wenzelm@64086
   539
    var started = Set.empty[String]
wenzelm@64085
   540
    var failed = Set.empty[String]
wenzelm@64085
   541
    var cancelled = Set.empty[String]
wenzelm@66913
   542
    var sources = Map.empty[String, String]
wenzelm@65627
   543
    var heap_sizes = Map.empty[String, Long]
wenzelm@66874
   544
    var theory_timings = Map.empty[String, Map[String, Timing]]
wenzelm@64119
   545
    var ml_statistics = Map.empty[String, List[Properties.T]]
wenzelm@65937
   546
    var errors = Map.empty[String, List[String]]
wenzelm@64119
   547
wenzelm@64085
   548
    def all_sessions: Set[String] =
wenzelm@64120
   549
      chapter.keySet ++ groups.keySet ++ threads.keySet ++ timing.keySet ++ ml_timing.keySet ++
wenzelm@66913
   550
      failed ++ cancelled ++ started ++ sources.keySet ++ heap_sizes.keySet ++
wenzelm@66913
   551
      theory_timings.keySet ++ ml_statistics.keySet
wenzelm@64085
   552
wenzelm@64054
   553
wenzelm@64062
   554
    for (line <- log_file.lines) {
wenzelm@64054
   555
      line match {
wenzelm@64085
   556
        case Session_No_Groups(Chapter_Name(chapt, name)) =>
wenzelm@64085
   557
          chapter += (name -> chapt)
wenzelm@64085
   558
          groups += (name -> Nil)
wenzelm@64119
   559
wenzelm@64085
   560
        case Session_Groups(Chapter_Name(chapt, name), grps) =>
wenzelm@64085
   561
          chapter += (name -> chapt)
wenzelm@64085
   562
          groups += (name -> Word.explode(grps))
wenzelm@64119
   563
wenzelm@64086
   564
        case Session_Started(name) =>
wenzelm@64086
   565
          started += name
wenzelm@64119
   566
wenzelm@64054
   567
        case Session_Finished1(name,
wenzelm@64054
   568
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   569
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   570
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   571
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64085
   572
          timing += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64119
   573
wenzelm@64054
   574
        case Session_Finished2(name,
wenzelm@64054
   575
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   576
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64085
   577
          timing += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64119
   578
wenzelm@64054
   579
        case Session_Timing(name,
wenzelm@64054
   580
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   581
          val elapsed = Time.seconds(e)
wenzelm@64054
   582
          val cpu = Time.seconds(c)
wenzelm@64054
   583
          val gc = Time.seconds(g)
wenzelm@64085
   584
          ml_timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   585
          threads += (name -> t)
wenzelm@64119
   586
wenzelm@66913
   587
        case Sources(name, s) =>
wenzelm@66913
   588
          sources += (name -> s)
wenzelm@66913
   589
wenzelm@64120
   590
        case Heap(name, Value.Long(size)) =>
wenzelm@64120
   591
          heap_sizes += (name -> size)
wenzelm@64120
   592
wenzelm@66874
   593
        case _ if line.startsWith(THEORY_TIMING_MARKER) && YXML.detect(line) =>
wenzelm@66874
   594
          line match {
wenzelm@66874
   595
            case Theory_Timing(name, theory_timing) =>
wenzelm@66874
   596
              theory_timings += (name -> (theory_timings.getOrElse(name, Map.empty) + theory_timing))
wenzelm@66874
   597
            case _ => log_file.err("malformed theory_timing " + quote(line))
wenzelm@66874
   598
          }
wenzelm@66874
   599
wenzelm@65937
   600
        case _ if parse_ml_statistics && line.startsWith(ML_STATISTICS_MARKER) && YXML.detect(line) =>
wenzelm@66874
   601
          Library.try_unprefix(ML_STATISTICS_MARKER, line).map(log_file.parse_props(_)) match {
wenzelm@66874
   602
            case Some((SESSION_NAME, name) :: props) =>
wenzelm@66874
   603
              ml_statistics += (name -> (props :: ml_statistics.getOrElse(name, Nil)))
wenzelm@66874
   604
            case _ => log_file.err("malformed ML_statistics " + quote(line))
wenzelm@66874
   605
          }
wenzelm@64119
   606
wenzelm@65937
   607
        case _ if line.startsWith(ERROR_MESSAGE_MARKER) && YXML.detect(line) =>
wenzelm@66874
   608
          Library.try_unprefix(ERROR_MESSAGE_MARKER, line).map(log_file.parse_props(_)) match {
wenzelm@66874
   609
            case Some(List((SESSION_NAME, name), (Markup.CONTENT, msg))) =>
wenzelm@66874
   610
              errors += (name -> (Library.decode_lines(msg) :: errors.getOrElse(name, Nil)))
wenzelm@66874
   611
            case _ => log_file.err("malformed error message " + quote(line))
wenzelm@66874
   612
          }
wenzelm@65937
   613
wenzelm@64054
   614
        case _ =>
wenzelm@64054
   615
      }
wenzelm@64054
   616
    }
wenzelm@64054
   617
wenzelm@64085
   618
    val sessions =
wenzelm@64085
   619
      Map(
wenzelm@64085
   620
        (for (name <- all_sessions.toList) yield {
wenzelm@64085
   621
          val status =
wenzelm@65633
   622
            if (failed(name)) Session_Status.failed
wenzelm@65633
   623
            else if (cancelled(name)) Session_Status.cancelled
wenzelm@64086
   624
            else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
wenzelm@65633
   625
              Session_Status.finished
wenzelm@65633
   626
            else if (started(name)) Session_Status.failed
wenzelm@65633
   627
            else Session_Status.existing
wenzelm@64085
   628
          val entry =
wenzelm@64085
   629
            Session_Entry(
wenzelm@65643
   630
              chapter = chapter.getOrElse(name, ""),
wenzelm@65643
   631
              groups = groups.getOrElse(name, Nil),
wenzelm@65643
   632
              threads = threads.get(name),
wenzelm@65643
   633
              timing = timing.getOrElse(name, Timing.zero),
wenzelm@65643
   634
              ml_timing = ml_timing.getOrElse(name, Timing.zero),
wenzelm@66913
   635
              sources = sources.get(name),
wenzelm@65643
   636
              heap_size = heap_sizes.get(name),
wenzelm@65643
   637
              status = Some(status),
wenzelm@65937
   638
              errors = errors.getOrElse(name, Nil).reverse,
wenzelm@66874
   639
              theory_timings = theory_timings.getOrElse(name, Map.empty),
wenzelm@65643
   640
              ml_statistics = ml_statistics.getOrElse(name, Nil).reverse)
wenzelm@64085
   641
          (name -> entry)
wenzelm@64085
   642
        }):_*)
wenzelm@64085
   643
    Build_Info(sessions)
wenzelm@64054
   644
  }
wenzelm@64099
   645
wenzelm@64099
   646
wenzelm@64099
   647
wenzelm@65276
   648
  /** session info: produced by isabelle build as session log.gz file **/
wenzelm@64099
   649
wenzelm@64099
   650
  sealed case class Session_Info(
wenzelm@64099
   651
    session_timing: Properties.T,
wenzelm@64099
   652
    command_timings: List[Properties.T],
wenzelm@66873
   653
    theory_timings: List[Properties.T],
wenzelm@64099
   654
    ml_statistics: List[Properties.T],
wenzelm@65934
   655
    task_statistics: List[Properties.T],
wenzelm@65934
   656
    errors: List[String])
wenzelm@66944
   657
  {
wenzelm@66944
   658
    def error(s: String): Session_Info =
wenzelm@66944
   659
      copy(errors = errors ::: List(s))
wenzelm@66944
   660
  }
wenzelm@64099
   661
wenzelm@64099
   662
  private def parse_session_info(
wenzelm@64099
   663
    log_file: Log_File,
wenzelm@64099
   664
    command_timings: Boolean,
wenzelm@66873
   665
    theory_timings: Boolean,
wenzelm@64099
   666
    ml_statistics: Boolean,
wenzelm@64099
   667
    task_statistics: Boolean): Session_Info =
wenzelm@64099
   668
  {
wenzelm@65290
   669
    Session_Info(
wenzelm@65290
   670
      session_timing = log_file.find_props("\fTiming = ") getOrElse Nil,
wenzelm@65290
   671
      command_timings = if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil,
wenzelm@66873
   672
      theory_timings = if (theory_timings) log_file.filter_props(THEORY_TIMING_MARKER) else Nil,
wenzelm@65290
   673
      ml_statistics = if (ml_statistics) log_file.filter_props(ML_STATISTICS_MARKER) else Nil,
wenzelm@65934
   674
      task_statistics = if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil,
wenzelm@65937
   675
      errors = log_file.filter_lines(ERROR_MESSAGE_MARKER).map(Library.decode_lines(_)))
wenzelm@64099
   676
  }
wenzelm@65595
   677
wenzelm@68018
   678
  def compress_errors(errors: List[String], cache: XZ.Cache = XZ.cache()): Option[Bytes] =
wenzelm@65937
   679
    if (errors.isEmpty) None
wenzelm@68018
   680
    else {
wenzelm@68018
   681
      Some(Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.string)(errors))).
wenzelm@68018
   682
        compress(cache = cache))
wenzelm@68018
   683
    }
wenzelm@65937
   684
wenzelm@68018
   685
  def uncompress_errors(bytes: Bytes, cache: XZ.Cache = XZ.cache()): List[String] =
wenzelm@65937
   686
    if (bytes.isEmpty) Nil
wenzelm@68018
   687
    else {
wenzelm@68018
   688
      XML.Decode.list(YXML.string_of_body(_))(YXML.parse_body(bytes.uncompress(cache = cache).text))
wenzelm@68018
   689
    }
wenzelm@65937
   690
wenzelm@65595
   691
wenzelm@65595
   692
wenzelm@65595
   693
  /** persistent store **/
wenzelm@65595
   694
wenzelm@65694
   695
  /* SQL data model */
wenzelm@65694
   696
wenzelm@65694
   697
  object Data
wenzelm@65694
   698
  {
wenzelm@65702
   699
    def build_log_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table =
wenzelm@65700
   700
      SQL.Table("isabelle_build_log_" + name, columns, body)
wenzelm@65700
   701
wenzelm@65700
   702
wenzelm@65694
   703
    /* main content */
wenzelm@65694
   704
wenzelm@66857
   705
    val log_name = SQL.Column.string("log_name").make_primary_key
wenzelm@66857
   706
    val session_name = SQL.Column.string("session_name").make_primary_key
wenzelm@66874
   707
    val theory_name = SQL.Column.string("theory_name").make_primary_key
wenzelm@65694
   708
    val chapter = SQL.Column.string("chapter")
wenzelm@65694
   709
    val groups = SQL.Column.string("groups")
wenzelm@65694
   710
    val threads = SQL.Column.int("threads")
wenzelm@65694
   711
    val timing_elapsed = SQL.Column.long("timing_elapsed")
wenzelm@65694
   712
    val timing_cpu = SQL.Column.long("timing_cpu")
wenzelm@65694
   713
    val timing_gc = SQL.Column.long("timing_gc")
wenzelm@65694
   714
    val timing_factor = SQL.Column.double("timing_factor")
wenzelm@65694
   715
    val ml_timing_elapsed = SQL.Column.long("ml_timing_elapsed")
wenzelm@65694
   716
    val ml_timing_cpu = SQL.Column.long("ml_timing_cpu")
wenzelm@65694
   717
    val ml_timing_gc = SQL.Column.long("ml_timing_gc")
wenzelm@65694
   718
    val ml_timing_factor = SQL.Column.double("ml_timing_factor")
wenzelm@66874
   719
    val theory_timing_elapsed = SQL.Column.long("theory_timing_elapsed")
wenzelm@66874
   720
    val theory_timing_cpu = SQL.Column.long("theory_timing_cpu")
wenzelm@66874
   721
    val theory_timing_gc = SQL.Column.long("theory_timing_gc")
wenzelm@65694
   722
    val heap_size = SQL.Column.long("heap_size")
wenzelm@65694
   723
    val status = SQL.Column.string("status")
wenzelm@65937
   724
    val errors = SQL.Column.bytes("errors")
wenzelm@66913
   725
    val sources = SQL.Column.string("sources")
wenzelm@65694
   726
    val ml_statistics = SQL.Column.bytes("ml_statistics")
wenzelm@65783
   727
    val known = SQL.Column.bool("known")
wenzelm@65694
   728
wenzelm@65694
   729
    val meta_info_table =
wenzelm@65702
   730
      build_log_table("meta_info", log_name :: Prop.all_props ::: Settings.all_settings)
wenzelm@65694
   731
wenzelm@65694
   732
    val sessions_table =
wenzelm@65702
   733
      build_log_table("sessions",
wenzelm@65694
   734
        List(log_name, session_name, chapter, groups, threads, timing_elapsed, timing_cpu,
wenzelm@65694
   735
          timing_gc, timing_factor, ml_timing_elapsed, ml_timing_cpu, ml_timing_gc, ml_timing_factor,
wenzelm@66913
   736
          heap_size, status, errors, sources))
wenzelm@65694
   737
wenzelm@66874
   738
    val theories_table =
wenzelm@66874
   739
      build_log_table("theories",
wenzelm@66874
   740
        List(log_name, session_name, theory_name, theory_timing_elapsed, theory_timing_cpu,
wenzelm@66874
   741
          theory_timing_gc))
wenzelm@66874
   742
wenzelm@65694
   743
    val ml_statistics_table =
wenzelm@65702
   744
      build_log_table("ml_statistics", List(log_name, session_name, ml_statistics))
wenzelm@65694
   745
wenzelm@65694
   746
wenzelm@66855
   747
    /* AFP versions */
wenzelm@66855
   748
wenzelm@66855
   749
    val isabelle_afp_versions_table: SQL.Table =
wenzelm@66855
   750
    {
wenzelm@66855
   751
      val version1 = Prop.isabelle_version
wenzelm@66855
   752
      val version2 = Prop.afp_version
wenzelm@66857
   753
      build_log_table("isabelle_afp_versions", List(version1.make_primary_key, version2),
wenzelm@66855
   754
        SQL.select(List(version1, version2), distinct = true) + meta_info_table +
wenzelm@66856
   755
        " WHERE " + version1.defined + " AND " + version2.defined)
wenzelm@66855
   756
    }
wenzelm@66855
   757
wenzelm@66855
   758
wenzelm@65705
   759
    /* earliest pull date for repository version (PostgreSQL queries) */
wenzelm@65694
   760
wenzelm@66880
   761
    def pull_date(afp: Boolean = false) =
wenzelm@66880
   762
      if (afp) SQL.Column.date("afp_pull_date")
wenzelm@66880
   763
      else SQL.Column.date("pull_date")
wenzelm@65694
   764
wenzelm@66863
   765
    def pull_date_table(afp: Boolean = false): SQL.Table =
wenzelm@65709
   766
    {
wenzelm@66863
   767
      val (name, versions) =
wenzelm@66863
   768
        if (afp) ("afp_pull_date", List(Prop.isabelle_version, Prop.afp_version))
wenzelm@66863
   769
        else ("pull_date", List(Prop.isabelle_version))
wenzelm@65694
   770
wenzelm@66880
   771
      build_log_table(name, versions.map(_.make_primary_key) ::: List(pull_date(afp)),
wenzelm@66880
   772
        "SELECT " + versions.mkString(", ") +
wenzelm@66880
   773
          ", min(" + Prop.build_start + ") AS " + pull_date(afp) +
wenzelm@66863
   774
        " FROM " + meta_info_table +
wenzelm@66863
   775
        " WHERE " + (versions ::: List(Prop.build_start)).map(_.defined).mkString(" AND ") +
wenzelm@66863
   776
        " GROUP BY " + versions.mkString(", "))
wenzelm@66855
   777
    }
wenzelm@66855
   778
wenzelm@66855
   779
wenzelm@66855
   780
    /* recent entries */
wenzelm@66855
   781
wenzelm@65736
   782
    def recent_time(days: Int): SQL.Source =
wenzelm@65736
   783
      "now() - INTERVAL '" + days.max(0) + " days'"
wenzelm@65736
   784
wenzelm@66863
   785
    def recent_pull_date_table(
wenzelm@66863
   786
      days: Int, rev: String = "", afp_rev: Option[String] = None): SQL.Table =
wenzelm@65702
   787
    {
wenzelm@66863
   788
      val afp = afp_rev.isDefined
wenzelm@66863
   789
      val rev2 = afp_rev.getOrElse("")
wenzelm@66863
   790
      val table = pull_date_table(afp)
wenzelm@66863
   791
wenzelm@66863
   792
      val version1 = Prop.isabelle_version
wenzelm@66863
   793
      val version2 = Prop.afp_version
wenzelm@66863
   794
      val eq1 = version1(table) + " = " + SQL.string(rev)
wenzelm@66863
   795
      val eq2 = version2(table) + " = " + SQL.string(rev2)
wenzelm@66863
   796
wenzelm@65777
   797
      SQL.Table("recent_pull_date", table.columns,
wenzelm@65783
   798
        table.select(table.columns,
wenzelm@66880
   799
          "WHERE " + pull_date(afp)(table) + " > " + recent_time(days) +
wenzelm@66863
   800
          (if (rev != "" && rev2 == "") " OR " + eq1
wenzelm@66863
   801
           else if (rev == "" && rev2 != "") " OR " + eq2
wenzelm@66863
   802
           else if (rev != "" && rev2 != "") " OR (" + eq1 + " AND " + eq2 + ")"
wenzelm@66863
   803
           else "")))
wenzelm@65702
   804
    }
wenzelm@65694
   805
wenzelm@65781
   806
    def select_recent_log_names(days: Int): SQL.Source =
wenzelm@65781
   807
    {
wenzelm@65781
   808
      val table1 = meta_info_table
wenzelm@65781
   809
      val table2 = recent_pull_date_table(days)
wenzelm@65781
   810
      table1.select(List(log_name), distinct = true) + SQL.join_inner + table2.query_named +
wenzelm@65781
   811
        " ON " + Prop.isabelle_version(table1) + " = " + Prop.isabelle_version(table2)
wenzelm@65781
   812
    }
wenzelm@65781
   813
wenzelm@66863
   814
    def select_recent_versions(days: Int,
wenzelm@66863
   815
      rev: String = "", afp_rev: Option[String] = None, sql: SQL.Source = ""): SQL.Source =
wenzelm@65783
   816
    {
wenzelm@66880
   817
      val afp = afp_rev.isDefined
wenzelm@66858
   818
      val version = Prop.isabelle_version
wenzelm@66863
   819
      val table1 = recent_pull_date_table(days, rev = rev, afp_rev = afp_rev)
wenzelm@65783
   820
      val table2 = meta_info_table
wenzelm@65783
   821
      val aux_table = SQL.Table("aux", table2.columns, table2.select(sql = sql))
wenzelm@65783
   822
wenzelm@65783
   823
      val columns =
wenzelm@66858
   824
        table1.columns.map(c => c(table1)) :::
wenzelm@66858
   825
          List(known.copy(expr = log_name(aux_table).defined))
wenzelm@65783
   826
      SQL.select(columns, distinct = true) +
wenzelm@65783
   827
        table1.query_named + SQL.join_outer + aux_table.query_named +
wenzelm@66858
   828
        " ON " + version(table1) + " = " + version(aux_table) +
wenzelm@66880
   829
        " ORDER BY " + pull_date(afp)(table1) + " DESC"
wenzelm@65783
   830
    }
wenzelm@65783
   831
wenzelm@65724
   832
wenzelm@65724
   833
    /* universal view on main data */
wenzelm@65724
   834
wenzelm@65724
   835
    val universal_table: SQL.Table =
wenzelm@65724
   836
    {
wenzelm@66880
   837
      val afp_pull_date = pull_date(afp = true)
wenzelm@66880
   838
      val version1 = Prop.isabelle_version
wenzelm@66880
   839
      val version2 = Prop.afp_version
wenzelm@65724
   840
      val table1 = meta_info_table
wenzelm@66880
   841
      val table2 = pull_date_table(afp = true)
wenzelm@66880
   842
      val table3 = pull_date_table()
wenzelm@65724
   843
wenzelm@66880
   844
      val a_columns = log_name :: afp_pull_date :: table1.columns.tail
wenzelm@65850
   845
      val a_table =
wenzelm@65850
   846
        SQL.Table("a", a_columns,
wenzelm@66880
   847
          SQL.select(List(log_name, afp_pull_date) ::: table1.columns.tail.map(_.apply(table1))) +
wenzelm@66880
   848
          table1 + SQL.join_outer + table2 +
wenzelm@66880
   849
          " ON " + version1(table1) + " = " + version1(table2) +
wenzelm@66880
   850
          " AND " + version2(table1) + " = " + version2(table2))
wenzelm@65724
   851
wenzelm@66880
   852
      val b_columns = log_name :: pull_date() :: a_columns.tail
wenzelm@65850
   853
      val b_table =
wenzelm@65850
   854
        SQL.Table("b", b_columns,
wenzelm@66880
   855
          SQL.select(
wenzelm@66880
   856
            List(log_name(a_table), pull_date()(table3)) ::: a_columns.tail.map(_.apply(a_table))) +
wenzelm@66880
   857
          a_table.query_named + SQL.join_outer + table3 +
wenzelm@66880
   858
          " ON " + version1(a_table) + " = " + version1(table3))
wenzelm@65850
   859
wenzelm@66880
   860
      val c_columns = b_columns ::: sessions_table.columns.tail
wenzelm@66880
   861
      val c_table =
wenzelm@66880
   862
        SQL.Table("c", c_columns,
wenzelm@66880
   863
          SQL.select(log_name(b_table) :: c_columns.tail) +
wenzelm@66880
   864
          b_table.query_named + SQL.join_inner + sessions_table +
wenzelm@66880
   865
          " ON " + log_name(b_table) + " = " + log_name(sessions_table))
wenzelm@66880
   866
wenzelm@66880
   867
      SQL.Table("isabelle_build_log", c_columns ::: List(ml_statistics),
wenzelm@65724
   868
        {
wenzelm@66880
   869
          SQL.select(c_columns.map(_.apply(c_table)) ::: List(ml_statistics)) +
wenzelm@66880
   870
          c_table.query_named + SQL.join_outer + ml_statistics_table +
wenzelm@66880
   871
          " ON " + log_name(c_table) + " = " + log_name(ml_statistics_table) +
wenzelm@66880
   872
          " AND " + session_name(c_table) + " = " + session_name(ml_statistics_table)
wenzelm@65724
   873
        })
wenzelm@65724
   874
    }
wenzelm@65694
   875
  }
wenzelm@65694
   876
wenzelm@65694
   877
wenzelm@65694
   878
  /* database access */
wenzelm@65694
   879
wenzelm@65595
   880
  def store(options: Options): Store = new Store(options)
wenzelm@65595
   881
wenzelm@65857
   882
  class Store private[Build_Log](options: Options)
wenzelm@65595
   883
  {
wenzelm@68169
   884
    val xml_cache: XML.Cache = XML.make_cache()
wenzelm@68018
   885
    val xz_cache: XZ.Cache = XZ.make_cache()
wenzelm@65857
   886
wenzelm@65595
   887
    def open_database(
wenzelm@65595
   888
      user: String = options.string("build_log_database_user"),
wenzelm@65595
   889
      password: String = options.string("build_log_database_password"),
wenzelm@65595
   890
      database: String = options.string("build_log_database_name"),
wenzelm@65595
   891
      host: String = options.string("build_log_database_host"),
wenzelm@65595
   892
      port: Int = options.int("build_log_database_port"),
wenzelm@65595
   893
      ssh_host: String = options.string("build_log_ssh_host"),
wenzelm@65595
   894
      ssh_user: String = options.string("build_log_ssh_user"),
wenzelm@65595
   895
      ssh_port: Int = options.int("build_log_ssh_port")): PostgreSQL.Database =
wenzelm@65595
   896
    {
wenzelm@65595
   897
      PostgreSQL.open_database(
wenzelm@65595
   898
        user = user, password = password, database = database, host = host, port = port,
wenzelm@65595
   899
        ssh =
wenzelm@65595
   900
          if (ssh_host == "") None
wenzelm@67067
   901
          else Some(SSH.open_session(options, host = ssh_host, user = ssh_user, port = port)),
wenzelm@65636
   902
        ssh_close = true)
wenzelm@65595
   903
    }
wenzelm@65599
   904
wenzelm@65723
   905
    def update_database(db: PostgreSQL.Database, dirs: List[Path], ml_statistics: Boolean = false)
wenzelm@65694
   906
    {
wenzelm@65694
   907
      write_info(db, Log_File.find_files(dirs), ml_statistics = ml_statistics)
wenzelm@65694
   908
wenzelm@66863
   909
      db.create_view(Data.pull_date_table())
wenzelm@66863
   910
      db.create_view(Data.pull_date_table(afp = true))
wenzelm@65724
   911
      db.create_view(Data.universal_table)
wenzelm@65694
   912
    }
wenzelm@65694
   913
wenzelm@65723
   914
    def snapshot_database(db: PostgreSQL.Database, sqlite_database: Path,
wenzelm@65705
   915
      days: Int = 100, ml_statistics: Boolean = false)
wenzelm@65694
   916
    {
wenzelm@65694
   917
      Isabelle_System.mkdirs(sqlite_database.dir)
wenzelm@65694
   918
      sqlite_database.file.delete
wenzelm@65694
   919
wenzelm@65694
   920
      using(SQLite.open_database(sqlite_database))(db2 =>
wenzelm@65694
   921
      {
wenzelm@65694
   922
        db.transaction {
wenzelm@65694
   923
          db2.transaction {
wenzelm@65705
   924
            // main content
wenzelm@65705
   925
            db2.create_table(Data.meta_info_table)
wenzelm@65705
   926
            db2.create_table(Data.sessions_table)
wenzelm@66874
   927
            db2.create_table(Data.theories_table)
wenzelm@65705
   928
            db2.create_table(Data.ml_statistics_table)
wenzelm@65705
   929
wenzelm@65705
   930
            val recent_log_names =
wenzelm@65781
   931
              db.using_statement(Data.select_recent_log_names(days))(stmt =>
wenzelm@65779
   932
                stmt.execute_query().iterator(_.string(Data.log_name)).toList)
wenzelm@65705
   933
wenzelm@65705
   934
            for (log_name <- recent_log_names) {
wenzelm@65705
   935
              read_meta_info(db, log_name).foreach(meta_info =>
wenzelm@65705
   936
                update_meta_info(db2, log_name, meta_info))
wenzelm@65705
   937
wenzelm@65705
   938
              update_sessions(db2, log_name, read_build_info(db, log_name))
wenzelm@65705
   939
wenzelm@65856
   940
              if (ml_statistics) {
wenzelm@65856
   941
                update_ml_statistics(db2, log_name,
wenzelm@65856
   942
                  read_build_info(db, log_name, ml_statistics = true))
wenzelm@65856
   943
              }
wenzelm@65705
   944
            }
wenzelm@65705
   945
wenzelm@65705
   946
            // pull_date
wenzelm@66880
   947
            for (afp <- List(false, true))
wenzelm@65694
   948
            {
wenzelm@66880
   949
              val afp_rev = if (afp) Some("") else None
wenzelm@66880
   950
              val table = Data.pull_date_table(afp)
wenzelm@65694
   951
              db2.create_table(table)
wenzelm@65698
   952
              db2.using_statement(table.insert())(stmt2 =>
wenzelm@65694
   953
              {
wenzelm@66880
   954
                db.using_statement(
wenzelm@66880
   955
                  Data.recent_pull_date_table(days, afp_rev = afp_rev).query)(stmt =>
wenzelm@65694
   956
                {
wenzelm@65740
   957
                  val res = stmt.execute_query()
wenzelm@65740
   958
                  while (res.next()) {
wenzelm@65748
   959
                    for ((c, i) <- table.columns.zipWithIndex) {
wenzelm@65748
   960
                      stmt2.string(i + 1) = res.get_string(c)
wenzelm@65748
   961
                    }
wenzelm@65740
   962
                    stmt2.execute()
wenzelm@65694
   963
                  }
wenzelm@65694
   964
                })
wenzelm@65694
   965
              })
wenzelm@65709
   966
            }
wenzelm@65705
   967
wenzelm@65705
   968
            // full view
wenzelm@65724
   969
            db2.create_view(Data.universal_table)
wenzelm@65694
   970
          }
wenzelm@65694
   971
        }
wenzelm@65708
   972
        db2.rebuild
wenzelm@65694
   973
      })
wenzelm@65694
   974
    }
wenzelm@65694
   975
wenzelm@65688
   976
    def domain(db: SQL.Database, table: SQL.Table, column: SQL.Column): Set[String] =
wenzelm@65698
   977
      db.using_statement(table.select(List(column), distinct = true))(stmt =>
wenzelm@65740
   978
        stmt.execute_query().iterator(_.string(column)).toSet)
wenzelm@65688
   979
wenzelm@65705
   980
    def update_meta_info(db: SQL.Database, log_name: String, meta_info: Meta_Info)
wenzelm@65632
   981
    {
wenzelm@65694
   982
      val table = Data.meta_info_table
wenzelm@65703
   983
      db.using_statement(db.insert_permissive(table))(stmt =>
wenzelm@65703
   984
      {
wenzelm@65748
   985
        stmt.string(1) = log_name
wenzelm@65703
   986
        for ((c, i) <- table.columns.tail.zipWithIndex) {
wenzelm@65703
   987
          if (c.T == SQL.Type.Date)
wenzelm@65748
   988
            stmt.date(i + 2) = meta_info.get_date(c)
wenzelm@65703
   989
          else
wenzelm@65748
   990
            stmt.string(i + 2) = meta_info.get(c)
wenzelm@65703
   991
        }
wenzelm@65703
   992
        stmt.execute()
wenzelm@65703
   993
      })
wenzelm@65600
   994
    }
wenzelm@65600
   995
wenzelm@65705
   996
    def update_sessions(db: SQL.Database, log_name: String, build_info: Build_Info)
wenzelm@65599
   997
    {
wenzelm@65694
   998
      val table = Data.sessions_table
wenzelm@65703
   999
      db.using_statement(db.insert_permissive(table))(stmt =>
wenzelm@65703
  1000
      {
wenzelm@66874
  1001
        val sessions =
wenzelm@66874
  1002
          if (build_info.sessions.isEmpty) Build_Info.sessions_dummy
wenzelm@66874
  1003
          else build_info.sessions
wenzelm@66874
  1004
        for ((session_name, session) <- sessions) {
wenzelm@65748
  1005
          stmt.string(1) = log_name
wenzelm@65748
  1006
          stmt.string(2) = session_name
wenzelm@65853
  1007
          stmt.string(3) = proper_string(session.chapter)
wenzelm@65748
  1008
          stmt.string(4) = session.proper_groups
wenzelm@65748
  1009
          stmt.int(5) = session.threads
wenzelm@65748
  1010
          stmt.long(6) = session.timing.elapsed.proper_ms
wenzelm@65748
  1011
          stmt.long(7) = session.timing.cpu.proper_ms
wenzelm@65748
  1012
          stmt.long(8) = session.timing.gc.proper_ms
wenzelm@65748
  1013
          stmt.double(9) = session.timing.factor
wenzelm@65748
  1014
          stmt.long(10) = session.ml_timing.elapsed.proper_ms
wenzelm@65748
  1015
          stmt.long(11) = session.ml_timing.cpu.proper_ms
wenzelm@65748
  1016
          stmt.long(12) = session.ml_timing.gc.proper_ms
wenzelm@65748
  1017
          stmt.double(13) = session.ml_timing.factor
wenzelm@65748
  1018
          stmt.long(14) = session.heap_size
wenzelm@65748
  1019
          stmt.string(15) = session.status.map(_.toString)
wenzelm@68018
  1020
          stmt.bytes(16) = compress_errors(session.errors, cache = xz_cache)
wenzelm@66913
  1021
          stmt.string(17) = session.sources
wenzelm@65703
  1022
          stmt.execute()
wenzelm@65703
  1023
        }
wenzelm@65703
  1024
      })
wenzelm@65642
  1025
    }
wenzelm@65642
  1026
wenzelm@66874
  1027
    def update_theories(db: SQL.Database, log_name: String, build_info: Build_Info)
wenzelm@66874
  1028
    {
wenzelm@66874
  1029
      val table = Data.theories_table
wenzelm@66874
  1030
      db.using_statement(db.insert_permissive(table))(stmt =>
wenzelm@66874
  1031
      {
wenzelm@66874
  1032
        val sessions =
wenzelm@66874
  1033
          if (build_info.sessions.forall({ case (_, session) => session.theory_timings.isEmpty }))
wenzelm@66874
  1034
            Build_Info.sessions_dummy
wenzelm@66874
  1035
          else build_info.sessions
wenzelm@66874
  1036
        for {
wenzelm@66874
  1037
          (session_name, session) <- sessions
wenzelm@66874
  1038
          (theory_name, timing) <- session.theory_timings
wenzelm@66874
  1039
        } {
wenzelm@66874
  1040
          stmt.string(1) = log_name
wenzelm@66874
  1041
          stmt.string(2) = session_name
wenzelm@66874
  1042
          stmt.string(3) = theory_name
wenzelm@66874
  1043
          stmt.long(4) = timing.elapsed.ms
wenzelm@66874
  1044
          stmt.long(5) = timing.cpu.ms
wenzelm@66874
  1045
          stmt.long(6) = timing.gc.ms
wenzelm@66874
  1046
          stmt.execute()
wenzelm@66874
  1047
        }
wenzelm@66874
  1048
      })
wenzelm@66874
  1049
    }
wenzelm@66874
  1050
wenzelm@65705
  1051
    def update_ml_statistics(db: SQL.Database, log_name: String, build_info: Build_Info)
wenzelm@65645
  1052
    {
wenzelm@65694
  1053
      val table = Data.ml_statistics_table
wenzelm@65703
  1054
      db.using_statement(db.insert_permissive(table))(stmt =>
wenzelm@65703
  1055
      {
wenzelm@65703
  1056
        val ml_stats: List[(String, Option[Bytes])] =
wenzelm@65703
  1057
          Par_List.map[(String, Session_Entry), (String, Option[Bytes])](
wenzelm@68018
  1058
            { case (a, b) => (a, Properties.compress(b.ml_statistics, cache = xz_cache).proper) },
wenzelm@65703
  1059
            build_info.sessions.iterator.filter(p => p._2.ml_statistics.nonEmpty).toList)
wenzelm@65703
  1060
        val entries = if (ml_stats.nonEmpty) ml_stats else List("" -> None)
wenzelm@65703
  1061
        for ((session_name, ml_statistics) <- entries) {
wenzelm@65748
  1062
          stmt.string(1) = log_name
wenzelm@65748
  1063
          stmt.string(2) = session_name
wenzelm@65748
  1064
          stmt.bytes(3) = ml_statistics
wenzelm@65703
  1065
          stmt.execute()
wenzelm@65703
  1066
        }
wenzelm@65703
  1067
      })
wenzelm@65645
  1068
    }
wenzelm@65645
  1069
wenzelm@65645
  1070
    def write_info(db: SQL.Database, files: List[JFile], ml_statistics: Boolean = false)
wenzelm@65642
  1071
    {
wenzelm@65705
  1072
      abstract class Table_Status(table: SQL.Table)
wenzelm@65642
  1073
      {
wenzelm@65688
  1074
        db.create_table(table)
wenzelm@65694
  1075
        private var known: Set[String] = domain(db, table, Data.log_name)
wenzelm@65688
  1076
wenzelm@65642
  1077
        def required(file: JFile): Boolean = !known(Log_File.plain_name(file.getName))
wenzelm@65705
  1078
wenzelm@65705
  1079
        def update_db(db: SQL.Database, log_file: Log_File): Unit
wenzelm@65642
  1080
        def update(log_file: Log_File)
wenzelm@65642
  1081
        {
wenzelm@65642
  1082
          if (!known(log_file.name)) {
wenzelm@65642
  1083
            update_db(db, log_file)
wenzelm@65642
  1084
            known += log_file.name
wenzelm@65618
  1085
          }
wenzelm@65614
  1086
        }
wenzelm@65605
  1087
      }
wenzelm@65642
  1088
      val status =
wenzelm@65642
  1089
        List(
wenzelm@65705
  1090
          new Table_Status(Data.meta_info_table) {
wenzelm@65705
  1091
            override def update_db(db: SQL.Database, log_file: Log_File): Unit =
wenzelm@65705
  1092
              update_meta_info(db, log_file.name, log_file.parse_meta_info())
wenzelm@65705
  1093
          },
wenzelm@65705
  1094
          new Table_Status(Data.sessions_table) {
wenzelm@65705
  1095
            override def update_db(db: SQL.Database, log_file: Log_File): Unit =
wenzelm@65705
  1096
              update_sessions(db, log_file.name, log_file.parse_build_info())
wenzelm@65705
  1097
          },
wenzelm@66874
  1098
          new Table_Status(Data.theories_table) {
wenzelm@66874
  1099
            override def update_db(db: SQL.Database, log_file: Log_File): Unit =
wenzelm@66874
  1100
              update_theories(db, log_file.name, log_file.parse_build_info())
wenzelm@66874
  1101
          },
wenzelm@65705
  1102
          new Table_Status(Data.ml_statistics_table) {
wenzelm@65705
  1103
            override def update_db(db: SQL.Database, log_file: Log_File): Unit =
wenzelm@65705
  1104
            if (ml_statistics) {
wenzelm@65705
  1105
              update_ml_statistics(db, log_file.name,
wenzelm@65705
  1106
                log_file.parse_build_info(ml_statistics = true))
wenzelm@65705
  1107
            }
wenzelm@65705
  1108
          })
wenzelm@65642
  1109
wenzelm@67743
  1110
      for (file_group <-
wenzelm@67743
  1111
            files.filter(file => status.exists(_.required(file))).
wenzelm@67743
  1112
              grouped(options.int("build_log_transaction_size") max 1))
wenzelm@67743
  1113
      {
wenzelm@65642
  1114
        val log_files = Par_List.map[JFile, Log_File](Log_File.apply _, file_group)
wenzelm@65642
  1115
        db.transaction { log_files.foreach(log_file => status.foreach(_.update(log_file))) }
wenzelm@65642
  1116
      }
wenzelm@65605
  1117
    }
wenzelm@65605
  1118
wenzelm@65621
  1119
    def read_meta_info(db: SQL.Database, log_name: String): Option[Meta_Info] =
wenzelm@65621
  1120
    {
wenzelm@65694
  1121
      val table = Data.meta_info_table
wenzelm@65642
  1122
      val columns = table.columns.tail
wenzelm@65699
  1123
      db.using_statement(table.select(columns, Data.log_name.where_equal(log_name)))(stmt =>
wenzelm@65621
  1124
      {
wenzelm@65740
  1125
        val res = stmt.execute_query()
wenzelm@65740
  1126
        if (!res.next) None
wenzelm@65621
  1127
        else {
wenzelm@65621
  1128
          val results =
wenzelm@65642
  1129
            columns.map(c => c.name ->
wenzelm@65621
  1130
              (if (c.T == SQL.Type.Date)
wenzelm@65740
  1131
                res.get_date(c).map(Log_File.Date_Format(_))
wenzelm@65621
  1132
               else
wenzelm@65740
  1133
                res.get_string(c)))
wenzelm@65621
  1134
          val n = Prop.all_props.length
wenzelm@65621
  1135
          val props = for ((x, Some(y)) <- results.take(n)) yield (x, y)
wenzelm@65621
  1136
          val settings = for ((x, Some(y)) <- results.drop(n)) yield (x, y)
wenzelm@65621
  1137
          Some(Meta_Info(props, settings))
wenzelm@65621
  1138
        }
wenzelm@65621
  1139
      })
wenzelm@65621
  1140
    }
wenzelm@65621
  1141
wenzelm@65621
  1142
    def read_build_info(
wenzelm@65629
  1143
      db: SQL.Database,
wenzelm@65629
  1144
      log_name: String,
wenzelm@65629
  1145
      session_names: List[String] = Nil,
wenzelm@65629
  1146
      ml_statistics: Boolean = false): Build_Info =
wenzelm@65621
  1147
    {
wenzelm@65694
  1148
      val table1 = Data.sessions_table
wenzelm@65694
  1149
      val table2 = Data.ml_statistics_table
wenzelm@65629
  1150
wenzelm@65645
  1151
      val where_log_name =
wenzelm@65699
  1152
        Data.log_name(table1).where_equal(log_name) + " AND " +
wenzelm@65701
  1153
        Data.session_name(table1) + " <> ''"
wenzelm@65621
  1154
      val where =
wenzelm@65645
  1155
        if (session_names.isEmpty) where_log_name
wenzelm@65804
  1156
        else where_log_name + " AND " + SQL.member(Data.session_name(table1).ident, session_names)
wenzelm@65629
  1157
wenzelm@65645
  1158
      val columns1 = table1.columns.tail.map(_.apply(table1))
wenzelm@65645
  1159
      val (columns, from) =
wenzelm@65645
  1160
        if (ml_statistics) {
wenzelm@65694
  1161
          val columns = columns1 ::: List(Data.ml_statistics(table2))
wenzelm@65668
  1162
          val join =
wenzelm@65775
  1163
            table1 + SQL.join_outer + table2 + " ON " +
wenzelm@65738
  1164
            Data.log_name(table1) + " = " + Data.log_name(table2) + " AND " +
wenzelm@65738
  1165
            Data.session_name(table1) + " = " + Data.session_name(table2)
wenzelm@65668
  1166
          (columns, SQL.enclose(join))
wenzelm@65645
  1167
        }
wenzelm@65695
  1168
        else (columns1, table1.ident)
wenzelm@65645
  1169
wenzelm@65621
  1170
      val sessions =
wenzelm@65698
  1171
        db.using_statement(SQL.select(columns) + from + " " + where)(stmt =>
wenzelm@65621
  1172
        {
wenzelm@65740
  1173
          stmt.execute_query().iterator(res =>
wenzelm@65621
  1174
          {
wenzelm@65740
  1175
            val session_name = res.string(Data.session_name)
wenzelm@65626
  1176
            val session_entry =
wenzelm@65626
  1177
              Session_Entry(
wenzelm@65740
  1178
                chapter = res.string(Data.chapter),
wenzelm@65740
  1179
                groups = split_lines(res.string(Data.groups)),
wenzelm@65740
  1180
                threads = res.get_int(Data.threads),
wenzelm@65741
  1181
                timing = res.timing(Data.timing_elapsed, Data.timing_cpu, Data.timing_gc),
wenzelm@65626
  1182
                ml_timing =
wenzelm@65741
  1183
                  res.timing(Data.ml_timing_elapsed, Data.ml_timing_cpu, Data.ml_timing_gc),
wenzelm@66913
  1184
                sources = res.get_string(Data.sources),
wenzelm@65740
  1185
                heap_size = res.get_long(Data.heap_size),
wenzelm@65740
  1186
                status = res.get_string(Data.status).map(Session_Status.withName(_)),
wenzelm@68018
  1187
                errors = uncompress_errors(res.bytes(Data.errors), cache = xz_cache),
wenzelm@65629
  1188
                ml_statistics =
wenzelm@68018
  1189
                  if (ml_statistics) {
wenzelm@68018
  1190
                    Properties.uncompress(
wenzelm@68018
  1191
                      res.bytes(Data.ml_statistics), cache = xz_cache, Some(xml_cache))
wenzelm@68018
  1192
                  }
wenzelm@65629
  1193
                  else Nil)
wenzelm@65626
  1194
            session_name -> session_entry
wenzelm@65621
  1195
          }).toMap
wenzelm@65621
  1196
        })
wenzelm@65621
  1197
      Build_Info(sessions)
wenzelm@65621
  1198
    }
wenzelm@65595
  1199
  }
wenzelm@64045
  1200
}