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()
.setMimeType(MimeType.JSON)
.setJSONContent(Maps.builder(String.class, Object.class)
.put("path", result.getResultPath(","))
.put("path", result.getResultPath())
.put("uuids", result.getResultUUIDs())
.build())
.build();

View File

@ -16,10 +16,7 @@
*/
package com.djrapitops.plan.storage.database.queries.filter;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
/**
* Represents a query filter for /query page.
@ -82,15 +79,26 @@ public interface Filter {
return currentUUIDs;
}
public StringBuilder getResultPath(String separator) {
StringBuilder builder;
if (previous == null) {
// First Result in chain
builder = new StringBuilder();
} else {
builder = previous.getResultPath(separator);
public List<ResultPath> getResultPath() {
List<ResultPath> path = new ArrayList<>();
Result current = this;
while (current != null) {
path.add(new ResultPath(current.filterKind, current.resultSize));
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.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;
public abstract class MultiOptionFilter implements Filter {
@ -32,10 +32,6 @@ public abstract class MultiOptionFilter implements Filter {
protected List<String> getSelected(FilterQuery query) {
String selected = query.get("selected").orElseThrow(IllegalArgumentException::new);
return Arrays.asList(deserializeOptions(selected));
}
private String[] deserializeOptions(String selected) {
return StringUtils.split(selected, ',');
return new Gson().fromJson(selected, new TypeToken<List<String>>() {}.getType());
}
}

View File

@ -15,8 +15,8 @@ class Filter {
return 'Unimplemented render function'
}
updateParameters() {
toObject() {
return {kind: this.kind}
}
}
@ -45,13 +45,17 @@ class MultipleChoiceFilter extends Filter {
return html;
}
updateParameters() {
let selected = "";
toObject() {
let selected = [];
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);
this.id = id;
this.label = label;
this.afterDate = options.after[0];
this.afterTime = options.after[1];
this.beforeDate = options.before[0];
this.beforeTime = options.before[1];
this.dateAfter = options.after[0];
this.timeAfter = options.after[1];
this.dateBefore = options.before[0];
this.timeBefore = options.before[1];
}
render(filterCount) {
@ -106,31 +110,34 @@ class BetweenDateFilter extends Filter {
`<div id="${id}" class="mt-2 input-group input-row">` +
`<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>` +
`<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 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>` +
`<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 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="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 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>` +
`<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>`
);
}
updateParameters() {
this.parameters = {
dateAfter: this.afterDate,
timeAfter: this.afterTime,
dateBefore: this.beforeDate,
timeBefore: this.beforeTime
toObject() {
return {
kind: this.kind,
parameters: {
dateAfter: this.dateAfter,
timeAfter: this.timeAfter,
dateBefore: this.dateBefore,
timeBefore: this.timeBefore
}
}
}
}
@ -245,11 +252,14 @@ function setFilterOption(
}
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(json);
console.error(error);
if (json) console.log(json);
if (error) console.error(error);
});
}