Update server to working condition

This commit is contained in:
Alexander Nozik 2022-11-23 13:51:53 +03:00
parent ef670f8418
commit bdc649fde6
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
11 changed files with 209 additions and 78 deletions

View File

@ -6,6 +6,7 @@
package ru.inr.mass.data.api package ru.inr.mass.data.api
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import ru.inr.mass.data.proto.HVData
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.get
import space.kscience.dataforge.meta.long import space.kscience.dataforge.meta.long
@ -40,6 +41,8 @@ public interface NumassSet : Iterable<NumassPoint>, Provider {
override val defaultTarget: String get() = NUMASS_POINT_TARGET override val defaultTarget: String get() = NUMASS_POINT_TARGET
public val hvData: HVData?
override fun content(target: String): Map<Name, Any> = if (target == NUMASS_POINT_TARGET) { override fun content(target: String): Map<Name, Any> = if (target == NUMASS_POINT_TARGET) {
points.associateBy { NameToken("point", it.voltage.toString()).asName() } points.associateBy { NameToken("point", it.voltage.toString()).asName() }
} else { } else {

View File

@ -51,18 +51,15 @@ public class NumassDirectorySet internal constructor(
@OptIn(DFExperimental::class) @OptIn(DFExperimental::class)
public fun getHvData(): HVData? { override val hvData: HVData?
val hvFile = path / "voltage" get() = (path / "voltage").takeIf { it.exists() }?.let { hvFile ->
return if (hvFile.exists()) {
val envelope = context.io.readEnvelopeFile(hvFile) val envelope = context.io.readEnvelopeFile(hvFile)
HVData.readEnvelope(envelope) HVData.readEnvelope(envelope)
} else {
null
} }
}
override fun content(target: String): Map<Name, Any> = if (target == NUMASS_HV_TARGET) { override fun content(target: String): Map<Name, Any> = if (target == NUMASS_HV_TARGET) {
val hvData = getHvData() val hvData = hvData
if (hvData != null) { if (hvData != null) {
mapOf("hv".asName() to hvData) mapOf("hv".asName() to hvData)
} else { } else {

View File

@ -68,6 +68,7 @@ public suspend fun NumassProtoPlugin.readRepository(path: Path): DataTree<Numass
static(name, value, value.meta) static(name, value, value.meta)
} }
} }
//TODO add file watcher //TODO add file watcher
} }

View File

@ -5,7 +5,7 @@ plugins {
val visionForgeVersion: String by rootProject.extra val visionForgeVersion: String by rootProject.extra
val production: Boolean by rootProject.extra(false) val production: Boolean by rootProject.extra(true)
kotlin { kotlin {
js(IR) { js(IR) {
@ -21,6 +21,7 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
implementation(project(":numass-data-model")) implementation(project(":numass-data-model"))
implementation("space.kscience:visionforge-core:$visionForgeVersion")
implementation("space.kscience:visionforge-plotly:$visionForgeVersion") implementation("space.kscience:visionforge-plotly:$visionForgeVersion")
} }
} }

View File

@ -26,8 +26,8 @@ public class NumassCommonPlugin(meta: Meta = Meta.EMPTY) : VisionPlugin(meta) {
polymorphic(Vision::class) { polymorphic(Vision::class) {
subclass(VisionOfNumassHv.serializer()) subclass(VisionOfNumassHv.serializer())
subclass(VisionOfNumassPoint.serializer()) subclass(VisionOfNumassPoint.serializer())
subclass(VisionOfNumassHv.serializer())
subclass(VisionOfNumassSet.serializer()) subclass(VisionOfNumassSet.serializer())
subclass(VisionOfNumassRepository.serializer())
} }
} }
} }

View File

@ -3,14 +3,11 @@ package ru.inr.mass.data.server
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import ru.inr.mass.data.api.NumassBlock import ru.inr.mass.data.api.NumassBlock
import ru.inr.mass.data.api.NumassPoint import ru.inr.mass.data.api.NumassPoint
import ru.inr.mass.data.api.NumassSet
import ru.inr.mass.data.api.NumassSet.Companion.NUMASS_HV_TARGET
import ru.inr.mass.data.api.channels import ru.inr.mass.data.api.channels
import ru.inr.mass.data.proto.HVData import ru.inr.mass.data.proto.HVData
import ru.inr.mass.data.proto.HVEntry import ru.inr.mass.data.proto.HVEntry
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.names.NameToken import space.kscience.dataforge.names.NameToken
import space.kscience.dataforge.provider.top
import space.kscience.visionforge.AbstractVision import space.kscience.visionforge.AbstractVision
@ -48,18 +45,3 @@ public class VisionOfNumassHv(public val hv: HVData) : AbstractVision(), Iterabl
private val VisionOfNumassPoint.token: NameToken get() = NameToken("point", index.toString()) private val VisionOfNumassPoint.token: NameToken get() = NameToken("point", index.toString())
@Serializable
public class VisionOfNumassSet(public val points: List<VisionOfNumassPoint>) : AbstractVision() {
// init {
// points.forEach {
// //childrenInternal[it.token] = it
// }
//
// }
}
public suspend fun NumassSet.toVision(): VisionOfNumassSet = VisionOfNumassSet(points.map { it.toVision() }).apply {
this@toVision.top<HVData>(NUMASS_HV_TARGET).forEach { (key, hv) ->
// set(key, VisionOfNumassHv(hv))
}
}

View File

@ -0,0 +1,29 @@
package ru.inr.mass.data.server
import kotlinx.serialization.Serializable
import ru.inr.mass.data.api.NumassSet
import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.DataTreeItem
import space.kscience.dataforge.data.await
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.NameToken
import space.kscience.dataforge.names.plus
import space.kscience.visionforge.AbstractVisionGroup
@Serializable
public class VisionOfNumassRepository : AbstractVisionGroup() {
override fun createGroup(): VisionOfNumassRepository = VisionOfNumassRepository()
}
public suspend fun VisionOfNumassRepository(
repoName: Name,
tree: DataTree<NumassSet>,
): VisionOfNumassRepository = VisionOfNumassRepository().apply {
tree.items.forEach { (key: NameToken, value) ->
children[key] = when (value) {
is DataTreeItem.Leaf -> VisionOfNumassSet(repoName + key, value.data.await())
is DataTreeItem.Node -> VisionOfNumassRepository(repoName + key, value.tree)
}
}
//TODO listen to changes
}

View File

@ -0,0 +1,36 @@
package ru.inr.mass.data.server
import kotlinx.serialization.Serializable
import ru.inr.mass.data.api.NumassSet
import ru.inr.mass.data.proto.HVData
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.misc.Named
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.plus
import space.kscience.visionforge.AbstractVision
@Serializable
public data class PointRef(
override val name: Name,
public val pointMeta: Meta,
public val index: Int,
public val voltage: Double,
) : Named
@Serializable
public class VisionOfNumassSet(
public val points: List<PointRef>,
public val hvData: HVData? = null,
) : AbstractVision()
public fun VisionOfNumassSet(setName: Name, set: NumassSet): VisionOfNumassSet = VisionOfNumassSet(
set.points.map { point ->
PointRef(
setName + point.index.toString(),
point.meta,
point.index,
point.voltage
)
},
set.hvData
)

View File

@ -0,0 +1,132 @@
package ru.inr.mass.data.server
import io.ktor.http.HttpStatusCode
import io.ktor.http.URLBuilder
import io.ktor.server.application.call
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import ru.inr.mass.data.api.NumassPoint
import ru.inr.mass.data.proto.NumassDirectorySet
import ru.inr.mass.data.proto.NumassProtoPlugin
import ru.inr.mass.data.proto.readRepository
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.fetch
import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.await
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.NameToken
import space.kscience.dataforge.names.cutLast
import space.kscience.dataforge.names.lastOrNull
import space.kscience.visionforge.server.close
import space.kscience.visionforge.server.openInBrowser
import space.kscience.visionforge.server.visionServer
public suspend fun main() {
val context = Context("Numass") {
plugin(NumassProtoPlugin)
plugin(NumassCommonPlugin)
}
val numassProto = context.fetch(NumassProtoPlugin)
val numassCommon = context.fetch(NumassCommonPlugin)
val visionManager = numassCommon.visionManager
val repositroyName = "D:\\Work\\Numass\\data\\test"
val port = 7777
val host = "localhost"
val url = URLBuilder(host = host, port = port).build()
val repository: DataTree<NumassDirectorySet> = numassProto.readRepository(repositroyName)
val visionOfNumassRepository = VisionOfNumassRepository(Name.EMPTY, repository)
val server = context.embeddedServer(CIO, port, host) {
routing {
get("/repository") {
call.respondText {
visionManager.encodeToString(visionOfNumassRepository)
}
}
get("/sets/{name...}") {
val setName: Name? = call.parameters.getAll("name")
?.map { NameToken.parse(it) }?.let(::Name)
if (setName == null) {
call.respondText(status = HttpStatusCode.BadRequest) { "Set name is empty" }
return@get
}
val set: NumassDirectorySet? = withContext(Dispatchers.IO) { repository[setName]?.await() }
if (set == null) {
call.respondText(status = HttpStatusCode.NotFound) { "A set with name $setName not found in the repository" }
return@get
}
call.respondText {
visionManager.encodeToString(VisionOfNumassSet(setName, set))
}
}
get("/points/{name...}") {
val fullName: Name? = call.parameters.getAll("name")
?.map { NameToken.parse(it) }?.let(::Name)
if (fullName == null) {
call.respondText(status = HttpStatusCode.BadRequest) { "Point name is empty" }
return@get
}
val setName = fullName.cutLast()
val set: NumassDirectorySet? = withContext(Dispatchers.IO) { repository[setName]?.await() }
if (set == null) {
call.respondText(status = HttpStatusCode.NotFound) { "A set with name $setName not found in the repository" }
return@get
}
val pointIndex: Int? = fullName.lastOrNull()?.body?.toIntOrNull()
val point: NumassPoint? = set.points.find { it.index == pointIndex }
if (point == null) {
call.respondText(status = HttpStatusCode.NotFound) { "A point with name $setName/$pointIndex not found in the repository" }
return@get
}
call.respondText {
visionManager.encodeToString(point.toVision())
}
}
}
visionServer(numassCommon.visionManager, url)
}.start()
// val server = context.visionManager.serve {
// header("numass", VisionPage.scriptHeader("js/numass-web.js"))
// page {
// div("flex-column") {
// h1 { +"Visionforge file demo" }
// vision { visionOfNumass }
// }
// }
// }
server.openInBrowser()
println("Enter 'exit' to close server")
while (readLine() != "exit") {
//
}
server.close()
}

View File

@ -1,50 +0,0 @@
package ru.inr.mass.data.server
import kotlinx.html.div
import kotlinx.html.h1
import ru.inr.mass.data.api.NumassPoint
import ru.inr.mass.data.proto.NumassProtoPlugin
import ru.inr.mass.data.proto.readNumassPointFile
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.fetch
import space.kscience.visionforge.html.VisionPage
import space.kscience.visionforge.server.close
import space.kscience.visionforge.server.openInBrowser
import space.kscience.visionforge.server.serve
import space.kscience.visionforge.visionManager
import java.nio.file.Path
public suspend fun main() {
val context = Context("Numass") {
plugin(NumassProtoPlugin)
plugin(NumassCommonPlugin)
}
val numassProto = context.fetch(NumassProtoPlugin)
val pointPath = Path.of("D:\\Work\\Numass\\data\\test\\set_7\\p120(30s)(HV1=13300)")
val point: NumassPoint = numassProto.readNumassPointFile(pointPath)!!
val visionOfNumass = point.toVision()
val server = context.visionManager.serve {
header("numass", VisionPage.scriptHeader("js/numass-web.js"))
page {
div("flex-column") {
h1 { +"Visionforge file demo" }
vision { visionOfNumass }
}
}
}
server.openInBrowser()
println("Enter 'exit' to close server")
while (readLine() != "exit") {
//
}
server.close()
}

View File

@ -128,7 +128,7 @@ fun Plotly.plotNumassSet(
} }
if (set is NumassDirectorySet) { if (set is NumassDirectorySet) {
set.getHvData()?.let { entries -> set.hvData?.let { entries ->
h2 { +"HV" } h2 { +"HV" }
plot { plot {
hvData(entries) hvData(entries)