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
This commit is contained in:
@@ -15,6 +15,8 @@ import java.net.URLEncoder;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -40,10 +42,13 @@ public class Main
|
|||||||
private static JSONParser parser = new JSONParser();
|
private static JSONParser parser = new JSONParser();
|
||||||
private static String testRequest;
|
private static String testRequest;
|
||||||
private static File errorFile;
|
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 requestLimit15Minutes = 100;
|
||||||
|
private static final int requestLimitDay = 1000 / 3;
|
||||||
|
|
||||||
|
private static int dailyRequestCount =0;
|
||||||
private static int waitTimeMil = 60000*3*15/requestLimit15Minutes;
|
private static int waitTimeMil = 60000*3*15/requestLimit15Minutes;
|
||||||
private static long lastRequest=0;
|
private static long lastRequestTimeInMillis=0;
|
||||||
|
|
||||||
private static void writeError(String text)
|
private static void writeError(String text)
|
||||||
{
|
{
|
||||||
@@ -224,6 +229,32 @@ public class Main
|
|||||||
return result;
|
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.
|
* 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.
|
* 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)
|
static String makeGetRequestWithRetry(String urlExtension, String token)
|
||||||
{
|
{
|
||||||
|
checkRequestLimit();
|
||||||
String json=null;
|
String json=null;
|
||||||
int count=0;
|
int count=0;
|
||||||
do
|
do
|
||||||
@@ -240,6 +272,7 @@ public class Main
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
json = makeOneGetRequest(urlExtension, token);
|
json = makeOneGetRequest(urlExtension, token);
|
||||||
|
dailyRequestCount++;
|
||||||
}
|
}
|
||||||
catch (ResponseCodeWrongException e)
|
catch (ResponseCodeWrongException e)
|
||||||
{
|
{
|
||||||
@@ -314,11 +347,12 @@ public class Main
|
|||||||
* Zip a list of files in one .zip file.
|
* Zip a list of files in one .zip file.
|
||||||
*
|
*
|
||||||
* @param files HasMap of <intended Filename, File> which should be zipped
|
* @param files HasMap of <intended Filename, File> which should be zipped
|
||||||
|
* @param count COunt or id to create distinct files each time
|
||||||
* @throws IOException If there was an error zipping
|
* @throws IOException If there was an error zipping
|
||||||
*/
|
*/
|
||||||
static void zipAllFiles(Map<String, File> files) throws IOException
|
static void zipFiles(Map<String, File> files, int count) throws IOException
|
||||||
{
|
{
|
||||||
FileOutputStream fos = new FileOutputStream("data.zip");
|
FileOutputStream fos = new FileOutputStream("data("+count+").zip");
|
||||||
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
for (String key : files.keySet())
|
for (String key : files.keySet())
|
||||||
{
|
{
|
||||||
@@ -359,7 +393,7 @@ public class Main
|
|||||||
HttpsURLConnection connection = null;
|
HttpsURLConnection connection = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
long timeSinceLastRequest =System.currentTimeMillis()-lastRequest;
|
long timeSinceLastRequest =System.currentTimeMillis()-lastRequestTimeInMillis;
|
||||||
if( timeSinceLastRequest<waitTimeMil)
|
if( timeSinceLastRequest<waitTimeMil)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -370,7 +404,7 @@ public class Main
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
lastRequest=System.currentTimeMillis();
|
lastRequestTimeInMillis=System.currentTimeMillis();
|
||||||
// Create connection
|
// Create connection
|
||||||
URL url = new URL(baseUrl + requestUrlExtension);
|
URL url = new URL(baseUrl + requestUrlExtension);
|
||||||
connection = (HttpsURLConnection) url.openConnection();
|
connection = (HttpsURLConnection) url.openConnection();
|
||||||
@@ -481,6 +515,7 @@ public class Main
|
|||||||
*/
|
*/
|
||||||
static Tuple getAccessToken(Triplet refreshInfo)
|
static Tuple getAccessToken(Triplet refreshInfo)
|
||||||
{
|
{
|
||||||
|
checkRequestLimit();
|
||||||
String json=null;
|
String json=null;
|
||||||
int count =0;
|
int count =0;
|
||||||
do
|
do
|
||||||
@@ -488,6 +523,7 @@ public class Main
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
json = makeOnePostRequest(refreshInfo);
|
json = makeOnePostRequest(refreshInfo);
|
||||||
|
dailyRequestCount++;
|
||||||
}
|
}
|
||||||
catch (ResponseCodeWrongException e)
|
catch (ResponseCodeWrongException e)
|
||||||
{
|
{
|
||||||
@@ -540,6 +576,7 @@ public class Main
|
|||||||
List<Triplet> newRefreshTokens = new ArrayList<>();
|
List<Triplet> newRefreshTokens = new ArrayList<>();
|
||||||
|
|
||||||
Map<String, File> allFiles = new HashMap<>();
|
Map<String, File> allFiles = new HashMap<>();
|
||||||
|
int zipcount =1;
|
||||||
for (Triplet oneUser : refreshTokens)
|
for (Triplet oneUser : refreshTokens)
|
||||||
{
|
{
|
||||||
// a is access_token and b is new refresh_token
|
// a is access_token and b is new refresh_token
|
||||||
@@ -556,11 +593,26 @@ public class Main
|
|||||||
{
|
{
|
||||||
allFiles.put("Athlete_" + athleteId + ".json", athlete);
|
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++;
|
athleteId++;
|
||||||
}
|
}
|
||||||
|
//zip the rest
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
zipAllFiles(allFiles);
|
zipFiles(allFiles, zipcount);
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -241,7 +241,7 @@ public class MainTest
|
|||||||
|
|
||||||
Map<String, File> map = new HashMap<>();
|
Map<String, File> map = new HashMap<>();
|
||||||
map.put("athlete_0.json", temp);
|
map.put("athlete_0.json", temp);
|
||||||
Main.zipAllFiles(map);
|
Main.zipFiles(map);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
|
|||||||
Reference in New Issue
Block a user