I am attempting to create a simple Java script which will connect to Rally, fetch all of the defects and return the defect details including the discussion as a Java object. The problem here is that the Discussion is returned as what I believe is a collection because only a URL is given. I am stuck on how to return the discussion for the defect as an object within the JSON rather than only another query which would have to be run separately (thousands of times I presume since we have thousands of defects).
Here is my code:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;
import org.json.simple.JSONArray;
public class ExtractData{
public static void main(String[] args) throws URISyntaxException, IOException, NumberFormatException
{
RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"), "apiKeyHere");
restApi.setProxy(URI.create("http://usernameHere:passwordHere0#proxyHere:8080"));
restApi.setApplicationName("QueryExample");
//Will store all of the parsed defect data
JSONArray defectData = new JSONArray();
try{
QueryRequest defects = new QueryRequest("defect");
defects.setFetch(new Fetch("FormattedID","Discussion","Resolution"));
defects.setQueryFilter(new QueryFilter("Resolution","=","Configuration Change"));
defects.setPageSize(5000);
defects.setLimit(5000);
QueryResponse queryResponse = restApi.query(defects);
if(queryResponse.wasSuccessful()){
System.out.println(String.format("\nTotal results: %d",queryResponse.getTotalResultCount()));
for(JsonElement result: queryResponse.getResults()){
JsonObject defect = result.getAsJsonObject();
System.out.println(defect);
}
}else{
System.err.print("The following errors occured: ");
for(String err: queryResponse.getErrors()){
System.err.println("\t+err");
}
}
}finally{
restApi.close();
}
}
}
Here is an example of what I am getting when I attempt this:
{"_rallyAPIMajor":"2","_rallyAPIMinor":"0","_ref":"https://rally1.rallydev.com/slm/webservice/v2.0/defect/30023232168","_refObjectUUID":"cea42323c2f-d276-4078-92cc-6fc32323ae","_objectVersion":"6","_refObjectName":"Example defect name","Discussion":{"_rallyAPIMajor":"2","_rallyAPIMinor":"0","_ref":"https://rally1.rallydev.com/slm/webservice/v2.0/Defect/32323912168/Discussion","_type":"ConversationPost","Count":0},"FormattedID":"DE332322","Resolution":"Configuration Change","Summary":{"Discussion":{"Count":0}},"_type":"Defect"}
As you can see the discussion is being returned as a URL rather than fetching the actual discussion. As this query will be used at runtime I'd prefer the entire object.
Unfortunately there is no way to get all of that data in one request- you'll have to load the Discussion collection for each defect you read. Also of note, the max page size is 2000.
This isn't exactly the same as what you're trying to do, but this example shows loading child stories much like you'd load discussions...
https://github.com/RallyCommunity/rally-java-rest-apps/blob/master/GetChildStories.java#L37
Related
I followed the steps on Google Cloud's Java and OpenTelemetry site (https://cloud.google.com/trace/docs/setup/java-ot) and made a simple hello world Java application locally and am trying to get my traces to show up on Google Cloud Trace using their trace exporter.
All the setup code is the same, and the program compiles and runs successfully. However, I don't see anything on the Trace dashboard. I know it is not an issue with IAM or my service account key because I ran the Python example and it shows up in Cloud Trace dashboard just fine.
Anyone have any guidance on why the Java version could be silently failing?
Thanks
package hello;
import org.joda.time.LocalTime;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
import java.io.IOException;
import java.util.Random;
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
import java.util.Collections;
import static java.util.Collections.singleton;
import java.time.Duration;
public class HelloWorld {
private static final Random random = new Random();
private static OpenTelemetry setupTraceExporter() {
try {
TraceExporter traceExporter = TraceExporter.createWithConfiguration(
TraceConfiguration.builder().setProjectId("my-test-id").build());
// Register the TraceExporter with OpenTelemetry
return OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())
.build())
.buildAndRegisterGlobal();
} catch (IOException e) {
System.out.println("Uncaught Exception");
System.out.println(e);
return null;
}
}
public static void main(String[] args) {
System.out.println("Starting the example application");
/* SET UP */
OpenTelemetry otel = setupTraceExporter();
/* Creating tracer */
Tracer tracer =
otel.getTracer("java foo");
Span span = tracer.spanBuilder("my span").startSpan();
// put the span into the current Context
try (Scope scope = span.makeCurrent()) {
System.out.println("Hello");
Thread.sleep(4000);
} catch (Throwable t) {
span.setStatus(StatusCode.ERROR, "error");
System.out.println(t);
} finally {
span.end();
}
System.out.println("Closing");
//otel.getSdkTracerProvider().shutdown();
}
}
After some debugging, I figured out the answer.
Seems like with this simple example, the BatchSpanProcessor is not a good idea because there is only one span that is getting traced.
SimpleSpanProcessor directly forwards the spans to Cloud Trace no matter what whereas BatchSpanProcessor waits until there is enough data before pushing to Cloud Trace. Hence why I was not seeing anything in Cloud Trace because BatchSpanProcessor hadn't registered enough spans for it to actually upload it to Google Cloud.
Span Processors Documentation
Change the following lines
return OpenTelemetrySdk.builder()
.setTracerProvider(
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(traceExporter))
.build())
.buildAndRegisterGlobal();
Hope this helps others!
I am looking for JAVA example of Amazon Forecast API so I can integrate this in my JAVA Application.
I searched and didn't found any solution, even I raised a support ticket with the AWS team and they are also unable to provide that which I am attaching as a screenshot.
Documentations are available for python, NodeJS, and other languages but not for JAVA.
I have already struggled a lot in integration with AWS Forecast Java SDK.
UPDATE
Finally, I got something that I am posting in my below answer but still looking for some better option.
After spending a few days in search of documentation or working example, I got this solution for me. I am able to get the predictions by using this code but still looking for some better approach (if possible).
package com.mayur.awsforecastexample;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.forecastquery.AmazonForecastQueryClientBuilder;
import com.amazonaws.services.forecastquery.model.DataPoint;
import com.amazonaws.services.forecastquery.model.Forecast;
import com.amazonaws.services.forecastquery.model.QueryForecastRequest;
import com.amazonaws.services.forecastquery.model.QueryForecastResult;
public class ForecastTest {
AmazonForecastQueryClientBuilder client = AmazonForecastQueryClientBuilder.standard();
public QueryForecastResult queryForecast(QueryForecastRequest request) {
client.setCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY")));
client.setRegion("REGION");
return client.build().queryForecast(request);
}
public static void main(String ar[]) {
Map<String, String> filters = new HashMap<String, String>();
filters.put("item_id", "YOUR_ITEM_ID");
QueryForecastRequest request = new QueryForecastRequest();
request.setForecastArn("FORECAST_ARN");
request.setFilters(filters);
request.setStartDate(null);
request.setEndDate(null);
ForecastTest forecastTest = new ForecastTest();
QueryForecastResult res = forecastTest.queryForecast(request);
Forecast f = res.getForecast();
Map<String, List<DataPoint>> predictions = f.getPredictions();
for (Entry<String, List<DataPoint>> entry : predictions.entrySet())
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
Please check the working Example
I am trying to pull discussions for a given defect. I understand from a prior question I asked that it is not possible to pull the discussion data as a property of the defect itself rather I must run a separate fetch request.
The problem is that I can not identify any query filter to use when pulling conversation posts. This leads me to believe I would have to loop through every single conversation post and try to find the matching defect number in the actual data returned which would be highly inefficient.
Rather I would prefer to simply run a query fetch for each defect that uses a query filter for the formatted ID that will only return the conversation posts that apply for that defect.
import com.google.gson.JsonElement;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class ExtractFull {
#SuppressWarnings("unchecked")
public static void main(String args[]) throws URISyntaxException, IOException {
RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"), "_myapikey");
restApi.setApplicationName("DANA Example");
restApi.setProxy(new URI("http://myproxy:8080"), "username", "pass");
System.out.println("Querying Rally for defects, this may take some time");
try {
QueryRequest defectRequest = new QueryRequest("ConversationPost");
defectRequest.setPageSize(2000);
defectRequest.setLimit(5000);
QueryFilter filter = new QueryFilter("FormattedID","=","DE10101");
defectRequest.setQueryFilter(filter);
defectRequest.setFetch(new Fetch());
QueryResponse queryResponse = restApi.query(defectRequest);
for(JsonElement result: queryResponse.getResults()){
System.out.println(result);
}
} finally {
restApi.close();
}
}
}
This code doesn't work. I assume because "FormattedId" isn't a valid object of the "ConversationPost" type. I don't know if it's possible to filter for parent defect ID when querying a conversation post but that is what I need to do.
Specifically the code I am referring to is here:
QueryRequest defectRequest = new QueryRequest("ConversationPost");
defectRequest.setPageSize(2000);
defectRequest.setLimit(5000);
QueryFilter filter = new QueryFilter("FormattedID","=","DE10101");
defectRequest.setQueryFilter(filter);
Use the standard WSAPI, I can query like this:
(Artifact.FormattedID = "US123")
Was able to solve this issue on my own. The problem was that I was attempting to use "QueryRequest" without having a valid "type" to pass to the constructor.
The correct solution was to use "GetRequest" with the path to the defect discussion page being passed as the url (without requiring me to set an object type in the constructor). This returned a GetRequest object which contained a result set with all of the conversation posts.
GetRequest getRequest = new GetRequest(discussionURL);
GetResponse getResponse = restApi.get(getRequest);
The "discussion url" did not contain the "https://rally1.rallydev.com" which was declared when creating the rallyApi - the discussionURL variable contains the entire defect discussion page URL but without the above rally api url so for example "/slm/webservice/v2.0/Defect/106032660792/Discussion"
I am trying to determine how to extract the discussion data for a defect in Rally using the Java Rally API. Unfortunately I can find no help online or in the documentation that tells me how to do this. I am able to obtain the URL to the discussion data and return it as a JSON element but I am not sure how to take the final step of querying that URL to get the discussions as another JSON object - I'd really appreciate help!
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.*;
import com.rallydev.rest.response.*;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class ExtractDiscussions
{
public static void main(String args[]) throws URISyntaxException, IOException {
RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"), "myApiKey");
restApi.setApplicationName("DANA Example");
restApi.setProxy(new URI("myProxy"),"myUsername","myPassword");
try {
QueryRequest defectRequest = new QueryRequest("defect");
defectRequest.setQueryFilter(new QueryFilter("FormattedID","=","DE123456"));
defectRequest.setFetch(new Fetch());
//defectRequest.setPageSize(25);
//defectRequest.setLimit(100);
QueryResponse queryResponse = restApi.query(defectRequest);
System.out.println(queryResponse.getTotalResultCount());
JsonObject obj = queryResponse.getResults().get(0).getAsJsonObject();
obj = obj.getAsJsonObject("Discussion");
JsonElement discussionLink = obj.get("_ref");
System.out.println(discussionLink);
//Code would go here to fetch the discussion using the discussion link
}finally{
restApi.close();
}
}
}
My Results:
1
"https://rally1.rallydev.com/slm/webservice/v2.0/Defect/1321234562/Discussion"
If you do a GetRequest on that URL, you will be given back the collection of Conversation Posts. Handy tips are in here: https://rally1.rallydev.com/slm/doc/webservice/
I want to know how can i get the list of all the repositories present in my github enterprise(private). I am unable to identify how should i use my personal access token to get authentication through java code.
I have already tried with public repositories and i am able to use everything in that but i am unable to do this with my enterprise github.
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.apache.commons.codec.binary.Base64;
public class httpget {
public static void main(String args[]) throws IOException, ParseException,JSONException
{
URL url=new URL("https://github---.com/api/v3/...");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
String token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String authString="Basic"+Base64.encodeBase64(token.getBytes());
conn.setRequestProperty("Authorization", authString);
conn.connect();
String inline="";
Scanner sc = new Scanner(url.openStream());
while(sc.hasNext())
{
inline+=sc.nextLine();
}
sc.close();
System.out.println(inline);
}
}
Best way do do that in java without the burden of rest authent is to use one of the Java API available.
This github page list all the APIs :
https://developer.github.com/v3/libraries/
And you have 2 java intersting API:
egit-github : https://github.com/eclipse/egit-github/tree/master/org.eclipse.egit.github.core
and
kohsuke : http://github-api.kohsuke.org/
egit-github seems quite easy to work with...