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