]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/Charts.java
Faster bounds calculation for zoom to selection and navigate to target
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / Charts.java
index 1358c402333b40a42d425e66acaef6faf339af42..230e905e441f70e8b031220ee35b0f929ffc76a2 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.charts;
 
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 import org.simantics.charts.editor.ChartData;
@@ -14,7 +15,9 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.history.HistoryException;
 import org.simantics.history.HistorySamplerItem;
+import org.simantics.history.HistorySamplerItem2;
 import org.simantics.history.ItemManager;
+import org.simantics.history.HistorySamplerItem2.LevelItem;
 import org.simantics.history.util.subscription.SamplingFormat;
 import org.simantics.modeling.subscription.SubscriptionItem;
 import org.simantics.modeling.subscription.SubscriptionItemQuery;
@@ -48,9 +51,13 @@ public final class Charts {
     public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
 
         try {
-            Resource model = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
-            if (model == null) {
-                throw new DatabaseException("There is no model for " + subscriptionItem);
+            Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
+            if (indexRoot == null) {
+                throw new DatabaseException("There is no index root for " + subscriptionItem);
+            }
+
+            if(data == null) {
+                throw new DatabaseException("There is no chart data for " + subscriptionItem);
             }
 
             ItemManager im = new ItemManager(data.history.getItems());
@@ -60,7 +67,7 @@ public final class Charts {
             List<Bean> items = im.search("variableId", i.variableId);
             Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
             if (items.isEmpty())
-                new DatabaseException("There is history item for " + i.variableId);
+                throw new DatabaseException("There is history item for " + i.variableId);
             Bean config = items.get(0);
             String historyId = (String) config.getFieldUnchecked("id");
 
@@ -72,4 +79,47 @@ public final class Charts {
         }
     }
 
+    public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
+        IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
+        ITrendSupport support = exp.getService(ITrendSupport.class);
+        ChartData data = support.getChartData();
+        return data != null ? createHistorySamplerItem2(graph, subscriptionItem, data) : null;
+    }
+
+    public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
+        try {
+            Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
+            if (indexRoot == null)
+                throw new DatabaseException("There is no index root for " + subscriptionItem);
+            if (data == null)
+                throw new DatabaseException("There is no chart data for " + subscriptionItem);
+
+            SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
+
+            // TODO: this is super-inefficient!
+            ItemManager im = new ItemManager(data.history.getItems());
+
+            List<Bean> items = im.search("variableId", i.variableId);
+            if (items.isEmpty())
+                throw new DatabaseException("There is history item for " + i.variableId);
+
+            LevelItem[] historyItems = items.stream().map(Charts::toLevelItem).sorted(ASCENDING_INTERVAL_ORDER).toArray(LevelItem[]::new);
+
+            return new HistorySamplerItem2(data.collector, data.history, historyItems, System.identityHashCode(data));
+        } catch (HistoryException e) {
+            throw new DatabaseException(e);
+        } catch (BindingException e) {
+            throw new DatabaseException(e);
+        }
+    }
+
+    private static LevelItem toLevelItem(Bean bean) {
+        String id = (String) bean.getFieldUnchecked("id");
+        double interval = (Double) bean.getFieldUnchecked("interval");
+        return new LevelItem(id, interval);
+    }
+
+    public final static Comparator<LevelItem> ASCENDING_INTERVAL_ORDER =
+            (o1, o2) -> SamplingFormat.compareSamplingInterval(o1.samplingInterval, o2.samplingInterval);
+
 }