Skip to content

Commit

Permalink
Second version, after review.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex0x08 committed Nov 3, 2023
1 parent 217077e commit d78798d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/main/java/ru/org/linux/search/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sksamuel.elastic4s.requests.searches.aggs.responses.FilterAggregationResult;
import com.sksamuel.elastic4s.requests.searches.aggs.responses.bucket.TermBucket;
import com.sksamuel.elastic4s.requests.searches.aggs.responses.bucket.Terms;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand Down Expand Up @@ -52,6 +53,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;

@Controller
public class SearchController {
Expand Down Expand Up @@ -107,7 +109,8 @@ public static Map<SearchRange, String> getRanges() {
public String search(
Model model,
@ModelAttribute("query") SearchRequest query,
BindingResult bindingResult
BindingResult bindingResult,
HttpServletRequest request
) {
Map<String, Object> params = model.asMap();

Expand All @@ -116,7 +119,8 @@ public String search(

SearchViewer sv = new SearchViewer(query, client);

SearchResponse response = sv.performSearch();
final DateTimeZone tz = (DateTimeZone)request.getAttribute("timezone");
SearchResponse response = sv.performSearch(tz);

long current = System.currentTimeMillis();

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/ru/org/linux/search/SearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2016 Linux.org.ru
* Copyright 1998-2023 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -15,6 +15,7 @@

package ru.org.linux.search;

import org.joda.time.DateTimeZone;
import ru.org.linux.search.SearchEnums.SearchInterval;
import ru.org.linux.search.SearchEnums.SearchRange;
import ru.org.linux.user.User;
Expand Down Expand Up @@ -163,19 +164,25 @@ public boolean isDateSelected() {
return dt >0;
}

public long atEndOfDaySelected() {
public long atEndOfDaySelected(DateTimeZone tz) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(dt));
if (tz!=null) {
calendar.setTimeZone(tz.toTimeZone());
}
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime().getTime();
}

public long atStartOfDaySelected() {
public long atStartOfDaySelected(DateTimeZone tz) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(dt));
if (tz!=null) {
calendar.setTimeZone(tz.toTimeZone());
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/ru/org/linux/reaction/ReactionDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,25 @@ class ReactionDao(ds: DataSource, val transactionManager: PlatformTransactionMan
reaction = rs.getString("reaction"))
}

def getReactionsView(originUser: User, offset: Int, size: Int,isReactionsOn: Boolean,isIncludeDeleted: Boolean): Seq[ReactionsView] =
def getReactionsView(originUser: User, offset: Int, size: Int,isReactionsOn: Boolean,includeDeleted: Boolean): Seq[ReactionsView] =
jdbcTemplate.queryAndMap(
if (isReactionsOn)
"WITH constants (selectedId) as ( values (?) ) " +
" select r.topic_id,r.comment_id,r.set_date, r.reaction,r.origin_user as \"target_user\", g.\"section\", g.urlname, t.title " +
" from reactions_log r " +
" join topics t ON r.topic_id = t.id " +
(if (!isIncludeDeleted) { " AND NOT t.deleted" } else "" ) +
(if (!includeDeleted) { " AND NOT t.deleted" } else "" ) +
" join groups g ON t.groupid = g.id " +
" WHERE r.comment_id is null and t.userid=(select selectedId from constants) " +
" UNION ALL " +
" select r.topic_id,r.comment_id,r.set_date, r.reaction, r.origin_user, g.\"section\", g.urlname, t.title " +
" from reactions_log r " +
" join topics t ON r.topic_id = t.id " +
(if (!isIncludeDeleted) { " AND NOT t.deleted" } else "") +
(if (!includeDeleted) { " AND NOT t.deleted" } else "") +
" JOIN comments c ON c.id = r.comment_id " +
" join groups g ON t.groupid = g.id " +
" WHERE c.userid=(select selectedId from constants) " +
(if (!isIncludeDeleted) { " AND NOT c.deleted" } else "") +
(if (!includeDeleted) { " AND NOT c.deleted" } else "") +
" order by set_date desc OFFSET ? LIMIT ?"
else
"SELECT topic_id, comment_id, set_date, reaction, topics.title, " +
Expand All @@ -156,7 +156,7 @@ class ReactionDao(ds: DataSource, val transactionManager: PlatformTransactionMan
"JOIN groups ON topics.groupid = groups.id " +
"LEFT JOIN comments ON comment_id = comments.id " +
"WHERE origin_user=? " +
(if (!isIncludeDeleted) { " AND NOT topics.deleted AND comments.deleted IS NOT TRUE " } else "") +
(if (!includeDeleted) { " AND NOT topics.deleted AND comments.deleted IS NOT TRUE " } else "") +
" ORDER BY set_date DESC OFFSET ? LIMIT ?",
originUser.getId, offset, size) { case (rs, _) =>
ReactionsView(
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/ru/org/linux/search/SearchViewer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.requests.searches.SearchResponse
import com.sksamuel.elastic4s.requests.searches.queries.Query
import com.sksamuel.elastic4s.requests.searches.queries.funcscorer.WeightScore
import org.joda.time.DateTimeZone

import scala.concurrent.Await
import scala.concurrent.duration.*
Expand Down Expand Up @@ -54,13 +55,11 @@ class SearchViewer(query: SearchRequest, elastic: ElasticClient) {
}
}

def performSearch: SearchResponse = {
def performSearch(tz:DateTimeZone): SearchResponse = {
val typeFilter = Option(query.getRange.getValue) map { value =>
termQuery(query.getRange.getColumn, value)
}
val selectedDateFilter = Option(query) map { query =>
rangeQuery(query.getInterval.getColumn) gte query.atStartOfDaySelected() lte query.atEndOfDaySelected()
}
val selectedDateFilter = rangeQuery(query.getInterval.getColumn) gte query.atStartOfDaySelected(tz) lte query.atEndOfDaySelected(tz)

val dateFilter = Option(query.getInterval.getRange) map { range =>
rangeQuery(query.getInterval.getColumn) gt range
Expand All @@ -74,7 +73,7 @@ class SearchViewer(query: SearchRequest, elastic: ElasticClient) {
}
}

val queryFilters = (typeFilter ++ (if (query.isDateSelected) selectedDateFilter else dateFilter) ++ userFilter).toSeq
val queryFilters = (typeFilter ++ (if (query.isDateSelected) Option(selectedDateFilter) else dateFilter) ++ userFilter).toSeq

val esQuery = wrapQuery(boost(processQueryString(query.getQ)), queryFilters)

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/jsp/whois.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
filled: "{date}<br>сообщений: {count}"
},
onClick: function (date, nb) {
window.open('../search.jsp?dt='+date.getTime()+'&user=${user.nick}', '_blank');
window.open('/search.jsp?dt='+date.getTime()+'&user=${user.nick}', '_blank');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SearchResultServiceIntegrationSpec extends SpecificationWithJUnit {

"SearchResultsService" should {
"prepare some results" in new IndexFixture {
val response = new SearchViewer(new SearchRequest(), elastic).performSearch
val response = new SearchViewer(new SearchRequest(), elastic).performSearch(null)

val prepared = response.hits.hits.map(service.prepare)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SearchViewerIntegrationSpec extends SpecificationWithJUnit {

"SearchViewer" should {
"make valid default search" in new IndexFixture {
val response = new SearchViewer(new SearchRequest(), elastic).performSearch
val response = new SearchViewer(new SearchRequest(), elastic).performSearch(null)

response.totalHits must be equalTo 0
}
Expand Down

0 comments on commit d78798d

Please sign in to comment.