-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add API with queue history in CSV #166
base: master
Are you sure you want to change the base?
Changes from 1 commit
7a027b3
6f8da31
7bf19c4
7ca2fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package me.simplq.controller; | ||
|
||
import javax.validation.Valid; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.simplq.controller.model.queue.CreateQueueRequest; | ||
import me.simplq.controller.model.queue.CreateQueueResponse; | ||
|
@@ -9,6 +10,7 @@ | |
import me.simplq.controller.model.queue.PatchQueueResponse; | ||
import me.simplq.controller.model.queue.PauseQueueRequest; | ||
import me.simplq.controller.model.queue.QueueDetailsResponse; | ||
import me.simplq.controller.model.queue.QueueEventsCsvResponse; | ||
import me.simplq.controller.model.queue.QueueEventsResponse; | ||
import me.simplq.controller.model.queue.QueueStatusResponse; | ||
import me.simplq.controller.model.queue.UpdateQueueStatusResponse; | ||
|
@@ -22,6 +24,7 @@ | |
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
|
@@ -86,4 +89,13 @@ public ResponseEntity<QueueEventsResponse> getQueueEvents( | |
@PathVariable("queueId") String queueId) { | ||
return ResponseEntity.ok(queueService.getQueueEvents(queueId)); | ||
} | ||
|
||
@GetMapping(path = "/queue/{queueId}/history", produces = "text/csv") | ||
@ResponseBody | ||
public QueueEventsCsvResponse getQueueHistoryCSV( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename function to |
||
@PathVariable("queueId") String queueId) { | ||
return queueService.getQueueEventsInCsv(queueId); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package me.simplq.controller.converters; | ||
|
||
import me.simplq.controller.model.queue.QueueEventsCsvResponse; | ||
import me.simplq.controller.model.queue.QueueEventsResponse; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
import org.springframework.http.ContentDisposition; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.AbstractHttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Component | ||
public class QueueEventsCsvResponseConverter extends AbstractHttpMessageConverter<QueueEventsCsvResponse> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's write a converter for As per this, it will use the media type to determine the converter, this should work. Can you try? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are going to use
What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the Accept header if that works would be the best way, in my opinion.
Yeah, that would be desirable. Can you try adding the queue Id and name to |
||
|
||
private static final MediaType TEXT_CSV = new MediaType("text", "csv", StandardCharsets.UTF_8); | ||
|
||
QueueEventsCsvResponseConverter() { | ||
super(TEXT_CSV); | ||
} | ||
|
||
@Override | ||
protected boolean supports(Class<?> aClass) { | ||
return QueueEventsCsvResponse.class.equals(aClass); | ||
} | ||
|
||
@Override | ||
protected QueueEventsCsvResponse readInternal(Class<? extends QueueEventsCsvResponse> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { | ||
throw new HttpMessageNotReadableException("Reading history from CSV not supported", httpInputMessage); | ||
} | ||
|
||
@Override | ||
protected void writeInternal(QueueEventsCsvResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException { | ||
output.getHeaders().setContentType(TEXT_CSV); | ||
output.getHeaders().setContentDisposition(ContentDisposition.builder("attachment").filename(response.getFileName()).build()); | ||
CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(output.getBody()), CSVFormat.DEFAULT); | ||
for (QueueEventsResponse.Event event : response.getEventResponse().getEvents()) { | ||
printEvent(csvPrinter, event); | ||
} | ||
csvPrinter.flush(); | ||
csvPrinter.close(); | ||
} | ||
|
||
private void printEvent(CSVPrinter csvPrinter, QueueEventsResponse.Event event) throws IOException { | ||
csvPrinter.printRecord( | ||
event.getTokenNumber(), | ||
event.getTokenName(), | ||
event.getEventType(), | ||
event.getEventTimestamp() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.simplq.controller.model.queue; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class QueueEventsCsvResponse { | ||
String fileName; | ||
QueueEventsResponse eventResponse; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need
@ResponseBody
?