Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync main branch with Apache main branch #7

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ protected void verifyNoDecisionEventWasProduced(String processInstanceId) throws
.with().pollInterval(1, SECONDS)
.untilAsserted(() -> sink.verify(1,
postRequestedFor(urlEqualTo("/"))
.withRequestBody(matchingJsonPath("kogitoprocinstanceid", equalTo(processInstanceId)))
.withRequestBody(matchingJsonPath("type", equalTo(PROCESS_RESULT_EVENT_TYPE)))
.withRequestBody(matchingJsonPath("data.decision", equalTo(DECISION_NO_DECISION)))));
.withHeader("ce-kogitoprocinstanceid", equalTo(processInstanceId))
.withHeader("ce-type", equalTo(PROCESS_RESULT_EVENT_TYPE))
.withRequestBody(matchingJsonPath("decision", equalTo(DECISION_NO_DECISION)))));

}

Expand Down
44 changes: 39 additions & 5 deletions data-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Data Audit «SpringBoot»: Provides the wiring to use Data Audit with SpringBoot

Now we have and implementation examples


Data Audit JPA Common: Provides the common exension not depending on the runtime
Data Audit JPA «Quarkus»: Provides the wiring between the specific implementation and Quarkus System
Data Audit JPA «SpringBoot»: Provides the wiring between the specific implementation and Springboot colocated system
Expand All @@ -35,9 +34,46 @@ Data Audit JPA «SpringBoot»: Provides the wiring between the specific implemen

The way to retrieve information from the data audit is using GraphQL. This way we can abstract how the information is retrieved and allow different needs depending on the user.

The Path is `${HOST}/data-audit/q` for sending GraphQL queries.

### Example

Execute a registered query, eg. `GetAllProcessInstancesState` with a definition of data fields that should be returned:

```
curl -H "Content-Type: application/json" -H "Accept: application/json" -s -X POST http://${HOST}/data-audit/q/ -d '
{
"query": "{GetAllProcessInstancesState {eventId, processInstanceId, eventType, eventDate}}"
}'|jq
```

To retrieve the GraphQL schema definition including a list of all registered queries, run a GET command to the `${HOST}/data-audit/r` endpoint. This endpoint can also be used to register new queries.

### Example

Register a new query with a complex data type:

```
curl -H "Content-Type: application/json" -H "Accept: application/json" -s -X POST http://${HOST}/data-audit/r/ -d '
{
"identifier" : "tests",
"graphQLDefinition" : "type EventTest { jobId : String, processInstanceId: String} type Query { tests (pagination: Pagination) : [ EventTest ] } ",
"query" : "SELECT o.job_id, o.process_instance_id FROM job_execution_log o"
}'
```

Once registered, the new query can be executed similar to the pre-registered ones using the `${HOST}/data-audit/q` endpoint:

```
curl -H "Content-Type: application/json" -H "Accept: application/json" -s -X POST http://${HOST}/data-audit/q/ -d '
{
"query": "{tests {jobId, processInstanceId}}"
}'|jq
```

## JPA implementation

The jpa implementation allows you to store those events to be stored in a database.
The jpa implementation allows you to store those events to be stored in a database. The only thing required in this case is to setup the datasource.

## Extension Points

Expand All @@ -54,7 +90,7 @@ org.kie.kogito.app.audit.spi.GraphQLSchemaQueryProvider: this allow the subsyste

## How to use in with Quarkus/Springboot

You need to add two different dependencies to your project.
You need to add two different dependencies to your project (collocated service)

<dependency>
<groupId>org.kie.kogito</groupId>
Expand All @@ -68,8 +104,6 @@ You need to add two different dependencies to your project.
</dependency>




The first dependency is related how to you want to deploy it. In this case as collocated/embedded service
The second dependency is which implementation you want to use.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.kie.kogito.app.audit.api;

public class DataAuditQuery {

private String identifier;

private String graphQLDefinition;

private String query;

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public String getGraphQLDefinition() {
return graphQLDefinition;
}

public void setGraphQLDefinition(String graphQLDefinition) {
this.graphQLDefinition = graphQLDefinition;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

@Override
public String toString() {
return "DataAuditQuery [identifier=" + identifier + ", graphQLDefinition=" + graphQLDefinition + ", query=" + query + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,21 @@
public class DataAuditQueryService {

private GraphQLSchemaManager graphQLManager;
private GraphQL graphQL;

private DataAuditQueryService(GraphQLSchemaManager graphQLManager) {
this.graphQLManager = graphQLManager;
this.graphQL = GraphQL.newGraphQL(graphQLManager.getGraphQLSchema()).build();
}

public GraphQLSchema getGraphQLSchema() {
return this.graphQLManager.getGraphQLSchema();
}

public ExecutionResult executeQuery(DataAuditContext context, String query) {
return executeQuery(context, query, emptyMap());
public GraphQL getGraphQL() {
return this.graphQLManager.getGraphQL();
}

public ExecutionResult executeQuery(String query) {
return executeQuery(null, query, emptyMap());
public ExecutionResult executeQuery(DataAuditContext context, String query) {
return executeQuery(context, query, emptyMap());
}

public ExecutionResult executeQuery(DataAuditContext context, String query, Map<String, Object> variables) {
Expand All @@ -59,7 +57,7 @@ public ExecutionResult executeQuery(DataAuditContext context, String query, Map<
.variables(variables)
.build();

return graphQL.execute(executionInput);
return graphQLManager.execute(executionInput);
}

public static DataAuditQueryService newAuditQuerySerice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.kie.kogito.app.audit.api;

import java.util.List;
import java.util.ServiceLoader;

import org.kie.kogito.app.audit.spi.DataAuditStore;
Expand Down Expand Up @@ -81,4 +82,13 @@ public static DataAuditStoreProxyService newAuditStoreService() {
LOGGER.debug("Creating new Data Audit Store proxy service with {}", service);
return new DataAuditStoreProxyService(service);
}

public void storeQuery(DataAuditContext newDataAuditContext, DataAuditQuery dataAuditQuery) {
LOGGER.info("Store query {}", dataAuditQuery);
auditStoreService.storeQuery(newDataAuditContext, dataAuditQuery);
}

public List<DataAuditQuery> findQueries(DataAuditContext context) {
return auditStoreService.findQueries(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private SubsystemConstants() {
}

public static final String DATA_AUDIT_PATH = "/data-audit";
public static final String DATA_AUDIT_QUERY_PATH = DATA_AUDIT_PATH + "/q";
public static final String DATA_AUDIT_REGISTRY_PATH = DATA_AUDIT_PATH + "/r";

public static final String KOGITO_PROCESSINSTANCES_EVENTS = "kogito-processinstances-events";
public static final String KOGITO_USERTASKINSTANCES_EVENTS = "kogito-usertaskinstances-events";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.kie.kogito.app.audit.graphql;

import java.util.Map;

import graphql.GraphQL;
import graphql.schema.GraphQLSchema;

public record GraphQLSchemaBuild(GraphQLSchema graphQLSchema, GraphQL graphQL, Map<String, String> additionalDefinitions) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,34 @@
*/
package org.kie.kogito.app.audit.graphql;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;

import org.kie.kogito.app.audit.api.DataAuditContext;
import org.kie.kogito.app.audit.api.DataAuditQuery;
import org.kie.kogito.app.audit.spi.GraphQLSchemaQuery;
import org.kie.kogito.app.audit.spi.GraphQLSchemaQueryProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.language.FieldDefinition;
import graphql.language.ObjectTypeDefinition;
import graphql.scalars.ExtendedScalars;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.SchemaPrinter;
import graphql.schema.idl.TypeDefinitionRegistry;

import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
Expand All @@ -49,12 +58,20 @@ public class GraphQLSchemaManager {

private GraphQLSchema graphQLSchema;

private GraphQL graphQL;

private Map<String, String> graphQLdefinitions;

public static GraphQLSchemaManager graphQLSchemaManagerInstance() {
return INSTANCE;
}

private GraphQLSchemaManager() {
this.graphQLdefinitions = new HashMap<>();
}

public GraphQLSchemaBuild rebuildDefinitions(DataAuditContext dataAuditContext, Map<String, String> additionalDefinitions) {
LOGGER.debug("Rebuilding graphQL definitions");
RuntimeWiring.Builder runtimeWiringBuilder = newRuntimeWiring();

runtimeWiringBuilder.scalar(ExtendedScalars.GraphQLBigInteger);
Expand All @@ -77,15 +94,19 @@ private GraphQLSchemaManager() {

ServiceLoader.load(GraphQLSchemaQueryProvider.class, classLoader).forEach(queryProvider -> {
graphqlSchemas.addAll(List.of(queryProvider.graphQLQueryExtension()));
for (GraphQLSchemaQuery<?> query : queryProvider.queries()) {
for (GraphQLSchemaQuery query : queryProvider.queries(dataAuditContext)) {
runtimeWiringBuilder.type("Query", builder -> builder.dataFetcher(query.name(), query::fetch));
}
});

List<InputStream> data = new ArrayList<>();
data.addAll(graphqlSchemas.stream().map(this::toInputStream).toList());
data.addAll(additionalDefinitions.values().stream().map(String::getBytes).map(ByteArrayInputStream::new).toList());

// now we have all of definitions
List<FieldDefinition> queryDefinitions = new ArrayList<>();
for (String graphQLSchema : graphqlSchemas) {
TypeDefinitionRegistry newTypes = readDefintionRegistry(graphQLSchema);
for (InputStream graphQLSchema : data) {
TypeDefinitionRegistry newTypes = readDefinitionRegistry(graphQLSchema);

// for allowing extension of the schema we need to merge this object manually
// we remove it from the new Types and aggregate in temporal list so we can add this at the end
Expand All @@ -103,22 +124,61 @@ private GraphQLSchemaManager() {
SchemaGenerator schemaGenerator = new SchemaGenerator();
// we merge the query object
typeDefinitionRegistry.add(ObjectTypeDefinition.newObjectTypeDefinition().name("Query").fieldDefinitions(queryDefinitions).build());
graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQLSchema newGraphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL newGraphQL = GraphQL.newGraphQL(newGraphQLSchema).build();

LOGGER.debug("Succesfuly rebuilding graphQL definitions");
return new GraphQLSchemaBuild(newGraphQLSchema, newGraphQL, additionalDefinitions);
}

private TypeDefinitionRegistry readDefintionRegistry(String graphQLSchema) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(graphQLSchema)) {
SchemaParser schemaParser = new SchemaParser();
return schemaParser.parse(is);
private InputStream toInputStream(String classpathFile) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathFile)) {
return new ByteArrayInputStream(is.readAllBytes());
} catch (IOException e) {
LOGGER.error("could not find or process {}", graphQLSchema, e);
return new TypeDefinitionRegistry();
return new ByteArrayInputStream(new byte[0]);
}
}

private TypeDefinitionRegistry readDefinitionRegistry(InputStream inputStream) {
SchemaParser schemaParser = new SchemaParser();
return schemaParser.parse(inputStream);
}

public GraphQL getGraphQL() {
return graphQL;
}

public GraphQLSchema getGraphQLSchema() {
return graphQLSchema;
}

public ExecutionResult execute(ExecutionInput executionInput) {
return graphQL.execute(executionInput);
}

public String getGraphQLSchemaDefinition() {
SchemaPrinter printer = new SchemaPrinter();
return printer.print(graphQL.getGraphQLSchema());
}

public void init(DataAuditContext dataAuditContext, Map<String, String> additionalQueries) {
setGraphQLSchemaBuild(rebuildDefinitions(dataAuditContext, additionalQueries));
}

public GraphQLSchemaBuild devireNewDataAuditQuerySchema(DataAuditContext dataAuditContext, DataAuditQuery dataAuditQuery) {
String graphQLDefinition = dataAuditQuery.getGraphQLDefinition();
TypeDefinitionRegistry registry = readDefinitionRegistry(new ByteArrayInputStream(graphQLDefinition.getBytes()));
LOGGER.debug("Registering data audit query {} with definition {}", dataAuditQuery.getIdentifier(), registry.getType("Query"));
Map<String, String> additionalDefinitions = new HashMap<>(this.graphQLdefinitions);
additionalDefinitions.put(dataAuditQuery.getIdentifier(), dataAuditQuery.getGraphQLDefinition());
return rebuildDefinitions(dataAuditContext, additionalDefinitions);
}

public void setGraphQLSchemaBuild(GraphQLSchemaBuild build) {
this.graphQL = build.graphQL();
this.graphQLSchema = build.graphQLSchema();
this.graphQLdefinitions = build.additionalDefinitions();
}

}
Loading
Loading