Skip to content

Commit

Permalink
Refactor: Move all form item handlers into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Oct 10, 2023
1 parent cf3356f commit 9fe6251
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 42 deletions.
32 changes: 0 additions & 32 deletions src/main/ApiUtil.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.github.statnett.loadflowservice

import com.powsybl.iidm.network.Network
import com.powsybl.loadflow.LoadFlowParameters
import com.powsybl.loadflow.json.JsonLoadFlowParameters
import com.powsybl.nad.NetworkAreaDiagram
import com.powsybl.sld.SingleLineDiagram
import com.powsybl.sld.SldParameters
Expand All @@ -19,36 +17,6 @@ fun busesFromRequest(
return busPropertiesFromNetwork(network)
}

interface FormItemLoadable {
fun formItemHandler(part: PartData.FormItem)
}

/**
* Convenience class used to deserialize and update a load parameter instance
*/
class LoadParameterContainer : AutoVersionableJsonParser(), FormItemLoadable {
var parameters = LoadFlowParameters()
private var parametersModified = false

override fun currentVersion(): String {
return LoadFlowParameters.VERSION
}

private fun update(jsonString: String) {
val withVersion = jsonStringWithVersion(jsonString)
this.parameters = JsonLoadFlowParameters.update(this.parameters, withVersion.byteInputStream())
this.parametersModified = true
}

override fun formItemHandler(part: PartData.FormItem) {
val name = part.name ?: ""
if (name == "load-flow-parameters") {
this.update(part.value)
logger.info { "Received load flow parameters: ${part.value}" }
}
}
}

suspend fun multiPartDataHandler(
multiPartData: MultiPartData,
formItemHandler: (part: PartData.FormItem) -> Unit = {}
Expand Down
1 change: 1 addition & 0 deletions src/main/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.statnett.loadflowservice

import com.github.statnett.loadflowservice.formItemHandlers.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.statnett.loadflowservice
package com.github.statnett.loadflowservice.formItemHandlers

import io.ktor.http.content.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.statnett.loadflowservice
package com.github.statnett.loadflowservice.formItemHandlers

import com.powsybl.contingency.contingency.list.ContingencyList
import com.powsybl.contingency.contingency.list.DefaultContingencyList
Expand Down
7 changes: 7 additions & 0 deletions src/main/formItemHandlers/FormItemLoadable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.statnett.loadflowservice.formItemHandlers

import io.ktor.http.content.*

interface FormItemLoadable {
fun formItemHandler(part: PartData.FormItem)
}
34 changes: 34 additions & 0 deletions src/main/formItemHandlers/LoadParameterContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.statnett.loadflowservice.formItemHandlers

import com.powsybl.loadflow.LoadFlowParameters
import com.powsybl.loadflow.json.JsonLoadFlowParameters
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.http.content.*

private val logger = KotlinLogging.logger {}

/**
* Convenience class used to deserialize and update a load parameter instance
*/
class LoadParameterContainer : AutoVersionableJsonParser(), FormItemLoadable {
var parameters = LoadFlowParameters()
private var parametersModified = false

override fun currentVersion(): String {
return LoadFlowParameters.VERSION
}

private fun update(jsonString: String) {
val withVersion = jsonStringWithVersion(jsonString)
this.parameters = JsonLoadFlowParameters.update(this.parameters, withVersion.byteInputStream())
this.parametersModified = true
}

override fun formItemHandler(part: PartData.FormItem) {
val name = part.name ?: ""
if (name == "load-flow-parameters") {
this.update(part.value)
logger.info { "Received load flow parameters: ${part.value}" }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.statnett.loadflowservice
package com.github.statnett.loadflowservice.formItemHandlers

import io.ktor.http.content.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.github.statnett.loadflowservice

package com.github.statnett.loadflowservice.formItemHandlers

import com.powsybl.sensitivity.SensitivityAnalysisParameters
import com.powsybl.sensitivity.json.JsonSensitivityAnalysisParameters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.statnett.loadflowservice
package com.github.statnett.loadflowservice.formItemHandlers

import com.powsybl.contingency.ContingencyContext
import com.powsybl.contingency.ContingencyContextType
Expand Down
2 changes: 1 addition & 1 deletion src/test/ContingencyListContainerTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.github.statnett.loadflowservice.ContingencyListContainer
import com.github.statnett.loadflowservice.formItemHandlers.ContingencyListContainer
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory
import testDataFactory.basicContingencyJson
import kotlin.test.Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/SensitivityAnalysisParametersContainerTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.github.statnett.loadflowservice.SensitivityAnalysisParametersContainer
import com.github.statnett.loadflowservice.formItemHandlers.SensitivityAnalysisParametersContainer
import kotlin.math.abs
import kotlin.test.Test
import kotlin.test.assertTrue
Expand Down
2 changes: 1 addition & 1 deletion src/test/SensitivityFactorContainerTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.github.statnett.loadflowservice.SensitivityFactorContainer
import com.github.statnett.loadflowservice.formItemHandlers.SensitivityFactorContainer
import testDataFactory.sensitivityFactorList
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
2 changes: 1 addition & 1 deletion src/test/testDataFactory/SensitivityRunFactory.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package testDataFactory

import com.github.statnett.loadflowservice.AutoSerializableSensitivityFactor
import com.github.statnett.loadflowservice.formItemHandlers.AutoSerializableSensitivityFactor
import io.ktor.client.request.forms.*
import io.ktor.http.content.*
import kotlinx.serialization.Serializable
Expand Down

0 comments on commit 9fe6251

Please sign in to comment.