How to find what went wrong during eval in R? - java

Part of the code:
Rengine re = getRengine();
re.eval("library(quantmod)");
re.eval("library(PerformanceAnalytics)");
re.eval("library(tseries)");
re.eval("library(FinTS)");
re.eval("library(rugarch)");
re.eval("library(robustbase)");
re.assign("arLagNum", new double[]{1});
re.assign("maLagNum", new double[]{1});
re.assign("archLagNum", new double[]{1});
re.assign("garchLagNum", new double[]{1});
re.eval("garchSpec <- ugarchspec(variance.model = list(model=\"iGARCH\", garchOrder=c(archLagNum,garchLagNum)), mean.model = list(armaOrder=c(arLagNum,maLagNum)), distribution.model=\"std\")");
re.assign("transformedTsValueData", new double[]{getSomeDoubles()};
re.eval("estimates <- ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))");
re.eval("estimates");
The last line returns null. The API documentation says: "the eval method returns null if something went wrong". How do I find out what went wrong?

Granted it's not the most elegant, but you could try getting some information if you put your command in a try catch:
re.eval("estimates <-tryCatch(suppressWarnings(ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");
you can then evaluate the response by inspecting the first few 3 characters.
If you don't want to do this for every call, you could repeat your last command if your response is null when you're not expecting it (and repeating something that went wrong usually doesn't take too long).
Edit:
Come to think of it, if the error occurs only when you evaluate "estimates", it may be better to wrap that last one in the try catch:
re.eval("tryCatch(suppressWarnings(estimates), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");

Related

GraphAPI Creating a new MSTeam right after creating a new group fails with 404

I want to form a new MSTeam from a newly created group.
I created a new GraphServiceClient
I created a new AuthProvider
I created a new Group with the client like this:
graphApplicationClient.groups().buildRequest().post(group);
It works till here. I get a Group object as a response and can work with it. It has an Id and so on
Right after that I want to create a new team out of this group, so I call
ITeamRequest iTeamRequest = graphApplicationClient.groups(pGroupId).team().buildRequest();
iTeamRequest.setMaxRetries(3);
iTeamRequest.setDelay(10);
Team newTeam = iTeamRequest.post(team);
where pGroupId is the Id I got from the call before.
(In this case: "team" is a new Team-Object created right before this call, like in the MS-Docs.)
I know the sentence from MS-Docs:
If the group was created less than 15 minutes ago, it's possible for
the Create team call to fail with a 404 error code due to replication
delays. The recommended pattern is to retry the Create team call three
times, with a 10 second delay between calls.
thats why I added the maxRetries and Delay..
I watched this video:
https://youtu.be/ybGm1qWVi-k?t=650
in which two MS Employees do exactly the same as I do...
What am I missing? Or is there a workaround for it?
This is the response:
404 : Not Found
Strict-Transport-Security : max-age=31536000
Cache-Control : private
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceC","Ring":"5","ScaleUnit":"002","RoleInstance":"AGSFE_IN_51"}}
client-request-id : b39c16e4-f786-4d42-865e-9f0cf23ed46f
request-id : 07efc77c-02d3-45f7-85ab-6ca2241d1859
Content-Length : 198
Date : Thu, 23 Apr 2020 12:39:30 GMT
Content-Type : application/json
{
"error": {
"code": "UnknownError",
"message": "",
"innerError": {
"request-id": "07efc77c-02d3-45f7-85ab-6ca2241d1859",
"date": "2020-04-23T12:39:30"
}
}
}
It also does not work, if I put more retries (10 = Max) and more Delay (180 = Max) in it...
And there is no PUT in java..
Best regards
EDIT1: I tried to get it done with two other options.. first with ScribeJava and second with a normal httpRequest.. none of them works. I only get it done with graphExplorer.. With the other options it says BadRequest, but when i copy everything I got in code to GraphExplorer it works... help me :(
EDIT2: I have a group that's been around for a day. From this I wanted to create a new team. Still 404 Not Found. I tried it with GraphAPI and ScribeJava.. None of these calls are working. Debugged through, copied all information, put it in Postman, works fine.
For everyone who has the same problem as I. GraphAPI provides a customRequest() function.
I use the same token, the same url, the same properties, but using the customRequest with PUT. Works...
CustomRequest<JsonObject> createTeamCustomRequest = graphApplicationClient.customRequest("/groups/" + pGroupId + "/team").buildRequest();
createTeamCustomRequest.setMaxRetries(3);
createTeamCustomRequest.setDelay(10);
JsonObject newTeam = _createTeamInJson();
JsonObject createdTeam = createTeamCustomRequest.put(newTeam);
This is the _createTeamInJson function. Returns the same body as I create a Team with GraphAPI
private JsonObject _createTeamInJson()
{
JsonObject teamPayload = new JsonObject();
JsonObject memberSettings = new JsonObject();
JsonObject messagingSettings = new JsonObject();
JsonObject funSettings = new JsonObject();
memberSettings.addProperty("allowCreateUpdateChannels", true);
messagingSettings.addProperty("allowUserEditMessages", true);
messagingSettings.addProperty("allowUserDeleteMessages", true);
funSettings.addProperty("allowGiphy", true);
funSettings.addProperty("giphyContentRating", "strict");
teamPayload.add("memberSettings", memberSettings);
teamPayload.add("messagingSettings", messagingSettings);
teamPayload.add("funSettings", funSettings);
return teamPayload;
}
This isn't the right solution for it, because it should work with the normal GraphAPI, that's why I don't mark this as an answer. But it is a workaround for everyone who has the same problem..

Iteration through json with multiple API calls for other requests

I am using Postman to iterate through a json of about 40 pairs of items. I need to then take that array created and run an API call for each element in the array to return a set of results. Using the code here, i'm only able to pull the final element in the array. I attempted to put the postman.setNextRequest in the for loop but then I found out that no matter where it is, it always executes last.
tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);
if (responseCode.code === 200) {
var jsonData = pm.response.json();
var json = [];
postman.setEnvironmentVariable("json", jsonData)
postman.setNextRequest('GetAdmins');
for (var key in jsonData ) {
if (jsonData.hasOwnProperty(key)) {
postman.setEnvironmentVariable("organizationId", jsonData[key].id)
postman.setEnvironmentVariable("orgname", jsonData[key].name)
tests[jsonData[key].name + " " + jsonData[key].id] = !!jsonData[key].name;
}
}
}
else {
postman.setNextRequest(null);
}
GetAdmins is another GET that uses {{organizationId}} in the call.
I think what i'm looking for is; what is the best way to go about running another API call on each element in the json?
Thanks in advance!
EDIT: Adding JSON output
[
{
"id": XXXXXX,
"name": "Name1"
},
{
"id": XXXXXX,
"name": "Name2"
},
{
"id": XXXXXX,
"name": "Name3"
}
]
This might work to get the data - I’ve not tried it out yet though so it might not work first time.
var jsonData = pm.response.json()
data = _.map(jsonData, item => {
organizationId: item.id
orgName: item.name
})
pm.environment.set('organizationData', JSON.stringify(data))
Then you have all of your organization data in a variable and you can use these to iterate over the Id’s in the next "Get Admins" request.
You would need to have some code in the Pre-request script of the next request to access each of the id’s to iterate over in the request. You need to parse the variable like this:
var orgID = pm.environment.get(JSON.parse("organizationData"))
Then orgID[0].organizationId would be the first one in the list.
Not a complete solution for your problem but it might help you get the data.
I was able to solve this using these two guides:
Loops and dynamic variables in Postman: part 1
Loops and dynamic variables in Postman: part 2
I also had to implement the bigint fix for java, but in Postman, which was very annoying... that can be found here:
Hacking bigint in API testing with Postman Runner Newman in CI Environment
Gist
A lot of google plus trial and error got me up and running.
Thanks anyway for all your help everyone!
This ended up being my final code:
GetOrgs
tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);
eval(postman.getGlobalVariable("bigint_fix"));
var jsonData = JSON.parse(responseBody);
var id_list = [];
jsonData.forEach(function(list) {
var testTitle = "Org: " + list.name + " has id: " + JSON.stringify(list.id);
id_list.push(list.id);
tests[testTitle] = !!list.id;
});
postman.setEnvironmentVariable("organizationId",JSON.stringify(id_list.shift()));
postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
postman.setNextRequest("GetAdmins");
GetAdmins
eval(postman.getGlobalVariable("bigint_fix"));
var jsonData = JSON.parse(responseBody);
jsonData.forEach(function(admin) {
var testTitle = "Admin: " + admin.name + " has " + admin.orgAccess;
tests[testTitle] = !!admin.name;
});
var id_list = JSON.parse(environment.id_list);
if (id_list.length > 0) {
postman.setEnvironmentVariable("organizationId", JSON.stringify(id_list.shift());
postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
postman.setNextRequest("GetAdmins");
}
else {
postman.clearEnvrionmentVariable("organizationId");
postman.clearEnvironmentVariable("id_list");
}

NoraUI - "Cannot infer Type argument" error using Result.Warning<> in a custom step

I need to raise a warning during one of my scenario but i don't stop to have this error appearing : "Cannot infer type arguments for Result.Warning<>"
I actually tried to raise the Warning the same way i was raising Failure until now :
new Result.Warning<>(targetKey, Messages.format(TaroMessages.WARNING_RESOURCES_VALUE_DIFFERENCE_AFTER_REAFFECTATION, existing_value, new_value), true, oscarAccesClientPage.getCallBack());
The custom step i am using it inside is the following : I'm trying to go over a list of Element and checking that the existing value of them is the same or not as the one saved before.
protected void checkXyResourcesValue(Integer xyIterator, List<WebElement> elements, String keyParameter) throws TechnicalException, FailureException {
try {
Integer resIterator = 1;
for(WebElement element : elements) {
String targetKey = "XY" + xyIterator + "RES" + resIterator + keyParameter;
String new_value = element.getAttribute(VALUE) != null ? element.getAttribute(VALUE) : element.getText();
String existing_value = Context.getValue(targetKey) != null ? Context.getValue(targetKey) : targetKey;
if (new_value != existing_value) {
new Result.Warning<>(targetKey, Messages.format(TaroMessages.WARNING_RESOURCES_VALUE_DIFFERENCE_AFTER_REAFFECTATION, existing_value, new_value), true, oscarAccesClientPage.getCallBack());
}
resIterator++;
}
} catch (Exception e) {
new Result.Failure<>(e.getMessage(), Messages.format(TaroMessages.FAIL_MESSAGE_ACCES_CLIENT_XY_CHECK_RESOURCES_VALUE, keyParameter, xyIterator), true, oscarAccesClientPage.getCallBack());
}
}
For the method to check and saved value I actually inspired myself for the piece of code from NoraUI to save a value on Context or read it from.
I'm using Eclipse Luna 4.4.2 and i try to compile using JDK1.8.0_131.
It may be more related to me not knowing how this work in Java than a real problem so thank you in advance for your help or insights. Don't hesitate to ask if you need more information on the piece of code or the context.
new Result.Warning<>(targetKey, Messages.format(TaroMessages.WARNING_RESOURCES_VALUE_DIFFERENCE_AFTER_REAFFECTATION, existing_value, new_value), true, 0);
use 0 if you do not use any Model (data serialized) or use id of your Object in the serial.

Conversation ID leads to unkown path in graph-api

I have a code that fetches conversations and the messages inside them (a specific number of pages). It works most of the time, but for certain conversations it throws an exception, such as:
Exception in thread "main" com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: Unknown path components: /[id of the message]/messages (code 2500, subcode null)
at com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookClient.java:1192)
at com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:1118)
at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:1059)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:970)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:932)
at com.restfb.DefaultFacebookClient.fetchConnection(DefaultFacebookClient.java:356)
at test.Test.main(Test.java:40)
After debugging I found the ID that doesn't work and tried to access it from graph-api, which results in an "unknown path components" error. I also attempted to manually find the conversation in me/conversations and click the next page link in the graph api explorer which also lead to the same error.
Is there a different way to retrieve a conversation than by ID? And if not, could someone show me an example to verify first if the conversation ID is valid, so if there are conversations I can't retrieve I could skip them instead of getting an error. Here's my current code:
Connection<Conversation> fetchedConversations = fbClient.fetchConnection("me/Conversations", Conversation.class);
int pageCnt = 2;
for (List<Conversation> conversationPage : fetchedConversations) {
for (Conversation aConversation : conversationPage) {
String id = aConversation.getId();
//The line of code which causes the exception
Connection<Message> messages = fbClient.fetchConnection(id + "/messages", Message.class, Parameter.with("fields", "message,created_time,from,id"));
int tempCnt = 0;
for (List<Message> messagePage : messages) {
for (Message msg : messagePage) {
System.out.println(msg.getFrom().getName());
System.out.println(msg.getMessage());
}
if (tempCnt == pageCnt) {
break;
}
tempCnt++;
}
}
}
Thanks in advance!
Update: Surrounded the problematic part with a try catch as a temporary solution, also counted the number of occurrences and it only effects 3 out of 53 conversations. I also printed all the IDs, and it seems that these 3 IDs are the only ones that contain a "/" symbol, I'm guessing it has something to do with the exception.
The IDs that work look something like this: t_[text] (sometimes a "." or a ":" symbol) and the ones that cause an exception are always t_[text]/[text]
conv_id/messages is not a valid graph api call.
messages is a field of conversation.
Here is what you do (single call to api):
Connection<Conversation> conversations = facebookClient.fetchConnection("me/conversations", Conversation.class);
for (Conversation conv : conversations.getData()) {
// To get list of messages for given conversation
LinkedList<Message> allConvMessagesStorage = new LinkedList<Message>();
Connection<Message> messages25 = facebookClient.fetchConnection(conv.getId()+"/messages", Message.class);
//Add messages returned
allConvMessagesStorage.addAll(messages25.getData());
//Check if there is next page to fetch
boolean progress = messages25.hasNext();
while(progress){
messages25 = facebookClient.fetchConnectionPage(messages25.getNextPageUrl(), Message.class);
//Append next page of messages
allConvMessagesStorage.addAll(messages25.getData());
progress = messages25.hasNext();
}
}

Getting JSON from System.out.println

I am trying to do sentiment analysis and project the values on Google Visualization.
I am calling this python script using my java program
Code Snippet (for AlchemyAPI)
https://github.com/AlchemyAPI/alchemyapi-twitter-python
I wrote a java program to call the python script.
import java.io.*;
public class twitmain {
public String twittersentiment(String[] args) throws IOException {
// set up the command and parameter
String pythonScriptPath = "/twitter/analyze.py"; // I'm calling AlchemyAPI
String[] cmd = new String[2 + args.length];
cmd[0] = "C:\\Python27\\python.exe";
cmd[1] = pythonScriptPath;
for (int i = 0; i < args.length; i++) {
cmd[i + 2] = args[i];
}
// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
// retrieve output from python script
BufferedReader bfr = new BufferedReader(new InputStreamReader(
pr.getInputStream()));
String line = ""; int i=0;
while ((line = bfr.readLine()) != null) {
System.out.println(line);
}
return line;
}
}
Output:
I am getting tweets and final statistics as follows:
##########################################################
# The Tweets #
##########################################################
#uDiZnoGouD
Date: Mon Apr 07 05:07:19 +0000 2014
To enjoy in case you win!
To help you sulk in case you loose!
#IndiavsSriLanka #T20final http://t.co/hRAsIa19zD
Document Sentiment: positive (Score: 0.261738)
##########################################################
# The Stats #
##########################################################
Document-Level Sentiment:
Positive: 3 (60.00%)
Negative: 1 (20.00%)
Neutral: 1 (20.00%)
Total: 5 (100.00%)
Problem (Question):
How do I just scrape Positive, Negitive, Neutral and send it to Google Visualization? (I.e make a JSON?)
Any help would be really appreciated.
Shoot, I just realized you were asking the other way around. To write the parsing app in Java.
Anyway the idea would be the same, but the language would differ.
But that would also mean that you have access to the sources of the python app, so you can maybe dig around there, and you could dump the result object into the console as a JSON object.
Original answer in python:
You should identify the types of lines and parse them and construct the JSON object yourself.
Like for every line:
import re
json_obj = {}
pattern = "^(\w+): (\d) \((\d{2}\.\d{2}%)\)$"
match = re.match(pattern, line)
if match:
prop_obj = { "value": match[2], "percent": match[3] }
json_obj[match[1]] = prop_obj
This would transform the line:
Positive: 3 (60.00%)
into
{
Positive: {
value: "3"
percent: "60.00%"
}
}
Taking this idea further, the parsing rules should be a dictionary of pattern-extractor_methods as key-values
var parse_rules = {
"^(\w+): (\d) \((\d{2}\.\d{2}%)\)$":
def (matches):
return { match[1]: { "value": match[2], "percent": match[3] }}
, ...
}
And for each line you would test against the parse rules and execute the methods if a match is found, and the result of the method is merged in the JSON result object
This is a lot of work (depending on the complexity of the java app, but I'd go this way if the Java app cannot be modified.
Regex explanation & example

Categories

Resources