You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by jo...@apache.org on 2022/06/13 16:51:05 UTC

[isis] 02/06: ISIS-3041 fixing unset errortype, code cosmetic, debugging info

This is an automated email from the ASF dual-hosted git repository.

joergrade pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit bb5574fa934e1d973f3d999f2f1b2eb0880caa7c
Author: Jörg Rade <jo...@kuehne-nagel.com>
AuthorDate: Thu Jun 2 14:58:31 2022 +0200

    ISIS-3041 fixing unset errortype, code cosmetic, debugging info
---
 .../kroviz/core/aggregator/AggregatorWithLayout.kt | 21 ++++++++++++++++-
 .../kroviz/core/aggregator/ObjectAggregator.kt     |  2 +-
 .../isis/client/kroviz/core/event/EventStore.kt    | 19 +++++++++++-----
 .../isis/client/kroviz/core/event/LogEntry.kt      | 26 ++++++++++++++++++----
 .../isis/client/kroviz/core/event/ResourceProxy.kt | 25 ++++++++++++++++++++-
 .../apache/isis/client/kroviz/IntegrationTest.kt   | 18 ++++++++++-----
 .../isis/client/kroviz/core/event/LogEntryTest.kt  |  9 +++-----
 7 files changed, 95 insertions(+), 25 deletions(-)

diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/AggregatorWithLayout.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/AggregatorWithLayout.kt
index dad0d12824..81f504fbc8 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/AggregatorWithLayout.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/AggregatorWithLayout.kt
@@ -1,3 +1,21 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
 package org.apache.isis.client.kroviz.core.aggregator
 
 import org.apache.isis.client.kroviz.core.event.LogEntry
@@ -19,12 +37,13 @@ abstract class AggregatorWithLayout : BaseAggregator() {
     }
 
     protected fun handleLayout(layout: Layout, dm: DisplayModelWithLayout, referrer: String) {
+        console.log("[AWL.handleLayout]")
+        console.log(layout)
         if (dm.layout == null) {
             dm.addLayout(layout)
             dm.properties.propertyLayoutList.forEach { p ->
                 val l = p.link
                 if (l == null) {
-                    console.log("[AWL.handleLayout]")
                     console.log(p.id + " link empty")  // ISIS-2846
                     console.log(p)
                 } else {
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ObjectAggregator.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ObjectAggregator.kt
index 0d1794c599..05c981c923 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ObjectAggregator.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ObjectAggregator.kt
@@ -122,7 +122,7 @@ class ObjectAggregator(val actionTitle: String) : AggregatorWithLayout() {
     private fun handleProperty(property: Property) {
         console.log("[OA.handleProperty]")
         console.log(property)
-        throw Throwable("[ObjectAggregator.handleProperty] not implemented yet")
+//        throw Throwable("[ObjectAggregator.handleProperty] not implemented yet")
     }
 
     private fun handleGrid(grid: Grid) {
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/EventStore.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/EventStore.kt
index 16d7a7c456..5758573d3e 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/EventStore.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/EventStore.kt
@@ -58,7 +58,7 @@ class EventStore {
         rs: ResourceSpecification,
         method: String,
         body: String = "",
-        aggregator: BaseAggregator? = null
+        aggregator: BaseAggregator? = null,
     ): LogEntry {
         val entry = LogEntry(rs = rs, method = method, request = body)
         if (aggregator != null) {
@@ -149,11 +149,11 @@ class EventStore {
     }
 
     internal fun updateStatus(entry: LogEntry) {
-        val successNo = log.count { le -> le.isSuccess() }
-        val runningNo = countRunning()
-        val errorNo = log.count { le -> le.isError() }
-        val viewNo = log.count { le -> le.isView() }
-        val status = StatusPo(successNo, runningNo, errorNo, viewNo)
+        val successCnt = log.count { le -> le.isSuccess() }
+        val runningCnt = countRunning()
+        val errorCnt = log.count { le -> le.isError() }
+        val viewCnt = log.count { le -> le.isView() }
+        val status = StatusPo(successCnt, runningCnt, errorCnt, viewCnt, 0)
         ViewManager.updateStatus(status)
     }
 
@@ -218,6 +218,13 @@ class EventStore {
         }
     }
 
+    fun findAll(reSpec: ResourceSpecification): List<LogEntry> {
+        return log.filter {
+            it.matches(reSpec)
+        }
+    }
+
+
     //public for test
     fun findExact(reSpec: ResourceSpecification): LogEntry? {
         return log.firstOrNull {
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/LogEntry.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/LogEntry.kt
index eaaf436b54..ef2024b1ef 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/LogEntry.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/LogEntry.kt
@@ -125,6 +125,7 @@ data class LogEntry(
         calculate()
         fault = error
         state = EventState.ERROR
+        type = Represention.ERROR.type
     }
 
     fun setUndefined(error: String) {
@@ -143,7 +144,8 @@ data class LogEntry(
         responseLength = response.length
         if (responseLength == 0) {
             // it's a blob
-            responseLength = blob?.size as Int
+            val size = blob?.size ?: 0
+            responseLength = size.toInt()
         }
         state = when {
             url.startsWith(Constants.krokiUrl) -> EventState.SUCCESS_IMG
@@ -178,6 +180,7 @@ data class LogEntry(
     }
 
     fun setTransferObject(to: TransferObject) {
+        console.log("[LE.setTransferObject]")
         this.obj = to
         when (to) {
             is WithLinks -> {
@@ -195,8 +198,20 @@ data class LogEntry(
             is Menubars -> {
                 this.type = Represention.LAYOUT_MENUBARS.type
             }
+            is HttpError -> {
+                this.type = Represention.ERROR.type
+            }
+            is TObject -> {
+                when {
+                    to == null -> {
+                        this.state = EventState.MISSING
+                        this.type = Represention.ERROR.type
+                        console.log("to == null for response:")
+                        console.log(response)
+                    }
+                }
+            }
             else -> {
-                console.log("[LE.setTransferObject]")
                 console.log(to)
             }
         }
@@ -232,12 +247,16 @@ data class LogEntry(
 
     fun retrieveResponse(): String {
         lastAccessedAt = Date()
-        cacheHits++
+        incrementCacheHits()
         return response
     }
 
 //end region response
 
+    fun incrementCacheHits() {
+        cacheHits++
+    }
+
     fun isSuccess(): Boolean {
         return state.name.startsWith("SUCCESS")
     }
@@ -276,7 +295,6 @@ data class LogEntry(
 
     fun addAggregator(aggregator: BaseAggregator) {
         if (aggregator is ActionDispatcher) {
-//            console.log("[LE.addAggregator] is ActionDispatcher")
             ViewManager.setBusyCursor()
         }
         aggregators.add(aggregator)
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/ResourceProxy.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/ResourceProxy.kt
index ec5d9d7f2b..f686fc130d 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/ResourceProxy.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/event/ResourceProxy.kt
@@ -61,7 +61,8 @@ class ResourceProxy {
               isRest: Boolean = true,
               referrer: String = "") {
         val rs = ResourceSpecification(link.href, subType = subType, referrerUrl = referrer)
-        val isCached = when (val le = SessionManager.getEventStore().findBy(rs)) {
+        val le = findAndSetupLogEntry(rs)
+        val isCached = when (le) {
             null -> false
             else -> le.isCached(rs, link.method)
         }
@@ -72,6 +73,28 @@ class ResourceProxy {
         }
     }
 
+    // in case of a race condition (many similar requests started within millis),
+    // it could happen, that similar entries are written to the log in parallel,
+    // although they should have been taken from the cache.
+    private fun findAndSetupLogEntry(rs: ResourceSpecification): LogEntry? {
+        val leList = SessionManager.getEventStore().findAll(rs)
+        val first = leList.firstOrNull()
+        if (first?.state == EventState.DUPLICATE) {
+            when (rs.subType) {
+                Constants.subTypeJson -> first.state = EventState.SUCCESS_JS
+                Constants.subTypeXml -> first.state = EventState.SUCCESS_XML
+                else -> first.state = EventState.SUCCESS_IMG
+            }
+        }
+        leList.forEachIndexed { index, logEntry ->
+            if (index > 0 && logEntry.state != EventState.DUPLICATE) {
+                logEntry.state = EventState.DUPLICATE
+                first?.incrementCacheHits()
+            }
+        }
+        return first
+    }
+
     private fun process(aggregator: BaseAggregator?, link: Link, subType: String, referrer: String) {
         if (aggregator is AggregatorWithLayout) {
             if (aggregator.tree == null) {
diff --git a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/IntegrationTest.kt b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/IntegrationTest.kt
index 426b85c04b..da1129056e 100644
--- a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/IntegrationTest.kt
+++ b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/IntegrationTest.kt
@@ -30,6 +30,7 @@ import org.apache.isis.client.kroviz.snapshots.Response
 import org.apache.isis.client.kroviz.to.Method
 import org.apache.isis.client.kroviz.ui.core.Constants
 import org.apache.isis.client.kroviz.ui.core.SessionManager
+import org.apache.isis.client.kroviz.ui.core.ViewManager
 import org.apache.isis.client.kroviz.utils.XmlHelper
 import org.w3c.xhr.XMLHttpRequest
 
@@ -38,6 +39,12 @@ import org.w3c.xhr.XMLHttpRequest
 open class IntegrationTest {
 
     fun isAppAvailable(): Boolean {
+        val app = App()
+        app.start()
+        ViewManager.app = app
+console.log("[IT.isAppAvailable]")
+        console.log(ViewManager.getRoApp())
+
         val user = "sven"
         val pw = "pass"
         val url = "http://${user}:${pw}@localhost:8080/restful/"
@@ -56,8 +63,7 @@ open class IntegrationTest {
         } finally {
 
         }
-        val answer = xhr.status.equals(200)
-        return answer
+        return xhr.status.equals(200)
     }
 
     fun mockResponse(response: Response, aggregator: BaseAggregator?): LogEntry {
@@ -69,10 +75,10 @@ open class IntegrationTest {
         val reSpec = ResourceSpecification(response.url, subType)
         val es = EventStore()
         es.start(
-                reSpec,
-                Method.GET.operation,
-                "",
-                aggregator)
+            reSpec,
+            Method.GET.operation,
+            "",
+            aggregator)
         val le = es.end(reSpec, str)!!
         ResponseHandler.handle(le)
         wait(100)
diff --git a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/core/event/LogEntryTest.kt b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/core/event/LogEntryTest.kt
index dea66af06d..38b4c5a492 100644
--- a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/core/event/LogEntryTest.kt
+++ b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/core/event/LogEntryTest.kt
@@ -28,10 +28,8 @@ class LogEntryTest {
     fun testTitle() {
         // given
         val url = "https://kroki.io"
-
         // when
         val le = LogEntry(ResourceSpecification(url))
-
         // then
         assertFalse(le.title.startsWith("/"))
     }
@@ -39,16 +37,15 @@ class LogEntryTest {
     @Test
     fun testCalculate() {
         // given
-        val le = LogEntry(ResourceSpecification("http://test/url"))
-
+        val rs = ResourceSpecification("http://test/url")
+        val le = LogEntry(rs)
         // when
         le.setSuccess()
-
         // then
         assertTrue(0 <= le.duration)
 
         if (le.duration < 0 && le.cacheHits == 0) {
-            //TODO add assert
+//            assertEquals(EventState.SUCCESS_JS, le.state)
         }
     }