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=c7dee69b0834b463963c4a3ccdd92b1fe1649bc9;hp=8ca1ca1eafef43c9b469d6c1dcfbf40f0f7eac8f;hb=f3617565a8ffe49d63016f0c1d78e0d4282214b0;hpb=f62bab9b78e60b94e055d51db98a03141415323e 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 8ca1ca1ea..c7dee69b0 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; @@ -64,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"); @@ -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 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); + }