Skip to content

Commit

Permalink
#3 New instances of PrefixCollection will now contain the correct Int…
Browse files Browse the repository at this point in the history
…erval.
  • Loading branch information
baardl committed Oct 10, 2014
1 parent 1896489 commit 274e602
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public ObservationsRepository(ObservationDao observationDao) {
this.observationDao = observationDao;
}

public void updateStatistics(String prefix, List<ObservedMethod> methods) {
PrefixCollection prefixCollection = getCollection(prefix);
public void updateStatistics(String prefix, Long intervalInSec,List<ObservedMethod> methods) {
PrefixCollection prefixCollection = getCollection(prefix, intervalInSec);

for (ObservedMethod method : methods) {
prefixCollection.updateStatistics(method);
Expand All @@ -39,40 +39,19 @@ public void updateStatistics(String prefix, List<ObservedMethod> methods) {
* @return Will always return a prefixCollection, unless prefix is null or empty
* @throws IllegalArgumentException If prefix is null or empty.
*/
synchronized PrefixCollection getCollection(String prefix) throws IllegalArgumentException{
synchronized PrefixCollection getCollection(String prefix, long intervalInSec) throws IllegalArgumentException{
if (prefix == null || prefix.isEmpty()) {
throw new IllegalArgumentException("Prefix may not be null, nor empty.");
}
PrefixCollection prefixCollection;
if (!prefixes.containsKey(prefix)) {
prefixCollection = new PrefixCollection(prefix);
prefixCollection = new PrefixCollection(prefix, intervalInSec);
prefixes.putIfAbsent(prefix, prefixCollection);
}
prefixCollection = prefixes.get(prefix);
return prefixCollection;
}

@Deprecated
public synchronized void persistStatistics(String prefix) {
log.trace("persistStatistics starts");
PrefixCollection prefixCollection = getCollection(prefix);
if (prefixCollection != null) {
// log.debug("Got prefixCollection {}", prefixCollection.toString());
List<ObservedInterval> intervals = prefixCollection.getIntervals();
log.debug("Got intervals size {}", intervals.size());
// clearCollection(prefix);

//log.debug("cleared collection");
int keysUpdated = updateMissingKeys(prefix, intervals);
//log.trace("updated {} keys", keysUpdated);
int intervalsUpdated = observationDao.updateStatistics(prefix, intervals);
log.trace("updated {} intervals", intervalsUpdated);
} else {
log.trace("Nothing to presist.");
}

}

public synchronized void persistAndResetStatistics(String prefix, long intervalInSec) {
log.trace("persistStatistics starts");
List<ObservedInterval> intervals = fetchAndClear(prefix, intervalInSec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ObservationsService implements QueryOperations, WriteOperations {
private static final long INITIAL_DELAY = MINUTES_15;
private static final long DELAY_BETWEEN_RUNS = MINUTES_15;
private final ObservationsRepository observationsRepository;
private int intervalSeconds = -1;
private long intervalSeconds = -1;
private List<ObservedMethod> observedMethodsChache;
private ObservationDao observationDao;
private final boolean persistMethodDetails; // = false;
Expand Down Expand Up @@ -59,7 +59,7 @@ public long addObservations(String prefix, List<ObservedMethod> observedMethods)
if (!isScheduled(prefix)) {
createScheduler(prefix);
}
observationsRepository.updateStatistics(prefix, observedMethods);
observationsRepository.updateStatistics(prefix, intervalSeconds, observedMethods);
if (isPersistMethodDetails()) {
observationDao.addAll(prefix, observedMethods);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private static Integer count(JdbcTemplate jdbcTemplate, String table) {


public void testObservationsRepositoryAddAndPersist() {
repository.updateStatistics(PREFIX,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX);
repository.updateStatistics(PREFIX,1L,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX, 1L);
List<ObservedInterval> intervalls = prefixCollection.getIntervals();
repository.persistAndResetStatistics(PREFIX, 1L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public ObservationsRepositoryTest(ObservationDao observationDao) {
public static void testObservationsRepositoryAddAndPersist() {
ObservationDao observationDaoMock = mock(ObservationDao.class);
ObservationsRepository repository = new ObservationsRepository(observationDaoMock);
repository.updateStatistics(PREFIX,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX);
repository.updateStatistics(PREFIX,1L,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX,1L);
List<ObservedInterval> intervalls = prefixCollection.getIntervals();
assertEquals(intervalls.size(),1);
repository.persistAndResetStatistics(PREFIX,1L);
Expand All @@ -43,13 +43,13 @@ public static void testObservationsRepositoryAddAndPersist() {
public static void verifyCollectionIsCleared() {
ObservationDao observationDaoMock = mock(ObservationDao.class);
ObservationsRepository repository = new ObservationsRepository(observationDaoMock);
repository.updateStatistics(PREFIX,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX);
repository.updateStatistics(PREFIX,1L,observedMethodsStubs());
PrefixCollection prefixCollection = repository.getCollection(PREFIX,1L);
log.debug("Collection size before {}", prefixCollection.getIntervals().size());
List<ObservedInterval> intervalls = prefixCollection.getIntervals();
assertEquals(intervalls.size(), 1);
repository.persistAndResetStatistics(PREFIX,1L);
assertNull(repository.getCollection(PREFIX));
assertNull(repository.getCollection(PREFIX,1L));
}

private static List<ObservedMethod> observedMethodsStubs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public void run() {
} catch (InterruptedException e) {
log.trace("Interupted {}", getName());
}
observationsRepository.updateStatistics(PREFIX, getFirstMethods());
observationsRepository.updateStatistics(PREFIX, 1L,getFirstMethods());
} else {
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
log.trace("Interupted {}", getName());
}
observationsRepository.updateStatistics(PREFIX,getSecondMethods());
observationsRepository.updateStatistics(PREFIX,1L,getSecondMethods());
}
}
}
Expand Down

0 comments on commit 274e602

Please sign in to comment.