X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2FCharts.java;h=230e905e441f70e8b031220ee35b0f929ffc76a2;hp=1358c402333b40a42d425e66acaef6faf339af42;hb=13edeabfa275ed09f6fa1b76923a96a1172fdc22;hpb=591f4572f18d20a08a797a8e5c4a8dfc1b3320c1 diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java b/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java index 1358c4023..230e905e4 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java @@ -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 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 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 ASCENDING_INTERVAL_ORDER = + (o1, o2) -> SamplingFormat.compareSamplingInterval(o1.samplingInterval, o2.samplingInterval); + }