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