#2640 add StatsKeyObject

This commit is contained in:
Shinsuke Sugaya 2022-04-10 22:17:44 +09:00
parent 5870c8c8bd
commit 38b2c9577b
2 changed files with 50 additions and 1 deletions

View file

@ -127,6 +127,8 @@ public class CrawlerStatsHelper {
protected String getUrl(final Object keyObj) {
if (keyObj instanceof UrlQueue<?> urlQueue) {
return escapeValue(urlQueue.getUrl());
} else if (keyObj instanceof StatsKeyObject statsKey) {
return escapeValue(statsKey.getUrl());
} else if (keyObj instanceof String key) {
return escapeValue(key);
} else if (keyObj instanceof Number key) {
@ -138,6 +140,8 @@ public class CrawlerStatsHelper {
protected OptionalThing<String> getCacheKey(final Object keyObj) {
if (keyObj instanceof UrlQueue<?> urlQueue) {
return OptionalThing.of(urlQueue.getId().toString());
} else if (keyObj instanceof StatsKeyObject statsKey) {
return OptionalThing.of(statsKey.getId());
} else if (keyObj instanceof String key) {
return OptionalThing.of(key);
} else if (keyObj instanceof Number key) {
@ -166,4 +170,29 @@ public class CrawlerStatsHelper {
this.cacheExpireAfterWrite = cacheExpireAfterWrite;
}
public static class StatsKeyObject {
private final String id;
private String url;
public StatsKeyObject(final String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUrl(final String url) {
this.url = url;
}
protected String getUrl() {
if (url != null) {
return url;
}
return id;
}
}
}

View file

@ -17,6 +17,7 @@ package org.codelibs.fess.helper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.fess.helper.CrawlerStatsHelper.StatsKeyObject;
import org.codelibs.fess.unit.UnitFessTestCase;
public class CrawlerStatsHelperTest extends UnitFessTestCase {
@ -92,4 +93,23 @@ public class CrawlerStatsHelperTest extends UnitFessTestCase {
crawlerStatsHelper.done(key);
assertNull(localLogMsg.get());
}
}
public void test_beginDoneWithRecord1WithStatsKeyObject() {
StatsKeyObject key = new StatsKeyObject("id");
crawlerStatsHelper.begin(key);
key.setUrl("test");
crawlerStatsHelper.record(key, "aaa");
crawlerStatsHelper.done(key);
logger.info(localLogMsg.get());
String[] values = localLogMsg.get().split("\t");
assertEquals(4, values.length);
assertEquals("url:test", values[0]);
assertTrue(values[1].startsWith("time:"));
assertTrue(values[2].startsWith("done:"));
assertTrue(values[3].startsWith("aaa:"));
localLogMsg.remove();
crawlerStatsHelper.done(key);
assertNull(localLogMsg.get());
}
}