| author | wenzelm | 
| Tue, 06 Sep 2022 21:06:20 +0200 | |
| changeset 76074 | 2456721602b2 | 
| parent 75393 | 87ebf5a50283 | 
| child 76351 | 2cee31cd92f0 | 
| permissions | -rw-r--r-- | 
| 65650 | 1 | /* Title: Pure/Admin/jenkins.scala | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 3 | |
| 65650 | 4 | Support for Jenkins continuous integration service. | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 5 | */ | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 6 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 7 | package isabelle | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 8 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 9 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 10 | import java.net.URL | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 11 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 12 | import scala.util.matching.Regex | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 13 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 14 | |
| 75393 | 15 | object Jenkins {
 | 
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 16 | /* server API */ | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 17 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 18 | def root(): String = | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 19 |     Isabelle_System.getenv_strict("ISABELLE_JENKINS_ROOT")
 | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 20 | |
| 75393 | 21 |   def invoke(url: String, args: String*): Any = {
 | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 22 |     val req = url + "/api/json?" + args.mkString("&")
 | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 23 | val result = Url.read(req) | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 24 |     try { JSON.parse(result) }
 | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 25 |     catch { case ERROR(_) => error("Malformed JSON from " + quote(req)) }
 | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 26 | } | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 27 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 28 | |
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 29 | /* build jobs */ | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 30 | |
| 65658 | 31 | def build_job_names(): List[String] = | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 32 |     for {
 | 
| 64545 
25045094d7bb
clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
 wenzelm parents: 
64160diff
changeset | 33 | job <- JSON.array(invoke(root()), "jobs").getOrElse(Nil) | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 34 | _class <- JSON.string(job, "_class") | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 35 | if _class == "hudson.model.FreeStyleProject" | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 36 | name <- JSON.string(job, "name") | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 37 | } yield name | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 38 | |
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 39 | |
| 68209 | 40 | def download_logs( | 
| 75393 | 41 | options: Options, | 
| 42 | job_names: List[String], | |
| 43 | dir: Path, | |
| 44 | progress: Progress = new Progress | |
| 45 |   ): Unit = {
 | |
| 68209 | 46 | val store = Sessions.store(options) | 
| 71601 | 47 | val infos = job_names.flatMap(build_job_infos) | 
| 65662 | 48 | Par_List.map((info: Job_Info) => info.download_log(store, dir, progress), infos) | 
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 49 | } | 
| 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 50 | |
| 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 51 | |
| 65747 | 52 | /* build log status */ | 
| 65736 | 53 | |
| 65788 | 54 |   val build_log_jobs = List("isabelle-nightly-benchmark", "isabelle-nightly-slow")
 | 
| 65736 | 55 | |
| 65747 | 56 | val build_status_profiles: List[Build_Status.Profile] = | 
| 65736 | 57 | build_log_jobs.map(job_name => | 
| 66880 
486f4af28db9
more thorough treatment of afp_version and afp_pull_date;
 wenzelm parents: 
65935diff
changeset | 58 |       Build_Status.Profile("jenkins " + job_name,
 | 
| 
486f4af28db9
more thorough treatment of afp_version and afp_pull_date;
 wenzelm parents: 
65935diff
changeset | 59 | sql = | 
| 73342 | 60 | Build_Log.Prop.build_engine.toString + " = " + SQL.string(Build_Log.Jenkins.engine) + | 
| 61 |           " AND " + Build_Log.Data.session_name + " <> " + SQL.string("Pure") + " AND " +
 | |
| 66880 
486f4af28db9
more thorough treatment of afp_version and afp_pull_date;
 wenzelm parents: 
65935diff
changeset | 62 | Build_Log.Data.status + " = " + SQL.string(Build_Log.Session_Status.finished.toString) + | 
| 
486f4af28db9
more thorough treatment of afp_version and afp_pull_date;
 wenzelm parents: 
65935diff
changeset | 63 |           " AND " + Build_Log.Data.log_name + " LIKE " + SQL.string("%" + job_name)))
 | 
| 65736 | 64 | |
| 65 | ||
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 66 | /* job info */ | 
| 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 67 | |
| 64054 | 68 | sealed case class Job_Info( | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 69 | job_name: String, | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 70 | timestamp: Long, | 
| 65655 | 71 | main_log: URL, | 
| 75393 | 72 | session_logs: List[(String, String, URL)] | 
| 73 |   ) {
 | |
| 69980 | 74 | val date: Date = Date(Time.ms(timestamp), Date.timezone_berlin) | 
| 65656 | 75 | |
| 76 | def log_filename: Path = | |
| 77 | Build_Log.log_filename(Build_Log.Jenkins.engine, date, List(job_name)) | |
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 78 | |
| 75393 | 79 |     def read_ml_statistics(store: Sessions.Store, session_name: String): List[Properties.T] = {
 | 
| 65655 | 80 | def get_log(ext: String): Option[URL] = | 
| 81 |         session_logs.collectFirst({ case (a, b, url) if a == session_name && b == ext => url })
 | |
| 82 | ||
| 83 |       get_log("db") match {
 | |
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 84 | case Some(url) => | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 85 |           Isabelle_System.with_tmp_file(session_name, "db") { database =>
 | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 86 | Bytes.write(database, Bytes.read(url)) | 
| 65935 | 87 | using(SQLite.open_database(database))(db => store.read_ml_statistics(db, session_name)) | 
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 88 | } | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 89 | case None => | 
| 65655 | 90 |           get_log("gz") match {
 | 
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 91 | case Some(url) => | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 92 | val log_file = Build_Log.Log_File(session_name, Url.read_gzip(url)) | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 93 | log_file.parse_session_info(ml_statistics = true).ml_statistics | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 94 | case None => Nil | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 95 | } | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 96 | } | 
| 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 97 | } | 
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 98 | |
| 75393 | 99 |     def download_log(store: Sessions.Store, dir: Path, progress: Progress = new Progress): Unit = {
 | 
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 100 | val log_dir = dir + Build_Log.log_subdir(date) | 
| 72575 | 101 | val log_path = log_dir + log_filename.xz | 
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 102 | |
| 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 103 |       if (!log_path.is_file) {
 | 
| 65660 | 104 | progress.echo(log_path.expand.implode) | 
| 72375 | 105 | Isabelle_System.make_directory(log_dir) | 
| 65660 | 106 | |
| 67008 | 107 | val ml_statistics = | 
| 71602 | 108 | session_logs.map(_._1).distinct.sorted.flatMap(session_name => | 
| 67008 | 109 | read_ml_statistics(store, session_name). | 
| 110 | map(props => (Build_Log.SESSION_NAME -> session_name) :: props)) | |
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 111 | |
| 67008 | 112 | File.write_xz(log_path, | 
| 113 | terminate_lines(Url.read(main_log) :: | |
| 71630 
50425e4c3910
clarified modules: global quasi-scope for markers;
 wenzelm parents: 
71620diff
changeset | 114 | ml_statistics.map(Protocol.ML_Statistics_Marker.apply)), | 
| 67008 | 115 | XZ.options(6)) | 
| 65657 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 116 | } | 
| 
2773b6859c55
download Jenkins logs with inlined ml_statistics;
 wenzelm parents: 
65656diff
changeset | 117 | } | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 118 | } | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 119 | |
| 75393 | 120 |   def build_job_infos(job_name: String): List[Job_Info] = {
 | 
| 65653 
f433c0e73307
read ml_statistics from session logs: .db or .gz files;
 wenzelm parents: 
65652diff
changeset | 121 |     val Session_Log = new Regex("""^.*/log/([^/]+)\.(db|gz)$""")
 | 
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 122 | |
| 65659 | 123 | val infos = | 
| 124 |       for {
 | |
| 125 | build <- | |
| 126 | JSON.array( | |
| 67008 | 127 | invoke(root() + "/job/" + job_name, "tree=allBuilds[number,timestamp,artifacts[*]]"), | 
| 65659 | 128 | "allBuilds").getOrElse(Nil) | 
| 129 | number <- JSON.int(build, "number") | |
| 130 | timestamp <- JSON.long(build, "timestamp") | |
| 131 |       } yield {
 | |
| 67008 | 132 | val job_prefix = root() + "/job/" + job_name + "/" + number | 
| 65659 | 133 | val main_log = Url(job_prefix + "/consoleText") | 
| 134 | val session_logs = | |
| 67008 | 135 |           for {
 | 
| 136 | artifact <- JSON.array(build, "artifacts").getOrElse(Nil) | |
| 137 | log_path <- JSON.string(artifact, "relativePath") | |
| 138 |             (name, ext) <- (log_path match { case Session_Log(a, b) => Some((a, b)) case _ => None })
 | |
| 139 | } yield (name, ext, Url(job_prefix + "/artifact/" + log_path)) | |
| 140 | Job_Info(job_name, timestamp, main_log, session_logs) | |
| 65659 | 141 | } | 
| 142 | ||
| 143 | infos.sortBy(info => - info.timestamp) | |
| 63646 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 144 | } | 
| 
74604a9fc4c8
API for Isabelle Jenkins continuous integration services;
 wenzelm parents: diff
changeset | 145 | } |