Skip to content

Commit

Permalink
Fixed: Show distinct queries in search history on search page. See #515
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jan 25, 2017
1 parent 9cc22bf commit a9f1b6a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# NZB Hydra changelog

----------
### 0.2.188
Fixed: Show distinct queries in search history on search page. See [#515](https://github.com/theotherp/nzbhydra/issues/515).

### 0.2.187
Added: You can access and repeat the latest 20 manual searches from a button on the search screen. See [#515](https://github.com/theotherp/nzbhydra/issues/515).

Expand Down
25 changes: 15 additions & 10 deletions nzbhydra/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import print_function
from __future__ import unicode_literals

import calendar
import logging
from itertools import groupby

Expand All @@ -13,7 +12,7 @@
from peewee import fn, JOIN, SQL

from nzbhydra import database
from nzbhydra.database import Indexer, IndexerApiAccess, IndexerNzbDownload, IndexerSearch, Search, IndexerStatus, TvIdCache, MovieIdCache, SearchResult
from nzbhydra.database import Indexer, IndexerApiAccess, IndexerNzbDownload, Search, IndexerStatus, TvIdCache, MovieIdCache, SearchResult
from nzbhydra.exceptions import IndexerNotFoundException
from nzbhydra.indexers import getIndexerByName

Expand Down Expand Up @@ -45,6 +44,7 @@ def getStats(after=None, before=None):
"timeBasedSearchStats": getTimeBasedSearchStats(after, before)
}


def get_avg_indexer_response_times(after, before):
result = []
response_times = []
Expand Down Expand Up @@ -86,7 +86,8 @@ def get_avg_indexer_search_results_share(afterSql, beforeSql):
continue
innerSelect = """(SELECT ps.search_id
FROM indexersearch ps, search s
WHERE ps.indexer_id == %(id)d AND ps.search_id = s.id AND ps.successful AND (s.episode NOT NULL OR s.season NOT NULL OR s.identifier_key NOT NULL OR s.query NOT NULL)) AND ps.time > %(after)s and ps.time < %(before)s""" % {"id": p.id, "after": afterSql, "before": beforeSql}
WHERE ps.indexer_id == %(id)d AND ps.search_id = s.id AND ps.successful AND (s.episode NOT NULL OR s.season NOT NULL OR s.identifier_key NOT NULL OR s.query NOT NULL)) AND ps.time > %(after)s and ps.time < %(before)s""" % {"id": p.id, "after": afterSql,
"before": beforeSql}

result = database.db.execute_sql(
"""
Expand Down Expand Up @@ -211,7 +212,7 @@ def get_avg_indexer_access_success(afterSql, beforeSql):
def getTimeBasedDownloadStats(after, before):
downloads = IndexerNzbDownload(). \
select(Indexer.name, IndexerApiAccess.response_successful, IndexerNzbDownload.time). \
where((IndexerApiAccess.time > after) & (IndexerApiAccess.time < before)) .\
where((IndexerApiAccess.time > after) & (IndexerApiAccess.time < before)). \
join(IndexerApiAccess, JOIN.LEFT_OUTER). \
join(Indexer, JOIN.LEFT_OUTER)
downloadTimes = [arrow.get(x.time).to(tz.tzlocal()) for x in downloads]
Expand Down Expand Up @@ -316,12 +317,14 @@ def get_nzb_downloads(page=0, limit=100, type=None):


# ((Search.identifier_value == MovieIdCache.imdb) & (Search.identifier_key == "imdbid"))
def get_search_requests(page=0, limit=100, sortModel=None, type=None, filterModel=None):
query = Search().select(Search.time, Search.internal, Search.query, Search.identifier_key, Search.identifier_value, Search.category, Search.season, Search.episode, Search.type, Search.username, Search.title, Search.author, TvIdCache.title.alias("tvtitle"), MovieIdCache.title.alias("movietitle")).join(TvIdCache,
JOIN.LEFT_OUTER, on=(
((Search.identifier_value == TvIdCache.tvdb) & (Search.identifier_key == "tvdbid")) |
((Search.identifier_value == TvIdCache.tvrage) & (Search.identifier_key == "rid"))
)).join(MovieIdCache, JOIN.LEFT_OUTER, on=(
def get_search_requests(page=0, limit=100, sortModel=None, type=None, filterModel=None, distinct=False):
columns = [Search.time, Search.internal, Search.query, Search.identifier_key, Search.identifier_value, Search.category, Search.season, Search.episode, Search.type, Search.username, Search.title, Search.author, TvIdCache.title.alias("tvtitle"), MovieIdCache.title.alias("movietitle")]

query = Search().select(*columns)
query = query.join(TvIdCache, JOIN.LEFT_OUTER, on=(
((Search.identifier_value == TvIdCache.tvdb) & (Search.identifier_key == "tvdbid")) |
((Search.identifier_value == TvIdCache.tvrage) & (Search.identifier_key == "rid"))
)).join(MovieIdCache, JOIN.LEFT_OUTER, on=(
((Search.identifier_value == MovieIdCache.imdb) & (Search.identifier_key == "imdbid")) |
((Search.identifier_value == MovieIdCache.tmdb) & (Search.identifier_key == "tmdbid"))))

Expand Down Expand Up @@ -350,6 +353,8 @@ def get_search_requests(page=0, limit=100, sortModel=None, type=None, filterMode
if sort["sort"] == "desc":
orderBy = orderBy.desc()
query = query.order_by(orderBy)
if distinct:
query = query.group_by(Search.internal, Search.query, Search.identifier_key, Search.identifier_value, Search.category, Search.season, Search.episode, Search.type, Search.username, Search.title, Search.author)
requests = list(query.paginate(page, limit).dicts())

search_requests = {"totalRequests": total_requests, "searchRequests": requests}
Expand Down
3 changes: 2 additions & 1 deletion nzbhydra/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ class SearchSchema(Schema):
limit = fields.Integer(missing=500)
sortModel = fields.Nested(SortModelSchema, many=True, missing=None)
type = fields.String(missing=None)
distinct = fields.Boolean(missing=False)


@app.route('/internalapi/getsearchrequests', methods=['GET', 'POST'])
Expand All @@ -999,7 +1000,7 @@ def internalapi_search_requests(args):
if not "sortModel" in args.keys() or args["sortModel"] is None:
args["sortModel"] = []
filterModel = request.json["filterModel"] if "filterModel" in request.json.keys() else None
return jsonify(get_search_requests(page=args["page"], limit=args["limit"], sortModel=args["sortModel"], type=args["type"], filterModel=filterModel))
return jsonify(get_search_requests(page=args["page"], limit=args["limit"], sortModel=args["sortModel"], type=args["type"], filterModel=filterModel, distinct=args["distinct"]))


internalapi__redirect_rid_args = {
Expand Down
6 changes: 3 additions & 3 deletions static/js/nzbhydra.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion static/js/nzbhydra.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui-src/js/search-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function SearchController($scope, $http, $stateParams, $state, $window, $filter,


function getAndSetSearchRequests() {
SearchHistoryService.getSearchHistory(1, 20, null, null, "internal").then(function (data) {
SearchHistoryService.getSearchHistory(1, 20, null, null, "internal", true).then(function (data) {
$scope.searchHistory = data.data.searchRequests;
});
}
Expand Down
4 changes: 2 additions & 2 deletions ui-src/js/search-history-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function SearchHistoryService($filter, $http) {
getStateParamsForRepeatedSearch: getStateParamsForRepeatedSearch
};

function getSearchHistory(pageNumber, limit, sortModel, filterModel, type) {
function getSearchHistory(pageNumber, limit, sortModel, filterModel, type, distinct) {
if (angular.isUndefined(pageNumber)) {
pageNumber = 1;
}
Expand All @@ -23,7 +23,7 @@ function SearchHistoryService($filter, $http) {
if (!filterModel) {
filterModel = {}
}
return $http.post("internalapi/getsearchrequests", {page: pageNumber, limit: limit, sortModel: sortModel, filterModel: filterModel, type: type}).success(function (response) {
return $http.post("internalapi/getsearchrequests", {page: pageNumber, limit: limit, sortModel: sortModel, filterModel: filterModel, type: type, distinct: distinct}).success(function (response) {
return {
searchRequests: response.searchRequests,
totalRequests: response.totalRequests
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.187
0.2.188

0 comments on commit a9f1b6a

Please sign in to comment.