Search in sources :

Example 1 with FileDownloadEntity

use of org.nzbhydra.downloading.FileDownloadEntity in project nzbhydra2 by theotherp.

the class StatsComponentTest method shouldCalculateDownloadsPerDayOfWeek.

@Test
public void shouldCalculateDownloadsPerDayOfWeek() throws Exception {
    FileDownloadEntity downloadFriday = new FileDownloadEntity();
    // Friday
    downloadFriday.setTime(Instant.ofEpochSecond(1490945310L));
    FileDownloadEntity downloadThursday1 = new FileDownloadEntity();
    // Thursday
    downloadThursday1.setTime(Instant.ofEpochSecond(1490858910L));
    FileDownloadEntity downloadThursday2 = new FileDownloadEntity();
    // Thursday
    downloadThursday2.setTime(Instant.ofEpochSecond(1490858910L));
    FileDownloadEntity downloadSunday = new FileDownloadEntity();
    // Sunday
    downloadSunday.setTime(Instant.ofEpochSecond(1490513310L));
    downloadRepository.save(Arrays.asList(downloadFriday, downloadSunday, downloadThursday1, downloadThursday2));
    List<CountPerDayOfWeek> result = stats.countPerDayOfWeek("INDEXERNZBDOWNLOAD", new StatsRequest(downloadFriday.getTime().minus(10, ChronoUnit.DAYS), downloadFriday.getTime().plus(10, ChronoUnit.DAYS), true));
    assertEquals(7, result.size());
    assertEquals("Thu", result.get(3).getDay());
    assertEquals(Integer.valueOf(2), result.get(3).getCount());
    assertEquals("Fri", result.get(4).getDay());
    assertEquals(Integer.valueOf(1), result.get(4).getCount());
    assertEquals("Sun", result.get(6).getDay());
    assertEquals(Integer.valueOf(1), result.get(6).getCount());
}
Also used : FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with FileDownloadEntity

use of org.nzbhydra.downloading.FileDownloadEntity in project nzbhydra2 by theotherp.

the class StatsComponentTest method shouldCalculateIndexerDownloadShares.

@Test
public void shouldCalculateIndexerDownloadShares() throws Exception {
    FileDownloadEntity download1 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity1 = getSearchResultEntity(indexer1, "1");
    download1.setSearchResult(searchResultEntity1);
    FileDownloadEntity download2 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity2 = getSearchResultEntity(indexer1, "2");
    download2.setSearchResult(searchResultEntity2);
    FileDownloadEntity download3 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity3 = getSearchResultEntity(indexer1, "3");
    download3.setSearchResult(searchResultEntity3);
    FileDownloadEntity download4 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity4 = getSearchResultEntity(indexer1, "4");
    download4.setSearchResult(searchResultEntity4);
    FileDownloadEntity download5 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity5 = getSearchResultEntity(indexer2, "5");
    download5.setSearchResult(searchResultEntity5);
    FileDownloadEntity download6 = new FileDownloadEntity();
    SearchResultEntity searchResultEntity6 = getSearchResultEntity(indexer2, "6");
    download6.setSearchResult(searchResultEntity6);
    searchResultRepository.save(Arrays.asList(searchResultEntity1, searchResultEntity2, searchResultEntity3, searchResultEntity4, searchResultEntity5, searchResultEntity6));
    downloadRepository.save(Arrays.asList(download1, download2, download3, download4, download5, download6));
    List<IndexerDownloadShare> shares = stats.indexerDownloadShares(new StatsRequest(Instant.now().minus(100, ChronoUnit.DAYS), Instant.now().plus(1, ChronoUnit.DAYS), true));
    assertThat(shares.get(0).getIndexerName(), is("indexer1"));
    assertThat((int) shares.get(0).getShare(), is(66));
    assertThat((int) shares.get(1).getShare(), is(33));
}
Also used : FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with FileDownloadEntity

use of org.nzbhydra.downloading.FileDownloadEntity in project nzbhydra2 by theotherp.

the class IndexerForSearchSelector method checkIndexerHitLimit.

protected boolean checkIndexerHitLimit(Indexer indexer) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    IndexerConfig indexerConfig = indexer.getConfig();
    if (!indexerConfig.getHitLimit().isPresent() && !indexerConfig.getDownloadLimit().isPresent()) {
        return true;
    }
    LocalDateTime comparisonTime;
    LocalDateTime now = LocalDateTime.now(clock);
    if (indexerConfig.getHitLimitResetTime().isPresent()) {
        comparisonTime = now.with(ChronoField.HOUR_OF_DAY, indexerConfig.getHitLimitResetTime().get());
        if (comparisonTime.isAfter(now)) {
            comparisonTime = comparisonTime.minus(1, ChronoUnit.DAYS);
        }
    } else {
        comparisonTime = now.minus(1, ChronoUnit.DAYS);
    }
    if (indexerConfig.getHitLimit().isPresent()) {
        Query query = entityManager.createNativeQuery("SELECT x.TIME FROM INDEXERAPIACCESS_SHORT x WHERE x.INDEXER_ID = (:indexerId) ORDER BY TIME DESC LIMIT (:hitLimit)");
        query.setParameter("indexerId", indexer.getIndexerEntity().getId());
        query.setParameter("hitLimit", indexerConfig.getHitLimit().get());
        List resultList = query.getResultList();
        if (resultList.size() == indexerConfig.getHitLimit().get()) {
            // Found as many as we want, so now we must check if they're all in the time window
            Instant earliestAccess = ((Timestamp) Iterables.getLast(resultList)).toInstant();
            if (earliestAccess.isAfter(comparisonTime.toInstant(ZoneOffset.UTC))) {
                LocalDateTime nextPossibleHit = calculateNextPossibleHit(indexerConfig, earliestAccess);
                String message = String.format("Not using %s because all %d allowed API hits were already made. The next API hit should be possible at %s", indexerConfig.getName(), indexerConfig.getHitLimit().get(), nextPossibleHit);
                logger.debug(LoggingMarkers.PERFORMANCE, "Detection of API limit reached took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
                return handleIndexerNotSelected(indexer, message, "API hit limit reached");
            }
        }
    }
    if (indexerConfig.getDownloadLimit().isPresent()) {
        Page<FileDownloadEntity> page = nzbDownloadRepository.findBySearchResultIndexerOrderByTimeDesc(indexer.getIndexerEntity(), new PageRequest(0, indexerConfig.getDownloadLimit().get()));
        if (page.getContent().size() == indexerConfig.getDownloadLimit().get() && Iterables.getLast(page.getContent()).getTime().isAfter(comparisonTime.toInstant(ZoneOffset.UTC))) {
            LocalDateTime nextPossibleHit = calculateNextPossibleHit(indexerConfig, page.getContent().get(page.getContent().size() - 1).getTime());
            String message = String.format("Not using %s because all %d allowed download were already made. The next download should be possible at %s", indexerConfig.getName(), indexerConfig.getDownloadLimit().get(), nextPossibleHit);
            logger.debug(LoggingMarkers.PERFORMANCE, "Detection of download limit reached took {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
            return handleIndexerNotSelected(indexer, message, "Download limit reached");
        }
    }
    logger.debug(LoggingMarkers.PERFORMANCE, "Detection if hit limits were reached for indexer {} took {}ms", indexer.getName(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
    return true;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Query(javax.persistence.Query) IndexerConfig(org.nzbhydra.config.IndexerConfig) Stopwatch(com.google.common.base.Stopwatch) Timestamp(java.sql.Timestamp) FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity)

Example 4 with FileDownloadEntity

use of org.nzbhydra.downloading.FileDownloadEntity in project nzbhydra2 by theotherp.

the class StatsComponentTest method shouldCalculateDownloadsAges.

@Test
public void shouldCalculateDownloadsAges() throws Exception {
    FileDownloadEntity download1 = new FileDownloadEntity();
    download1.setAge(10);
    FileDownloadEntity download2 = new FileDownloadEntity();
    download2.setAge(1000);
    FileDownloadEntity download3 = new FileDownloadEntity();
    download3.setAge(1500);
    FileDownloadEntity download4 = new FileDownloadEntity();
    download4.setAge(2001);
    FileDownloadEntity download5 = new FileDownloadEntity();
    download5.setAge(3499);
    FileDownloadEntity download6 = new FileDownloadEntity();
    download6.setAge(3400);
    downloadRepository.save(Arrays.asList(download1, download2, download3, download4, download5, download6));
    List<DownloadPerAge> downloadPerAges = stats.downloadsPerAge();
    assertThat(downloadPerAges.get(34).getAge(), is(3400));
    assertThat(downloadPerAges.get(34).getCount(), is(2));
    DownloadPerAgeStats downloadPerAgeStats = stats.downloadsPerAgeStats();
    assertThat(downloadPerAgeStats.getAverageAge(), is(1901));
    assertThat(downloadPerAgeStats.getPercentOlder1000(), is(66));
    assertThat(downloadPerAgeStats.getPercentOlder2000(), is(50));
    assertThat(downloadPerAgeStats.getPercentOlder3000(), is(33));
}
Also used : FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with FileDownloadEntity

use of org.nzbhydra.downloading.FileDownloadEntity in project nzbhydra2 by theotherp.

the class SqliteMigration method migrateDownloads.

private void migrateDownloads(Map<Integer, IndexerEntity> oldIdToIndexersMap) throws SQLException {
    Statement statement = connection.createStatement();
    int countDownloads = getCount(statement, "INDEXERNZBDOWNLOAD");
    logger.info("Migrating {} downloads from old database", countDownloads);
    eventPublisher.publishEvent(new MigrationMessageEvent("Migrating " + countDownloads + " NZB download entries"));
    ResultSet oldDownloads = statement.executeQuery("SELECT * FROM indexernzbdownload LEFT JOIN indexerapiaccess ON indexernzbdownload.apiAccess_id = indexerapiaccess.id");
    List<FileDownloadEntity> downloadEntities = new ArrayList<>();
    List<SearchResultEntity> dummySearchResultEntities = new ArrayList<>();
    while (oldDownloads.next()) {
        FileDownloadEntity entity = new FileDownloadEntity();
        entity.setTime(timestampToInstant(oldDownloads.getString("time")));
        IndexerEntity indexerEntity = oldIdToIndexersMap.get(oldDownloads.getInt("indexer_id"));
        if (indexerEntity == null) {
            logger.info("Skipping migration of download with indexer that doesn't exist anymore");
            continue;
        }
        entity.setError(oldDownloads.getString("error"));
        entity.setUsername(oldDownloads.getString("username"));
        entity.setAccessSource(oldDownloads.getBoolean("internal") ? SearchSource.INTERNAL : SearchSource.API);
        entity.setNzbAccessType(oldDownloads.getString("mode").equals("redirect") ? FileDownloadAccessType.REDIRECT : FileDownloadAccessType.PROXY);
        entity.setStatus(FileDownloadStatus.NONE);
        // long hash = hash64((result.getIndexer().getName() + result.getIndexerGuid() + result.getTitle() + result.getLink()));
        Instant dummyTime = Instant.now().minus(10000, ChronoUnit.DAYS);
        String title = oldDownloads.getString("title");
        if (title == null) {
            logger.warn("Skipping migration of download without title");
        }
        String randomIntSTring = String.valueOf(random.nextInt()) + indexerEntity.getName();
        // Must set a random link because the calculator would always return the same ID
        SearchResultEntity searchResultEntity = new SearchResultEntity(indexerEntity, dummyTime, title, "guid" + randomIntSTring, "link" + randomIntSTring, "details" + randomIntSTring, null, dummyTime);
        entity.setSearchResult(searchResultEntity);
        dummySearchResultEntities.add(searchResultEntity);
        downloadEntities.add(entity);
    }
    searchResultRepository.save(dummySearchResultEntities);
    downloadRepository.save(downloadEntities);
    logger.info("Successfully migrated downloads from old database");
    eventPublisher.publishEvent(new MigrationMessageEvent("Successfully migrated NZB download entries"));
}
Also used : Instant(java.time.Instant) MigrationMessageEvent(org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent) FileDownloadEntity(org.nzbhydra.downloading.FileDownloadEntity)

Aggregations

FileDownloadEntity (org.nzbhydra.downloading.FileDownloadEntity)5 Test (org.junit.Test)3 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Stopwatch (com.google.common.base.Stopwatch)1 Timestamp (java.sql.Timestamp)1 Instant (java.time.Instant)1 Query (javax.persistence.Query)1 IndexerConfig (org.nzbhydra.config.IndexerConfig)1 MigrationMessageEvent (org.nzbhydra.migration.FromPythonMigration.MigrationMessageEvent)1 PageRequest (org.springframework.data.domain.PageRequest)1