I am trying to create a Java script to retrieve all of the logs from a particular workspace for a given date range and running into an issue where when trying to retrieve the additional pages, it seems that I get stuck in an infinite loop. Can someone help guide me in what I am doing wrong? FYI, I am using the “ibm-watson-7.2.0-jar-with-dependencies.jar” library
Here is the code that I have trying to get this working for the logs on 2019-08-08 (Aug 8, 2019)
Any and all help would be appreciated.
import com.ibm.watson.assistant.v1.Assistant;
import com.ibm.cloud.sdk.core.service.security.IamOptions;
import com.ibm.watson.assistant.v1.model.ListLogsOptions;
import com.ibm.watson.assistant.v1.model.LogCollection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
private static String apikey="<apikey>";
private static String version="2019-02-28";
private static String url="https://gateway.watsonplatform.net/assistant/api";
private static String workspaceId = "<workspaceid>";
private static int pagelimit=500;
private static LogCollection response;
private static String next_url,next_cursor;
private static String filter = "(response_timestamp>2019-08-07,response_timestamp<2019-08-09)";
public static void main(String[] args) {
// Connecting
IamOptions iamOptions = new IamOptions.Builder().apiKey(apikey).build();
Assistant service = new Assistant(version, iamOptions);
service.setEndPoint(url);
// Get first log set
ListLogsOptions options = new ListLogsOptions.Builder(workspaceId)
.pageLimit(pagelimit)
.filter(filter)
.build();
response = service.listLogs(options).execute().getResult();
next_url=get_nexturl(response);
next_cursor=get_nextcursor(response);
// while next_cursor has a value, keep looping
while (next_cursor!="") {
options = new ListLogsOptions.Builder(workspaceId).
cursor(next_cursor).
pageLimit(pagelimit).
filter(filter).
build();
response = service.listLogs(options).execute().getResult();
next_url=get_nexturl(response);
next_cursor=get_nextcursor(response);
} //end while
} //end main
private static String get_nexturl(LogCollection r) {
String p, nu;
// Get Pagination and next_url
p=return_key(r.toString(),"pagination");
nu=return_key(p,"next_url");
return nu;
} // end get_nexturl
private static String get_nextcursor(LogCollection r) {
String p, nc;
// Get Pagination and next_cursor
p=return_key(r.toString(),"pagination");
nc=return_key(p,"next_cursor");
return nc;
} // end get_nextcursor
// returns the value of a JSON key, with JSON being passed as a string surrounded by "{ }"
private static String return_key(String r, String key) {
String p;
try {
// Get the pagination
JSONObject jo = new JSONObject(r);
p=jo.get(key).toString();
return p;
} catch (JSONException e) {
return "";
}
} // end return_key
} // end class
Answer by mkistler (201) | Aug 21 at 05:02 PM
Not sure if this will solve your problem, but you don't need the functions for parsing through the result to get the nexturl and next cursor -- the SDK gives you accessors for these.
next_url = response.getPagination().getNextUrl();
next_cursor = response.getPagination().getNextCursor();
Thanks, that did indeed help. Using the built in functions, I was able to change my while loop condition to
while (next_cursor!=null) vs while (next_cursor!="")
and it is now working reliably
Thanks for the assist!