Browse Source

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
master
Bianca Steffes 3 years ago
parent
commit
68a111cd69
  1. 66
      export/src/export/Main.java
  2. 2
      export/src/export/MainTest.java

66
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 <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
*/
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);
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<waitTimeMil)
{
try
@ -370,7 +404,7 @@ public class Main
{
}
};
lastRequest=System.currentTimeMillis();
lastRequestTimeInMillis=System.currentTimeMillis();
// Create connection
URL url = new URL(baseUrl + requestUrlExtension);
connection = (HttpsURLConnection) url.openConnection();
@ -481,6 +515,7 @@ public class Main
*/
static Tuple getAccessToken(Triplet refreshInfo)
{
checkRequestLimit();
String json=null;
int count =0;
do
@ -488,6 +523,7 @@ public class Main
try
{
json = makeOnePostRequest(refreshInfo);
dailyRequestCount++;
}
catch (ResponseCodeWrongException e)
{
@ -540,6 +576,7 @@ public class Main
List<Triplet> newRefreshTokens = new ArrayList<>();
Map<String, File> 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)
{

2
export/src/export/MainTest.java

@ -241,7 +241,7 @@ public class MainTest
Map<String, File> map = new HashMap<>();
map.put("athlete_0.json", temp);
Main.zipAllFiles(map);
Main.zipFiles(map);
}
catch (IOException e)

Loading…
Cancel
Save