Created
March 12, 2020 10:24
-
-
Save RichardBradley/01f6a277f09877621ecbe9d6063505c7 to your computer and use it in GitHub Desktop.
AWS Gameday 2020 Market Chaser
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
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | |
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; | |
import com.amazonaws.services.dynamodbv2.model.AttributeValue; | |
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest; | |
import com.amazonaws.services.dynamodbv2.model.PutItemRequest; | |
import com.amazonaws.services.dynamodbv2.model.ScanResult; | |
import com.google.common.collect.ImmutableMap; | |
import com.google.common.collect.ImmutableSet; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
import java.net.URL; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
import static com.google.common.base.Preconditions.checkState; | |
public class MarketChaserMain { | |
public static void main(String[] args) throws Exception { | |
URL marketURL = new URL("http://mod-g-loadb-16ekcvsn17alj-247636672.us-east-1.elb.amazonaws.com/marketplace/"); | |
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().withRegion("us-east-1").build(); | |
Set<String> competitors = ImmutableSet.of("HR Approved", "Dookie"); | |
while (true) { | |
try { | |
System.out.printf("\n##########\n"); | |
Document marketDoc = Jsoup.parse(marketURL, 10000); | |
boolean isFirst = true; | |
List<Service> services = new ArrayList<>(); | |
for (Element row : marketDoc.body().getElementsByTag("tr")) { | |
if (isFirst) { | |
String str = row.getElementsByTag("th").stream().map(e -> e.text()).collect(Collectors.joining(", ")); | |
checkState(str.equals("Team Name, Service Type, URI, Average Latency (seconds), Success Rate (%)"), "Found: %s", str); | |
isFirst = false; | |
} else { | |
Service service = new Service(); | |
Elements cells = row.getElementsByTag("td"); | |
service.teamName = cells.get(0).text(); | |
service.type = cells.get(1).text(); | |
service.uri = cells.get(2).text(); | |
service.averageLatency = Double.parseDouble(cells.get(3).text()); | |
service.successPercent = Double.parseDouble(cells.get(4).text()); | |
services.add(service); | |
} | |
} | |
String[] serviceTypes = new String[]{"reverser", "leeter", "swapcaser"}; | |
ScanResult currentTable = amazonDynamoDB.scan("service-table", Collections.emptyMap()); | |
Set<String> currentServiceUrls = new HashSet<>(); | |
for (Map<String, AttributeValue> item : currentTable.getItems()) { | |
currentServiceUrls.add(item.get("Endpoint").getS()); | |
} | |
List<PutItemRequest> puts = new ArrayList<>(); | |
Set<String> chosenServiceUrls = new HashSet<>(); | |
for (String serviceType : serviceTypes) { | |
List<Service> best = services.stream() | |
.filter(s -> s.type.equals(serviceType)) | |
.filter(s -> s.successPercent == 100) | |
.filter(s -> !s.teamName.equals("Softwere")) | |
.filter(s -> !competitors.contains(s.teamName)) | |
.collect(Collectors.toList()); | |
System.out.printf("Found %s %s service(s): %s\n", best.size(), serviceType, best); | |
checkState(best.size() > 0); | |
for (Service chosenService : best) { | |
chosenServiceUrls.add(chosenService.uri); | |
if (!currentServiceUrls.contains(chosenService.uri)) { | |
puts.add(new PutItemRequest("service-table", ImmutableMap.of( | |
"Endpoint", new AttributeValue(chosenService.uri), | |
"ServiceType", new AttributeValue(chosenService.type), | |
"TeamName", new AttributeValue(chosenService.teamName)))); | |
} | |
} | |
} | |
List<DeleteItemRequest> deletes = | |
currentTable.getItems().stream() | |
.filter(item -> !chosenServiceUrls.contains(item.get("Endpoint").getS())) | |
.map(item -> new DeleteItemRequest( | |
"service-table", | |
ImmutableMap.of( | |
"Endpoint", new AttributeValue(item.get("Endpoint").getS()), | |
"ServiceType", new AttributeValue(item.get("ServiceType").getS())))) | |
.collect(Collectors.toList()); | |
System.out.printf("Adding: \n%s\n", puts); | |
puts.forEach(p -> amazonDynamoDB.putItem(p)); | |
System.out.printf("Deleting: \n%s\n", deletes); | |
deletes.forEach(d -> amazonDynamoDB.deleteItem(d)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
Thread.sleep(10000); | |
} | |
} | |
private static class Service { | |
String teamName; | |
String type; | |
String uri; | |
double averageLatency; | |
double successPercent; | |
@Override | |
public String toString() { | |
return "Service{" + | |
"teamName='" + teamName + '\'' + | |
", type='" + type + '\'' + | |
", uri='" + uri + '\'' + | |
", averageLatency=" + averageLatency + | |
", successPercent=" + successPercent + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment