]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.charts;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.List;
6
7 import org.simantics.charts.editor.ChartData;
8 import org.simantics.charts.editor.ChartKeys;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.util.Bean;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.PossibleIndexRoot;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.history.HistoryException;
17 import org.simantics.history.HistorySamplerItem;
18 import org.simantics.history.HistorySamplerItem2;
19 import org.simantics.history.ItemManager;
20 import org.simantics.history.HistorySamplerItem2.LevelItem;
21 import org.simantics.history.util.subscription.SamplingFormat;
22 import org.simantics.modeling.subscription.SubscriptionItem;
23 import org.simantics.modeling.subscription.SubscriptionItemQuery;
24 import org.simantics.project.IProject;
25 import org.simantics.simulation.experiment.IExperiment;
26
27 /**
28  * Main facade for externally dealing with the trending system.
29  * 
30  * @author Tuukka Lehtonen
31  * @author Antti Villberg
32  * 
33  */
34 public final class Charts {
35
36     public static void resetChartEditorData(IProject project, Resource model, ChartData editorData) {
37         if (editorData != null) {
38             project.setHint(ChartKeys.chartSourceKey(model), editorData);
39         } else {
40             project.removeHint(ChartKeys.chartSourceKey(model));
41         }
42     }
43
44         public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
45                         IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
46                         ITrendSupport support = exp.getService(ITrendSupport.class);
47                         ChartData data = support.getChartData();
48                         return createHistorySamplerItem(graph, subscriptionItem, data);
49         }
50
51     public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
52
53         try {
54             Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
55             if (indexRoot == null) {
56                 throw new DatabaseException("There is no index root for " + subscriptionItem);
57             }
58
59             if(data == null) {
60                 throw new DatabaseException("There is no chart data for " + subscriptionItem);
61             }
62
63             ItemManager im = new ItemManager(data.history.getItems());
64
65             SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
66
67             List<Bean> items = im.search("variableId", i.variableId);
68             Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
69             if (items.isEmpty())
70                 throw new DatabaseException("There is history item for " + i.variableId);
71             Bean config = items.get(0);
72             String historyId = (String) config.getFieldUnchecked("id");
73
74             return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data));
75         } catch (HistoryException e) {
76             throw new DatabaseException(e);
77         } catch (BindingException e) {
78             throw new DatabaseException(e);
79         }
80     }
81
82     public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
83         IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
84         ITrendSupport support = exp.getService(ITrendSupport.class);
85         ChartData data = support.getChartData();
86         return createHistorySamplerItem2(graph, subscriptionItem, data);
87     }
88
89     public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
90         try {
91             Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
92             if (indexRoot == null)
93                 throw new DatabaseException("There is no index root for " + subscriptionItem);
94             if (data == null)
95                 throw new DatabaseException("There is no chart data for " + subscriptionItem);
96
97             SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
98
99             // TODO: this is super-inefficient!
100             ItemManager im = new ItemManager(data.history.getItems());
101
102             List<Bean> items = im.search("variableId", i.variableId);
103             if (items.isEmpty())
104                 throw new DatabaseException("There is history item for " + i.variableId);
105
106             LevelItem[] historyItems = items.stream().map(Charts::toLevelItem).sorted(ASCENDING_INTERVAL_ORDER).toArray(LevelItem[]::new);
107
108             return new HistorySamplerItem2(data.collector, data.history, historyItems, System.identityHashCode(data));
109         } catch (HistoryException e) {
110             throw new DatabaseException(e);
111         } catch (BindingException e) {
112             throw new DatabaseException(e);
113         }
114     }
115
116     private static LevelItem toLevelItem(Bean bean) {
117         String id = (String) bean.getFieldUnchecked("id");
118         double interval = (Double) bean.getFieldUnchecked("interval");
119         return new LevelItem(id, interval);
120     }
121
122     public final static Comparator<LevelItem> ASCENDING_INTERVAL_ORDER =
123             (o1, o2) -> SamplingFormat.compareSamplingInterval(o1.samplingInterval, o2.samplingInterval);
124
125 }