Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: download operators result #3241

Merged
merged 24 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package edu.uci.ics.texera.web.model.websocket.request

case class ResultExportRequest(
exportType: String,
exportType: String, // e.g. "csv", "google_sheet", "arrow", "data"
workflowId: Int,
workflowName: String,
operatorId: String,
operatorName: String,
operatorIds: List[String], // changed from single operatorId: String -> List of strings
datasetIds: List[Int],
rowIndex: Int,
columnIndex: Int,
filename: String
rowIndex: Int, // used by "data" export
columnIndex: Int, // used by "data" export
filename: String, // optional filename override
destination: String // "dataset" or "local"
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package edu.uci.ics.texera.web.model.websocket.response

import edu.uci.ics.texera.web.model.websocket.event.TexeraWebSocketEvent

case class ResultExportResponse(status: String, message: String) extends TexeraWebSocketEvent
case class ResultExportResponse(status: String, message: String)
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import edu.uci.ics.texera.web.service.ResultExportService
import io.dropwizard.auth.Auth

import javax.ws.rs._
import javax.ws.rs.core.Response
import javax.ws.rs.core.{MediaType, Response}
import javax.ws.rs.core.Response.Status
import scala.jdk.CollectionConverters._

@Path("/result")
@Produces(Array(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM))
class ResultResource extends LazyLogging {

@POST
Expand All @@ -22,21 +24,73 @@ class ResultResource extends LazyLogging {
@Auth user: SessionUser
): Response = {

if (request.operatorIds.size <= 0)
Response
.status(Response.Status.BAD_REQUEST)
.`type`(MediaType.APPLICATION_JSON)
.entity(Map("error" -> "No operator selected").asJava)
.build()

try {
val resultExportService = new ResultExportService(WorkflowIdentity(request.workflowId))
request.destination match {
case "local" =>
// CASE A: multiple operators => produce ZIP
if (request.operatorIds.size > 1) {
val resultExportService = new ResultExportService(WorkflowIdentity(request.workflowId))
val (zipStream, zipFileNameOpt) =
resultExportService.exportOperatorsAsZip(user.user, request)

if (zipStream == null) {
throw new RuntimeException("Zip stream is null")
}

val finalFileName = zipFileNameOpt.getOrElse("operators.zip")
return Response
.ok(zipStream, "application/zip")
.header("Content-Disposition", "attachment; filename=\"" + finalFileName + "\"")
.build()
}

val exportResponse: ResultExportResponse =
resultExportService.exportResult(user.user, request)
// CASE B: exactly one operator => single file
if (request.operatorIds.size != 1) {
return Response
.status(Response.Status.BAD_REQUEST)
.`type`(MediaType.APPLICATION_JSON)
.entity(Map("error" -> "Local download supports no operator or many.").asJava)
.build()
}
val singleOpId = request.operatorIds.head

Response.ok(exportResponse).build()
val resultExportService = new ResultExportService(WorkflowIdentity(request.workflowId))
val (streamingOutput, fileNameOpt) =
resultExportService.exportOperatorResultAsStream(request, singleOpId)

if (streamingOutput == null) {
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.`type`(MediaType.APPLICATION_JSON)
.entity(Map("error" -> "Failed to export operator").asJava)
.build()
}

val finalFileName = fileNameOpt.getOrElse("download.dat")
Response
.ok(streamingOutput, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + finalFileName + "\"")
.build()
case _ =>
// destination = "dataset" by default
val resultExportService = new ResultExportService(WorkflowIdentity(request.workflowId))
val exportResponse = resultExportService.exportResult(user.user, request)
Response.ok(exportResponse).build()
}
} catch {
case ex: Exception =>
Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.`type`(MediaType.APPLICATION_JSON)
.entity(Map("error" -> ex.getMessage).asJava)
.build()
}
}

}
Loading
Loading