Skip to content

Commit

Permalink
[NU-1806] extract common parts, minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gskrobisz committed Sep 14, 2024
1 parent 72808f2 commit 87c1166
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import com.typesafe.scalalogging.LazyLogging
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import pl.touk.nussknacker.engine.api.graph.ScenarioGraph
import pl.touk.nussknacker.ui.process.ProcessService
import pl.touk.nussknacker.ui.process.newactivity.ScenarioActivityService
import pl.touk.nussknacker.ui.process.newactivity.ActivityInfoService
import pl.touk.nussknacker.ui.process.processingtype.provider.ProcessingTypeDataProvider
import pl.touk.nussknacker.ui.security.api.LoggedUser

import scala.concurrent.ExecutionContext

class ActivityInfoResources(
protected val processService: ProcessService,
scenarioActivityServices: ProcessingTypeDataProvider[ScenarioActivityService, _]
activityInfoService: ProcessingTypeDataProvider[ActivityInfoService, _]
)(implicit val ec: ExecutionContext)
extends Directives
with FailFastCirceSupport
Expand All @@ -25,10 +25,11 @@ class ActivityInfoResources(
pathPrefix("activityInfo" / ProcessNameSegment) { processName =>
(post & processDetailsForName(processName)) { processDetails =>
entity(as[ScenarioGraph]) { scenarioGraph =>
val scenarioTestService = scenarioActivityServices.forProcessingTypeUnsafe(processDetails.processingType)
path("activityParameters") {
complete {
scenarioTestService.getActivityParameters(scenarioGraph, processName, processDetails.isFragment)
activityInfoService
.forProcessingTypeUnsafe(processDetails.processingType)
.getActivityParameters(scenarioGraph, processName, processDetails.isFragment)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import pl.touk.nussknacker.ui.definition.DefinitionsService
import pl.touk.nussknacker.ui.security.api.LoggedUser
import pl.touk.nussknacker.ui.uiresolving.UIProcessResolver

class ScenarioActivityService(activityInfoProvider: ActivityInfoProvider, processResolver: UIProcessResolver) {
// TODO: move to ActivityService? execute node compilation only once with ScenarioTestService?
class ActivityInfoService(activityInfoProvider: ActivityInfoProvider, processResolver: UIProcessResolver) {

def getActivityParameters(scenarioGraph: ScenarioGraph, processName: ProcessName, isFragment: Boolean)(
implicit user: LoggedUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import pl.touk.nussknacker.ui.process.deployment.{
}
import pl.touk.nussknacker.ui.process.fragment.{DefaultFragmentRepository, FragmentResolver}
import pl.touk.nussknacker.ui.process.migrate.{HttpRemoteEnvironment, ProcessModelMigrator, TestModelMigrations}
import pl.touk.nussknacker.ui.process.newactivity.{ActivityService, ScenarioActivityService}
import pl.touk.nussknacker.ui.process.newactivity.{ActivityInfoService, ActivityService}
import pl.touk.nussknacker.ui.process.newdeployment.synchronize.{
DeploymentsStatusesSynchronizationConfig,
DeploymentsStatusesSynchronizationScheduler,
Expand Down Expand Up @@ -198,7 +198,7 @@ class AkkaHttpBasedRouteProvider(
)
}
val scenarioActivityService = scenarioTestServiceDeps.mapValues { case (_, processResolver, _, modelData, _) =>
new ScenarioActivityService(
new ActivityInfoService(
new ModelDataActivityInfoProvider(modelData),
processResolver
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import slick.dbio.DBIOAction
import cats.effect.unsafe.implicits.global
import pl.touk.nussknacker.engine.definition.activity.ModelDataActivityInfoProvider
import pl.touk.nussknacker.ui.LoadableConfigBasedNussknackerConfig
import pl.touk.nussknacker.ui.process.newactivity.ScenarioActivityService
import pl.touk.nussknacker.ui.process.newactivity.ActivityInfoService

import java.net.URI
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -215,12 +215,6 @@ trait NuResourcesTest
)
)

protected def createScenarioActivityService: ScenarioActivityService =
new ScenarioActivityService(
new ModelDataActivityInfoProvider(modelData),
processResolver
)

protected def deployRoute() =
new ManagementResources(
processAuthorizer = processAuthorizer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pl.touk.nussknacker.engine.definition.activity

import pl.touk.nussknacker.engine.ModelData
import pl.touk.nussknacker.engine.api.process.ComponentUseCase
import pl.touk.nussknacker.engine.api.{MetaData, NodeId, process}
import pl.touk.nussknacker.engine.canonicalgraph.CanonicalProcess
import pl.touk.nussknacker.engine.compile.ExpressionCompiler
import pl.touk.nussknacker.engine.compile.nodecompilation.{LazyParameterCreationStrategy, NodeCompiler}
import pl.touk.nussknacker.engine.definition.fragment.FragmentParametersDefinitionExtractor
import pl.touk.nussknacker.engine.graph.node.{SourceNodeData, asFragmentInputDefinition, asSource}
import pl.touk.nussknacker.engine.resultcollector.ProductionServiceInvocationCollector

class CommonModelDataInfoProvider(modelData: ModelData) {

private lazy val expressionCompiler = ExpressionCompiler.withoutOptimization(modelData).withLabelsDictTyper

private lazy val nodeCompiler = new NodeCompiler(
modelData.modelDefinition,
new FragmentParametersDefinitionExtractor(modelData.modelClassLoader.classLoader),
expressionCompiler,
modelData.modelClassLoader.classLoader,
Seq.empty,
ProductionServiceInvocationCollector,
ComponentUseCase.TestDataGeneration,
nonServicesLazyParamStrategy = LazyParameterCreationStrategy.default
)

protected def prepareSourceObj(
source: SourceNodeData
)(implicit metaData: MetaData, nodeId: NodeId): Option[process.Source] = {
nodeCompiler.compileSource(source).compiledObject.toOption
}

protected def collectAllSources(scenario: CanonicalProcess): List[SourceNodeData] = {
scenario.collectAllNodes.flatMap(asSource) ++ scenario.collectAllNodes.flatMap(asFragmentInputDefinition)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,15 @@ package pl.touk.nussknacker.engine.definition.activity

import pl.touk.nussknacker.engine.ModelData
import pl.touk.nussknacker.engine.api.definition.Parameter
import pl.touk.nussknacker.engine.api.process.{ComponentUseCase, WithActivityParameters}
import pl.touk.nussknacker.engine.api.{MetaData, NodeId, process}
import pl.touk.nussknacker.engine.api.process.WithActivityParameters
import pl.touk.nussknacker.engine.api.{MetaData, NodeId}
import pl.touk.nussknacker.engine.canonicalgraph.CanonicalProcess
import pl.touk.nussknacker.engine.compile.ExpressionCompiler
import pl.touk.nussknacker.engine.compile.nodecompilation.{LazyParameterCreationStrategy, NodeCompiler}
import pl.touk.nussknacker.engine.definition.fragment.FragmentParametersDefinitionExtractor
import pl.touk.nussknacker.engine.graph.node.{SourceNodeData, asFragmentInputDefinition, asSource}
import pl.touk.nussknacker.engine.resultcollector.ProductionServiceInvocationCollector
import pl.touk.nussknacker.engine.graph.node.SourceNodeData
import pl.touk.nussknacker.engine.util.Implicits.RichScalaMap

class ModelDataActivityInfoProvider(modelData: ModelData) extends ActivityInfoProvider {

private lazy val expressionCompiler = ExpressionCompiler.withoutOptimization(modelData).withLabelsDictTyper

private lazy val nodeCompiler = new NodeCompiler(
modelData.modelDefinition,
new FragmentParametersDefinitionExtractor(modelData.modelClassLoader.classLoader),
expressionCompiler,
modelData.modelClassLoader.classLoader,
Seq.empty,
ProductionServiceInvocationCollector,
ComponentUseCase.TestDataGeneration,
nonServicesLazyParamStrategy = LazyParameterCreationStrategy.default
)

private def prepareSourceObj(
source: SourceNodeData
)(implicit metaData: MetaData, nodeId: NodeId): Option[process.Source] = {
nodeCompiler.compileSource(source).compiledObject.toOption
}
class ModelDataActivityInfoProvider(modelData: ModelData)
extends CommonModelDataInfoProvider(modelData)
with ActivityInfoProvider {

override def getActivityParameters(scenario: CanonicalProcess): Map[String, Map[String, List[Parameter]]] = {
modelData.withThisAsContextClassLoader {
Expand Down Expand Up @@ -64,8 +43,4 @@ class ModelDataActivityInfoProvider(modelData: ModelData) extends ActivityInfoPr
}
}

private def collectAllSources(scenario: CanonicalProcess): List[SourceNodeData] = {
scenario.collectAllNodes.flatMap(asSource) ++ scenario.collectAllNodes.flatMap(asFragmentInputDefinition)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,19 @@ import cats.data.NonEmptyList
import com.typesafe.scalalogging.LazyLogging
import pl.touk.nussknacker.engine.ModelData
import pl.touk.nussknacker.engine.api.definition.Parameter
import pl.touk.nussknacker.engine.api.process.{
ComponentUseCase,
SourceTestSupport,
TestDataGenerator,
TestWithParametersSupport
}
import pl.touk.nussknacker.engine.api.process.{SourceTestSupport, TestDataGenerator, TestWithParametersSupport}
import pl.touk.nussknacker.engine.api.test.{ScenarioTestData, ScenarioTestJsonRecord}
import pl.touk.nussknacker.engine.api.{MetaData, NodeId, process}
import pl.touk.nussknacker.engine.api.{MetaData, NodeId}
import pl.touk.nussknacker.engine.canonicalgraph.CanonicalProcess
import pl.touk.nussknacker.engine.compile.ExpressionCompiler
import pl.touk.nussknacker.engine.compile.nodecompilation.{LazyParameterCreationStrategy, NodeCompiler}
import pl.touk.nussknacker.engine.definition.fragment.FragmentParametersDefinitionExtractor
import pl.touk.nussknacker.engine.graph.node.{SourceNodeData, asFragmentInputDefinition, asSource}
import pl.touk.nussknacker.engine.resultcollector.ProductionServiceInvocationCollector
import pl.touk.nussknacker.engine.definition.activity.CommonModelDataInfoProvider
import pl.touk.nussknacker.engine.graph.node.SourceNodeData
import pl.touk.nussknacker.engine.util.ListUtil
import shapeless.syntax.typeable._

class ModelDataTestInfoProvider(modelData: ModelData) extends TestInfoProvider with LazyLogging {

private lazy val expressionCompiler = ExpressionCompiler.withoutOptimization(modelData).withLabelsDictTyper

private lazy val nodeCompiler = new NodeCompiler(
modelData.modelDefinition,
new FragmentParametersDefinitionExtractor(modelData.modelClassLoader.classLoader),
expressionCompiler,
modelData.modelClassLoader.classLoader,
Seq.empty,
ProductionServiceInvocationCollector,
ComponentUseCase.TestDataGeneration,
nonServicesLazyParamStrategy = LazyParameterCreationStrategy.default
)
class ModelDataTestInfoProvider(modelData: ModelData)
extends CommonModelDataInfoProvider(modelData)
with TestInfoProvider
with LazyLogging {

override def getTestingCapabilities(scenario: CanonicalProcess): TestingCapabilities = {
collectAllSources(scenario).map(getTestingCapabilities(_, scenario.metaData)) match {
Expand Down Expand Up @@ -112,12 +94,6 @@ class ModelDataTestInfoProvider(modelData: ModelData) extends TestInfoProvider w
.getOrElse(Left("Scenario doesn't have any source supporting test data generation"))
}

private def prepareSourceObj(
source: SourceNodeData
)(implicit metaData: MetaData, nodeId: NodeId): Option[process.Source] = {
nodeCompiler.compileSource(source).compiledObject.toOption
}

private def generateTestData(generators: NonEmptyList[(NodeId, TestDataGenerator)], size: Int) = {
val sourceTestDataList = generators.map { case (sourceId, testDataGenerator) =>
val sourceTestRecords = testDataGenerator.generateTestData(size).testRecords
Expand Down Expand Up @@ -150,10 +126,6 @@ class ModelDataTestInfoProvider(modelData: ModelData) extends TestInfoProvider w
.map(scenarioTestRecords => ScenarioTestData(scenarioTestRecords.toList))
}

private def collectAllSources(scenario: CanonicalProcess): List[SourceNodeData] = {
scenario.collectAllNodes.flatMap(asSource) ++ scenario.collectAllNodes.flatMap(asFragmentInputDefinition)
}

private def formatError(error: String, recordIdx: Int): String = {
s"Record ${recordIdx + 1} - $error"
}
Expand Down

0 comments on commit 87c1166

Please sign in to comment.