From d5e99bdbe5ab215fe6a5fdd51048b3af8ddf6385 Mon Sep 17 00:00:00 2001 From: Brian Schlining Date: Sun, 21 Jun 2026 20:08:27 +0200 Subject: [PATCH 01/20] task: upgrade to sbt 2 --- build.sbt | 4 ++-- project/Dependencies.scala | 24 +++++++++----------- project/build.properties | 2 +- project/plugins.sbt | 8 +++---- src/main/scala/org/mbari/beholder/Main.scala | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/build.sbt b/build.sbt index f527a74..f33b02a 100644 --- a/build.sbt +++ b/build.sbt @@ -54,8 +54,8 @@ lazy val root = project dockerUpdateLatest := true, // Set version based on git tag. I use "0.0.0" format (no leading "v", which is the default) // Use `show gitCurrentTags` in sbt to update/see the tags - git.gitTagToVersionNumber := { tag: String => - if(tag matches "[0-9]+\\..*") Some(tag) + git.gitTagToVersionNumber := { (gitTag: String) => + if(gitTag matches "[0-9]+\\..*") Some(gitTag) else None }, git.useGitDescribe := true, diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 274ee9b..6db0dfd 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,29 +1,27 @@ import sbt.* -object Dependencies { +object Dependencies: - lazy val auth0 = "com.auth0" % "java-jwt" % "4.5.1" - lazy val auth0jwk = "com.auth0" % "jwks-rsa" % "0.23.0" - private val circeVersion = "0.14.14" + lazy val auth0 = "com.auth0" % "java-jwt" % "4.5.2" + lazy val auth0jwk = "com.auth0" % "jwks-rsa" % "0.24.1" + private val circeVersion = "0.14.15" lazy val circeCore = "io.circe" %% "circe-core" % circeVersion lazy val circeGeneric = "io.circe" %% "circe-generic" % circeVersion lazy val circeParser = "io.circe" %% "circe-parser" % circeVersion - lazy val jansi = "org.fusesource.jansi" % "jansi" % "2.4.2" - lazy val logback = "ch.qos.logback" % "logback-classic" % "1.5.32" + lazy val jansi = "org.fusesource.jansi" % "jansi" % "2.4.3" + lazy val logback = "ch.qos.logback" % "logback-classic" % "1.5.34" lazy val methanol = "com.github.mizosoft.methanol" % "methanol" % "1.9.0" - lazy val munit = "org.scalameta" %% "munit" % "1.2.3" + lazy val munit = "org.scalameta" %% "munit" % "1.3.3" lazy val picocli = "info.picocli" % "picocli" % "4.7.7" - lazy val slf4jVersion = "2.0.17" + lazy val slf4jVersion = "2.0.18" lazy val slf4jApi = "org.slf4j" % "slf4j-api" % slf4jVersion lazy val slf4jJul = "org.slf4j" % "jul-to-slf4j" % slf4jVersion lazy val slf4jJdk = "org.slf4j" % "slf4j-jdk-platform-logging" % slf4jVersion lazy val sttpCirceClient = "com.softwaremill.sttp.client3" %% "circe" % "3.11.0" - private val tapirVersion = "1.13.9" + private val tapirVersion = "1.13.23" lazy val tapirStubServer = "com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % tapirVersion lazy val tapirSwagger = "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % tapirVersion lazy val tapirCirce = "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion lazy val tapirVertx = "com.softwaremill.sttp.tapir" %% "tapir-vertx-server" % tapirVersion - lazy val typesafeConfig = "com.typesafe" % "config" % "1.4.6" - lazy val zio = "dev.zio" %% "zio" % "2.1.24" - -} \ No newline at end of file + lazy val typesafeConfig = "com.typesafe" % "config" % "1.4.9" + lazy val zio = "dev.zio" %% "zio" % "2.1.26" diff --git a/project/build.properties b/project/build.properties index 070db1c..a0ccf4c 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.12.4 \ No newline at end of file +sbt.version=2.0.0 \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 091efe1..e1f0d48 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,6 +1,6 @@ // addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3") -addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.4") +addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.7.0") addSbtPlugin("com.github.sbt" % "sbt-git" % "2.1.0") -addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.11.1") -addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.5") \ No newline at end of file +addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.11.7") +addSbtPlugin("com.github.sbt" % "sbt-header" % "5.11.0") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.6.1") \ No newline at end of file diff --git a/src/main/scala/org/mbari/beholder/Main.scala b/src/main/scala/org/mbari/beholder/Main.scala index 5d53c8e..ec16826 100644 --- a/src/main/scala/org/mbari/beholder/Main.scala +++ b/src/main/scala/org/mbari/beholder/Main.scala @@ -110,7 +110,7 @@ object Main: val interpreter = VertxFutureServerInterpreter() // Add CORS - val corsHandler = CorsHandler.create("*") + val corsHandler = CorsHandler.create() router.route().handler(corsHandler) // create cache if needed From 8b9f02d2f9bf30d7338b328faddb5afd56220264 Mon Sep 17 00:00:00 2001 From: Brian Schlining Date: Sun, 21 Jun 2026 20:53:14 +0200 Subject: [PATCH 02/20] Starting implementation --- .../{Jpeg.scala => CachedImage.scala} | 63 ++-- .../scala/org/mbari/beholder/ImageCache.scala | 26 +- .../scala/org/mbari/beholder/ImageType.scala | 34 +++ .../scala/org/mbari/beholder/JpegCache.scala | 287 +++++++++--------- .../scala/org/mbari/beholder/JpegCache2.scala | 206 ------------- .../org/mbari/beholder/JpegCapture.scala | 8 +- src/main/scala/org/mbari/beholder/Main.scala | 2 +- .../beholder/etc/ffmpeg/FfmpegUtil.scala | 69 ++++- .../org/mbari/beholder/etc/jdk/PathUtil.scala | 11 + .../org/mbari/beholder/JpegCache2Suite.scala | 105 ------- .../org/mbari/beholder/JpegCacheSuite.scala | 53 ++-- .../scala/org/mbari/beholder/JpegSuite.scala | 2 +- .../beholder/api/CaptureEndpointsSuite.scala | 3 +- .../beholder/etc/jdk/PathUtilSuite.scala | 82 ++--- 14 files changed, 375 insertions(+), 576 deletions(-) rename src/main/scala/org/mbari/beholder/{Jpeg.scala => CachedImage.scala} (55%) create mode 100644 src/main/scala/org/mbari/beholder/ImageType.scala delete mode 100644 src/main/scala/org/mbari/beholder/JpegCache2.scala delete mode 100644 src/test/scala/org/mbari/beholder/JpegCache2Suite.scala diff --git a/src/main/scala/org/mbari/beholder/Jpeg.scala b/src/main/scala/org/mbari/beholder/CachedImage.scala similarity index 55% rename from src/main/scala/org/mbari/beholder/Jpeg.scala rename to src/main/scala/org/mbari/beholder/CachedImage.scala index c115516..489fd0c 100644 --- a/src/main/scala/org/mbari/beholder/Jpeg.scala +++ b/src/main/scala/org/mbari/beholder/CachedImage.scala @@ -16,13 +16,18 @@ package org.mbari.beholder +import org.mbari.beholder.ImageType.Jpeg + import java.net.URL import java.nio.file.{Files, Path, Paths} import java.time.{Duration, Instant} import org.mbari.beholder.etc.jdk.{DurationUtil, PathUtil} import org.mbari.beholder.util.NumberUtil + import java.net.URI + + /** * Information about the source of a JPEG * @param videoUri @@ -36,19 +41,20 @@ import java.net.URI * @param sizeBytes * The size of the jpeg file in bytes */ -case class Jpeg( +case class CachedImage( videoUri: URI, elapsedTime: Duration, path: Path, created: Instant = Instant.now(), - sizeBytes: Option[Long] = None + sizeBytes: Option[Long] = None, + imageType: ImageType = ImageType.Jpeg ): val sizeMB: Option[Double] = sizeBytes.map(NumberUtil.byteToMB) -object Jpeg: +object CachedImage: /** - * Generates jpeg info + * Generates cachedImage info * @param root * The root directory of the cache * @param uri @@ -56,42 +62,53 @@ object Jpeg: * @param elapsedTime * The elapsed tie into the video * @return - * The jpeg info + * The cachedImage info */ - def toPath(root: Path, uri: URI, elapsedTime: Duration): Jpeg = - val filename = DurationUtil.toHMS(elapsedTime).replace(":", "_") + ".jpg" + def toPath(root: Path, uri: URI, elapsedTime: Duration, imageType: ImageType = Jpeg): CachedImage = + val filename = DurationUtil.toHMS(elapsedTime).replace(":", "_") + imageType.extension val parent = PathUtil.toPath(root, uri.toURL) val path = parent.resolve(filename) - Jpeg(uri, elapsedTime, path) + CachedImage(uri, elapsedTime, path, imageType = imageType) /** - * Generates Jpeg info + * Generates cachedImage info * @param root * The root directory of the cache * @param file * the jpeg file (must be under the root fo the cache directory) * @return - * A jpeg info. None the file is not a jpeg or if it's not under the cache directory + * A cachedImage info. None the file is not a cachedImage or if it's not under the cache directory */ - def fromPath(root: Path, file: Path): Option[Jpeg] = - if !Files.isDirectory(file) && PathUtil.isChild(root, file) && PathUtil.isJpeg(file) then + def fromPath(root: Path, file: Path): Option[CachedImage] = + if !Files.isDirectory(file) && PathUtil.isChild(root, file) && (PathUtil.isJpeg(file) || PathUtil.isPng(file)) then val parent = file.getParent - PathUtil - .fromPath(root, parent) - .map(videoUrl => - val filename = PathUtil.dropExtension(file).replace("_", ":") - val elapsedTime = DurationUtil.fromHMS(filename) - val size = if Files.isRegularFile(file) then Some(Files.size(file)) else None - Jpeg(videoUrl.toURI, elapsedTime, file, sizeBytes = size) - ) + val opt = ImageType.fromPath(file) + + opt.flatMap(imageType => { + PathUtil + .fromPath(root, parent) + .map(videoUrl => + val filename = PathUtil.dropExtension(file).replace("_", ":") + val elapsedTime = DurationUtil.fromHMS(filename) + val size = if Files.isRegularFile(file) then Some(Files.size(file)) else None + CachedImage(videoUrl.toURI, elapsedTime, file, sizeBytes = size, imageType = imageType) + ) + }) + + else None private val fakeUrl = URI.create("http://www.mbari.org") - private val fakePath = Paths.get("/foo/bar.jpg") + private val fakePath = Paths.get("/foo/bar") /** * Constructs a fake/mock jpeg that is useful for searchies * @param elapsedTime */ - def fake(elapsedTime: Duration): Jpeg = Jpeg(fakeUrl, elapsedTime, fakePath) - def fake(uri: URI, elapsedTime: Duration) = Jpeg(uri, elapsedTime, fakePath) + def fake(elapsedTime: Duration, imageType: ImageType): CachedImage = + val path = PathUtil.useExtension(fakePath, imageType.extension) + CachedImage(fakeUrl, elapsedTime, path, imageType = imageType ) + + def fake(uri: URI, elapsedTime: Duration, imageType: ImageType): CachedImage = + val path = PathUtil.useExtension(fakePath, imageType.extension) + CachedImage(uri, elapsedTime, path, imageType = imageType) diff --git a/src/main/scala/org/mbari/beholder/ImageCache.scala b/src/main/scala/org/mbari/beholder/ImageCache.scala index 0e09ee0..e6b1584 100644 --- a/src/main/scala/org/mbari/beholder/ImageCache.scala +++ b/src/main/scala/org/mbari/beholder/ImageCache.scala @@ -32,7 +32,7 @@ trait ImageCache: * @return * The jpeg in the cache */ - def get(jpeg: Jpeg): Option[Jpeg] + def get(cachedImage: CachedImage): Option[CachedImage] /** * Store a jpeg in the cache @@ -42,7 +42,7 @@ trait ImageCache: * @return * The stored jpeg */ - def put(jpeg: Jpeg): Jpeg + def put(cachedImage: CachedImage): CachedImage /** * Remove a jpeg from the cache. @@ -50,14 +50,14 @@ trait ImageCache: * @param jpeg * the jpeg to remove */ - def remove(jpeg: Jpeg): Option[Jpeg] + def remove(cachedImage: CachedImage): Option[CachedImage] // ------------------------------- - def get(uri: URI, elapsedTime: DurationString): Option[Jpeg] = + def get(uri: URI, elapsedTime: DurationString, imageType: ImageType): Option[CachedImage] = DurationString.unapply(elapsedTime) match case None => None - case Some(dur) => get(uri, dur) + case Some(dur) => get(uri, dur, imageType) /** * @param uri @@ -65,12 +65,12 @@ trait ImageCache: * @param elapsedTime * The elapsed time into the video */ - def get(uri: URI, elapsedTime: Duration): Option[Jpeg] = - get(Jpeg.fake(uri, elapsedTime)) + def get(uri: URI, elapsedTime: Duration, imageType: ImageType): Option[CachedImage] = + get(CachedImage.fake(uri, elapsedTime, imageType)) /** - * Store a jpeg in the cache + * Store a cachedImage in the cache * * @param uri * The video URL @@ -79,15 +79,15 @@ trait ImageCache: * @param path * The on disk location of the jpeg. It must be under the cache's root directory */ - def put(uri: URI, elapsedTime: Duration, path: Path): Option[Jpeg] = - Jpeg.fromPath(root, path).map(put) + def put(uri: URI, elapsedTime: Duration, path: Path): Option[CachedImage] = + CachedImage.fromPath(root, path).map(put) - def put(uri: URI, elapsedTime: DurationString, path: Path): Option[Jpeg] = + def put(uri: URI, elapsedTime: DurationString, path: Path): Option[CachedImage] = DurationString.unapply(elapsedTime) match case None => None case Some(dur) => put(uri, dur, path) - def remove(uri: URI, elapsedTime: Duration): Option[Jpeg] = - remove(Jpeg.fake(uri, elapsedTime)) + def remove(uri: URI, elapsedTime: Duration, imageType: ImageType): Option[CachedImage] = + remove(CachedImage.fake(uri, elapsedTime, imageType)) diff --git a/src/main/scala/org/mbari/beholder/ImageType.scala b/src/main/scala/org/mbari/beholder/ImageType.scala new file mode 100644 index 0000000..734cdcb --- /dev/null +++ b/src/main/scala/org/mbari/beholder/ImageType.scala @@ -0,0 +1,34 @@ +/* + * Copyright 2022 MBARI + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.mbari.beholder + +import org.mbari.beholder.etc.jdk.PathUtil +import java.nio.file.Path + +enum ImageType(val extension: String, val mediaType: String): + case Jpeg extends ImageType(".jpg", "image/jpeg") + case Png extends ImageType(".png", "image/png") + + +object ImageType: + def fromExtension(ext: String): Option[ImageType] = + ImageType.values.find(_.extension == ext) + + def fromPath(path: Path): Option[ImageType] = + if PathUtil.isJpeg(path) then Some(Jpeg) + else if PathUtil.isPng(path) then Some(Png) + else None \ No newline at end of file diff --git a/src/main/scala/org/mbari/beholder/JpegCache.scala b/src/main/scala/org/mbari/beholder/JpegCache.scala index 11a7d4c..f883293 100644 --- a/src/main/scala/org/mbari/beholder/JpegCache.scala +++ b/src/main/scala/org/mbari/beholder/JpegCache.scala @@ -16,41 +16,33 @@ package org.mbari.beholder -import java.net.URL -import java.nio.file.{Files, Path, Paths} +import org.mbari.beholder.etc.ffmpeg.DurationString.DurationString + +import java.net.URI +import java.nio.file.{Files, Path} import java.time.{Duration, Instant} -import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentSkipListSet -import java.util.{Comparator, EnumSet, TreeMap} -import org.mbari.beholder.etc.ffmpeg.DurationString.* -import org.mbari.beholder.etc.jdk.{DurationUtil, PathUtil} +import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong} import org.mbari.beholder.etc.jdk.Logging.given -import org.mbari.beholder.util.{ListUtil, NumberUtil} +import org.mbari.beholder.util.NumberUtil -import scala.collection.Searching.Found -import scala.concurrent.duration.Duration.apply import scala.jdk.CollectionConverters.* -import scala.util.chaining.* -import org.mbari.beholder.util.VectorUtil - -import java.net.URI -import scala.collection.mutable /** - * In memory cache of paths to Jpegs that were captured. The cache is organized by videoUrl and elpased time into the - * video. When the cache on disk size exceeds the allowed size, the oldest jpegs are cleared out of the cache and - * removed from disk. The number of jpegs removed is specified by the cacheClearPct. + * Disk-backed JPEG frame cache. Frames are indexed by (videoUri, elapsedTime) for O(1) lookups + * and evicted oldest-first when total on-disk size exceeds `maxCacheSizeMB`. * - * Jpegs are expected to be stored in jpegs named as follows: /