From 68a111cd69b6093c8f3fe5d2a4093677471327ce Mon Sep 17 00:00:00 2001 From: Bianca Steffes Date: Mon, 18 Oct 2021 15:04:20 +0200 Subject: [PATCH] Retry fixed so it doesn't exhaust the daily limit: it now considers the 15 Minute limit AND the daily limit Additionally the zipping was changed: Always after 10 athletes, these files will already be zipped so there won't be one big resulting zip file --- export/src/export/Main.java | 66 +++++++++++++++++++++++++++++---- export/src/export/MainTest.java | 2 +- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/export/src/export/Main.java b/export/src/export/Main.java index 569926b..15dd233 100644 --- a/export/src/export/Main.java +++ b/export/src/export/Main.java @@ -15,6 +15,8 @@ import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -40,10 +42,13 @@ public class Main private static JSONParser parser = new JSONParser(); private static String testRequest; private static File errorFile; - //TODO: enduco needs to insert the correct request limit for 15 Minutes here + //TODO: enduco needs to insert the correct request limits here private static final int requestLimit15Minutes = 100; + private static final int requestLimitDay = 1000 / 3; + + private static int dailyRequestCount =0; private static int waitTimeMil = 60000*3*15/requestLimit15Minutes; - private static long lastRequest=0; + private static long lastRequestTimeInMillis=0; private static void writeError(String text) { @@ -224,6 +229,32 @@ public class Main return result; } + /** + * In the case that the daily request limit has been reached, this method waits for the next day + */ + static void checkRequestLimit() + { + if(dailyRequestCount>requestLimitDay) //daily request limit reached, wait until the next day + { + Calendar tomorrow = new GregorianCalendar(); + tomorrow.setTimeInMillis(System.currentTimeMillis()); + tomorrow.set(Calendar.DAY_OF_YEAR, tomorrow.get(Calendar.DAY_OF_YEAR)+1); + if(tomorrow.get(Calendar.DAY_OF_YEAR)==1) //reached a new year ... + { + tomorrow.set(Calendar.YEAR, 2022); + } + tomorrow.set(Calendar.HOUR_OF_DAY, 0); + tomorrow.set(Calendar.MINUTE, 0); + try + { + TimeUnit.MILLISECONDS.sleep(tomorrow.getTimeInMillis()-System.currentTimeMillis()); + } + catch (InterruptedException e1) + { + } + } + } + /** * Method is used to find the next request window: It tries the same request again after 5 minutes. * After a set number of times (retryTimes) it stops if it still wasn't successful. @@ -233,6 +264,7 @@ public class Main */ static String makeGetRequestWithRetry(String urlExtension, String token) { + checkRequestLimit(); String json=null; int count=0; do @@ -240,6 +272,7 @@ public class Main try { json = makeOneGetRequest(urlExtension, token); + dailyRequestCount++; } catch (ResponseCodeWrongException e) { @@ -314,11 +347,12 @@ public class Main * Zip a list of files in one .zip file. * * @param files HasMap of which should be zipped + * @param count COunt or id to create distinct files each time * @throws IOException If there was an error zipping */ - static void zipAllFiles(Map files) throws IOException + static void zipFiles(Map files, int count) throws IOException { - FileOutputStream fos = new FileOutputStream("data.zip"); + FileOutputStream fos = new FileOutputStream("data("+count+").zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String key : files.keySet()) { @@ -359,7 +393,7 @@ public class Main HttpsURLConnection connection = null; try { - long timeSinceLastRequest =System.currentTimeMillis()-lastRequest; + long timeSinceLastRequest =System.currentTimeMillis()-lastRequestTimeInMillis; if( timeSinceLastRequest newRefreshTokens = new ArrayList<>(); Map allFiles = new HashMap<>(); + int zipcount =1; for (Triplet oneUser : refreshTokens) { // a is access_token and b is new refresh_token @@ -556,11 +593,26 @@ public class Main { allFiles.put("Athlete_" + athleteId + ".json", athlete); } + //pack zip-files of 10 athletes + if(allFiles.size()>=10) + { + try + { + zipFiles(allFiles, zipcount); + zipcount++; + allFiles=new HashMap<>(); + } + catch (IOException e) + { + writeError("Files coulnd't be zipped"); + } + } athleteId++; } + //zip the rest try { - zipAllFiles(allFiles); + zipFiles(allFiles, zipcount); } catch (IOException e) { diff --git a/export/src/export/MainTest.java b/export/src/export/MainTest.java index 86d712e..5bc61f6 100644 --- a/export/src/export/MainTest.java +++ b/export/src/export/MainTest.java @@ -241,7 +241,7 @@ public class MainTest Map map = new HashMap<>(); map.put("athlete_0.json", temp); - Main.zipAllFiles(map); + Main.zipFiles(map); } catch (IOException e)