Skip to content

Commit

Permalink
Improve a bit of logging and code
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherp committed Nov 13, 2024
1 parent 2ac65e4 commit 6d51600
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public File createDebugInfosZipFile() throws IOException {
final Set<String> metricsNames = metricsEndpoint.listNames().getNames();
for (String metric : metricsNames) {
final MetricsEndpoint.MetricDescriptor response = metricsEndpoint.metric(metric, null);
logger.info(metric + ": " + response.getMeasurements().stream()
logger.info("{}: {}", metric, response.getMeasurements().stream()
.map(x -> x.getStatistic().name() + ": " + formatSample(metric, x.getValue()))
.collect(Collectors.joining(", ")));
}
Expand Down Expand Up @@ -315,8 +315,10 @@ private void logConfigChanges(String anonymizedConfig) throws IOException {
}

public String logThreadDump() {

try {
StringBuilder sb = new StringBuilder();
sb.append("Thread dump:\n");
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
sb.append("Thread name: ").append(entry.getKey().getName()).append("\n");
Expand Down Expand Up @@ -382,9 +384,9 @@ protected void logDatabaseFolderSize() {

protected void logNumberOfTableRows(final String tableName) {
try {
logger.info("Number of rows in table " + tableName + ": " + entityManager.createNativeQuery("select count(*) from " + tableName).getSingleResult());
logger.info("Number of rows in table {}: {}", tableName, entityManager.createNativeQuery("select count(*) from " + tableName).getSingleResult());
} catch (Exception e) {
logger.error("Unable to get number of rows in table " + tableName, e);
logger.error("Unable to get number of rows in table {}", tableName, e);
}
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/nzbhydra/mediainfo/TvMazeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TvMazeHandler {
protected RestTemplate restTemplate;


public TvMazeSearchResult getInfos(String id, MediaIdType idType) throws InfoProviderException {
TvMazeSearchResult getInfos(String id, MediaIdType idType) throws InfoProviderException {
if (idType == MediaIdType.TVTITLE) {
return fromTitle(id);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ private TvMazeSearchResult getSearchResultFromShow(TvmazeShow show) {
return new TvMazeSearchResult(String.valueOf(show.getId()), show.getExternals().getTvrage(), show.getExternals().getThetvdb(), show.getExternals().getImdb(), show.getName(), year, makePosterLinksSecure(show).getMediumPosterUrl());
}

public List<TvMazeSearchResult> search(String title) throws InfoProviderException {
List<TvMazeSearchResult> search(String title) throws InfoProviderException {
logger.info("Searching TVMaze for shows with title '{}", title);
List<TvmazeShowSearch> shows = searchByTitle(title);
logger.info("TVMaze found {} shows for title '{}'", shows.size(), title);
Expand Down Expand Up @@ -111,14 +111,14 @@ private TvmazeShow makePosterLinksSecure(TvmazeShow show) {


@Data
@ReflectionMarker
@ReflectionMarker
private static class TvmazeShowSearch { //Without static deserialization fails
private Integer score;
private TvmazeShow show;
}

@Data
@ReflectionMarker
@ReflectionMarker
private static class TvmazeShow { //Without static deserialization fails
private Integer id;
private String name;
Expand All @@ -142,7 +142,7 @@ public String toString() {
}

@Data
@ReflectionMarker
@ReflectionMarker
private static class TvmazeExternals { //Without static deserialization fails
private String tvrage;
private String thetvdb;
Expand All @@ -159,7 +159,7 @@ public String toString() {
}

@Data
@ReflectionMarker
@ReflectionMarker
private static class TvmazeImage { //Without static deserialization fails
private String medium;
private String original;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Joiner;
import com.google.common.base.Stopwatch;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -40,7 +41,6 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -236,7 +236,7 @@ protected boolean checkIndexerSelected(Indexer indexer) {
boolean indexerNotSelected = !searchRequest.getIndexers().get().contains(indexer.getName());
if (indexerNotSelected) {
//Don't send a search log message for this because showing it to the leader would be useless. He knows he hasn't selected it
logger.info(String.format("Not using %s because it's not in selection %s", indexer.getName(), searchRequest.getIndexers().get()));
logger.info("Not using {} because it's not in selection {}", indexer.getName(), searchRequest.getIndexers().get());
notSelectedIndersWithReason.put(indexer, "Not selected by the user");
return false;
}
Expand Down Expand Up @@ -264,12 +264,12 @@ protected boolean checkIndexerHitLimit(Indexer indexer) {
LocalDateTime comparisonTime;
LocalDateTime now = LocalDateTime.now(clock);
if (indexerConfig.getHitLimitResetTime().isPresent()) {
comparisonTime = now.with(ChronoField.HOUR_OF_DAY, indexerConfig.getHitLimitResetTime().get());
comparisonTime = now.withHour(indexerConfig.getHitLimitResetTime().get());
if (comparisonTime.isAfter(now)) {
comparisonTime = comparisonTime.minus(1, ChronoUnit.DAYS);
comparisonTime = comparisonTime.minusDays(1);
}
} else {
comparisonTime = now.minus(1, ChronoUnit.DAYS);
comparisonTime = now.minusDays(1);
}
if (indexerConfig.getHitLimit().isPresent()) {
boolean limitExceeded = checkIfHitLimitIsExceeded(indexer, indexerConfig, comparisonTime, IndexerApiAccessType.SEARCH, indexerConfig.getHitLimit().get(), "API hit");
Expand Down Expand Up @@ -308,7 +308,7 @@ private boolean checkTooManyFrequentHits(Indexer indexer) {
if (oldestAccess.isBefore(Instant.now(clock).minusSeconds(timespanSeconds))) {
return true;
}
logger.info(String.format("Not using %s because too many frequent hits were made: More than %d in %d seconds. Oldest of these hits was %d seconds ago", indexer.getName(), limitHits, timespanSeconds, oldestSecondsAgo));
logger.info("Not using {} because too many frequent hits were made: More than {} in {} seconds. Oldest of these hits was {} seconds ago", indexer.getName(), limitHits, timespanSeconds, oldestSecondsAgo);
notSelectedIndersWithReason.put(indexer, "Too many frequent hits");
return false;
}
Expand Down Expand Up @@ -358,7 +358,7 @@ private boolean checkIfHitLimitIsExceeded(Indexer indexer, IndexerConfig indexer

Instant nextAccess = oldestAccess.plus(24, ChronoUnit.HOURS);
String message = String.format("Not using %s because all %d allowed " + type + "s were already made. The next " + type + " should be possible at %s", indexerConfig.getName(), limit, nextAccess);
logger.debug(LoggingMarkers.PERFORMANCE, "Detection that " + type + " limit has been reached for indexer {} took {}ms", indexerConfig.getName(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
logger.debug(LoggingMarkers.PERFORMANCE, "Detection that {} limit has been reached for indexer {} took {}ms", type, indexerConfig.getName(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
return !handleIndexerNotSelected(indexer, message, type + " limit reached");
}

Expand Down Expand Up @@ -445,13 +445,13 @@ LocalDateTime calculateNextPossibleHit(IndexerConfig indexerConfig, Instant firs
if (indexerConfig.getHitLimitResetTime().isPresent()) {
//Next possible hit is at the hour of day defined by the reset time. If that is already in the past it will be the next day at that time
LocalDateTime now = LocalDateTime.now(clock);
nextPossibleHit = now.with(ChronoField.HOUR_OF_DAY, indexerConfig.getHitLimitResetTime().get()).with(ChronoField.MINUTE_OF_HOUR, 0);
nextPossibleHit = now.withHour(indexerConfig.getHitLimitResetTime().get()).withMinute(0);
if (nextPossibleHit.isBefore(now)) {
nextPossibleHit = nextPossibleHit.plus(1, ChronoUnit.DAYS);
nextPossibleHit = nextPossibleHit.plusDays(1);
}
} else {
//Next possible hit is at the earlierst 24 hours after the first hit in the hit limit time window (5 hits are limit, the first was made now, then the next hit is available tomorrow at this time)
nextPossibleHit = LocalDateTime.ofInstant(firstInWindowAccessTime, ZoneOffset.UTC).plus(1, ChronoUnit.DAYS);
nextPossibleHit = LocalDateTime.ofInstant(firstInWindowAccessTime, ZoneOffset.UTC).plusDays(1);
}
return nextPossibleHit;
}
Expand Down Expand Up @@ -518,10 +518,10 @@ protected boolean isInTime(String scheduleTime) {
}

if (matcher.group("hour1") != null) {
int fromHour = Integer.valueOf(matcher.group("hour1"));
int fromHour = Integer.parseInt(matcher.group("hour1"));
int toHour;
if (matcher.group("hour2") != null) {
toHour = Integer.valueOf(matcher.group("hour2"));
toHour = Integer.parseInt(matcher.group("hour2"));
} else {
toHour = fromHour;
}
Expand All @@ -544,15 +544,17 @@ private boolean inRange(int a, int b, int val) {
}

private boolean handleIndexerNotSelected(Indexer indexer, String message, String reason) {
logger.info(message);
notSelectedIndersWithReason.put(indexer, reason);
eventPublisher.publishEvent(new SearchMessageEvent(searchRequest, message));
if (!Strings.isNullOrEmpty(message)) {
logger.info(message);
eventPublisher.publishEvent(new SearchMessageEvent(searchRequest, message));
}
return false;
}


@Data
@ReflectionMarker
@ReflectionMarker
@NoArgsConstructor
public static class IndexerForSearchSelection {
private Map<Indexer, String> notPickedIndexersWithReason = new HashMap<>();
Expand Down

0 comments on commit 6d51600

Please sign in to comment.