Skip to content

Commit

Permalink
add SSE responses to the sample Atlas controller (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Graff committed May 17, 2017
1 parent 6ed889d commit b7bd270
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,55 @@
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
* This is a temporary stand-in for the /fetch endpoint that will be exposed by OSS Atlas.
*/
@RestController
@RequestMapping("/api/v1/fetch")
@RequestMapping("/api/v2/fetch")
public class SampleAtlasController {

@Autowired
ResourceLoader resourceLoader;
private ResourceLoader resourceLoader;

@RequestMapping(method = RequestMethod.GET)
private static final Map<String, String> queryMap = createQueryMap();

private static Map<String, String> createQueryMap()
{
Map<String,String> temp = new HashMap<String,String>();

temp.put("nf.app,app_mocha,:eq,name,CpuRawSystem,:eq,:and,(,nf.cluster,),:by",
"com/netflix/kayenta/controllers/sample00.sse");

temp.put("nf.app,app_wesley,:eq,name,CpuRawSystem,:eq,:and",
"com/netflix/kayenta/controllers/sample01.sse");

temp.put("nf.app,app_leo,:eq,name,CpuRawSystem,:eq,:and,(,nf.node,),:by",
"com/netflix/kayenta/controllers/sample02.sse");

temp.put("nf.app,app_nyassa,:eq,name,CpuRawSystem,:eq,:and,(,nf.node,),:by",
"com/netflix/kayenta/controllers/sample03.sse");

return temp;
}

@RequestMapping(method = RequestMethod.GET, produces = "text/event-stream")
public String queryMetrics(@RequestParam final String q) throws IOException {
return getJsonContent("com/netflix/kayenta/controllers/sample-atlas-response.json");
return getSSEContent(queryMap.getOrDefault(q, "com/netflix/kayenta/controllers/empty.sse"));
}

private String getJsonContent(String jsonFilename) throws IOException {
try (InputStream jsonInputStream = resourceLoader.getResource("classpath:" + jsonFilename).getInputStream()) {
private String getSSEContent(String sseFilename) throws IOException {
try (InputStream jsonInputStream = resourceLoader.getResource("classpath:" + sseFilename).getInputStream()) {
return IOUtils.toString(jsonInputStream, Charsets.UTF_8.name());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data: {"type":"close","message":"operation complete"}

0 comments on commit b7bd270

Please sign in to comment.