From fd5b177b30011e40a5fac8e3de36007015668564 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 5 Nov 2017 22:10:29 +0300 Subject: [PATCH] Using Names instead of Strings wherever possible --- build.gradle | 2 + .../kotlin/inr/numass/control/BoardView.kt | 2 +- .../numass/control/msp/MspViewConnection.kt | 4 +- .../readvac/VacCollectorViewConnection.kt | 4 +- .../data/storage/NumassDataFactory.java | 6 +- numass-kserver/build.gradle | 28 ++++++ .../kotlin/inr/numass/server/Interceptors.kt | 91 +++++++++++++++++++ .../plotfit/PlotFitResultAction.java | 4 +- .../src/main/java/inr/numass/NumassIO.java | 2 +- .../src/main/java/inr/numass/models/FSS.java | 4 +- .../models/TransmissionInterpolator.java | 7 +- .../inr/numass/actions/TimeAnalyzerAction.kt | 4 +- .../kotlin/inr/numass/viewer/AmplitudeView.kt | 8 +- .../main/kotlin/inr/numass/viewer/HVView.kt | 4 +- .../kotlin/inr/numass/viewer/SpectrumView.kt | 8 +- .../kotlin/inr/numass/viewer/StorageView.kt | 6 +- settings.gradle | 1 + 17 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 numass-kserver/build.gradle create mode 100644 numass-kserver/src/main/kotlin/inr/numass/server/Interceptors.kt diff --git a/build.gradle b/build.gradle index d92fcc74..02790dcf 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,8 @@ allprojects{ jcenter() mavenCentral() maven { url "https://jitpack.io" } + maven { url "http://dl.bintray.com/kotlin/ktor" } + maven { url "https://dl.bintray.com/kotlin/kotlinx" } } // tasks.withType(JavaCompile) { diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt index 70e58f7b..7f4de6f0 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt @@ -69,7 +69,7 @@ class BoardView : View("Numass control board", ImageView(dfIcon)) { if (storage is FileStorage) { "Path: " + storage.dataDir; } else { - "Name: " + storage.fullPath + "Name: " + storage.fullName } } }) diff --git a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt index 3fe835e6..2d41c415 100644 --- a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt +++ b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt @@ -77,8 +77,8 @@ class MspViewConnection() : DeviceViewConnection(), DeviceListener, N val basePlotConfig = MetaBuilder("plotFrame") .setNode(MetaBuilder("yAxis") .setValue("type", "log") - .setValue("axisTitle", "partial pressure") - .setValue("axisUnits", "mbar") + .setValue("title", "partial pressure") + .setValue("units", "mbar") ) .setValue("xAxis.type", "time") diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/VacCollectorViewConnection.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/VacCollectorViewConnection.kt index bd2c148c..6b9384bb 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/VacCollectorViewConnection.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/VacCollectorViewConnection.kt @@ -99,8 +99,8 @@ class VacCollectorViewConnection : DeviceViewConnection() { "xAxis.type" to "time" node("yAxis") { "type" to "log" - "axisTitle" to "presure" - "axisUnits" to "mbar" + "title" to "presure" + "units" to "mbar" } } right { diff --git a/numass-core/src/main/java/inr/numass/data/storage/NumassDataFactory.java b/numass-core/src/main/java/inr/numass/data/storage/NumassDataFactory.java index f2ff13ee..bf2b5f70 100644 --- a/numass-core/src/main/java/inr/numass/data/storage/NumassDataFactory.java +++ b/numass-core/src/main/java/inr/numass/data/storage/NumassDataFactory.java @@ -25,9 +25,9 @@ public class NumassDataFactory extends DataFactory { @Override protected void fill(DataTree.Builder builder, Context context, Meta meta) { NumassStorage storage = new NumassStorage(context,meta); - StorageUtils.loaderStream(storage).forEach(pair -> { - if (pair.getValue() instanceof NumassSet) { - builder.putStatic(pair.getKey(), (NumassSet) pair.getValue()); + StorageUtils.loaderStream(storage).forEach(loader -> { + if (loader instanceof NumassSet) { + builder.putStatic(loader.getFullName().toUnescaped(), (NumassSet) loader); } }); } diff --git a/numass-kserver/build.gradle b/numass-kserver/build.gradle new file mode 100644 index 00000000..900371e7 --- /dev/null +++ b/numass-kserver/build.gradle @@ -0,0 +1,28 @@ +plugins { + id "com.github.johnrengelman.shadow" version "2.0.1" +} + +apply plugin: 'kotlin' + +description = 'kodex/ktor based server' + +//mainClassName = "inr.numass.server.ServerRunner" + +dependencies { + compile "hep.dataforge:kodex-server" + compile "hep.dataforge:dataforge-storage" + compile project(":numass-core") +} + +compileKotlin { + kotlinOptions { + jvmTarget = "1.8" + javaParameters = true + } +} + +kotlin { + experimental { + coroutines "enable" + } +} \ No newline at end of file diff --git a/numass-kserver/src/main/kotlin/inr/numass/server/Interceptors.kt b/numass-kserver/src/main/kotlin/inr/numass/server/Interceptors.kt new file mode 100644 index 00000000..23978564 --- /dev/null +++ b/numass-kserver/src/main/kotlin/inr/numass/server/Interceptors.kt @@ -0,0 +1,91 @@ +package inr.numass.server + +import hep.dataforge.context.Context +import hep.dataforge.meta.Meta +import hep.dataforge.providers.Path +import hep.dataforge.server.* +import hep.dataforge.storage.api.TableLoader +import hep.dataforge.storage.commons.StorageManager +import hep.dataforge.storage.commons.StorageUtils +import hep.dataforge.values.Value +import io.ktor.application.ApplicationCall +import io.ktor.application.call +import io.ktor.http.ContentType +import io.ktor.response.respondText +import io.ktor.routing.get +import javax.json.JsonObjectBuilder + + +private suspend fun ApplicationCall.error(type: String, message: String) { + this.respondText(ContentType("application", "json")) { + jsonObject { + add("status", "ERROR") + add("type", type) + add("message", message) + }.render() + } +} + +private suspend fun ApplicationCall.json(json: suspend JsonObjectBuilder.() -> Unit) { + this.respondText(ContentType("application", "json")) { + jsonObject(json).add("status", "OK").render() + } +} + + +class StorageInterceptorBuilder : InterceptorBuilder { + override fun build(context: Context, meta: Meta): ServerInterceptor { + val storageManager = context.getFeature(StorageManager::class.java); + val storage = storageManager.buildStorage(meta); + return ServerInterceptor("storage") { + get("listStorage") { + val path = call.request.queryParameters["path"] ?: "" + val shelf = storage.optShelf(path) + if (shelf.isPresent) { + call.json { + val loaders = jsonArray(); + for (loader in StorageUtils.loaderStream(shelf.get())) { + loaders.add(jsonObject { + add("name", loader.name) + add("path", loader.path.toString()) + add("type", loader.type) + add("meta", loader.laminate.asJson()) + }) + } + add("loaders", loaders) + } + } else { + call.error("storage.shelfNotFound", "The shelf with path '$path' not found") + } + } + get("getPlotData") { + val path = call.request.queryParameters["path"] + if (path == null) { + call.error("storage.missingParameter", "Missing request parameter 'path'") + } else { + val loaderObject = storage.provide(Path.of(path)) + if (loaderObject.isPresent) { + val loader = loaderObject.get(); + if (loader is TableLoader) { + val from = Value.of(call.request.queryParameters["from"] ?: "") + val to = Value.of(call.request.queryParameters["to"] ?: "") + val maxItems = (call.request.queryParameters.get("maxItems") ?: "1000").toInt() + call.json { + add("path", loader.path.toString()) + val data = jsonArray() + for (point in loader.index.pull(from, to, maxItems)) { + data.add(point.asJson()) + } + } + } else { + call.error("storage.incorrectLoaderType", "Loader $path is not a TableLoader") + } + } else { + call.error("storage.loaderNotFound", "Can't find TableLoader with path = '$path'") + } + } + } + } + } + +} \ No newline at end of file diff --git a/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java b/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java index 37b89d35..d341d126 100644 --- a/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java +++ b/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java @@ -29,7 +29,7 @@ import hep.dataforge.plots.data.XYFunctionPlot; import hep.dataforge.stat.fit.FitResult; import hep.dataforge.stat.fit.FitState; import hep.dataforge.stat.models.XYModel; -import hep.dataforge.tables.NavigablePointSource; +import hep.dataforge.tables.NavigableValuesSource; import hep.dataforge.tables.XYAdapter; import java.util.function.Function; @@ -48,7 +48,7 @@ public class PlotFitResultAction extends OneToOneAction { FitState state = input.optState().orElseThrow(()->new UnsupportedOperationException("Can't work with fit result not containing state, sorry! Will fix it later")); - NavigablePointSource data = input.getData(); + NavigableValuesSource data = input.getData(); if (!(state.getModel() instanceof XYModel)) { context.getChronicle(name).reportError("The fit model should be instance of XYModel for this action. Action failed!"); return input; diff --git a/numass-main/src/main/java/inr/numass/NumassIO.java b/numass-main/src/main/java/inr/numass/NumassIO.java index 715d3809..04a104f4 100644 --- a/numass-main/src/main/java/inr/numass/NumassIO.java +++ b/numass-main/src/main/java/inr/numass/NumassIO.java @@ -104,7 +104,7 @@ public class NumassIO extends BasicIOManager { } String dirName = String.join(File.separator, tokens); - String fileName = name.removeNameSpace().toString() + getExtension(type); + String fileName = name.toString() + getExtension(type); OutputStream out = buildOut(getWorkDirectory(), dirName, fileName); registry.add(out); return out; diff --git a/numass-main/src/main/java/inr/numass/models/FSS.java b/numass-main/src/main/java/inr/numass/models/FSS.java index 4cd59792..d05fe20d 100644 --- a/numass-main/src/main/java/inr/numass/models/FSS.java +++ b/numass-main/src/main/java/inr/numass/models/FSS.java @@ -16,7 +16,7 @@ package inr.numass.models; import hep.dataforge.io.IOUtils; -import hep.dataforge.tables.PointSource; +import hep.dataforge.tables.ValuesSource; import hep.dataforge.values.Values; import java.io.InputStream; @@ -31,7 +31,7 @@ public class FSS { private double norm; public FSS(InputStream stream) { - PointSource data = IOUtils.readColumnedData(stream, "E", "P"); + ValuesSource data = IOUtils.readColumnedData(stream, "E", "P"); norm = 0; for (Values dp : data) { es.add(dp.getDouble("E")); diff --git a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java index 8b58c446..e9f307db 100644 --- a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java +++ b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java @@ -20,15 +20,12 @@ import hep.dataforge.context.Context; import hep.dataforge.data.DataNode; import hep.dataforge.io.ColumnedDataReader; import hep.dataforge.meta.Meta; -import hep.dataforge.tables.PointSource; import hep.dataforge.tables.Table; +import hep.dataforge.tables.ValuesSource; import hep.dataforge.values.Values; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.interpolation.LinearInterpolator; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -55,7 +52,7 @@ public class TransmissionInterpolator implements UnivariateFunction { public static TransmissionInterpolator fromAction(Context context, Meta actionAnnotation, String xName, String yName, int nSmooth, double w, double border) throws InterruptedException { DataNode node = ActionUtils.runConfig(context, actionAnnotation); - PointSource data = node.getData().get(); + ValuesSource data = node.getData().get(); return new TransmissionInterpolator(data, xName, yName, nSmooth, w, border); } diff --git a/numass-main/src/main/kotlin/inr/numass/actions/TimeAnalyzerAction.kt b/numass-main/src/main/kotlin/inr/numass/actions/TimeAnalyzerAction.kt index 0cc88936..1813165b 100644 --- a/numass-main/src/main/kotlin/inr/numass/actions/TimeAnalyzerAction.kt +++ b/numass-main/src/main/kotlin/inr/numass/actions/TimeAnalyzerAction.kt @@ -72,8 +72,8 @@ class TimeAnalyzerAction : OneToOneAction() { histPlot.configure { node("xAxis") { - "axisTitle" to "delay" - "axisUnits" to "us" + "title" to "delay" + "units" to "us" } node("yAxis") { "type" to "log" diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt index 27ccdbd5..50ad4825 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/AmplitudeView.kt @@ -36,13 +36,13 @@ class AmplitudeView( private val frame: PlotFrame = JFreeChartFrame().configure { "title" to "Detector response plot" node("xAxis") { - "axisTitle" to "ADC" - "axisUnits" to "channels" + "title" to "ADC" + "units" to "channels" } node("yAxis") { - "axisTitle" to "count rate" - "axisUnits" to "Hz" + "title" to "count rate" + "units" to "Hz" } "legend.show" to false } diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/HVView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/HVView.kt index bbc67e20..c93b0b2b 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/HVView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/HVView.kt @@ -22,9 +22,9 @@ import tornadofx.* class HVView : View(title = "High voltage time plot", icon = ImageView(dfIcon)) { private val frame: PlotFrame = JFreeChartFrame().configure { - "xAxis.axisTitle" to "time" + "xAxis.title" to "time" "xAxis.type" to "time" - "yAxis.axisTitle" to "HV" + "yAxis.title" to "HV" } private val container = PlotContainer(frame); diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt index e6bd0b86..92dd339b 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/SpectrumView.kt @@ -40,10 +40,10 @@ class SpectrumView( ) : View(title = "Numass spectrum plot", icon = ImageView(dfIcon)) { private val frame: PlotFrame = JFreeChartFrame().configure { - "xAxis.axisTitle" to "U" - "xAxis.axisUnits" to "V" - "yAxis.axisTitle" to "count rate" - "yAxis.axisUnits" to "Hz" + "xAxis.title" to "U" + "xAxis.units" to "V" + "yAxis.title" to "count rate" + "yAxis.units" to "Hz" //"legend.show" to false } private val container = PlotContainer(frame); diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt index ba781d0c..a0305d26 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt @@ -228,7 +228,7 @@ class StorageView(private val context: Context = Global.instance()) : View(title private fun buildContainer(content: Any, parent: Container): Container { return when (content) { is Storage -> { - Container(content.fullPath, content) + Container(content.fullName.toString(), content) } is NumassSet -> { val id = if (content is NumassDataLoader) { @@ -236,13 +236,13 @@ class StorageView(private val context: Context = Global.instance()) : View(title } else { content.name } - Container(id, content) + Container(id.toString(), content) } is NumassPoint -> { Container("${parent.id}/${content.voltage}".replace(".", "_"), content) } is Loader -> { - Container(content.path, content); + Container(content.path.toString(), content); } else -> throw IllegalArgumentException("Unknown content type: ${content::class.java}"); } diff --git a/settings.gradle b/settings.gradle index 0e0c5581..7e2ed424 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,6 +13,7 @@ include ":numass-core" include ":numass-client" include ":numass-server" include ":numass-server" +include ":numass-kserver" include ":numass-test" // include ":numass-viewer"