Skip to content

Commit

Permalink
feat: add query to response and cache queries
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Sep 27, 2023
1 parent 405f731 commit 8dc15f8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
17 changes: 17 additions & 0 deletions interweb-core/src/main/java/de/l3s/interweb/core/Results.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({ "elapsed_time", "query", "results" })
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Results<T extends ConnectorResults> {
/**
* Time in ms that the request took to complete.
*/
@JsonProperty("elapsed_time")
private long elapsedTime;

/**
* The query that was used to generate the results.
*/
@JsonProperty("query")
private Query query;

/**
* The list of results from different connectors.
*/
Expand All @@ -27,6 +36,14 @@ public Results(Collection<T> results) {
add(results);
}

public Query getQuery() {
return query;
}

public void setQuery(Query query) {
this.query = query;
}

public long getElapsedTime() {
return elapsedTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import de.l3s.interweb.core.util.StringUtils;

@JsonPropertyOrder({ "rank", "id", "type", "url", "title", "description", "author", "author_url" })
@RegisterForReflection
public class SearchItem implements Serializable {
@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import de.l3s.interweb.core.Query;

@RegisterForReflection
@JsonPropertyOrder({ "id", "query", "services", "content_types", "extras", "date_from", "date_to", "page", "per_page", "lang", "sort", "timeout" })
@JsonIgnoreProperties(ignoreUnknown = false)
public class SearchQuery extends Query {
private String id;
Expand Down Expand Up @@ -141,6 +143,7 @@ public void setPerPage(final Integer perPage) {
this.perPage = perPage;
}

@JsonIgnore
public int getOffset() {
if (page == null || perPage == null) {
return 0;
Expand Down Expand Up @@ -196,13 +199,12 @@ public boolean equals(final Object o) {
&& Objects.equals(extras, query1.extras);
}


@Override
public int hashCode() {
return Objects.hash(query, dateFrom, dateTo, language, getServices(), contentTypes, extras, page, perPage, sort);
return Objects.hash(query, dateFrom, dateTo, language, contentTypes, extras, page, perPage, sort, getServices());
}

public int hashCodeWithoutPage() {
return Objects.hash(query, dateFrom, dateTo, language, getServices(), contentTypes, extras, perPage, sort);
return Objects.hash(query, dateFrom, dateTo, language, contentTypes, extras, perPage, sort, getServices());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public Uni<CompletionResults> completions(@Valid CompletionQuery query) {
return getOrCreateChat(query).flatMap(chat -> {
long start = System.currentTimeMillis();
return chatService.completions(query).call(results -> {
results.setElapsedTime(System.currentTimeMillis() - start);
results.setChatId(chat.id);
results.setElapsedTime(System.currentTimeMillis() - start);
return persistMessages(chat, query.getMessages(), results);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

import io.quarkus.cache.CacheResult;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
import org.jboss.resteasy.reactive.RestQuery;
Expand Down Expand Up @@ -42,6 +43,7 @@ public Uni<DescribeResults> describe(@RestQuery String link,

@POST
@Authenticated
@CacheResult(cacheName = "describe")
public Uni<DescribeResults> describe(@Valid DescribeQuery query) {
describeService.validateServices(query.getServices());
if (StringUtils.isEmpty(query.getLink()) && (StringUtils.isEmpty(query.getId()) || query.getServices().isEmpty())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import jakarta.inject.Inject;
import jakarta.validation.Valid;
Expand All @@ -15,6 +16,7 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context;

import io.quarkus.cache.CacheResult;
import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
Expand Down Expand Up @@ -63,9 +65,13 @@ public Uni<SearchResults> search(@Parameter(description = "The search query", ex

@POST
@Authenticated
@CacheResult(cacheName = "search")
public Uni<SearchResults> search(@NotNull @Valid SearchQuery query) {
query.setId(UUID.randomUUID().toString());

long start = System.currentTimeMillis();
return searchService.search(query).map(results -> {
results.setQuery(query);
results.setElapsedTime(System.currentTimeMillis() - start);
return results;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

import io.quarkus.cache.CacheResult;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
import org.jboss.resteasy.reactive.RestQuery;
Expand Down Expand Up @@ -45,10 +46,12 @@ public Uni<SuggestResults> suggest(@NotEmpty @RestQuery String q,

@POST
@Authenticated
@CacheResult(cacheName = "suggest")
public Uni<SuggestResults> suggest(@Valid SuggestQuery query) {
long start = System.currentTimeMillis();
suggestService.validateServices(query.getServices());
return suggestService.suggest(query).map(results -> {
results.setQuery(query);
results.setElapsedTime(System.currentTimeMillis() - start);
return results;
});
Expand Down

0 comments on commit 8dc15f8

Please sign in to comment.