]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/Charts.java
Added another sampling method to HistorySampler
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / Charts.java
index 8ca1ca1eafef43c9b469d6c1dcfbf40f0f7eac8f..c7dee69b0834b463963c4a3ccdd92b1fe1649bc9 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;
@@ -64,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");
 
@@ -76,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 createHistorySamplerItem2(graph, subscriptionItem, data);
+    }
+
+    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);
+
 }