Skip to content

Commit

Permalink
#118 Introduce FragmentOperation API.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszmichalak committed Mar 12, 2020
1 parent ffa6866 commit 1151995
Show file tree
Hide file tree
Showing 56 changed files with 340 additions and 277 deletions.
5 changes: 5 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ plugins {

dependencies {
implementation(platform("io.knotx:knotx-dependencies:${project.version}"))

api("io.knotx:knotx-server-http-api:${project.version}")

implementation(group = "io.vertx", name = "vertx-core")
implementation(group = "io.vertx", name = "vertx-service-proxy")
implementation(group = "io.vertx", name = "vertx-rx-java2")
implementation(group = "org.apache.commons", name = "commons-lang3")
}

Expand Down
36 changes: 36 additions & 0 deletions api/docs/asciidoc/dataobjects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ Type of a Fragment. Different types of Fragments can be processed in separate wa
+++
|===

[[FragmentContext]]
== FragmentContext


[cols=">25%,25%,50%"]
[frame="topbot"]
|===
^|Name | Type ^| Description
|[[clientRequest]]`@clientRequest`|`link:dataobjects.html#ClientRequest[ClientRequest]`|-
|[[fragment]]`@fragment`|`link:dataobjects.html#Fragment[Fragment]`|-
|===

[[FragmentResult]]
== FragmentResult

++++
Result of the link.
++++
'''

[cols=">25%,25%,50%"]
[frame="topbot"]
|===
^|Name | Type ^| Description
|[[fragment]]`@fragment`|`link:dataobjects.html#Fragment[Fragment]`|+++
A <code>Fragment</code> transformed or updated during applying the link.
+++
|[[log]]`@log`|`Json object`|+++
Log data produced by link. It is a JSON-based value specific to the
operation.
+++
|[[transition]]`@transition`|`String`|+++
A text value state of link that determines next steps in business logic.
+++
|===

Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.knotx.fragments.engine.api.node.single;
package io.knotx.fragments.api;

import io.knotx.fragments.api.Fragment;
import io.knotx.server.api.context.ClientRequest;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import java.util.Objects;

/**
* This data structure is passed between {@code Actions} that are vertices of a graph.
*/
@DataObject
public class FragmentContext {

Expand All @@ -49,21 +45,10 @@ public JsonObject toJson() {
.put(CLIENT_REQUEST_KEY, clientRequest.toJson());
}

/**
* Fragment that is passed between Actions. It might be transformend or updated by any {@code
* Action}.
*
* @return a Fragment
*/
public Fragment getFragment() {
return fragment;
}

/**
* Original {@code ClientRequest}. This property is immutable for the Fragments graph processing.
*
* @return client request
*/
public ClientRequest getClientRequest() {
return clientRequest;
}
Expand Down
36 changes: 36 additions & 0 deletions api/src/main/java/io/knotx/fragments/api/FragmentOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Knot.x Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.knotx.fragments.api;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.lang.rx.RxGen;

@RxGen(FragmentOperation.class)
@VertxGen
public interface FragmentOperation {

/**
* Transforms a fragment into the new one. It returns a fragment result containing the new
* fragment and a transition (which tells about the status of the fragment transformation (e.g. `_success` or `_error`).
*
* @param fragmentContext - contains both fragment and client request
* @param resultHandler - handler that is invoked when the new fragment is ready
*/
void apply(FragmentContext fragmentContext, Handler<AsyncResult<FragmentResult>> resultHandler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.knotx.fragments.engine.api.node.single;
package io.knotx.fragments.api;

import io.vertx.codegen.annotations.DataObject;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import io.knotx.fragments.api.Fragment;
import io.vertx.core.json.JsonObject;

/**
* Result of the {@code Action} fragment processing.
* Result of the {@link FragmentOperation}.
*/
@DataObject
public class FragmentResult {
Expand All @@ -34,16 +33,16 @@ public class FragmentResult {

private static final String FRAGMENT_KEY = "fragment";
private static final String TRANSITION_KEY = "transition";
private static final String NODE_LOG_KEY = "nodeLog";
private static final String LOG_KEY = "log";

private final Fragment fragment;
private final String transition;
private final JsonObject nodeLog;
private final JsonObject log;

public FragmentResult(Fragment fragment, String transition, JsonObject nodeLog) {
public FragmentResult(Fragment fragment, String transition, JsonObject log) {
this.fragment = fragment;
this.transition = transition;
this.nodeLog = nodeLog;
this.log = log;
}

public FragmentResult(Fragment fragment, String transition) {
Expand All @@ -53,18 +52,18 @@ public FragmentResult(Fragment fragment, String transition) {
public FragmentResult(JsonObject json) {
this.fragment = new Fragment(json.getJsonObject(FRAGMENT_KEY));
this.transition = json.getString(TRANSITION_KEY);
this.nodeLog = json.getJsonObject(NODE_LOG_KEY);
this.log = json.getJsonObject(LOG_KEY);
}

public JsonObject toJson() {
return new JsonObject()
.put(FRAGMENT_KEY, fragment.toJson())
.put(TRANSITION_KEY, transition)
.put(NODE_LOG_KEY, nodeLog);
.put(LOG_KEY, log);
}

/**
* A {@code Fragment} transformed or updated during applying the {@code Action}.
* A {@code Fragment} transformed or updated during applying the {@link FragmentOperation}.
*
* @return transformed or updated Fragment
*/
Expand All @@ -73,9 +72,9 @@ public Fragment getFragment() {
}

/**
* Name of the next step in the graph that is defined as the {@code Action} output.
* A text value state of {@link FragmentOperation} that determines next steps in business logic.
*
* @return next transition
* @return a state of {@link FragmentOperation}
*/
public String getTransition() {
if (StringUtils.isBlank(transition)) {
Expand All @@ -86,12 +85,13 @@ public String getTransition() {
}

/**
* Log produced by node execution.
* Log data produced by {@link FragmentOperation}. It is a JSON-based value specific to the
* operation.
*
* @return node log
* @return operation log
*/
public JsonObject getNodeLog() {
return nodeLog;
public JsonObject getLog() {
return log;
}

@Override
Expand All @@ -105,20 +105,20 @@ public boolean equals(Object o) {
FragmentResult that = (FragmentResult) o;
return Objects.equals(fragment, that.fragment) &&
Objects.equals(transition, that.transition) &&
Objects.equals(nodeLog, that.nodeLog);
Objects.equals(log, that.log);
}

@Override
public int hashCode() {
return Objects.hash(fragment, transition, nodeLog);
return Objects.hash(fragment, transition, log);
}

@Override
public String toString() {
return "FragmentResult{" +
"fragment=" + fragment +
", transition='" + transition + '\'' +
", nodeLog=" + nodeLog +
", log=" + log +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import io.knotx.fragments.handler.api.Cacheable;
import io.knotx.fragments.handler.api.actionlog.ActionLogLevel;
import io.knotx.fragments.handler.api.actionlog.ActionLogger;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.fragments.api.FragmentResult;
import io.knotx.fragments.handler.action.helper.TimeCalculator;
import io.knotx.reactivex.fragments.api.FragmentOperation;
import io.knotx.server.api.context.ClientRequest;
import io.knotx.server.common.placeholders.PlaceholdersResolver;
import io.knotx.server.common.placeholders.SourceDefinitions;
Expand Down Expand Up @@ -122,7 +123,7 @@ private Single<FragmentResult> callDoActionAndCache(FragmentContext fragmentCont
String cacheKey,
ActionLogger actionLogger) {
long startTime = Instant.now().toEpochMilli();
return io.knotx.fragments.handler.reactivex.api.Action.newInstance(doAction)
return FragmentOperation.newInstance(doAction)
.rxApply(fragmentContext)
.doOnSuccess(fr -> logDoAction(actionLogger, startTime, fr))
.doOnSuccess(fr -> savePayloadToCache(actionLogger, cacheKey, fr))
Expand Down Expand Up @@ -218,9 +219,9 @@ private static void logDoAction(ActionLogger actionLogger, long startTime,
FragmentResult fragmentResult) {
long executionTime = TimeCalculator.executionTime(startTime);
if (isSuccessTransition(fragmentResult)) {
actionLogger.doActionLog(executionTime, fragmentResult.getNodeLog());
actionLogger.doActionLog(executionTime, fragmentResult.getLog());
} else {
actionLogger.failureDoActionLog(executionTime, fragmentResult.getNodeLog());
actionLogger.failureDoActionLog(executionTime, fragmentResult.getLog());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.knotx.fragments.handler.api.Cacheable;
import io.knotx.fragments.handler.api.actionlog.ActionLogLevel;
import io.knotx.fragments.handler.api.actionlog.ActionLogger;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.fragments.api.FragmentResult;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.knotx.fragments.handler.api.Cacheable;
import io.knotx.fragments.handler.api.actionlog.ActionLogLevel;
import io.knotx.fragments.handler.api.actionlog.ActionLogger;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.fragments.api.FragmentResult;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.knotx.fragments.api.Fragment;
import io.knotx.fragments.handler.api.Action;
import io.knotx.fragments.handler.api.ActionFactory;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentResult;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import io.knotx.fragments.handler.api.actionlog.ActionLog;
import io.knotx.fragments.handler.api.actionlog.ActionLogLevel;
import io.knotx.fragments.handler.api.actionlog.ActionLogger;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.fragments.api.FragmentResult;
import io.vertx.circuitbreaker.CircuitBreaker;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
Expand Down Expand Up @@ -92,7 +92,7 @@ private void handleResult(Promise<FragmentResult> promise,
FragmentResult result, AtomicInteger counter, long startTime,
ActionLogger actionLogger) {
if (isErrorTransition(result)) {
handleFail(promise, result.getNodeLog(), startTime,
handleFail(promise, result.getLog(), startTime,
new DoActionExecuteException(
format("Action end up %s transition", result.getTransition())),
actionLogger);
Expand All @@ -114,7 +114,7 @@ private static void handleFail(Promise<FragmentResult> promise, JsonObject nodeL
private static void handleSuccess(Promise<FragmentResult> f, FragmentResult result,
AtomicInteger counter, long startTime, ActionLogger actionLogger) {
actionLogger.info(INVOCATION_COUNT_LOG_KEY, valueOf(counter.get()));
actionLogger.doActionLog(TimeCalculator.executionTime(startTime), result.getNodeLog());
actionLogger.doActionLog(TimeCalculator.executionTime(startTime), result.getLog());
f.complete(new FragmentResult(result.getFragment(), result.getTransition(),
actionLogger.toLog().toJson()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.knotx.fragments.handler.action.cb;

import static io.knotx.fragments.handler.api.actionlog.ActionLogLevel.ERROR;
import static io.knotx.fragments.engine.api.node.single.FragmentResult.ERROR_TRANSITION;
import static io.knotx.fragments.api.FragmentResult.ERROR_TRANSITION;

import io.vertx.circuitbreaker.CircuitBreakerOptions;
import io.vertx.codegen.annotations.DataObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import io.knotx.fragments.handler.action.http.response.EndpointResponseProcessor;
import io.knotx.fragments.handler.api.Action;
import io.knotx.fragments.handler.api.actionlog.ActionLogLevel;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.fragments.api.FragmentResult;
import io.knotx.fragments.handler.api.domain.payload.ActionPayload;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.Single;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import io.knotx.commons.http.request.AllowedHeadersFilter;
import io.knotx.commons.http.request.MultiMapCollector;
import io.knotx.fragments.handler.action.http.options.EndpointOptions;
import io.knotx.fragments.engine.api.node.single.FragmentContext;
import io.knotx.fragments.api.FragmentContext;
import io.knotx.server.api.context.ClientRequest;
import io.knotx.server.common.placeholders.PlaceholdersResolver;
import io.knotx.server.common.placeholders.SourceDefinitions;
import io.vertx.reactivex.core.MultiMap;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class EndpointRequestComposer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package io.knotx.fragments.handler.action.http.response;

import static io.knotx.fragments.engine.api.node.single.FragmentResult.ERROR_TRANSITION;
import static io.knotx.fragments.api.FragmentResult.ERROR_TRANSITION;

import io.knotx.commons.json.MultiMapTransformer;
import io.knotx.fragments.handler.action.http.HttpAction.HttpActionResult;
import io.knotx.fragments.handler.action.http.log.HttpActionLogger;
import io.knotx.fragments.handler.action.http.options.ResponseOptions;
import io.knotx.fragments.handler.action.http.request.EndpointRequest;
import io.knotx.fragments.engine.api.node.single.FragmentResult;
import io.knotx.fragments.api.FragmentResult;
import io.knotx.fragments.handler.api.domain.payload.ActionPayload;
import io.knotx.fragments.handler.api.domain.payload.ActionRequest;
import io.knotx.fragments.handler.api.domain.payload.ActionResponse;
Expand Down
Loading

0 comments on commit 1151995

Please sign in to comment.