Skip to content

Commit

Permalink
feat(ZNTA-1302): create JobStatusService; add more fields to JobStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Mar 2, 2017
1 parent fe907ca commit ad3d6d8
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 42 deletions.
144 changes: 138 additions & 6 deletions api/zanata-common-api/src/main/java/org/zanata/rest/dto/JobStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "jobStatus")
@XmlType(name = "jobStatusType")
// FIXME see also AsynchronousProcessResource.startSourceDocCreation() and ProcessStatus
public class JobStatus {
private @Nullable Long jobId;
private @Nullable String successMessage;
private @Nullable String errorMessage;
private Long jobId;
private String successMessage;
private String errorMessage;
private String username;
// ISO8601 dates:
private String startTime;
private String statusTime;
private String estimatedCompletionTime;
private int percentCompleted;
private long currentItem;
private long totalItems;
private boolean totalItemsIsEstimated;
private List<JobStatusMessage> messages = new ArrayList<>();

public JobStatus() {
}
Expand Down Expand Up @@ -57,8 +69,128 @@ public String getErrorMessage() {

@Override
public String toString() {
return "Job: uploadId=" + jobId
+ " successMessage=\"" + successMessage + "\" errorMessage=\""
+ errorMessage + "\".";
return "JobStatus{" +
"jobId=" + jobId +
", successMessage='" + successMessage + '\'' +
", errorMessage='" + errorMessage + '\'' +
", username='" + username + '\'' +
", startTime='" + startTime + '\'' +
", statusTime='" + statusTime + '\'' +
", estimatedCompletionTime='" + estimatedCompletionTime + '\'' +
", percentCompleted=" + percentCompleted +
", currentItem=" + currentItem +
", totalItems=" + totalItems +
", totalItemsIsEstimated=" + totalItemsIsEstimated +
", messages=" + messages +
'}';
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getStartTime() {
return startTime;
}

public void setStartTime(String startTime) {
this.startTime = startTime;
}

public String getStatusTime() {
return statusTime;
}

public void setStatusTime(String statusTime) {
this.statusTime = statusTime;
}

public String getEstimatedCompletionTime() {
return estimatedCompletionTime;
}

public void setEstimatedCompletionTime(String estimatedCompletionTime) {
this.estimatedCompletionTime = estimatedCompletionTime;
}

public int getPercentCompleted() {
return percentCompleted;
}

public void setPercentCompleted(int percentCompleted) {
this.percentCompleted = percentCompleted;
}

public long getCurrentItem() {
return currentItem;
}

public void setCurrentItem(long currentItem) {
this.currentItem = currentItem;
}

public long getTotalItems() {
return totalItems;
}

public void setTotalItems(long totalItems) {
this.totalItems = totalItems;
}

public boolean isTotalItemsIsEstimated() {
return totalItemsIsEstimated;
}

public void setTotalItemsIsEstimated(boolean totalItemsIsEstimated) {
this.totalItemsIsEstimated = totalItemsIsEstimated;
}

public List<JobStatusMessage> getMessages() {
return messages;
}

public void setMessages(
List<JobStatusMessage> messages) {
this.messages = messages;
}

@XmlType(name = "jobStatusMessage")
public static class JobStatusMessage {
private String time;
private String level;
private String text;

public JobStatusMessage() {
}
public JobStatusMessage(String time, String level, String text) {
this.time = time;
this.level = level;
this.text = text;
}

public String getTime() {
return time;
}

public String getLevel() {
return level;
}

public String getText() {
return text;
}

@Override
public String toString() {
return "JobStatusMessage{" +
"time='" + time + '\'' +
", level='" + level + '\'' +
", text='" + text + '\'' +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.rest.service;

import org.zanata.rest.dto.JobStatus;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
@Path("/job/{jobId}")
@Produces({ MediaType.APPLICATION_JSON })
public interface JobStatusResource {
/**
* Fetches the status of an asynchronous job (eg file upload)
* @param jobId id of job
* @return current status of the job
* @see JobStatus
*/
@GET
Response getJobStatus(@PathParam("jobId") long jobId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@
import org.zanata.common.ProjectType;
import org.zanata.rest.dto.JobStatus;

import static org.zanata.rest.service.SourceFileResource.SERVICE_PATH;

/**
* REST Interface for upload and download of source files.
* @author Sean Flanigan <a
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
@Path(SERVICE_PATH)
@Path("/proj/{projectSlug}/ver/{versionSlug}/document/source")
@Consumes({ MediaType.WILDCARD })
@Produces({ MediaType.WILDCARD })
public interface SourceFileResource extends RestResource {
String SERVICE_PATH = "/proj/{projectSlug}/ver/{versionSlug}/document/source";

/**
* Upload a source file (or file chunk) to Zanata. Allows breaking up files
Expand All @@ -66,7 +63,8 @@ public interface SourceFileResource extends RestResource {
* @param fileStream Contents of the file to be uploaded
* @param size size of the file in bytes (for sanity checking; use -1 if unknown)
* @param projectType A ProjectType used for mapping of file extensions.
* @return A message with information about the upload operation.
* @return A JobStatus with information about the upload operation.
* @see JobStatus
*/
@POST
@Consumes({ MediaType.WILDCARD })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@

import java.io.InputStream;

import static org.zanata.rest.service.TranslatedFileResource.SERVICE_PATH;

/**
* REST Interface for upload and download of translation files.
* @author Sean Flanigan <a
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
@Path(SERVICE_PATH)
@Path("/proj/{projectSlug}/ver/{versionSlug}/document/trans/{localeId}")
@Produces({ MediaType.WILDCARD })
@Consumes({ MediaType.WILDCARD })
public interface TranslatedFileResource extends RestResource {
String SERVICE_PATH = "/proj/{projectSlug}/ver/{versionSlug}/document/trans/{localeId}";

/**
* Upload a translation file (or file chunk) to Zanata. Allows breaking up files
Expand All @@ -66,7 +63,8 @@ public interface TranslatedFileResource extends RestResource {
* translations with the contents of the uploaded file.
* @param fileStream Contents of the file to be uploaded
* @param size size of the file in bytes (for sanity checking; use -1 if unknown)
* @return A message with information about the upload operation.
* @return A JobStatus with information about the upload operation.
* @see JobStatus
*/
@POST
@Produces({ MediaType.APPLICATION_JSON })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.rest.service;

import org.zanata.rest.dto.JobStatus;

import javax.ws.rs.core.Response;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
public class MockJobStatusResource implements JobStatusResource {
@Override
public Response getJobStatus(long jobId) {
String time = ZonedDateTime.now(ZoneId.systemDefault()).format(
DateTimeFormatter.ISO_INSTANT);
JobStatus jobStatus = new JobStatus(1L,
"Job complete for jobId: " + jobId);
jobStatus.setStartTime(time);
jobStatus.setStatusTime(time);
jobStatus.setPercentCompleted(100);
jobStatus.getMessages().add(new JobStatus.JobStatusMessage(
time, "INFO", "job complete"));
return Response.status(Response.Status.ACCEPTED).entity(jobStatus).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MockResourcesApplication extends Application {
ImmutableSet.Builder<Class<?>> builder = ImmutableSet.builder();
builder.add(
MockVersionResource.class,
MockJobStatusResource.class,
MockSourceDocResource.class,
MockTranslatedDocResource.class,
MockStatisticsResource.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand All @@ -49,11 +52,18 @@ public Response uploadSourceFile(String projectSlug, String versionSlug,
try {
long actual = IOUtils
.copyLarge(fileStream, new NullOutputStream());
return Response.status(Response.Status.CREATED).entity(
new JobStatus(1L, "Upload of new source document successful: (" +
String time = ZonedDateTime.now(ZoneId.systemDefault()).format(
DateTimeFormatter.ISO_INSTANT);
JobStatus jobStatus = new JobStatus(1L,
"Upload of new source document successful: (" +
actual + "/" + size + " bytes): " +
projectSlug + "/" + versionSlug + "/" + docId + ":" + projectType))
.build();
projectType + ":" + projectSlug + "/" +
versionSlug + "/" + docId);
jobStatus.setStartTime(time);
jobStatus.setStatusTime(time);
jobStatus.getMessages().add(new JobStatus.JobStatusMessage(
time, "INFO", "upload has been scheduled"));
return Response.status(Response.Status.ACCEPTED).entity(jobStatus).build();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand All @@ -48,11 +51,18 @@ public Response uploadTranslationFile(String projectSlug,
InputStream fileStream, long size, String projectType) {
try {
long actual = IOUtils.copyLarge(fileStream, new NullOutputStream());
return Response.ok(
new JobStatus(1L, "Upload of translation document successful (" +
String time = ZonedDateTime.now(ZoneId.systemDefault()).format(
DateTimeFormatter.ISO_INSTANT);
JobStatus jobStatus = new JobStatus(1L,
"Upload of translation document successful (" +
actual + "/" + size + " bytes): " +
projectSlug + "/" + iterationSlug + "/" + docId + ":" + projectType + ":" + localeId))
.build();
projectType + ":" + projectSlug + "/" +
iterationSlug + "/" + docId + ":" + localeId);
jobStatus.setStartTime(time);
jobStatus.setStatusTime(time);
jobStatus.getMessages().add(new JobStatus.JobStatusMessage(
time, "INFO", "upload has been scheduled"));
return Response.status(Response.Status.ACCEPTED).entity(jobStatus).build();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit ad3d6d8

Please sign in to comment.