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

Add call logging + add test for passing load flow parameters #12

Merged
merged 1 commit into from
Sep 30, 2023
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@
<artifactId>ktor-server-status-pages-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-call-logging-jvm</artifactId>
<version>${ktor.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.9.20-Beta2</version>
</dependency>
<dependency>
<groupId>io.github.oshai</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
22 changes: 20 additions & 2 deletions src/main/ApiUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import com.powsybl.loadflow.LoadFlowParameters
import com.powsybl.loadflow.json.JsonLoadFlowParameters
import com.powsybl.nad.NetworkAreaDiagram
import com.powsybl.sld.SingleLineDiagram
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.http.content.*
import java.io.ByteArrayInputStream
import java.io.StringWriter

private val logger = KotlinLogging.logger {}

fun busesFromRequest(
type: String,
body: ByteArray,
Expand All @@ -25,15 +28,30 @@ class FileContent(val name: String, val bytes: ByteArray)
class LoadParameterContainer {
var parameters = LoadFlowParameters()
private var parametersModified = false

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

private fun addVersionToJsonString(jsonString: String): String {
return "{\"version\": ${currentVersion()}," + jsonString.drop(1)
}

private fun hasVersion(jsonString: String): Boolean {
return jsonString.contains("version")
}

private fun update(jsonString: String) {
this.parameters = JsonLoadFlowParameters.update(this.parameters, ByteArrayInputStream(jsonString.toByteArray()))
val jsonStringWithVersion = if (hasVersion(jsonString)) jsonString else addVersionToJsonString(jsonString)
this.parameters = JsonLoadFlowParameters.update(this.parameters, jsonStringWithVersion.byteInputStream())
this.parametersModified = true
}

fun formItemHandler(part: PartData.FormItem) {
val name = part.name ?: ""
if (name == "load-parameters") {
if (name == "load-flow-parameters") {
this.update(part.value)
logger.info { "Received load flow parameters: ${part.value}" }
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/App.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.statnett.loadflowservice

import com.powsybl.commons.PowsyblException
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.statuspages.*
Expand All @@ -30,6 +30,8 @@ fun Application.module() {
}
}

install(CallLogging)

routing {
get("/") {
call.respondText("Hello, world!")
Expand Down
1 change: 0 additions & 1 deletion src/main/Solver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.statnett.loadflowservice

import com.powsybl.cgmes.conversion.CgmesImport
import com.powsybl.ieeecdf.converter.IeeeCdfImporter
import com.powsybl.iidm.network.ImportersLoader
import com.powsybl.iidm.network.ImportersLoaderList
import com.powsybl.iidm.network.Network
import com.powsybl.iidm.xml.XMLImporter
Expand Down
41 changes: 41 additions & 0 deletions src/test/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,47 @@ class ApplicationTest {
)
}

@Test
fun `test dc solve ok`() =
testApplication {
val formData = formDataFromFile(ieeeCdfNetwork14File())
val loadParams = formData {
append("load-flow-parameters", "{\"dc\": true}")
}

val response = client.submitFormWithBinaryData(
url = "/run-load-flow",
formData = formData + loadParams
)

val body = response.bodyAsText()
assertEquals(HttpStatusCode.OK, response.status)

val regex = Regex("\"isOk\":([^,]+)")
val match = regex.find(body)!!
val solveStatus = match.groupValues[1].toBoolean()
assertTrue(solveStatus)
}

@Test
fun `test descriptive response on incompatible version of load parameters`() =
testApplication {
val formData = formDataFromFile(ieeeCdfNetwork14File())
val loadParams = formData {
append("load-flow-parameters", "{\"version\":\"1.0\",\"dc\": true}")
}

val response = client.submitFormWithBinaryData(
url = "/run-load-flow",
formData = formData + loadParams
)
val body = response.bodyAsText()
assertEquals(HttpStatusCode.InternalServerError, response.status)

// dc flag was introduced in v1.4
assertTrue(body.contains(">= 1.4"))
}

@Test
fun `test response ok network`() =
testApplication {
Expand Down
Loading