tune up transaction generator

This commit is contained in:
kacperlo 2025-06-10 15:49:43 +02:00
parent 06f79923bb
commit 41abb20094

View File

@ -8,7 +8,7 @@ import java.util.concurrent.ThreadLocalRandom;
public class TransactionGenerator {
private static final int NUM_CARDS = 10000;
private static final int NUM_USERS = 5000; // Assuming each user has ~2 cards on average
private static final Map<String, Set<Double[]>> cardLocations = new HashMap<>();
private static final Map<String, Set<LocationPoint>> cardLocations = new HashMap<>();
private static final Map<String, Double> cardLimits = new HashMap<>();
private static final Map<String, String> cardToUser = new HashMap<>();
private static final List<String> cardIds = new ArrayList<>();
@ -57,13 +57,20 @@ public class TransactionGenerator {
actualAnomalyType = (anomalyType >= 0 && anomalyType <= 3) ?
anomalyType : ThreadLocalRandom.current().nextInt(1, 4);
} else if (ThreadLocalRandom.current().nextDouble() < ANOMALY_PROBABILITY) {
actualAnomalyType = ThreadLocalRandom.current().nextInt(1, 4);
double roll = ThreadLocalRandom.current().nextDouble();
if (roll < 0.3) {
actualAnomalyType = ANOMALY_LOCATION;
} else if (roll < 0.8) {
actualAnomalyType = ANOMALY_AMOUNT;
} else {
actualAnomalyType = ANOMALY_FREQUENCY;
}
}
// Get or generate location
Double[] location = getLocationForCard(cardId, actualAnomalyType == ANOMALY_LOCATION);
double latitude = location[0];
double longitude = location[1];
LocationPoint location = getLocationForCard(cardId, actualAnomalyType == ANOMALY_LOCATION);
double latitude = location.latitude;
double longitude = location.longitude;
// Generate transaction amount
double amount;
@ -93,14 +100,14 @@ public class TransactionGenerator {
return transaction;
}
private Double[] getLocationForCard(String cardId, boolean generateAnomaly) {
Set<Double[]> locations = cardLocations.get(cardId);
private LocationPoint getLocationForCard(String cardId, boolean generateAnomaly) {
Set<LocationPoint> locations = cardLocations.get(cardId);
if (locations.isEmpty() || generateAnomaly) {
// Generate a random worldwide location
double latitude = ThreadLocalRandom.current().nextDouble(-90, 90);
double longitude = ThreadLocalRandom.current().nextDouble(-180, 180);
Double[] newLocation = {latitude, longitude};
LocationPoint newLocation = new LocationPoint(latitude, longitude);
// Store this location for future use unless it's an anomaly
if (!generateAnomaly) {
@ -110,7 +117,7 @@ public class TransactionGenerator {
return newLocation;
} else {
// Pick a random location from the card's history
Double[][] locArray = locations.toArray(new Double[0][]);
LocationPoint[] locArray = locations.toArray(new LocationPoint[0]);
return locArray[ThreadLocalRandom.current().nextInt(locArray.length)];
}
}
@ -135,9 +142,9 @@ public class TransactionGenerator {
double availableLimit = cardLimits.get(cardId);
// Get location
Double[] location = getLocationForCard(cardId, forceAnomaly && anomalyType == ANOMALY_LOCATION);
double latitude = location[0];
double longitude = location[1];
LocationPoint location = getLocationForCard(cardId, forceAnomaly && anomalyType == ANOMALY_LOCATION);
double latitude = location.latitude;
double longitude = location.longitude;
// Generate amount
double amount;
@ -161,4 +168,28 @@ public class TransactionGenerator {
Instant.now()
);
}
private static class LocationPoint {
final double latitude;
final double longitude;
LocationPoint(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LocationPoint that = (LocationPoint) o;
return Double.compare(that.latitude, latitude) == 0 &&
Double.compare(that.longitude, longitude) == 0;
}
@Override
public int hashCode() {
return Objects.hash(latitude, longitude);
}
}
}