WIP: Submit transcoding jobs via a HTTP API #6
@ -70,22 +70,22 @@ public class JDBCTranscriptionRepository implements TranscriptionRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getFiles(Transcription transcription) {
|
public List<SourceFile> getFiles(Transcription transcription) {
|
||||||
return jdbcClient.sql("""
|
return jdbcClient.sql("""
|
||||||
SELECT filename
|
SELECT id, filename, transcribed_result_uri
|
||||||
FROM transcriptions_files
|
FROM transcriptions_files
|
||||||
WHERE transcription_id = :transcription_id
|
WHERE transcription_id = :transcription_id
|
||||||
""")
|
""")
|
||||||
.param("transcription_id", transcription.id())
|
.param("transcription_id", transcription.id())
|
||||||
.query(String.class)
|
.query(SourceFileRowMapper.INSTANCE)
|
||||||
.list();
|
.list();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createNewJob(Transcription transcription, Job job) {
|
public void createNewJob(Transcription transcription, Job job) {
|
||||||
jdbcClient.sql("""
|
jdbcClient.sql("""
|
||||||
INSERT INTO jobs (id, transcription_id, result_file_absolute_path, error_message)
|
INSERT INTO jobs (id, transcription_id, result_file_absolute_path, error_message, source_file_id)
|
||||||
VALUES (:id, :transcription_id, :result_file, :error_message)
|
VALUES (:id, :transcription_id, :result_file, :error_message, :source_file_id)
|
||||||
""")
|
""")
|
||||||
.param("id", job.id())
|
.param("id", job.id())
|
||||||
.param("transcription_id", transcription.id())
|
.param("transcription_id", transcription.id())
|
||||||
@ -98,15 +98,17 @@ public class JDBCTranscriptionRepository implements TranscriptionRepository {
|
|||||||
case Job.Status.Completed(JobCompletion.Failure(String errorMessage)) -> errorMessage;
|
case Job.Status.Completed(JobCompletion.Failure(String errorMessage)) -> errorMessage;
|
||||||
default -> null;
|
default -> null;
|
||||||
})
|
})
|
||||||
|
.param("source_file_id", job.sourceFile().id())
|
||||||
.update();
|
.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Job> findJobById(UUID jobId) {
|
public Optional<Job> findJobById(UUID jobId) {
|
||||||
return jdbcClient.sql("""
|
return jdbcClient.sql("""
|
||||||
SELECT id, result_file_absolute_path, error_message
|
SELECT jobs.id as jobId, result_file_absolute_path, error_message, tf.id, tf.filename, tf.transcribed_result_uri
|
||||||
FROM jobs
|
FROM jobs
|
||||||
WHERE id = :id
|
INNER JOIN transcriptions_files tf on jobs.source_file_id = tf.id
|
||||||
|
WHERE jobs.id = :id
|
||||||
""")
|
""")
|
||||||
.param("id", jobId)
|
.param("id", jobId)
|
||||||
.query(JobRowMapper.INSTANCE)
|
.query(JobRowMapper.INSTANCE)
|
||||||
@ -153,9 +155,10 @@ public class JDBCTranscriptionRepository implements TranscriptionRepository {
|
|||||||
@Override
|
@Override
|
||||||
public List<Job> getJobs(Transcription transcription) {
|
public List<Job> getJobs(Transcription transcription) {
|
||||||
return jdbcClient.sql("""
|
return jdbcClient.sql("""
|
||||||
SELECT id, result_file_absolute_path, error_message
|
SELECT jobs.id as jobId, result_file_absolute_path, error_message, tf.id, tf.filename, tf.transcribed_result_uri
|
||||||
FROM jobs
|
FROM jobs
|
||||||
WHERE transcription_id = :transcription_id
|
INNER JOIN transcriptions_files tf on jobs.source_file_id = tf.id
|
||||||
|
WHERE jobs.transcription_id = :transcription_id
|
||||||
""")
|
""")
|
||||||
.param("transcription_id", transcription.id())
|
.param("transcription_id", transcription.id())
|
||||||
.query(JobRowMapper.INSTANCE)
|
.query(JobRowMapper.INSTANCE)
|
||||||
@ -225,8 +228,9 @@ public class JDBCTranscriptionRepository implements TranscriptionRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Job mapRow(ResultSet rs, int rowNum) throws SQLException {
|
public Job mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||||
UUID id = UUID.fromString(rs.getString("id"));
|
SourceFile sourceFile = SourceFileRowMapper.INSTANCE.mapRow(rs, rowNum);
|
||||||
return new Job(id, getStatus(rs));
|
UUID id = UUID.fromString(rs.getString("jobId"));
|
||||||
|
return new Job(id, getStatus(rs), sourceFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Job.Status getStatus(ResultSet rs) throws SQLException {
|
private Job.Status getStatus(ResultSet rs) throws SQLException {
|
||||||
@ -246,4 +250,18 @@ public class JDBCTranscriptionRepository implements TranscriptionRepository {
|
|||||||
|
|
||||||
record SimplePrincipal(String getName) implements Principal {
|
record SimplePrincipal(String getName) implements Principal {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum SourceFileRowMapper implements RowMapper<SourceFile> {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SourceFile mapRow(ResultSet rs, int rowNum)
|
||||||
|
throws SQLException
|
||||||
|
{
|
||||||
|
UUID id = UUID.fromString(rs.getString("id"));
|
||||||
|
String filename = rs.getString("filename");
|
||||||
|
URI downloadUri = URI.create(rs.getString("transcribed_result_uri"));
|
||||||
|
return new SourceFile(id, filename, downloadUri);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package se.su.dsv.whisperapi.core;
|
|||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public record Job(UUID id, Status status) {
|
public record Job(UUID id, Status status, SourceFile sourceFile) {
|
||||||
public sealed interface Status {
|
public sealed interface Status {
|
||||||
record Pending() implements Status {}
|
record Pending() implements Status {}
|
||||||
record Completed(JobCompletion completion) implements Status {}
|
record Completed(JobCompletion completion) implements Status {}
|
||||||
|
@ -14,10 +14,10 @@ public interface TranscriptionRepository {
|
|||||||
void addFileToTranscription(Transcription transcription, SourceFile sourceFile);
|
void addFileToTranscription(Transcription transcription, SourceFile sourceFile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the list of filenames that have been {@link #addFileToTranscription added}
|
* @return all the files that have been {@link #addFileToTranscription added}
|
||||||
* to the transcription.
|
* to the transcription.
|
||||||
*/
|
*/
|
||||||
List<String> getFiles(Transcription transcription);
|
List<SourceFile> getFiles(Transcription transcription);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param transcription the transcription this specific job is a part of
|
* @param transcription the transcription this specific job is a part of
|
||||||
|
@ -72,10 +72,10 @@ public class TranscriptionService {
|
|||||||
public void submitTranscriptionJob(Transcription transcription, CallbackUriGenerator callbackUriGenerator) {
|
public void submitTranscriptionJob(Transcription transcription, CallbackUriGenerator callbackUriGenerator) {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||||
List<String> filenames = transcriptionRepository.getFiles(transcription);
|
List<SourceFile> files = transcriptionRepository.getFiles(transcription);
|
||||||
for (String filename : filenames) {
|
for (SourceFile file : files) {
|
||||||
UUID jobId = UUID.randomUUID();
|
UUID jobId = UUID.randomUUID();
|
||||||
Path fileToBeTranscribed = fileDirectory.resolve(transcription.id().toString()).resolve(filename);
|
Path fileToBeTranscribed = fileDirectory.resolve(transcription.id().toString()).resolve(file.id().toString());
|
||||||
Path jobFile = jobsDirectory.resolve(jobId + ".json");
|
Path jobFile = jobsDirectory.resolve(jobId + ".json");
|
||||||
URI callbackUri = callbackUriGenerator.generateCallbackUri(jobId);
|
URI callbackUri = callbackUriGenerator.generateCallbackUri(jobId);
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ public class TranscriptionService {
|
|||||||
|
|
||||||
try (var out = Files.newOutputStream(jobFile, StandardOpenOption.CREATE_NEW)) {
|
try (var out = Files.newOutputStream(jobFile, StandardOpenOption.CREATE_NEW)) {
|
||||||
objectMapper.writeValue(out, whisperJob);
|
objectMapper.writeValue(out, whisperJob);
|
||||||
Job job = new Job(jobId, new Job.Status.Pending());
|
Job job = new Job(jobId, new Job.Status.Pending(), file);
|
||||||
transcriptionRepository.createNewJob(transcription, job);
|
transcriptionRepository.createNewJob(transcription, job);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
@ -158,12 +158,13 @@ public class TranscriptionService {
|
|||||||
URI callbackUri = transcription.callbackUri();
|
URI callbackUri = transcription.callbackUri();
|
||||||
List<Callback.File> files = jobs.stream()
|
List<Callback.File> files = jobs.stream()
|
||||||
.<Callback.File>map(job -> {
|
.<Callback.File>map(job -> {
|
||||||
|
SourceFile sourceFile = job.sourceFile();
|
||||||
switch (job.status()) {
|
switch (job.status()) {
|
||||||
case Job.Status.Completed(JobCompletion.Success ignored) -> {
|
case Job.Status.Completed(JobCompletion.Success ignored) -> {
|
||||||
return new Callback.File.Transcribed("<unknown>", "http://"); //job.transcribedDownloadLink());
|
return new Callback.File.Transcribed(sourceFile.filename(), sourceFile.downloadUri().toString());
|
||||||
}
|
}
|
||||||
case Job.Status.Completed(JobCompletion.Failure(String errorMessage)) -> {
|
case Job.Status.Completed(JobCompletion.Failure(String errorMessage)) -> {
|
||||||
return new Callback.File.Failed("<unknown>", errorMessage);
|
return new Callback.File.Failed(sourceFile.filename(), errorMessage);
|
||||||
}
|
}
|
||||||
case Job.Status.Pending() -> throw new IllegalStateException("Job should be completed");
|
case Job.Status.Pending() -> throw new IllegalStateException("Job should be completed");
|
||||||
}
|
}
|
||||||
@ -175,11 +176,7 @@ public class TranscriptionService {
|
|||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(callbackUri)
|
.uri(callbackUri)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString("""
|
.POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(callback)))
|
||||||
{
|
|
||||||
"id": "%s",
|
|
||||||
"status": "completed"
|
|
||||||
}"""))
|
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
return response.statusCode() == 200;
|
return response.statusCode() == 200;
|
||||||
|
6
src/main/resources/db/migration/V6__job_source_file.sql
Normal file
6
src/main/resources/db/migration/V6__job_source_file.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
ALTER TABLE `jobs`
|
||||||
|
ADD COLUMN `source_file_id` UUID NOT NULL;
|
||||||
|
|
||||||
|
ALTER TABLE `jobs`
|
||||||
|
ADD CONSTRAINT `FK_jobs_transcription_files_source_file`
|
||||||
|
FOREIGN KEY (`source_file_id`) REFERENCES `transcriptions_files` (`id`);
|
Loading…
x
Reference in New Issue
Block a user