Search in sources :

Example 1 with TrackRequest

use of io.jenkins.blueocean.analytics.Analytics.TrackRequest in project blueocean-plugin by jenkinsci.

the class PipelinePluginAnalytics method onCompleted.

@Override
public void onCompleted(WorkflowRun workflowRun, @Nonnull TaskListener listener) {
    Analytics analytics = Analytics.get();
    if (analytics == null) {
        return;
    }
    // Tally up all the steps used in this run
    Tally tally = new Tally();
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(workflowRun);
    builder.getPipelineNodeSteps(new Link("steps/")).forEach(step -> tally.count(step.getStepType()));
    boolean isDeclarative = workflowRun.getParent().getAction(DeclarativeJobAction.class) != null;
    Result result = workflowRun.getResult();
    String resultAsString = result != null ? result.toString() : "UNKNOWN";
    // Send event for each step used in this run
    tally.get().forEach((key, value) -> {
        Map<String, Object> props = new HashMap<>();
        props.put("type", key);
        props.put("timesUsed", value);
        props.put("isDeclarative", isDeclarative);
        props.put("runResult", resultAsString);
        analytics.track(new TrackRequest("pipeline_step_used", props));
    });
}
Also used : Tally(io.jenkins.blueocean.service.embedded.analytics.Tally) HashMap(java.util.HashMap) TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) DeclarativeJobAction(org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction) Analytics(io.jenkins.blueocean.analytics.Analytics) Link(io.jenkins.blueocean.rest.hal.Link) NodeGraphBuilder(io.jenkins.blueocean.rest.impl.pipeline.NodeGraphBuilder) Result(hudson.model.Result)

Example 2 with TrackRequest

use of io.jenkins.blueocean.analytics.Analytics.TrackRequest in project blueocean-plugin by jenkinsci.

the class PipelinePluginAnalyticsTest method testGenerateAnalyticsEventWithDeclarative.

@Test(timeout = 120000)
public void testGenerateAnalyticsEventWithDeclarative() throws Exception {
    // Create single scripted pipeline
    createAndRunPipeline("JobAnalyticsTest-declarative.jenkinsfile");
    AnalyticsImpl analytics = (AnalyticsImpl) Analytics.get();
    Assert.assertNotNull(analytics);
    TrackRequest req = analytics.lastReq;
    Assert.assertNotNull(req);
    Assert.assertEquals("pipeline_step_used", req.name);
    Map<String, Object> properties = req.properties;
    Assert.assertEquals("type", "org.jenkinsci.plugins.workflow.steps.EchoStep", properties.get("type"));
    Assert.assertEquals("timesUsed", 2, properties.get("timesUsed"));
    Assert.assertEquals("isDeclarative", true, properties.get("isDeclarative"));
    Assert.assertEquals("runResult", "SUCCESS", properties.get("runResult"));
}
Also used : TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 3 with TrackRequest

use of io.jenkins.blueocean.analytics.Analytics.TrackRequest in project blueocean-plugin by jenkinsci.

the class PipelinePluginAnalyticsTest method testGeneratedAnalyticsEventWithScriptedFunction.

@Test(timeout = 120000)
public void testGeneratedAnalyticsEventWithScriptedFunction() throws Exception {
    createAndRunPipeline("PipelinePluginAnalyticsTest-scripted-function.jenkinsfile");
    AnalyticsImpl analytics = (AnalyticsImpl) Analytics.get();
    Assert.assertNotNull(analytics);
    TrackRequest req = analytics.lastReq;
    Assert.assertNotNull(req);
    Assert.assertEquals("pipeline_step_used", req.name);
    Map<String, Object> properties = req.properties;
    Assert.assertEquals("type", "org.jenkinsci.plugins.workflow.steps.EchoStep", properties.get("type"));
    Assert.assertEquals("timesUsed", 1, properties.get("timesUsed"));
    Assert.assertEquals("isDeclarative", false, properties.get("isDeclarative"));
    Assert.assertEquals("runResult", "SUCCESS", properties.get("runResult"));
}
Also used : TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 4 with TrackRequest

use of io.jenkins.blueocean.analytics.Analytics.TrackRequest in project blueocean-plugin by jenkinsci.

the class AnalyticsTest method nullNameInTrackRequest.

@Test
public void nullNameInTrackRequest() {
    try {
        analytics.track(new TrackRequest(null, null));
        Assert.fail("did not throw exception");
    } catch (ServiceException.BadRequestException e) {
        Assert.assertEquals("missing name", e.getMessage());
    }
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) Test(org.junit.Test)

Example 5 with TrackRequest

use of io.jenkins.blueocean.analytics.Analytics.TrackRequest in project blueocean-plugin by jenkinsci.

the class AnalyticsRoute method track.

@POST
@WebMethod(name = "track")
public void track(StaplerRequest staplerRequest) throws IOException {
    Analytics analytics = Analytics.get();
    if (analytics == null) {
        return;
    }
    TrackRequest req;
    try (InputStream is = staplerRequest.getInputStream()) {
        req = JsonConverter.toJava(is, TrackRequest.class);
    }
    analytics.track(req);
}
Also used : TrackRequest(io.jenkins.blueocean.analytics.Analytics.TrackRequest) InputStream(java.io.InputStream) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Aggregations

TrackRequest (io.jenkins.blueocean.analytics.Analytics.TrackRequest)9 Test (org.junit.Test)6 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)3 HashMap (java.util.HashMap)3 Analytics (io.jenkins.blueocean.analytics.Analytics)2 Result (hudson.model.Result)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 Link (io.jenkins.blueocean.rest.hal.Link)1 NodeGraphBuilder (io.jenkins.blueocean.rest.impl.pipeline.NodeGraphBuilder)1 Tally (io.jenkins.blueocean.service.embedded.analytics.Tally)1 InputStream (java.io.InputStream)1 Jenkins (jenkins.model.Jenkins)1 DeclarativeJobAction (org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction)1 WebMethod (org.kohsuke.stapler.WebMethod)1 POST (org.kohsuke.stapler.verb.POST)1