Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: diff supports server-side summary and limit with truncation metadata

riley02276aaf3f70759d
diff --git a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
index e81dcdb7..c491ef52 100644
--- a/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
+++ b/memory-layer/src/main/scala/ix/memory/api/DiffRoutes.scala
@@ -12,7 +12,13 @@ import org.http4s.dsl.io._
import ix.memory.db.GraphQueryApi
import ix.memory.model._
-case class DiffRequest(fromRev: Long, toRev: Long, entityId: Option[String])
+case class DiffRequest(
+ fromRev: Long,
+ toRev: Long,
+ entityId: Option[String] = None,
+ summary: Option[Boolean] = None,
+ limit: Option[Int] = None
+)
object DiffRequest {
implicit val decoder: Decoder[DiffRequest] = deriveDecoder[DiffRequest]
@@ -31,48 +37,78 @@ object DiffEntry {
implicit val encoder: Encoder[DiffEntry] = deriveEncoder[DiffEntry]
}
-case class DiffResponse(fromRev: Long, toRev: Long, changes: Vector[DiffEntry])
+case class DiffResponse(
+ fromRev: Long,
+ toRev: Long,
+ changes: Vector[DiffEntry],
+ truncated: Boolean,
+ totalChanges: Int
+)
object DiffResponse {
implicit val encoder: Encoder[DiffResponse] = deriveEncoder[DiffResponse]
}
+case class DiffSummaryResponse(
+ fromRev: Long,
+ toRev: Long,
+ summary: Map[String, Int],
+ total: Int
+)
+
+object DiffSummaryResponse {
+ implicit val encoder: Encoder[DiffSummaryResponse] = deriveEncoder[DiffSummaryResponse]
+}
+
class DiffRoutes(queryApi: GraphQueryApi) {
val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
case req @ POST -> Root / "v1" / "diff" =>
(for {
- body <- req.as[DiffRequest]
- _ <- IO.raiseWhen(body.fromRev >= body.toRev)(
+ body <- req.as[DiffRequest]
+ _ <- IO.raiseWhen(body.fromRev >= body.toRev)(
new IllegalArgumentException("fromRev must be less than toRev")
)
- changes <- body.entityId match {
- case Some(eidStr) =>
- // Diff a single entity
- for {
- eid <- IO.fromTry(scala.util.Try(UUID.fromString(eidStr))).map(NodeId(_))
- result <- diffEntity(eid, Rev(body.fromRev), Rev(body.toRev))
- } yield result.toVector
- case None =>
- queryApi.getChangedEntities(Rev(body.fromRev), Rev(body.toRev)).map { pairs =>
- pairs.map { case (atTo, atFromOpt) =>
- val changeType = if (atFromOpt.isEmpty) "added"
- else if (atTo.deletedRev.isDefined) "removed"
- else "modified"
- val summary = atFromOpt.map { atFrom =>
- diffAttrs(atFrom.attrs, atTo.attrs)
+ resp <- if (body.summary.getOrElse(false)) {
+ for {
+ counts <- queryApi.getDiffSummary(Rev(body.fromRev), Rev(body.toRev))
+ r <- Ok(DiffSummaryResponse(body.fromRev, body.toRev, counts, counts.values.sum))
+ } yield r
+ } else {
+ val effectiveLimit = body.limit.getOrElse(100)
+ val changesIO = body.entityId match {
+ case Some(eidStr) =>
+ for {
+ eid <- IO.fromTry(scala.util.Try(UUID.fromString(eidStr))).map(NodeId(_))
+ result <- diffEntity(eid, Rev(body.fromRev), Rev(body.toRev))
+ } yield result.toVector
+ case None =>
+ queryApi.getChangedEntities(Rev(body.fromRev), Rev(body.toRev)).map { pairs =>
+ pairs.map { case (atTo, atFromOpt) =>
+ val changeType = if (atFromOpt.isEmpty) "added"
+ else if (atTo.deletedRev.isDefined) "removed"
+ else "modified"
+ val attrSummary = atFromOpt.map { atFrom =>
+ diffAttrs(atFrom.attrs, atTo.attrs)
+ }
+ DiffEntry(
+ entityId = atTo.id,
+ changeType = changeType,
+ atFromRev = atFromOpt,
+ atToRev = Some(atTo),
+ summary = attrSummary.filter(_.nonEmpty)
+ )
}
- DiffEntry(
- entityId = atTo.id,
- changeType = changeType,
- atFromRev = atFromOpt,
- atToRev = Some(atTo),
- summary = summary.filter(_.nonEmpty)
- )
}
- }
+ }
+ for {
+ allChanges <- changesIO
+ totalCount = allChanges.size
+ truncated = totalCount > effectiveLimit
+ limited = if (truncated) allChanges.take(effectiveLimit) else allChanges
+ r <- Ok(DiffResponse(body.fromRev, body.toRev, limited, truncated, totalCount))
+ } yield r
}
- resp <- Ok(DiffResponse(body.fromRev, body.toRev, changes))
} yield resp).handleErrorWith(ErrorHandler.handle(_))
}