Skip to content

Commit

Permalink
fix(jdbc): fix template repository
Browse files Browse the repository at this point in the history
close #630
  • Loading branch information
tchiotludo committed Jun 24, 2022
1 parent 35cfea0 commit 5fb86dd
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;

public interface TemplateRepositoryInterface {
Optional<Template> findById(String namespace, String id);

List<Template> findAll();

ArrayListTotal<Template> find(String query, Pageable pageable);
ArrayListTotal<Template> find(
Pageable pageable,
@Nullable String query,
@Nullable String namespace
);

List<Template> findByNamespace(String namespace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@MicronautTest
public class MysqlTemplateRepositoryTest extends AbstractJdbcTemplateRepositoryTest {
@Test
@Disabled("TODO: Seems to have issue with autocommit on mysql ?")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.kestra.repository.postgres;

import io.kestra.jdbc.repository.AbstractJdbcTemplateRepositoryTest;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;

@MicronautTest
public class PostgresTemplateRepositoryTest extends AbstractJdbcTemplateRepositoryTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.validation.ConstraintViolationException;

@Singleton
Expand Down Expand Up @@ -66,7 +67,11 @@ public List<Template> findAll() {

abstract protected Condition findCondition(String query);

public ArrayListTotal<Template> find(String query, Pageable pageable) {
public ArrayListTotal<Template> find(
Pageable pageable,
@Nullable String query,
@Nullable String namespace
) {
return this.jdbcRepository
.getDslContextWrapper()
.transactionResult(configuration -> {
Expand All @@ -84,6 +89,10 @@ public ArrayListTotal<Template> find(String query, Pageable pageable) {
select.and(this.findCondition(query));
}

if (namespace != null) {
select.and(field("namespace").likeIgnoreCase(namespace + "%"));
}

return this.jdbcRepository.fetchPage(context, select, pageable);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.Sort;
import jakarta.inject.Inject;
import org.jooq.DSLContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -26,13 +24,13 @@ void find() {
templateRepository.create(builder("io.kestra.unitest").build());
templateRepository.create(builder("com.kestra.test").build());

List<Template> save = templateRepository.find(null, Pageable.from(1, 10, Sort.UNSORTED));
List<Template> save = templateRepository.find(Pageable.from(1, 10, Sort.UNSORTED), null, null);
assertThat(save.size(), is(2));

save = templateRepository.find("kestra", Pageable.from(1, 10, Sort.UNSORTED));
assertThat(save.size(), is(2));
save = templateRepository.find(Pageable.from(1, 10, Sort.UNSORTED), "kestra", "com");
assertThat(save.size(), is(1));

save = templateRepository.find("kestra unit", Pageable.from(1, 10, Sort.of(Sort.Order.asc("id"))));
save = templateRepository.find(Pageable.from(1, 10, Sort.of(Sort.Order.asc("id"))), "kestra unit", null);
assertThat(save.size(), is(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
import io.kestra.core.utils.ExecutorsUtils;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.data.model.Pageable;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.builder.SearchSourceBuilder;

import java.util.List;
import java.util.Optional;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import javax.annotation.Nullable;
import javax.validation.ConstraintViolationException;

@Singleton
Expand Down Expand Up @@ -94,8 +95,25 @@ public List<Template> findAll() {
}

@Override
public ArrayListTotal<Template> find(String query, Pageable pageable) {
return super.findQueryString(INDEX_NAME, query, pageable);
public ArrayListTotal<Template> find(
Pageable pageable,
@Nullable String query,
@Nullable String namespace
) {
BoolQueryBuilder bool = this.defaultFilter();

if (query != null) {
bool.must(queryString(query).field("*.fulltext"));
}

if (namespace != null) {
bool.must(QueryBuilders.prefixQuery("namespace", namespace));
}


SearchSourceBuilder sourceBuilder = this.searchSource(bool, Optional.empty(), pageable);

return this.query(INDEX_NAME, sourceBuilder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<Template> findAll() {
}

@Override
public ArrayListTotal<Template> find(String query, Pageable pageable) {
public ArrayListTotal<Template> find(Pageable pageable, String query, String namespace) {
if (pageable.getNumber() < 1) {
throw new ValueException("Page cannot be < 1");
}
Expand Down
22 changes: 6 additions & 16 deletions ui/src/components/templates/Templates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
import DataTable from "../layout/DataTable";
import SearchField from "../layout/SearchField";
import Kicon from "../Kicon"
import qb from "../../utils/queryBuilder";
import RestoreUrl from "../../mixins/restoreUrl";
import _merge from "lodash/merge";
export default {
mixins: [RouteContext, RestoreUrl, DataTableActions],
Expand Down Expand Up @@ -138,28 +138,18 @@
},
},
methods: {
loadQuery() {
let filter = []
let query = this.queryWithFilter();
loadQuery(base) {
let queryFilter = this.queryWithFilter();
if (query.namespace) {
filter.push(`namespace:${query.namespace}*`)
}
if (query.q) {
filter.push(qb.toLucene(query.q));
}
return filter.join(" AND ") || "*"
return _merge(base, queryFilter)
},
loadData(callback) {
this.$store
.dispatch("template/findTemplates", {
q: this.loadQuery(),
.dispatch("template/findTemplates", this.loadQuery({
size: parseInt(this.$route.query.size || 25),
page: parseInt(this.$route.query.page || 1),
sort: this.$route.query.sort || "id:asc",
})
}))
.then(() => {
callback();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public Template index(
@Get(uri = "/search", produces = MediaType.TEXT_JSON)
@Operation(tags = {"Template"}, summary = "Search for templates")
public PagedResults<Template> find(
@Parameter(description = "Lucene string filter") @QueryValue(value = "q") String query,
@Parameter(description = "The current page") @QueryValue(value = "page", defaultValue = "1") int page,
@Parameter(description = "The current page size") @QueryValue(value = "size", defaultValue = "10") int size,
@Parameter(description = "The sort of current page") @Nullable @QueryValue(value = "sort") List<String> sort
@Parameter(description = "The sort of current page") @Nullable @QueryValue(value = "sort") List<String> sort,
@Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query,
@Parameter(description = "A namespace filter prefix") @Nullable @QueryValue(value = "namespace") String namespace
) throws HttpStatusException {
return PagedResults.of(templateRepository.find(query, PageableUtils.from(page, size, sort)));
return PagedResults.of(templateRepository.find(PageableUtils.from(page, size, sort), query, namespace));
}

@ExecuteOn(TaskExecutors.IO)
Expand Down

0 comments on commit 5fb86dd

Please sign in to comment.