-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
447 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/org/torproject/metrics/onionoo/userstats/Merger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.torproject.metrics.onionoo.userstats; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class Merger { | ||
private static int idCounter = 1; | ||
|
||
public static List<Merged> mergeFirstTime(List<Imported> importedList) { | ||
List<Merged> mergedList = new ArrayList<>(); | ||
|
||
// Step 1: Group by unique fields (fingerprint, nickname, node, metric, country, transport, version) | ||
Map<String, List<Imported>> groupedImported = importedList.stream().collect(Collectors.groupingBy( | ||
imported -> String.join("-", imported.getFingerprint(), imported.getNickname(), | ||
imported.getNode(), imported.getMetric(), | ||
imported.getCountry(), imported.getTransport(), imported.getVersion()) | ||
)); | ||
|
||
// Step 2: Process each group independently | ||
for (List<Imported> group : groupedImported.values()) { | ||
// Sort each group by startTime to ensure intervals are processed in sequence | ||
group.sort(Comparator.comparing(Imported::getStatsStart)); | ||
|
||
// Initialize variables to track merging within the group | ||
long lastStartTime = group.get(0).getStatsStart(); | ||
long lastEndTime = group.get(0).getStatsEnd(); | ||
double lastVal = group.get(0).getVal(); | ||
|
||
// Use first entry to initialize shared fields for Merged | ||
String fingerprint = group.get(0).getFingerprint(); | ||
String nickname = group.get(0).getNickname(); | ||
String node = group.get(0).getNode(); | ||
String metric = group.get(0).getMetric(); | ||
String country = group.get(0).getCountry(); | ||
String transport = group.get(0).getTransport(); | ||
String version = group.get(0).getVersion(); | ||
|
||
// Merge intervals within the sorted group | ||
for (int i = 1; i < group.size(); i++) { | ||
Imported current = group.get(i); | ||
|
||
if (current.getStatsStart() <= lastEndTime) { | ||
// Overlapping or adjacent interval, extend the end time and accumulate the value | ||
lastEndTime = Math.max(lastEndTime, current.getStatsEnd()); | ||
lastVal += current.getVal(); | ||
} else { | ||
// No overlap, add the previous merged interval to mergedList | ||
mergedList.add(new Merged(idCounter++, fingerprint, nickname, node, metric, country, | ||
transport, version, lastStartTime, lastEndTime, lastVal)); | ||
|
||
// Start a new interval | ||
lastStartTime = current.getStatsStart(); | ||
lastEndTime = current.getStatsEnd(); | ||
lastVal = current.getVal(); | ||
} | ||
} | ||
|
||
// Add the last merged interval of the group to mergedList | ||
mergedList.add(new Merged(idCounter++, fingerprint, nickname, node, metric, country, | ||
transport, version, lastStartTime, lastEndTime, lastVal)); | ||
} | ||
|
||
return mergedList; | ||
} | ||
} |
Oops, something went wrong.