src/Pure/Admin/build_log.scala
author wenzelm
Mon May 01 22:45:39 2017 +0200 (2017-05-01)
changeset 65674 23897f5d885d
parent 65670 490649872acc
child 65679 45632d594bdb
permissions -rw-r--r--
approximate repository identify job based on isabelle-nightly-slow;
more Jenkins changeset patterns found in the wild;
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@65599
    14
import java.sql.PreparedStatement
wenzelm@64061
    15
wenzelm@65600
    16
import scala.collection.immutable.SortedMap
wenzelm@64054
    17
import scala.collection.mutable
wenzelm@64054
    18
import scala.util.matching.Regex
wenzelm@64054
    19
wenzelm@64054
    20
wenzelm@64045
    21
object Build_Log
wenzelm@64045
    22
{
wenzelm@64298
    23
  /** content **/
wenzelm@64101
    24
wenzelm@64298
    25
  /* properties */
wenzelm@64150
    26
wenzelm@64298
    27
  object Prop
wenzelm@64298
    28
  {
wenzelm@65624
    29
    val build_tags = SQL.Column.string("build_tags")  // lines
wenzelm@65624
    30
    val build_args = SQL.Column.string("build_args")  // lines
wenzelm@65591
    31
    val build_group_id = SQL.Column.string("build_group_id")
wenzelm@65591
    32
    val build_id = SQL.Column.string("build_id")
wenzelm@65591
    33
    val build_engine = SQL.Column.string("build_engine")
wenzelm@65591
    34
    val build_host = SQL.Column.string("build_host")
wenzelm@65591
    35
    val build_start = SQL.Column.date("build_start")
wenzelm@65591
    36
    val build_end = SQL.Column.date("build_end")
wenzelm@65591
    37
    val isabelle_version = SQL.Column.string("isabelle_version")
wenzelm@65591
    38
    val afp_version = SQL.Column.string("afp_version")
wenzelm@65591
    39
wenzelm@65611
    40
    val all_props: List[SQL.Column] =
wenzelm@65591
    41
      List(build_tags, build_args, build_group_id, build_id, build_engine,
wenzelm@65591
    42
        build_host, build_start, build_end, isabelle_version, afp_version)
wenzelm@64298
    43
  }
wenzelm@64150
    44
wenzelm@64150
    45
wenzelm@64298
    46
  /* settings */
wenzelm@64080
    47
wenzelm@64081
    48
  object Settings
wenzelm@64081
    49
  {
wenzelm@65611
    50
    val ISABELLE_BUILD_OPTIONS = SQL.Column.string("ISABELLE_BUILD_OPTIONS")
wenzelm@65611
    51
    val ML_PLATFORM = SQL.Column.string("ML_PLATFORM")
wenzelm@65611
    52
    val ML_HOME = SQL.Column.string("ML_HOME")
wenzelm@65611
    53
    val ML_SYSTEM = SQL.Column.string("ML_SYSTEM")
wenzelm@65611
    54
    val ML_OPTIONS = SQL.Column.string("ML_OPTIONS")
wenzelm@65611
    55
wenzelm@65611
    56
    val ml_settings = List(ML_PLATFORM, ML_HOME, ML_SYSTEM, ML_OPTIONS)
wenzelm@65611
    57
    val all_settings = ISABELLE_BUILD_OPTIONS :: ml_settings
wenzelm@64081
    58
wenzelm@64081
    59
    type Entry = (String, String)
wenzelm@64081
    60
    type T = List[Entry]
wenzelm@64080
    61
wenzelm@64081
    62
    object Entry
wenzelm@64081
    63
    {
wenzelm@64081
    64
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    65
        s.indexOf('=') match {
wenzelm@64081
    66
          case -1 => None
wenzelm@64081
    67
          case i =>
wenzelm@64081
    68
            val a = s.substring(0, i)
wenzelm@64081
    69
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    70
            Some((a, b))
wenzelm@64081
    71
        }
wenzelm@64081
    72
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    73
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    74
    }
wenzelm@64080
    75
wenzelm@64081
    76
    def show(): String =
wenzelm@64081
    77
      cat_lines(
wenzelm@65611
    78
        List(Entry.getenv(ISABELLE_BUILD_OPTIONS.name), "") :::
wenzelm@65611
    79
        ml_settings.map(c => Entry.getenv(c.name)))
wenzelm@64080
    80
  }
wenzelm@64080
    81
wenzelm@64080
    82
wenzelm@64298
    83
  /* file names */
wenzelm@64298
    84
wenzelm@64298
    85
  def log_date(date: Date): String =
wenzelm@64298
    86
    String.format(Locale.ROOT, "%s.%05d",
wenzelm@64298
    87
      DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date.rep),
wenzelm@64298
    88
      new java.lang.Long((date.time - date.midnight.time).ms / 1000))
wenzelm@64298
    89
wenzelm@64298
    90
  def log_subdir(date: Date): Path =
wenzelm@64298
    91
    Path.explode("log") + Path.explode(date.rep.getYear.toString)
wenzelm@64298
    92
wenzelm@64298
    93
  def log_filename(engine: String, date: Date, more: List[String] = Nil): Path =
wenzelm@64298
    94
    Path.explode((engine :: log_date(date) :: more).mkString("", "_", ".log"))
wenzelm@64298
    95
wenzelm@64298
    96
wenzelm@64100
    97
wenzelm@64062
    98
  /** log file **/
wenzelm@64045
    99
wenzelm@64155
   100
  def print_date(date: Date): String = Log_File.Date_Format(date)
wenzelm@64155
   101
wenzelm@64062
   102
  object Log_File
wenzelm@64062
   103
  {
wenzelm@65607
   104
    /* log file */
wenzelm@65607
   105
wenzelm@65609
   106
    def plain_name(name: String): String =
wenzelm@65609
   107
    {
wenzelm@65609
   108
      List(".log", ".log.gz", ".log.xz", ".gz", ".xz").find(name.endsWith(_)) match {
wenzelm@65609
   109
        case Some(s) => Library.try_unsuffix(s, name).get
wenzelm@65609
   110
        case None => name
wenzelm@65609
   111
      }
wenzelm@65609
   112
    }
wenzelm@65609
   113
wenzelm@64062
   114
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@65609
   115
      new Log_File(plain_name(name), lines)
wenzelm@64062
   116
wenzelm@64062
   117
    def apply(name: String, text: String): Log_File =
wenzelm@64063
   118
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64090
   119
wenzelm@64090
   120
    def apply(file: JFile): Log_File =
wenzelm@64090
   121
    {
wenzelm@64090
   122
      val name = file.getName
wenzelm@65609
   123
      val text =
wenzelm@65609
   124
        if (name.endsWith(".gz")) File.read_gzip(file)
wenzelm@65609
   125
        else if (name.endsWith(".xz")) File.read_xz(file)
wenzelm@65609
   126
        else File.read(file)
wenzelm@65609
   127
      apply(name, text)
wenzelm@64090
   128
    }
wenzelm@64090
   129
wenzelm@64090
   130
    def apply(path: Path): Log_File = apply(path.file)
wenzelm@64101
   131
wenzelm@64110
   132
wenzelm@65607
   133
    /* log file collections */
wenzelm@65607
   134
wenzelm@65607
   135
    def is_log(file: JFile,
wenzelm@65607
   136
      prefixes: List[String] =
wenzelm@65664
   137
        List(Build_History.log_prefix, Identify.log_prefix, Isatest.log_prefix,
wenzelm@65664
   138
          AFP_Test.log_prefix, Jenkins.log_prefix),
wenzelm@65609
   139
      suffixes: List[String] = List(".log", ".log.gz", ".log.xz")): Boolean =
wenzelm@65607
   140
    {
wenzelm@65607
   141
      val name = file.getName
wenzelm@65639
   142
wenzelm@65607
   143
      prefixes.exists(name.startsWith(_)) &&
wenzelm@65639
   144
      suffixes.exists(name.endsWith(_)) &&
wenzelm@65639
   145
      name != "isatest.log" &&
wenzelm@65639
   146
      name != "afp-test.log" &&
wenzelm@65639
   147
      name != "main.log"
wenzelm@65607
   148
    }
wenzelm@65607
   149
wenzelm@65607
   150
    def find_files(dirs: Iterable[Path]): List[JFile] =
wenzelm@65607
   151
      dirs.iterator.flatMap(dir => File.find_files(dir.file, is_log(_))).toList
wenzelm@65607
   152
wenzelm@65607
   153
wenzelm@64110
   154
    /* date format */
wenzelm@64110
   155
wenzelm@64101
   156
    val Date_Format =
wenzelm@64101
   157
    {
wenzelm@64101
   158
      val fmts =
wenzelm@64101
   159
        Date.Formatter.variants(
wenzelm@64116
   160
          List("EEE MMM d HH:mm:ss O yyyy", "EEE MMM d HH:mm:ss VV yyyy"),
wenzelm@64104
   161
          List(Locale.ENGLISH, Locale.GERMAN)) :::
wenzelm@64110
   162
        List(
wenzelm@64110
   163
          DateTimeFormatter.RFC_1123_DATE_TIME,
wenzelm@64110
   164
          Date.Formatter.pattern("EEE MMM d HH:mm:ss yyyy").withZone(ZoneId.of("Europe/Berlin")))
wenzelm@64101
   165
wenzelm@64104
   166
      def tune_timezone(s: String): String =
wenzelm@64104
   167
        s match {
wenzelm@64101
   168
          case "CET" | "MET" => "GMT+1"
wenzelm@64101
   169
          case "CEST" | "MEST" => "GMT+2"
wenzelm@64104
   170
          case "EST" => "Europe/Berlin"
wenzelm@64104
   171
          case _ => s
wenzelm@64104
   172
        }
wenzelm@64104
   173
      def tune_weekday(s: String): String =
wenzelm@64104
   174
        s match {
wenzelm@64104
   175
          case "Die" => "Di"
wenzelm@64104
   176
          case "Mit" => "Mi"
wenzelm@64104
   177
          case "Don" => "Do"
wenzelm@64104
   178
          case "Fre" => "Fr"
wenzelm@64104
   179
          case "Sam" => "Sa"
wenzelm@64104
   180
          case "Son" => "So"
wenzelm@64104
   181
          case _ => s
wenzelm@64104
   182
        }
wenzelm@64104
   183
wenzelm@64104
   184
      def tune(s: String): String =
wenzelm@64104
   185
        Word.implode(
wenzelm@64104
   186
          Word.explode(s) match {
wenzelm@64104
   187
            case a :: "M\uFFFDr" :: bs => tune_weekday(a) :: "Mär" :: bs.map(tune_timezone(_))
wenzelm@64104
   188
            case a :: bs => tune_weekday(a) :: bs.map(tune_timezone(_))
wenzelm@64104
   189
            case Nil => Nil
wenzelm@64104
   190
          }
wenzelm@64104
   191
        )
wenzelm@64101
   192
wenzelm@64101
   193
      Date.Format.make(fmts, tune)
wenzelm@64101
   194
    }
wenzelm@64117
   195
wenzelm@64117
   196
wenzelm@64117
   197
    /* inlined content */
wenzelm@64117
   198
wenzelm@64119
   199
    def print_props(marker: String, props: Properties.T): String =
wenzelm@65624
   200
      marker + YXML.string_of_body(XML.Encode.properties(Properties.encode_lines(props)))
wenzelm@64102
   201
  }
wenzelm@64102
   202
wenzelm@64102
   203
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64102
   204
  {
wenzelm@64102
   205
    log_file =>
wenzelm@64102
   206
wenzelm@64102
   207
    override def toString: String = name
wenzelm@64102
   208
wenzelm@64102
   209
    def text: String = cat_lines(lines)
wenzelm@64102
   210
wenzelm@64102
   211
    def err(msg: String): Nothing =
wenzelm@64102
   212
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64102
   213
wenzelm@64102
   214
wenzelm@64102
   215
    /* date format */
wenzelm@64101
   216
wenzelm@64101
   217
    object Strict_Date
wenzelm@64101
   218
    {
wenzelm@64101
   219
      def unapply(s: String): Some[Date] =
wenzelm@64102
   220
        try { Some(Log_File.Date_Format.parse(s)) }
wenzelm@64101
   221
        catch { case exn: DateTimeParseException => log_file.err(exn.getMessage) }
wenzelm@64101
   222
    }
wenzelm@64101
   223
wenzelm@64101
   224
wenzelm@64062
   225
    /* inlined content */
wenzelm@64062
   226
wenzelm@64062
   227
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
   228
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
   229
wenzelm@64196
   230
    def find_line(marker: String): Option[String] =
wenzelm@64196
   231
      find(Library.try_unprefix(marker, _))
wenzelm@64196
   232
wenzelm@64062
   233
    def find_match(regex: Regex): Option[String] =
wenzelm@64062
   234
      lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@64062
   235
        map(res => res.get.head)
wenzelm@64062
   236
wenzelm@64062
   237
wenzelm@64062
   238
    /* settings */
wenzelm@64062
   239
wenzelm@64091
   240
    def get_setting(a: String): Option[Settings.Entry] =
wenzelm@64091
   241
      lines.find(_.startsWith(a + "=")) match {
wenzelm@64091
   242
        case Some(line) => Settings.Entry.unapply(line)
wenzelm@64091
   243
        case None => None
wenzelm@64091
   244
      }
wenzelm@64045
   245
wenzelm@65611
   246
    def get_all_settings: Settings.T =
wenzelm@65611
   247
      for { c <- Settings.all_settings; entry <- get_setting(c.name) }
wenzelm@65611
   248
      yield entry
wenzelm@64062
   249
wenzelm@64062
   250
wenzelm@64062
   251
    /* properties (YXML) */
wenzelm@64062
   252
wenzelm@64062
   253
    val xml_cache = new XML.Cache()
wenzelm@64062
   254
wenzelm@64062
   255
    def parse_props(text: String): Properties.T =
wenzelm@65624
   256
      xml_cache.props(Properties.decode_lines(XML.Decode.properties(YXML.parse_body(text))))
wenzelm@64062
   257
wenzelm@64119
   258
    def filter_props(marker: String): List[Properties.T] =
wenzelm@64196
   259
      for {
wenzelm@64196
   260
        line <- lines
wenzelm@64196
   261
        s <- Library.try_unprefix(marker, line)
wenzelm@64196
   262
        if YXML.detect(s)
wenzelm@64196
   263
      } yield parse_props(s)
wenzelm@64062
   264
wenzelm@64119
   265
    def find_props(marker: String): Option[Properties.T] =
wenzelm@64196
   266
      find_line(marker) match {
wenzelm@64196
   267
        case Some(text) if YXML.detect(text) => Some(parse_props(text))
wenzelm@64196
   268
        case _ => None
wenzelm@64196
   269
      }
wenzelm@64062
   270
wenzelm@64062
   271
wenzelm@64062
   272
    /* parse various formats */
wenzelm@64062
   273
wenzelm@64105
   274
    def parse_meta_info(): Meta_Info = Build_Log.parse_meta_info(log_file)
wenzelm@64105
   275
wenzelm@65646
   276
    def parse_build_info(ml_statistics: Boolean = false): Build_Info =
wenzelm@65646
   277
      Build_Log.parse_build_info(log_file, ml_statistics)
wenzelm@64105
   278
wenzelm@64082
   279
    def parse_session_info(
wenzelm@64082
   280
        command_timings: Boolean = false,
wenzelm@64082
   281
        ml_statistics: Boolean = false,
wenzelm@64082
   282
        task_statistics: Boolean = false): Session_Info =
wenzelm@65318
   283
      Build_Log.parse_session_info(log_file, command_timings, ml_statistics, task_statistics)
wenzelm@64045
   284
  }
wenzelm@64045
   285
wenzelm@64045
   286
wenzelm@64098
   287
wenzelm@65276
   288
  /** digested meta info: produced by Admin/build_history in log.xz file **/
wenzelm@64045
   289
wenzelm@64108
   290
  object Meta_Info
wenzelm@64099
   291
  {
wenzelm@64108
   292
    val empty: Meta_Info = Meta_Info(Nil, Nil)
wenzelm@65605
   293
wenzelm@65609
   294
    val log_name = SQL.Column.string("log_name", primary_key = true)
wenzelm@65605
   295
    val table =
wenzelm@65611
   296
      SQL.Table("isabelle_build_log_meta_info", log_name :: Prop.all_props ::: Settings.all_settings)
wenzelm@64099
   297
  }
wenzelm@64098
   298
wenzelm@65611
   299
  sealed case class Meta_Info(props: Properties.T, settings: Settings.T)
wenzelm@64103
   300
  {
wenzelm@64103
   301
    def is_empty: Boolean = props.isEmpty && settings.isEmpty
wenzelm@65599
   302
wenzelm@65611
   303
    def get(c: SQL.Column): Option[String] =
wenzelm@65611
   304
      Properties.get(props, c.name) orElse
wenzelm@65611
   305
      Properties.get(settings, c.name)
wenzelm@65611
   306
wenzelm@65611
   307
    def get_date(c: SQL.Column): Option[Date] =
wenzelm@65611
   308
      get(c).map(Log_File.Date_Format.parse(_))
wenzelm@64103
   309
  }
wenzelm@64061
   310
wenzelm@65625
   311
  object Identify
wenzelm@65625
   312
  {
wenzelm@65625
   313
    val log_prefix = "isabelle_identify_"
wenzelm@65674
   314
wenzelm@65674
   315
    def engine(log_file: Log_File): String =
wenzelm@65674
   316
      if (log_file.name.startsWith(Jenkins.log_prefix)) "jenkins_identify"
wenzelm@65674
   317
      else "identify"
wenzelm@65674
   318
wenzelm@65674
   319
    def content(date: Date, isabelle_version: Option[String], afp_version: Option[String]): String =
wenzelm@65674
   320
      terminate_lines(
wenzelm@65674
   321
        List("isabelle_identify: " + Build_Log.print_date(date), "") :::
wenzelm@65674
   322
        isabelle_version.map("Isabelle version: " + _).toList :::
wenzelm@65674
   323
        afp_version.map("AFP version: " + _).toList)
wenzelm@65674
   324
wenzelm@65625
   325
    val Start = new Regex("""^isabelle_identify: (.+)$""")
wenzelm@65625
   326
    val No_End = new Regex("""$.""")
wenzelm@65625
   327
    val Isabelle_Version = new Regex("""^Isabelle version: (\S+)$""")
wenzelm@65625
   328
    val AFP_Version = new Regex("""^AFP version: (\S+)$""")
wenzelm@65625
   329
  }
wenzelm@65625
   330
wenzelm@64095
   331
  object Isatest
wenzelm@64095
   332
  {
wenzelm@65588
   333
    val log_prefix = "isatest-makeall-"
wenzelm@64108
   334
    val engine = "isatest"
wenzelm@64109
   335
    val Start = new Regex("""^------------------- starting test --- (.+) --- (.+)$""")
wenzelm@64109
   336
    val End = new Regex("""^------------------- test (?:successful|FAILED) --- (.+) --- .*$""")
wenzelm@64095
   337
    val Isabelle_Version = new Regex("""^Isabelle version: (\S+)$""")
wenzelm@64095
   338
    val No_AFP_Version = new Regex("""$.""")
wenzelm@64095
   339
  }
wenzelm@64095
   340
wenzelm@64109
   341
  object AFP_Test
wenzelm@64061
   342
  {
wenzelm@65588
   343
    val log_prefix = "afp-test-devel-"
wenzelm@64108
   344
    val engine = "afp-test"
wenzelm@64109
   345
    val Start = new Regex("""^Start test(?: for .+)? at ([^,]+), (.*)$""")
wenzelm@64109
   346
    val Start_Old = new Regex("""^Start test(?: for .+)? at ([^,]+)$""")
wenzelm@64109
   347
    val End = new Regex("""^End test on (.+), .+, elapsed time:.*$""")
wenzelm@64087
   348
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\S+)$""")
wenzelm@64087
   349
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\S+)$""")
wenzelm@64104
   350
    val Bad_Init = new Regex("""^cp:.*: Disc quota exceeded$""")
wenzelm@64061
   351
  }
wenzelm@64061
   352
wenzelm@64110
   353
  object Jenkins
wenzelm@64110
   354
  {
wenzelm@65664
   355
    val log_prefix = "jenkins_"
wenzelm@64110
   356
    val engine = "jenkins"
wenzelm@65663
   357
    val Host = new Regex("""^Building remotely on (\S+) \((\S+)\).*$""")
wenzelm@65665
   358
    val Start = new Regex("""^(?:Started by an SCM change|Started from command line by admin|).*$""")
wenzelm@64110
   359
    val Start_Date = new Regex("""^Build started at (.+)$""")
wenzelm@64110
   360
    val No_End = new Regex("""$.""")
wenzelm@65674
   361
    val Isabelle_Version =
wenzelm@65674
   362
      new Regex("""^(?:Build for Isabelle id |Isabelle id |ISABELLE_CI_REPO_ID=")(\w+).*$""")
wenzelm@65674
   363
    val AFP_Version =
wenzelm@65674
   364
      new Regex("""^(?:Build for AFP id |AFP id |ISABELLE_CI_AFP_ID=")(\w+).*$""")
wenzelm@64110
   365
    val CONFIGURATION = "=== CONFIGURATION ==="
wenzelm@64110
   366
    val BUILD = "=== BUILD ==="
wenzelm@64110
   367
  }
wenzelm@64110
   368
wenzelm@64105
   369
  private def parse_meta_info(log_file: Log_File): Meta_Info =
wenzelm@64061
   370
  {
wenzelm@64108
   371
    def parse(engine: String, host: String, start: Date,
wenzelm@64109
   372
      End: Regex, Isabelle_Version: Regex, AFP_Version: Regex): Meta_Info =
wenzelm@64091
   373
    {
wenzelm@64296
   374
      val build_id =
wenzelm@64296
   375
      {
wenzelm@64296
   376
        val prefix = if (host != "") host else if (engine != "") engine else ""
wenzelm@64296
   377
        (if (prefix == "") "build" else prefix) + ":" + start.time.ms
wenzelm@64296
   378
      }
wenzelm@65591
   379
      val build_engine = if (engine == "") Nil else List(Prop.build_engine.name -> engine)
wenzelm@65591
   380
      val build_host = if (host == "") Nil else List(Prop.build_host.name -> host)
wenzelm@64108
   381
wenzelm@65599
   382
      val start_date = List(Prop.build_start.name -> print_date(start))
wenzelm@64091
   383
      val end_date =
wenzelm@64091
   384
        log_file.lines.last match {
wenzelm@64109
   385
          case End(log_file.Strict_Date(end_date)) =>
wenzelm@65599
   386
            List(Prop.build_end.name -> print_date(end_date))
wenzelm@64091
   387
          case _ => Nil
wenzelm@64091
   388
        }
wenzelm@64091
   389
wenzelm@64091
   390
      val isabelle_version =
wenzelm@65591
   391
        log_file.find_match(Isabelle_Version).map(Prop.isabelle_version.name -> _)
wenzelm@64091
   392
      val afp_version =
wenzelm@65591
   393
        log_file.find_match(AFP_Version).map(Prop.afp_version.name -> _)
wenzelm@64062
   394
wenzelm@65591
   395
      Meta_Info((Prop.build_id.name -> build_id) :: build_engine ::: build_host :::
wenzelm@64108
   396
          start_date ::: end_date ::: isabelle_version.toList ::: afp_version.toList,
wenzelm@65611
   397
        log_file.get_all_settings)
wenzelm@64091
   398
    }
wenzelm@64091
   399
wenzelm@64091
   400
    log_file.lines match {
wenzelm@64119
   401
      case line :: _ if line.startsWith(Build_History.META_INFO_MARKER) =>
wenzelm@64119
   402
        Meta_Info(log_file.find_props(Build_History.META_INFO_MARKER).get,
wenzelm@65611
   403
          log_file.get_all_settings)
wenzelm@64117
   404
wenzelm@65625
   405
      case Identify.Start(log_file.Strict_Date(start)) :: _ =>
wenzelm@65674
   406
        parse(Identify.engine(log_file), "", start, Identify.No_End,
wenzelm@65625
   407
          Identify.Isabelle_Version, Identify.AFP_Version)
wenzelm@65625
   408
wenzelm@64109
   409
      case Isatest.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   410
        parse(Isatest.engine, host, start, Isatest.End,
wenzelm@64108
   411
          Isatest.Isabelle_Version, Isatest.No_AFP_Version)
wenzelm@64099
   412
wenzelm@64109
   413
      case AFP_Test.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   414
        parse(AFP_Test.engine, host, start, AFP_Test.End,
wenzelm@64109
   415
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   416
wenzelm@64109
   417
      case AFP_Test.Start_Old(log_file.Strict_Date(start)) :: _ =>
wenzelm@64109
   418
        parse(AFP_Test.engine, "", start, AFP_Test.End,
wenzelm@64109
   419
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   420
wenzelm@65665
   421
      case Jenkins.Start() :: _ =>
wenzelm@64110
   422
        log_file.lines.dropWhile(_ != Jenkins.BUILD) match {
wenzelm@64110
   423
          case Jenkins.BUILD :: _ :: Jenkins.Start_Date(log_file.Strict_Date(start)) :: _ =>
wenzelm@65663
   424
            val host =
wenzelm@65663
   425
              log_file.lines.takeWhile(_ != Jenkins.CONFIGURATION).collectFirst({
wenzelm@65663
   426
                case Jenkins.Host(a, b) => a + "." + b
wenzelm@65663
   427
              }).getOrElse("")
wenzelm@65663
   428
            parse(Jenkins.engine, host, start.to(ZoneId.of("Europe/Berlin")), Jenkins.No_End,
wenzelm@64110
   429
              Jenkins.Isabelle_Version, Jenkins.AFP_Version)
wenzelm@64110
   430
          case _ => Meta_Info.empty
wenzelm@64110
   431
        }
wenzelm@64110
   432
wenzelm@64341
   433
      case line :: _ if line.startsWith("\u0000") => Meta_Info.empty
wenzelm@64109
   434
      case List(Isatest.End(_)) => Meta_Info.empty
wenzelm@64109
   435
      case _ :: AFP_Test.Bad_Init() :: _ => Meta_Info.empty
wenzelm@64105
   436
      case Nil => Meta_Info.empty
wenzelm@64104
   437
wenzelm@64110
   438
      case _ => log_file.err("cannot detect log file format")
wenzelm@64061
   439
    }
wenzelm@64061
   440
  }
wenzelm@64061
   441
wenzelm@64085
   442
wenzelm@64098
   443
wenzelm@65276
   444
  /** build info: toplevel output of isabelle build or Admin/build_history **/
wenzelm@64119
   445
wenzelm@64119
   446
  val ML_STATISTICS_MARKER = "\fML_statistics = "
wenzelm@64119
   447
  val SESSION_NAME = "session_name"
wenzelm@64085
   448
wenzelm@64061
   449
  object Session_Status extends Enumeration
wenzelm@64061
   450
  {
wenzelm@65633
   451
    val existing, finished, failed, cancelled = Value
wenzelm@64061
   452
  }
wenzelm@64061
   453
wenzelm@65643
   454
  object Session_Entry
wenzelm@65643
   455
  {
wenzelm@65643
   456
    val empty: Session_Entry = Session_Entry()
wenzelm@65643
   457
  }
wenzelm@65643
   458
wenzelm@64085
   459
  sealed case class Session_Entry(
wenzelm@65643
   460
    chapter: String = "",
wenzelm@65643
   461
    groups: List[String] = Nil,
wenzelm@65643
   462
    threads: Option[Int] = None,
wenzelm@65643
   463
    timing: Timing = Timing.zero,
wenzelm@65643
   464
    ml_timing: Timing = Timing.zero,
wenzelm@65643
   465
    heap_size: Option[Long] = None,
wenzelm@65643
   466
    status: Option[Session_Status.Value] = None,
wenzelm@65643
   467
    ml_statistics: List[Properties.T] = Nil)
wenzelm@64085
   468
  {
wenzelm@65631
   469
    def proper_chapter: Option[String] = if (chapter == "") None else Some(chapter)
wenzelm@65631
   470
    def proper_groups: Option[String] = if (groups.isEmpty) None else Some(cat_lines(groups))
wenzelm@65643
   471
    def finished: Boolean = status == Some(Session_Status.finished)
wenzelm@64085
   472
  }
wenzelm@64054
   473
wenzelm@65599
   474
  object Build_Info
wenzelm@65599
   475
  {
wenzelm@65613
   476
    val session_name = SQL.Column.string("session_name", primary_key = true)
wenzelm@65613
   477
    val chapter = SQL.Column.string("chapter")
wenzelm@65613
   478
    val groups = SQL.Column.string("groups")
wenzelm@65613
   479
    val threads = SQL.Column.int("threads")
wenzelm@65616
   480
    val timing_elapsed = SQL.Column.long("timing_elapsed")
wenzelm@65616
   481
    val timing_cpu = SQL.Column.long("timing_cpu")
wenzelm@65616
   482
    val timing_gc = SQL.Column.long("timing_gc")
wenzelm@65616
   483
    val ml_timing_elapsed = SQL.Column.long("ml_timing_elapsed")
wenzelm@65616
   484
    val ml_timing_cpu = SQL.Column.long("ml_timing_cpu")
wenzelm@65616
   485
    val ml_timing_gc = SQL.Column.long("ml_timing_gc")
wenzelm@65613
   486
    val heap_size = SQL.Column.long("heap_size")
wenzelm@65613
   487
    val status = SQL.Column.string("status")
wenzelm@65627
   488
    val ml_statistics = SQL.Column.bytes("ml_statistics")
wenzelm@65605
   489
wenzelm@65645
   490
    val sessions_table =
wenzelm@65645
   491
      SQL.Table("isabelle_build_log_sessions",
wenzelm@65645
   492
        List(Meta_Info.log_name, session_name, chapter, groups, threads, timing_elapsed, timing_cpu,
wenzelm@65645
   493
          timing_gc, ml_timing_elapsed, ml_timing_cpu, ml_timing_gc, heap_size, status))
wenzelm@65645
   494
    val ml_statistics_table =
wenzelm@65645
   495
      SQL.Table("isabelle_build_log_ml_statistics",
wenzelm@65645
   496
        List(Meta_Info.log_name, session_name, ml_statistics))
wenzelm@65599
   497
  }
wenzelm@65599
   498
wenzelm@64085
   499
  sealed case class Build_Info(sessions: Map[String, Session_Entry])
wenzelm@64054
   500
  {
wenzelm@64085
   501
    def session(name: String): Session_Entry = sessions(name)
wenzelm@64085
   502
    def get_session(name: String): Option[Session_Entry] = sessions.get(name)
wenzelm@64054
   503
wenzelm@64089
   504
    def get_default[A](name: String, f: Session_Entry => A, x: A): A =
wenzelm@64085
   505
      get_session(name) match {
wenzelm@64089
   506
        case Some(entry) => f(entry)
wenzelm@64089
   507
        case None => x
wenzelm@64085
   508
      }
wenzelm@64085
   509
wenzelm@64119
   510
    def finished_sessions: List[String] = sessions.keySet.iterator.filter(finished(_)).toList
wenzelm@64089
   511
    def finished(name: String): Boolean = get_default(name, _.finished, false)
wenzelm@64089
   512
    def timing(name: String): Timing = get_default(name, _.timing, Timing.zero)
wenzelm@64089
   513
    def ml_timing(name: String): Timing = get_default(name, _.ml_timing, Timing.zero)
wenzelm@65052
   514
    def ml_statistics(name: String): ML_Statistics =
wenzelm@65052
   515
      get_default(name, entry => ML_Statistics(name, entry.ml_statistics), ML_Statistics.empty)
wenzelm@64054
   516
  }
wenzelm@64054
   517
wenzelm@65646
   518
  private def parse_build_info(log_file: Log_File, parse_ml_statistics: Boolean): Build_Info =
wenzelm@64085
   519
  {
wenzelm@64085
   520
    object Chapter_Name
wenzelm@64085
   521
    {
wenzelm@64085
   522
      def unapply(s: String): Some[(String, String)] =
wenzelm@64085
   523
        space_explode('/', s) match {
wenzelm@64085
   524
          case List(chapter, name) => Some((chapter, name))
wenzelm@64085
   525
          case _ => Some(("", s))
wenzelm@64085
   526
        }
wenzelm@64085
   527
    }
wenzelm@64054
   528
wenzelm@64085
   529
    val Session_No_Groups = new Regex("""^Session (\S+)$""")
wenzelm@64085
   530
    val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
wenzelm@64085
   531
    val Session_Finished1 =
wenzelm@64085
   532
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64085
   533
    val Session_Finished2 =
wenzelm@64085
   534
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64085
   535
    val Session_Timing =
wenzelm@64085
   536
      new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64086
   537
    val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
wenzelm@64085
   538
    val Session_Failed = new Regex("""^(\S+) FAILED""")
wenzelm@64085
   539
    val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
wenzelm@64120
   540
    val Heap = new Regex("""^Heap (\S+) \((\d+) bytes\)$""")
wenzelm@64085
   541
wenzelm@64085
   542
    var chapter = Map.empty[String, String]
wenzelm@64085
   543
    var groups = Map.empty[String, List[String]]
wenzelm@64085
   544
    var threads = Map.empty[String, Int]
wenzelm@64054
   545
    var timing = Map.empty[String, Timing]
wenzelm@64085
   546
    var ml_timing = Map.empty[String, Timing]
wenzelm@64086
   547
    var started = Set.empty[String]
wenzelm@64085
   548
    var failed = Set.empty[String]
wenzelm@64085
   549
    var cancelled = Set.empty[String]
wenzelm@65627
   550
    var heap_sizes = Map.empty[String, Long]
wenzelm@64119
   551
    var ml_statistics = Map.empty[String, List[Properties.T]]
wenzelm@64119
   552
wenzelm@64085
   553
    def all_sessions: Set[String] =
wenzelm@64120
   554
      chapter.keySet ++ groups.keySet ++ threads.keySet ++ timing.keySet ++ ml_timing.keySet ++
wenzelm@65627
   555
      failed ++ cancelled ++ started ++ heap_sizes.keySet ++ ml_statistics.keySet
wenzelm@64085
   556
wenzelm@64054
   557
wenzelm@64062
   558
    for (line <- log_file.lines) {
wenzelm@64054
   559
      line match {
wenzelm@64085
   560
        case Session_No_Groups(Chapter_Name(chapt, name)) =>
wenzelm@64085
   561
          chapter += (name -> chapt)
wenzelm@64085
   562
          groups += (name -> Nil)
wenzelm@64119
   563
wenzelm@64085
   564
        case Session_Groups(Chapter_Name(chapt, name), grps) =>
wenzelm@64085
   565
          chapter += (name -> chapt)
wenzelm@64085
   566
          groups += (name -> Word.explode(grps))
wenzelm@64119
   567
wenzelm@64086
   568
        case Session_Started(name) =>
wenzelm@64086
   569
          started += name
wenzelm@64119
   570
wenzelm@64054
   571
        case Session_Finished1(name,
wenzelm@64054
   572
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   573
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   574
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   575
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64085
   576
          timing += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64119
   577
wenzelm@64054
   578
        case Session_Finished2(name,
wenzelm@64054
   579
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   580
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64085
   581
          timing += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64119
   582
wenzelm@64054
   583
        case Session_Timing(name,
wenzelm@64054
   584
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   585
          val elapsed = Time.seconds(e)
wenzelm@64054
   586
          val cpu = Time.seconds(c)
wenzelm@64054
   587
          val gc = Time.seconds(g)
wenzelm@64085
   588
          ml_timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   589
          threads += (name -> t)
wenzelm@64119
   590
wenzelm@64120
   591
        case Heap(name, Value.Long(size)) =>
wenzelm@64120
   592
          heap_sizes += (name -> size)
wenzelm@64120
   593
wenzelm@65646
   594
        case _
wenzelm@65646
   595
        if parse_ml_statistics && line.startsWith(ML_STATISTICS_MARKER) && YXML.detect(line) =>
wenzelm@64119
   596
          val (name, props) =
wenzelm@64119
   597
            Library.try_unprefix(ML_STATISTICS_MARKER, line).map(log_file.parse_props(_)) match {
wenzelm@64119
   598
              case Some((SESSION_NAME, session_name) :: props) => (session_name, props)
wenzelm@64119
   599
              case _ => log_file.err("malformed ML_statistics " + quote(line))
wenzelm@64119
   600
            }
wenzelm@64120
   601
          ml_statistics += (name -> (props :: ml_statistics.getOrElse(name, Nil)))
wenzelm@64119
   602
wenzelm@64054
   603
        case _ =>
wenzelm@64054
   604
      }
wenzelm@64054
   605
    }
wenzelm@64054
   606
wenzelm@64085
   607
    val sessions =
wenzelm@64085
   608
      Map(
wenzelm@64085
   609
        (for (name <- all_sessions.toList) yield {
wenzelm@64085
   610
          val status =
wenzelm@65633
   611
            if (failed(name)) Session_Status.failed
wenzelm@65633
   612
            else if (cancelled(name)) Session_Status.cancelled
wenzelm@64086
   613
            else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
wenzelm@65633
   614
              Session_Status.finished
wenzelm@65633
   615
            else if (started(name)) Session_Status.failed
wenzelm@65633
   616
            else Session_Status.existing
wenzelm@64085
   617
          val entry =
wenzelm@64085
   618
            Session_Entry(
wenzelm@65643
   619
              chapter = chapter.getOrElse(name, ""),
wenzelm@65643
   620
              groups = groups.getOrElse(name, Nil),
wenzelm@65643
   621
              threads = threads.get(name),
wenzelm@65643
   622
              timing = timing.getOrElse(name, Timing.zero),
wenzelm@65643
   623
              ml_timing = ml_timing.getOrElse(name, Timing.zero),
wenzelm@65643
   624
              heap_size = heap_sizes.get(name),
wenzelm@65643
   625
              status = Some(status),
wenzelm@65643
   626
              ml_statistics = ml_statistics.getOrElse(name, Nil).reverse)
wenzelm@64085
   627
          (name -> entry)
wenzelm@64085
   628
        }):_*)
wenzelm@64085
   629
    Build_Info(sessions)
wenzelm@64054
   630
  }
wenzelm@64099
   631
wenzelm@64099
   632
wenzelm@64099
   633
wenzelm@65276
   634
  /** session info: produced by isabelle build as session log.gz file **/
wenzelm@64099
   635
wenzelm@64099
   636
  sealed case class Session_Info(
wenzelm@64099
   637
    session_timing: Properties.T,
wenzelm@64099
   638
    command_timings: List[Properties.T],
wenzelm@64099
   639
    ml_statistics: List[Properties.T],
wenzelm@64099
   640
    task_statistics: List[Properties.T])
wenzelm@64099
   641
wenzelm@64099
   642
  private def parse_session_info(
wenzelm@64099
   643
    log_file: Log_File,
wenzelm@64099
   644
    command_timings: Boolean,
wenzelm@64099
   645
    ml_statistics: Boolean,
wenzelm@64099
   646
    task_statistics: Boolean): Session_Info =
wenzelm@64099
   647
  {
wenzelm@65290
   648
    Session_Info(
wenzelm@65290
   649
      session_timing = log_file.find_props("\fTiming = ") getOrElse Nil,
wenzelm@65290
   650
      command_timings = if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil,
wenzelm@65290
   651
      ml_statistics = if (ml_statistics) log_file.filter_props(ML_STATISTICS_MARKER) else Nil,
wenzelm@65290
   652
      task_statistics = if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil)
wenzelm@64099
   653
  }
wenzelm@65595
   654
wenzelm@65595
   655
wenzelm@65595
   656
wenzelm@65595
   657
  /** persistent store **/
wenzelm@65595
   658
wenzelm@65595
   659
  def store(options: Options): Store = new Store(options)
wenzelm@65595
   660
wenzelm@65595
   661
  class Store private[Build_Log](options: Options) extends Properties.Store
wenzelm@65595
   662
  {
wenzelm@65595
   663
    def open_database(
wenzelm@65595
   664
      user: String = options.string("build_log_database_user"),
wenzelm@65595
   665
      password: String = options.string("build_log_database_password"),
wenzelm@65595
   666
      database: String = options.string("build_log_database_name"),
wenzelm@65595
   667
      host: String = options.string("build_log_database_host"),
wenzelm@65595
   668
      port: Int = options.int("build_log_database_port"),
wenzelm@65595
   669
      ssh_host: String = options.string("build_log_ssh_host"),
wenzelm@65595
   670
      ssh_user: String = options.string("build_log_ssh_user"),
wenzelm@65595
   671
      ssh_port: Int = options.int("build_log_ssh_port")): PostgreSQL.Database =
wenzelm@65595
   672
    {
wenzelm@65595
   673
      PostgreSQL.open_database(
wenzelm@65595
   674
        user = user, password = password, database = database, host = host, port = port,
wenzelm@65595
   675
        ssh =
wenzelm@65595
   676
          if (ssh_host == "") None
wenzelm@65636
   677
          else Some(SSH.init_context(options).open_session(ssh_host, ssh_user, port)),
wenzelm@65636
   678
        ssh_close = true)
wenzelm@65595
   679
    }
wenzelm@65599
   680
wenzelm@65642
   681
    def update_meta_info(db: SQL.Database, log_file: Log_File)
wenzelm@65632
   682
    {
wenzelm@65642
   683
      val meta_info = log_file.parse_meta_info()
wenzelm@65642
   684
      val table = Meta_Info.table
wenzelm@65614
   685
wenzelm@65642
   686
      db.transaction {
wenzelm@65642
   687
        using(db.delete(table, Meta_Info.log_name.sql_where_equal(log_file.name)))(_.execute)
wenzelm@65642
   688
        using(db.insert(table))(stmt =>
wenzelm@65642
   689
        {
wenzelm@65642
   690
          db.set_string(stmt, 1, log_file.name)
wenzelm@65642
   691
          for ((c, i) <- table.columns.tail.zipWithIndex) {
wenzelm@65642
   692
            if (c.T == SQL.Type.Date)
wenzelm@65642
   693
              db.set_date(stmt, i + 2, meta_info.get_date(c))
wenzelm@65642
   694
            else
wenzelm@65642
   695
              db.set_string(stmt, i + 2, meta_info.get(c))
wenzelm@65642
   696
          }
wenzelm@65642
   697
          stmt.execute()
wenzelm@65642
   698
        })
wenzelm@65614
   699
      }
wenzelm@65600
   700
    }
wenzelm@65600
   701
wenzelm@65645
   702
    def update_sessions(db: SQL.Database, log_file: Log_File)
wenzelm@65599
   703
    {
wenzelm@65642
   704
      val build_info = log_file.parse_build_info()
wenzelm@65645
   705
      val table = Build_Info.sessions_table
wenzelm@65642
   706
wenzelm@65642
   707
      db.transaction {
wenzelm@65642
   708
        using(db.delete(table, Meta_Info.log_name.sql_where_equal(log_file.name)))(_.execute)
wenzelm@65643
   709
        using(db.insert(table))(stmt =>
wenzelm@65643
   710
        {
wenzelm@65645
   711
          val entries_iterator =
wenzelm@65643
   712
            if (build_info.sessions.isEmpty) Iterator("" -> Session_Entry.empty)
wenzelm@65643
   713
            else build_info.sessions.iterator
wenzelm@65645
   714
          for ((session_name, session) <- entries_iterator) {
wenzelm@65642
   715
            db.set_string(stmt, 1, log_file.name)
wenzelm@65643
   716
            db.set_string(stmt, 2, session_name)
wenzelm@65643
   717
            db.set_string(stmt, 3, session.proper_chapter)
wenzelm@65643
   718
            db.set_string(stmt, 4, session.proper_groups)
wenzelm@65643
   719
            db.set_int(stmt, 5, session.threads)
wenzelm@65643
   720
            db.set_long(stmt, 6, session.timing.elapsed.proper_ms)
wenzelm@65643
   721
            db.set_long(stmt, 7, session.timing.cpu.proper_ms)
wenzelm@65643
   722
            db.set_long(stmt, 8, session.timing.gc.proper_ms)
wenzelm@65643
   723
            db.set_long(stmt, 9, session.ml_timing.elapsed.proper_ms)
wenzelm@65643
   724
            db.set_long(stmt, 10, session.ml_timing.cpu.proper_ms)
wenzelm@65643
   725
            db.set_long(stmt, 11, session.ml_timing.gc.proper_ms)
wenzelm@65643
   726
            db.set_long(stmt, 12, session.heap_size)
wenzelm@65643
   727
            db.set_string(stmt, 13, session.status.map(_.toString))
wenzelm@65642
   728
            stmt.execute()
wenzelm@65643
   729
          }
wenzelm@65643
   730
        })
wenzelm@65642
   731
      }
wenzelm@65642
   732
    }
wenzelm@65642
   733
wenzelm@65645
   734
    def update_ml_statistics(db: SQL.Database, log_file: Log_File)
wenzelm@65645
   735
    {
wenzelm@65646
   736
      val build_info = log_file.parse_build_info(ml_statistics = true)
wenzelm@65645
   737
      val table = Build_Info.ml_statistics_table
wenzelm@65645
   738
wenzelm@65645
   739
      db.transaction {
wenzelm@65645
   740
        using(db.delete(table, Meta_Info.log_name.sql_where_equal(log_file.name)))(_.execute)
wenzelm@65645
   741
        using(db.insert(table))(stmt =>
wenzelm@65645
   742
        {
wenzelm@65645
   743
          val ml_stats: List[(String, Option[Bytes])] =
wenzelm@65645
   744
            Par_List.map[(String, Session_Entry), (String, Option[Bytes])](
wenzelm@65645
   745
              { case (a, b) => (a, compress_properties(b.ml_statistics).proper) },
wenzelm@65645
   746
              build_info.sessions.iterator.filter(p => p._2.ml_statistics.nonEmpty).toList)
wenzelm@65645
   747
          val entries = if (ml_stats.nonEmpty) ml_stats else List("" -> None)
wenzelm@65645
   748
          for ((session_name, ml_statistics) <- entries) {
wenzelm@65645
   749
            db.set_string(stmt, 1, log_file.name)
wenzelm@65645
   750
            db.set_string(stmt, 2, session_name)
wenzelm@65645
   751
            db.set_bytes(stmt, 3, ml_statistics)
wenzelm@65645
   752
            stmt.execute()
wenzelm@65645
   753
          }
wenzelm@65645
   754
        })
wenzelm@65645
   755
      }
wenzelm@65645
   756
    }
wenzelm@65645
   757
wenzelm@65645
   758
    def write_info(db: SQL.Database, files: List[JFile], ml_statistics: Boolean = false)
wenzelm@65642
   759
    {
wenzelm@65642
   760
      class Table_Status(table: SQL.Table, update_db: (SQL.Database, Log_File) => Unit)
wenzelm@65642
   761
      {
wenzelm@65642
   762
        private var known: Set[String] =
wenzelm@65642
   763
        {
wenzelm@65642
   764
          db.create_table(table)
wenzelm@65642
   765
          val key = Meta_Info.log_name
wenzelm@65642
   766
          using(db.select(table, List(key), distinct = true))(
wenzelm@65642
   767
            stmt => SQL.iterator(stmt.executeQuery)(db.string(_, key)).toSet)
wenzelm@65642
   768
        }
wenzelm@65642
   769
        def required(file: JFile): Boolean = !known(Log_File.plain_name(file.getName))
wenzelm@65642
   770
        def update(log_file: Log_File)
wenzelm@65642
   771
        {
wenzelm@65642
   772
          if (!known(log_file.name)) {
wenzelm@65642
   773
            update_db(db, log_file)
wenzelm@65642
   774
            known += log_file.name
wenzelm@65618
   775
          }
wenzelm@65614
   776
        }
wenzelm@65605
   777
      }
wenzelm@65642
   778
      val status =
wenzelm@65642
   779
        List(
wenzelm@65642
   780
          new Table_Status(Meta_Info.table, update_meta_info _),
wenzelm@65645
   781
          new Table_Status(Build_Info.sessions_table, update_sessions _),
wenzelm@65645
   782
          new Table_Status(Build_Info.ml_statistics_table,
wenzelm@65645
   783
            if (ml_statistics) update_ml_statistics _
wenzelm@65645
   784
            else (_: SQL.Database, _: Log_File) => ()))
wenzelm@65642
   785
wenzelm@65645
   786
      for (file_group <- files.filter(file => status.exists(_.required(file))).grouped(100)) {
wenzelm@65642
   787
        val log_files = Par_List.map[JFile, Log_File](Log_File.apply _, file_group)
wenzelm@65642
   788
        db.transaction { log_files.foreach(log_file => status.foreach(_.update(log_file))) }
wenzelm@65642
   789
      }
wenzelm@65605
   790
    }
wenzelm@65605
   791
wenzelm@65621
   792
    def read_meta_info(db: SQL.Database, log_name: String): Option[Meta_Info] =
wenzelm@65621
   793
    {
wenzelm@65642
   794
      val table = Meta_Info.table
wenzelm@65642
   795
      val columns = table.columns.tail
wenzelm@65642
   796
      using(db.select(table, columns, Meta_Info.log_name.sql_where_equal(log_name)))(stmt =>
wenzelm@65621
   797
      {
wenzelm@65621
   798
        val rs = stmt.executeQuery
wenzelm@65621
   799
        if (!rs.next) None
wenzelm@65621
   800
        else {
wenzelm@65621
   801
          val results =
wenzelm@65642
   802
            columns.map(c => c.name ->
wenzelm@65621
   803
              (if (c.T == SQL.Type.Date)
wenzelm@65621
   804
                db.get(rs, c, db.date _).map(Log_File.Date_Format(_))
wenzelm@65621
   805
               else
wenzelm@65624
   806
                db.get(rs, c, db.string _)))
wenzelm@65621
   807
          val n = Prop.all_props.length
wenzelm@65621
   808
          val props = for ((x, Some(y)) <- results.take(n)) yield (x, y)
wenzelm@65621
   809
          val settings = for ((x, Some(y)) <- results.drop(n)) yield (x, y)
wenzelm@65621
   810
          Some(Meta_Info(props, settings))
wenzelm@65621
   811
        }
wenzelm@65621
   812
      })
wenzelm@65621
   813
    }
wenzelm@65621
   814
wenzelm@65621
   815
    def read_build_info(
wenzelm@65629
   816
      db: SQL.Database,
wenzelm@65629
   817
      log_name: String,
wenzelm@65629
   818
      session_names: List[String] = Nil,
wenzelm@65629
   819
      ml_statistics: Boolean = false): Build_Info =
wenzelm@65621
   820
    {
wenzelm@65645
   821
      val table1 = Build_Info.sessions_table
wenzelm@65645
   822
      val table2 = Build_Info.ml_statistics_table
wenzelm@65629
   823
wenzelm@65645
   824
      val where_log_name =
wenzelm@65645
   825
        Meta_Info.log_name(table1).sql_where_equal(log_name) + " AND " +
wenzelm@65649
   826
          Build_Info.session_name(table1).sql + " <> ''"
wenzelm@65621
   827
      val where =
wenzelm@65645
   828
        if (session_names.isEmpty) where_log_name
wenzelm@65621
   829
        else
wenzelm@65645
   830
          where_log_name + " AND " +
wenzelm@65649
   831
          session_names.map(a => Build_Info.session_name(table1).sql + " = " + SQL.string(a)).
wenzelm@65621
   832
            mkString("(", " OR ", ")")
wenzelm@65629
   833
wenzelm@65645
   834
      val columns1 = table1.columns.tail.map(_.apply(table1))
wenzelm@65645
   835
      val (columns, from) =
wenzelm@65645
   836
        if (ml_statistics) {
wenzelm@65645
   837
          val columns = columns1 ::: List(Build_Info.ml_statistics(table2))
wenzelm@65668
   838
          val join =
wenzelm@65668
   839
            SQL.join_outer(table1, table2,
wenzelm@65649
   840
              Meta_Info.log_name(table1).sql + " = " +
wenzelm@65649
   841
              Meta_Info.log_name(table2).sql + " AND " +
wenzelm@65649
   842
              Build_Info.session_name(table1).sql + " = " +
wenzelm@65668
   843
              Build_Info.session_name(table2).sql)
wenzelm@65668
   844
          (columns, SQL.enclose(join))
wenzelm@65645
   845
        }
wenzelm@65649
   846
        else (columns1, table1.sql)
wenzelm@65645
   847
wenzelm@65621
   848
      val sessions =
wenzelm@65645
   849
        using(db.statement(SQL.select(columns) + from + " " + where))(stmt =>
wenzelm@65621
   850
        {
wenzelm@65621
   851
          SQL.iterator(stmt.executeQuery)(rs =>
wenzelm@65621
   852
          {
wenzelm@65621
   853
            val session_name = db.string(rs, Build_Info.session_name)
wenzelm@65626
   854
            val session_entry =
wenzelm@65626
   855
              Session_Entry(
wenzelm@65626
   856
                chapter = db.string(rs, Build_Info.chapter),
wenzelm@65626
   857
                groups = split_lines(db.string(rs, Build_Info.groups)),
wenzelm@65626
   858
                threads = db.get(rs, Build_Info.threads, db.int _),
wenzelm@65626
   859
                timing =
wenzelm@65626
   860
                  Timing(Time.ms(db.long(rs, Build_Info.timing_elapsed)),
wenzelm@65626
   861
                    Time.ms(db.long(rs, Build_Info.timing_cpu)),
wenzelm@65626
   862
                    Time.ms(db.long(rs, Build_Info.timing_gc))),
wenzelm@65626
   863
                ml_timing =
wenzelm@65626
   864
                  Timing(Time.ms(db.long(rs, Build_Info.ml_timing_elapsed)),
wenzelm@65626
   865
                    Time.ms(db.long(rs, Build_Info.ml_timing_cpu)),
wenzelm@65626
   866
                    Time.ms(db.long(rs, Build_Info.ml_timing_gc))),
wenzelm@65626
   867
                heap_size = db.get(rs, Build_Info.heap_size, db.long _),
wenzelm@65643
   868
                status =
wenzelm@65643
   869
                  db.get(rs, Build_Info.status, db.string _).
wenzelm@65643
   870
                    map(Session_Status.withName(_)),
wenzelm@65629
   871
                ml_statistics =
wenzelm@65629
   872
                  if (ml_statistics) uncompress_properties(db.bytes(rs, Build_Info.ml_statistics))
wenzelm@65629
   873
                  else Nil)
wenzelm@65626
   874
            session_name -> session_entry
wenzelm@65621
   875
          }).toMap
wenzelm@65621
   876
        })
wenzelm@65621
   877
      Build_Info(sessions)
wenzelm@65621
   878
    }
wenzelm@65595
   879
  }
wenzelm@65670
   880
wenzelm@65670
   881
wenzelm@65670
   882
  /* full view on build_log data */
wenzelm@65670
   883
wenzelm@65670
   884
  // WARNING: This may cause performance problems, e.g. with sqlitebrowser
wenzelm@65670
   885
wenzelm@65670
   886
  val full_view: SQL.View =
wenzelm@65670
   887
    SQL.View("isabelle_build_log",
wenzelm@65670
   888
      Meta_Info.table.columns :::
wenzelm@65670
   889
        Build_Info.sessions_table.columns.tail.map(_.copy(primary_key = false)))
wenzelm@65670
   890
wenzelm@65670
   891
  def create_full_view(db: SQL.Database)
wenzelm@65670
   892
  {
wenzelm@65670
   893
    if (!db.tables.contains(full_view.name)) {
wenzelm@65670
   894
      val table1 = Meta_Info.table
wenzelm@65670
   895
      val table2 = Build_Info.sessions_table
wenzelm@65670
   896
      db.create_view(full_view,
wenzelm@65670
   897
        SQL.select(Meta_Info.log_name(table1) :: full_view.columns.tail) +
wenzelm@65670
   898
        SQL.join(table1, table2,
wenzelm@65670
   899
          Meta_Info.log_name(table1).sql + " = " + Meta_Info.log_name(table2).sql))
wenzelm@65670
   900
    }
wenzelm@65670
   901
  }
wenzelm@64045
   902
}