Search in sources :

Example 1 with SearchModuleType

use of org.nzbhydra.config.SearchModuleType in project nzbhydra2 by theotherp.

the class Stats method indexerSearchShares.

/**
 * Calculates how much of the search results indexers provide to searches on average and how much the share of unique results is. Excludes:
 * <ul>
 * <li>raw search engines from uniqe shares because they return result names that are rarely matched as duplicates</li>
 * <li>"update queries" because here some indexers return a "total" of 1000 and some of 1000000 or something like that</li>
 * <p>
 * </ul>
 */
@Transactional(readOnly = true)
List<IndexerSearchResultsShare> indexerSearchShares(final StatsRequest statsRequest) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    logger.debug("Calculating indexer search shares");
    List<IndexerSearchResultsShare> indexerSearchResultsShares = new ArrayList<>();
    Map<Integer, String> indexerIdToName = new HashMap<>();
    List<Indexer> indexersToInclude = statsRequest.isIncludeDisabled() ? searchModuleProvider.getIndexers() : searchModuleProvider.getEnabledIndexers();
    List<SearchModuleType> typesToUse = Arrays.asList(SearchModuleType.NEWZNAB, SearchModuleType.TORZNAB, SearchModuleType.ANIZB);
    indexersToInclude = indexersToInclude.stream().filter(x -> typesToUse.contains(x.getConfig().getSearchModuleType())).collect(Collectors.toList());
    for (Indexer indexer : indexersToInclude) {
        indexerIdToName.put(indexer.getIndexerEntity().getId(), indexer.getName());
    }
    Set<Integer> indexerIdsToInclude = indexerIdToName.keySet();
    String countResultsSql = "SELECT\n" + "  INDEXER_ENTITY_ID,\n" + "  INDEXERRESULTSSUM,\n" + "  ALLRESULTSSUM,\n" + "  INDEXERUNIQUERESULTSSUM,\n" + "  ALLUNIQUERESULTSSUM\n" + "FROM\n" + "  (SELECT\n" + "     SUM(INDEXERSEARCH.RESULTS_COUNT)  AS INDEXERRESULTSSUM,\n" + "     SUM(INDEXERSEARCH.UNIQUE_RESULTS) AS INDEXERUNIQUERESULTSSUM,\n" + "     INDEXERSEARCH.INDEXER_ENTITY_ID\n" + "   FROM indexersearch\n" + "   WHERE indexersearch.ID IN (SELECT INDEXERSEARCH.ID\n" + "                FROM indexersearch\n" + "                  LEFT JOIN SEARCH ON INDEXERSEARCH.SEARCH_ENTITY_ID = SEARCH.ID\n" + "                WHERE indexersearch.INDEXER_ENTITY_ID IN (:indexerIds) \n" + "                      AND INDEXERSEARCH.successful AND\n" + "                      INDEXERSEARCH.SEARCH_ENTITY_ID IN (SELECT SEARCH.ID\n" + "                                                         FROM SEARCH\n" + "                                                           LEFT JOIN SEARCH_IDENTIFIERS ON SEARCH.ID = SEARCH_IDENTIFIERS.SEARCH_ENTITY_ID\n" + "                                                         WHERE\n" + "                                                           (SEARCH.episode IS NOT NULL OR SEARCH.season IS NOT NULL OR SEARCH.query IS NOT NULL OR SEARCH_IDENTIFIERS.SEARCH_ENTITY_ID IS NOT NULL OR SEARCH.AUTHOR IS NOT NULL OR SEARCH.TITLE IS NOT NULL) \n" + buildWhereFromStatsRequest(true, statsRequest) + "                      )\n" + "   )" + "   GROUP BY INDEXER_ENTITY_ID" + ") FORINDEXER,\n" + "  (SELECT\n" + "      sum(INDEXERSEARCH.RESULTS_COUNT)  AS ALLRESULTSSUM,\n" + "      SUM(INDEXERSEARCH.UNIQUE_RESULTS) AS ALLUNIQUERESULTSSUM\n" + "    FROM INDEXERSEARCH\n" + "    WHERE INDEXERSEARCH.SEARCH_ENTITY_ID IN (SELECT SEARCH.ID\n" + "                                             FROM indexersearch\n" + "                                               LEFT JOIN SEARCH ON INDEXERSEARCH.SEARCH_ENTITY_ID = SEARCH.ID\n" + "                                               LEFT JOIN SEARCH_IDENTIFIERS ON SEARCH.ID = SEARCH_IDENTIFIERS.SEARCH_ENTITY_ID\n" + "                                             WHERE indexersearch.INDEXER_ENTITY_ID IN (:indexerIds) \n" + "                                                   AND INDEXERSEARCH.successful AND\n" + "                                                   INDEXERSEARCH.SEARCH_ENTITY_ID IN (SELECT SEARCH.ID\n" + "                                                                                      FROM SEARCH\n" + "                                                                                        LEFT JOIN SEARCH_IDENTIFIERS ON SEARCH.ID = SEARCH_IDENTIFIERS.SEARCH_ENTITY_ID\n" + "                                                                                      WHERE\n" + "                                                                                        (SEARCH.episode IS NOT NULL OR SEARCH.season IS NOT NULL OR SEARCH.query IS NOT NULL OR\n" + "                                                                                         SEARCH_IDENTIFIERS.SEARCH_ENTITY_ID IS NOT NULL OR SEARCH.AUTHOR IS NOT NULL OR SEARCH.TITLE IS NOT NULL)\n" + buildWhereFromStatsRequest(true, statsRequest) + "                                                   )\n" + "                                                   AND INDEXERSEARCH.successful)" + "         AND INDEXERSEARCH.successful\n" + "  ) FORALL";
    Query countQuery = entityManager.createNativeQuery(countResultsSql).setParameter("indexerIds", indexerIdsToInclude);
    List results = countQuery.getResultList();
    for (Object resultObject : results) {
        Object[] resultSet = (Object[]) resultObject;
        Float allShare = null;
        if (resultSet[1] != null && resultSet[2] != null) {
            BigInteger indexerResultsSum = (BigInteger) resultSet[1];
            BigInteger allResultsSum = (BigInteger) resultSet[2];
            if (indexerResultsSum.intValue() > 0) {
                allShare = 100 / (allResultsSum.floatValue() / indexerResultsSum.floatValue());
            }
        }
        Float uniqueShare = null;
        if (resultSet[3] != null && resultSet[4] != null) {
            BigInteger indexerUniqueResultsSum = (BigInteger) resultSet[3];
            BigInteger allUniqueResultsSum = (BigInteger) resultSet[4];
            if (allUniqueResultsSum.intValue() > 0) {
                uniqueShare = 100 / (allUniqueResultsSum.floatValue() / indexerUniqueResultsSum.floatValue());
            }
        }
        Integer indexerId = (Integer) resultSet[0];
        indexerSearchResultsShares.add(new IndexerSearchResultsShare(indexerIdToName.get(indexerId), allShare, uniqueShare));
    }
    logger.debug(LoggingMarkers.PERFORMANCE, "Calculated indexer search shares. Took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
    return indexerSearchResultsShares;
}
Also used : SearchModuleType(org.nzbhydra.config.SearchModuleType) Query(javax.persistence.Query) Stopwatch(com.google.common.base.Stopwatch) BigInteger(java.math.BigInteger) Indexer(org.nzbhydra.indexers.Indexer) BigInteger(java.math.BigInteger) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 BigInteger (java.math.BigInteger)1 Query (javax.persistence.Query)1 SearchModuleType (org.nzbhydra.config.SearchModuleType)1 Indexer (org.nzbhydra.indexers.Indexer)1 Transactional (org.springframework.transaction.annotation.Transactional)1