Refactored some odd decisions in the new code

This commit is contained in:
Risto Lahtela 2021-01-08 18:34:11 +02:00
parent fd673dee2b
commit 47b3921204
4 changed files with 59 additions and 45 deletions

View File

@ -67,7 +67,7 @@ public class QueryJSONResolver implements Resolver {
return Response.builder() return Response.builder()
.setMimeType(MimeType.JSON) .setMimeType(MimeType.JSON)
.setJSONContent(Maps.builder(String.class, Object.class) .setJSONContent(Maps.builder(String.class, Object.class)
.put("path", result.getResultPath(",")) .put("path", result.getResultPath())
.put("uuids", result.getResultUUIDs()) .put("uuids", result.getResultUUIDs())
.build()) .build())
.build(); .build();

View File

@ -16,10 +16,7 @@
*/ */
package com.djrapitops.plan.storage.database.queries.filter; package com.djrapitops.plan.storage.database.queries.filter;
import java.util.Collections; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/** /**
* Represents a query filter for /query page. * Represents a query filter for /query page.
@ -82,15 +79,26 @@ public interface Filter {
return currentUUIDs; return currentUUIDs;
} }
public StringBuilder getResultPath(String separator) { public List<ResultPath> getResultPath() {
StringBuilder builder; List<ResultPath> path = new ArrayList<>();
if (previous == null) {
// First Result in chain Result current = this;
builder = new StringBuilder(); while (current != null) {
} else { path.add(new ResultPath(current.filterKind, current.resultSize));
builder = previous.getResultPath(separator); current = current.previous;
} }
return builder.append(separator).append("-> ").append(filterKind).append(": ").append(resultSize);
return path;
}
}
class ResultPath {
final String kind;
final int size;
public ResultPath(String kind, int size) {
this.kind = kind;
this.size = size;
} }
} }
} }

View File

@ -18,9 +18,9 @@ package com.djrapitops.plan.storage.database.queries.filter.filters;
import com.djrapitops.plan.storage.database.queries.filter.Filter; import com.djrapitops.plan.storage.database.queries.filter.Filter;
import com.djrapitops.plan.storage.database.queries.filter.FilterQuery; import com.djrapitops.plan.storage.database.queries.filter.FilterQuery;
import org.apache.commons.lang3.StringUtils; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.List; import java.util.List;
public abstract class MultiOptionFilter implements Filter { public abstract class MultiOptionFilter implements Filter {
@ -32,10 +32,6 @@ public abstract class MultiOptionFilter implements Filter {
protected List<String> getSelected(FilterQuery query) { protected List<String> getSelected(FilterQuery query) {
String selected = query.get("selected").orElseThrow(IllegalArgumentException::new); String selected = query.get("selected").orElseThrow(IllegalArgumentException::new);
return Arrays.asList(deserializeOptions(selected)); return new Gson().fromJson(selected, new TypeToken<List<String>>() {}.getType());
}
private String[] deserializeOptions(String selected) {
return StringUtils.split(selected, ',');
} }
} }

View File

@ -15,8 +15,8 @@ class Filter {
return 'Unimplemented render function' return 'Unimplemented render function'
} }
updateParameters() { toObject() {
return {kind: this.kind}
} }
} }
@ -45,13 +45,17 @@ class MultipleChoiceFilter extends Filter {
return html; return html;
} }
updateParameters() { toObject() {
let selected = ""; let selected = [];
for (let option of document.querySelector('#' + filter.id + " select").selectedOptions) { for (let option of document.querySelector('#' + filter.id + " select").selectedOptions) {
selected += option.text + ','; selected.push(option.text);
}
selected = JSON.stringify(selected);
return {
kind: this.kind,
parameters: {selected}
} }
selected = selected.substr(0, selected.length - 1); // Remove trailing comma
this.parameters = {selected};
} }
} }
@ -92,10 +96,10 @@ class BetweenDateFilter extends Filter {
super(kind); super(kind);
this.id = id; this.id = id;
this.label = label; this.label = label;
this.afterDate = options.after[0]; this.dateAfter = options.after[0];
this.afterTime = options.after[1]; this.timeAfter = options.after[1];
this.beforeDate = options.before[0]; this.dateBefore = options.before[0];
this.beforeTime = options.before[1]; this.timeBefore = options.before[1];
} }
render(filterCount) { render(filterCount) {
@ -106,31 +110,34 @@ class BetweenDateFilter extends Filter {
`<div id="${id}" class="mt-2 input-group input-row">` + `<div id="${id}" class="mt-2 input-group input-row">` +
`<div class="col-3"><div class="input-group mb-2">` + `<div class="col-3"><div class="input-group mb-2">` +
`<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-calendar"></i></div></div>` + `<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-calendar"></i></div></div>` +
`<input id="${id}-afterdate" onkeyup="setFilterOption('${id}', '${id}-afterdate', 'afterDate', isValidDate, correctDate)" class="form-control" placeholder="${this.afterDate}" type="text">` + `<input id="${id}-afterdate" onkeyup="setFilterOption('${id}', '${id}-afterdate', 'dateAfter', isValidDate, correctDate)" class="form-control" placeholder="${this.dateAfter}" type="text">` +
`</div></div>` + `</div></div>` +
`<div class="col-2"><div class="input-group mb-2">` + `<div class="col-2"><div class="input-group mb-2">` +
`<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-clock"></i></div></div>` + `<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-clock"></i></div></div>` +
`<input id="${id}-aftertime" onkeyup="setFilterOption('${id}', '${id}-aftertime', 'afterTime', isValidTime, correctTime)" class="form-control" placeholder="${this.afterTime}" type="text">` + `<input id="${id}-aftertime" onkeyup="setFilterOption('${id}', '${id}-aftertime', 'timeAfter', isValidTime, correctTime)" class="form-control" placeholder="${this.timeAfter}" type="text">` +
`</div></div>` + `</div></div>` +
`<div class="col-auto"><label class="mt-2 mb-0" for="inlineFormCustomSelectPref">&</label></div>` + `<div class="col-auto"><label class="mt-2 mb-0" for="inlineFormCustomSelectPref">&</label></div>` +
`<div class="col-3"><div class="input-group mb-2">` + `<div class="col-3"><div class="input-group mb-2">` +
`<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-calendar"></i></div></div>` + `<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-calendar"></i></div></div>` +
`<input id="${id}-beforedate" onkeyup="setFilterOption('${id}', '${id}-beforedate', 'beforeDate', isValidDate, correctDate)" class="form-control" placeholder="${this.beforeDate}" type="text">` + `<input id="${id}-beforedate" onkeyup="setFilterOption('${id}', '${id}-beforedate', 'dateBefore', isValidDate, correctDate)" class="form-control" placeholder="${this.dateBefore}" type="text">` +
`</div></div>` + `</div></div>` +
`<div class="col-2"><div class="input-group mb-2">` + `<div class="col-2"><div class="input-group mb-2">` +
`<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-clock"></i></div></div>` + `<div class="input-group-prepend"><div class="input-group-text"><i class="far fa-clock"></i></div></div>` +
`<input id="${id}-beforetime" onkeyup="setFilterOption('${id}', '${id}-beforetime', 'beforeTime', isValidTime, correctTime)" class="form-control" placeholder="${this.beforeTime}" type="text">` + `<input id="${id}-beforetime" onkeyup="setFilterOption('${id}', '${id}-beforetime', 'timeBefore', isValidTime, correctTime)" class="form-control" placeholder="${this.timeBefore}" type="text">` +
`</div></div>` + `</div></div>` +
`</div>` `</div>`
); );
} }
updateParameters() { toObject() {
this.parameters = { return {
dateAfter: this.afterDate, kind: this.kind,
timeAfter: this.afterTime, parameters: {
dateBefore: this.beforeDate, dateAfter: this.dateAfter,
timeBefore: this.beforeTime timeAfter: this.timeAfter,
dateBefore: this.dateBefore,
timeBefore: this.timeBefore
}
} }
} }
} }
@ -245,11 +252,14 @@ function setFilterOption(
} }
function performQuery() { function performQuery() {
for (filter of filterQuery) filter.updateParameters(); const query = [];
for (filter of filterQuery) {
query.push(filter.toObject());
}
jsonRequest(`./v1/query?q=${encodeURIComponent(JSON.stringify(filterQuery))}`, function (json, error) { jsonRequest(`./v1/query?q=${encodeURIComponent(JSON.stringify(query))}`, function (json, error) {
console.log(filterQuery); console.log(filterQuery);
console.log(json); if (json) console.log(json);
console.error(error); if (error) console.error(error);
}); });
} }