use of dontweave.gson.JsonArray in project h2o-2 by h2oai.
the class TypeaheadFileRequest method serveHdfs.
protected JsonArray serveHdfs(String filter, int limit) {
JsonArray array = new JsonArray();
Configuration conf = PersistHdfs.CONF;
if (conf == null)
return array;
try {
Path p = new Path(filter);
Path expand = p;
if (!filter.endsWith("/"))
expand = p.getParent();
FileSystem fs = FileSystem.get(p.toUri(), conf);
for (FileStatus file : fs.listStatus(expand)) {
Path fp = file.getPath();
if (fp.toString().startsWith(p.toString())) {
array.add(new JsonPrimitive(fp.toString()));
}
if (array.size() == limit)
break;
}
} catch (Throwable xe) {
}
return array;
}
use of dontweave.gson.JsonArray in project h2o-2 by h2oai.
the class TypeaheadHdfsPathRequest method serve.
@Override
protected JsonArray serve(String filter, int limit) {
JsonArray array = new JsonArray();
Configuration conf = PersistHdfs.CONF;
if (conf == null)
return array;
try {
Path p = new Path(filter);
Path expand = p;
if (!filter.endsWith("/"))
expand = p.getParent();
FileSystem fs = FileSystem.get(p.toUri(), conf);
for (FileStatus file : fs.listStatus(expand)) {
Path fp = file.getPath();
if (fp.toString().startsWith(p.toString())) {
array.add(new JsonPrimitive(fp.toString()));
}
if (array.size() == limit)
break;
}
} catch (Throwable xe) {
}
return array;
}
use of dontweave.gson.JsonArray in project h2o-2 by h2oai.
the class TypeaheadRequest method serve.
@Override
protected final Response serve() {
JsonArray array = serve(_filter.value(), _limit.value());
JsonObject response = new JsonObject();
response.add(ITEMS, array);
return Response.done(response);
}
use of dontweave.gson.JsonArray in project h2o-2 by h2oai.
the class ConfusionMatrix method toJson.
public JsonArray toJson() {
JsonArray res = new JsonArray();
JsonArray header = new JsonArray();
header.add(new JsonPrimitive("Actual / Predicted"));
for (int i = 0; i < _arr.length; ++i) header.add(new JsonPrimitive("class " + i));
header.add(new JsonPrimitive("Error"));
res.add(header);
for (int i = 0; i < _arr.length; ++i) {
JsonArray row = new JsonArray();
row.add(new JsonPrimitive("class " + i));
long s = 0;
for (int j = 0; j < _arr.length; ++j) {
s += _arr[i][j];
row.add(new JsonPrimitive(_arr[i][j]));
}
double err = s - _arr[i][i];
err /= s;
row.add(new JsonPrimitive(err));
res.add(row);
}
JsonArray totals = new JsonArray();
totals.add(new JsonPrimitive("Totals"));
long S = 0;
long DS = 0;
for (int i = 0; i < _arr.length; ++i) {
long s = 0;
for (int j = 0; j < _arr.length; ++j) s += _arr[j][i];
totals.add(new JsonPrimitive(s));
S += s;
DS += _arr[i][i];
}
double err = (S - DS) / (double) S;
totals.add(new JsonPrimitive(err));
res.add(totals);
return res;
}
use of dontweave.gson.JsonArray in project h2o-2 by h2oai.
the class Jobs method serve.
@Override
protected Response serve() {
JsonObject result = new JsonObject();
JsonArray array = new JsonArray();
Job[] jobs = Job.all();
for (int i = jobs.length - 1; i >= 0; i--) {
JsonObject json = new JsonObject();
json.addProperty(KEY, jobs[i].self().toString());
json.addProperty(DESCRIPTION, jobs[i].description);
json.addProperty(DEST_KEY, jobs[i].dest() != null ? jobs[i].dest().toString() : "");
json.addProperty(START_TIME, RequestBuilders.ISO8601.get().format(new Date(jobs[i].start_time)));
long end = jobs[i].end_time;
JsonObject jobResult = new JsonObject();
Job job = jobs[i];
boolean cancelled;
if (cancelled = (job.state == JobState.CANCELLED || job.state == JobState.FAILED)) {
if (job.exception != null) {
jobResult.addProperty("exception", "1");
jobResult.addProperty("val", jobs[i].exception);
} else {
jobResult.addProperty("val", "CANCELLED");
}
} else if (job.state == JobState.DONE)
jobResult.addProperty("val", "OK");
json.addProperty(END_TIME, end == 0 ? "" : RequestBuilders.ISO8601.get().format(new Date(end)));
json.addProperty(PROGRESS, job.state == JobState.RUNNING || job.state == JobState.DONE ? jobs[i].progress() : -1);
json.addProperty(PROGRESS, end == 0 ? (cancelled ? -2 : jobs[i].progress()) : (cancelled ? -2 : -1));
json.addProperty(CANCELLED, cancelled);
json.add("result", jobResult);
array.add(json);
}
result.add(JOBS, array);
Response r = Response.done(result);
r.setBuilder(JOBS, new ArrayBuilder() {
@Override
public String caption(JsonArray array, String name) {
return "";
}
});
r.setBuilder(JOBS + "." + KEY, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
String html;
if (!Job.isRunning(Key.make(elm.getAsString())))
html = "<button disabled class='btn btn-mini'>X</button>";
else {
String keyParam = KEY + "=" + elm.getAsString();
html = "<a href='/Cancel.html?" + keyParam + "'><button class='btn btn-danger btn-mini'>X</button></a>";
}
return html;
}
});
r.setBuilder(JOBS + "." + DEST_KEY, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
String str = elm.getAsString();
String key = null;
try {
key = URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
key = str;
}
return ("".equals(key) || DKV.get(Key.make(str)) == null) ? key : Inspector.link(str, str);
}
});
r.setBuilder(JOBS + "." + START_TIME, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return date(elm.toString());
}
});
r.setBuilder(JOBS + "." + END_TIME, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return date(elm.toString());
}
});
r.setBuilder(JOBS + "." + PROGRESS, new ArrayRowElementBuilder() {
@Override
public String elementToString(JsonElement elm, String contextName) {
return progress(Float.parseFloat(elm.getAsString()));
}
});
r.setBuilder(JOBS + "." + "result", new ElementBuilder() {
@Override
public String objectToString(JsonObject obj, String contextName) {
if (obj.has("exception")) {
String rid = Key.make().toString();
String ex = obj.get("val").getAsString().replace("'", "");
String[] lines = ex.split("\n");
StringBuilder sb = new StringBuilder(lines[0]);
for (int i = 1; i < lines.length; ++i) {
sb.append("\\n" + lines[i]);
}
// ex = ex.substring(0,ex.indexOf('\n'));
ex = sb.toString();
String res = "\n<a onClick=\"" + "var showhide=document.getElementById('" + rid + "');" + "if(showhide.innerHTML == '') showhide.innerHTML = '<pre>" + ex + "</pre>';" + "else showhide.innerHTML = '';" + "\">FAILED</a>\n<div id='" + rid + "'></div>\n";
return res;
} else if (obj.has("val")) {
return obj.get("val").getAsString();
}
return "";
}
@Override
public String build(String elementContents, String elementName) {
return "<td>" + elementContents + "</td>";
}
});
return r;
}
Aggregations