001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web.client.admin;
017
018import com.google.gwt.user.client.rpc.AsyncCallback;
019import com.smartgwt.client.util.SC;
020import com.smartgwt.client.widgets.form.DynamicForm;
021import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
022import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
023import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
024import com.smartgwt.client.widgets.layout.HLayout;
025import com.smartgwt.client.widgets.layout.VLayout;
026import java.util.Collection;
027import org.modeshape.web.client.chart.Chart;
028import org.modeshape.web.shared.Stats;
029
030/**
031 *
032 * @author kulikov
033 */
034public class Metrics extends VLayout {
035
036    private final Chart chart1 = new Chart("");
037    private final Chart chart2 = new Chart("");
038    
039    private final DynamicForm f1 = new DynamicForm();
040    private final DynamicForm f2 = new DynamicForm();
041    
042    private final ComboBoxItem cb1 = new ComboBoxItem("Select parameter");
043    private final ComboBoxItem cb2 = new ComboBoxItem("Select parameter");
044    
045    private final ComboBoxItem tw1 = new ComboBoxItem("Time window");
046    private final ComboBoxItem tw2 = new ComboBoxItem("Time window");
047
048    private final AdminView adminView;
049    
050    public Metrics(AdminView adminView) {
051        this.adminView = adminView;
052        
053        setHeight(500);
054        setWidth100();
055
056        VLayout strut = new VLayout();
057        strut.setHeight(30);
058
059        addMember(strut);
060
061        loadValueMetrics();
062        loadDurationMetrics();
063        loadTimeUnits();
064
065        cb1.addChangedHandler(new ChangedHandler() {
066            @Override
067            public void onChanged(ChangedEvent event) {
068                drawChart1();
069            }
070        });
071
072        tw1.addChangedHandler(new ChangedHandler() {
073            @Override
074            public void onChanged(ChangedEvent event) {
075                drawChart1();
076            }
077        });
078
079        cb2.addChangedHandler(new ChangedHandler() {
080            @Override
081            public void onChanged(ChangedEvent event) {
082                drawChart2();
083            }
084        });
085
086        tw2.addChangedHandler(new ChangedHandler() {
087            @Override
088            public void onChanged(ChangedEvent event) {
089                drawChart2();
090            }
091        });
092
093        f1.setItems(cb1, tw1);
094        f2.setItems(cb2, tw2);
095
096        HLayout layout = new HLayout();
097
098        layout.setWidth100();
099        layout.setHeight100();
100
101        VLayout p1 = new VLayout();
102        VLayout p2 = new VLayout();
103
104        addMember(layout);
105
106        layout.addMember(p1);
107        layout.addMember(p2);
108
109        chart1.setHeight100();
110        chart2.setHeight100();
111
112        chart1.setWidth100();
113        chart2.setWidth100();
114
115        p1.addMember(f1);
116        p1.addMember(chart1);
117
118        p2.addMember(f2);
119        p2.addMember(chart2);
120
121    }
122
123    private void loadValueMetrics() {
124        adminView.jcrService().getValueMetrics(new AsyncCallback<String[]>() {
125            @Override
126            public void onFailure(Throwable caught) {
127                SC.say(caught.getMessage());
128            }
129
130            @Override
131            public void onSuccess(String[] result) {
132                cb1.setValueMap(result);
133            }
134        });
135    }
136
137    private void loadDurationMetrics() {
138        adminView.jcrService().getDurationMetrics(new AsyncCallback<String[]>() {
139            @Override
140            public void onFailure(Throwable caught) {
141                SC.say(caught.getMessage());
142            }
143
144            @Override
145            public void onSuccess(String[] result) {
146                cb2.setValueMap(result);
147            }
148        });
149    }
150
151    private void loadTimeUnits() {
152        adminView.jcrService().getTimeUnits(new AsyncCallback<String[]>() {
153            @Override
154            public void onFailure(Throwable caught) {
155                SC.say(caught.getMessage());
156            }
157
158            @Override
159            public void onSuccess(String[] result) {
160                tw1.setValueMap(result);
161                tw2.setValueMap(result);
162
163                tw1.setValue(result[0]);
164                tw2.setValue(result[0]);
165            }
166        });
167    }
168
169    private void drawChart1() {
170        drawValueHistory(chart1, cb1.getValueAsString(), tw1.getValueAsString());
171    }
172
173    private void drawChart2() {
174        drawDurationHistory(chart2, cb2.getValueAsString(), tw2.getValueAsString());
175    }
176
177    private void drawValueHistory(final Chart chart, String param, String time) {
178        adminView.jcrService().getValueStats(adminView.repository(), param, time, new AsyncCallback<Collection<Stats>>() {
179            @Override
180            public void onFailure(Throwable caught) {
181                SC.say(caught.getMessage());
182            }
183
184            @Override
185            public void onSuccess(Collection<Stats> stats) {
186                drawHistory(chart, stats);
187            }
188        });
189    }
190
191    private void drawDurationHistory(final Chart chart, String param, String time) {
192        adminView.jcrService().getDurationStats(adminView.repository(), param, time, new AsyncCallback<Collection<Stats>>() {
193            @Override
194            public void onFailure(Throwable caught) {
195                SC.say(caught.getMessage());
196            }
197
198            @Override
199            public void onSuccess(Collection<Stats> stats) {
200                drawHistory(chart, stats);
201            }
202        });
203    }
204
205    private void drawHistory(Chart chart, Collection<Stats> stats) {
206        double[] x = new double[stats.size()];
207        double[] y1 = new double[stats.size()];
208        double[] y2 = new double[stats.size()];
209        double[] y3 = new double[stats.size()];
210
211        int c = stats.size() > 24 ? 24 : stats.size();
212        int dx = stats.size() / c;
213
214        String[] xl = new String[c];
215
216        int i = 0;
217        for (Stats s : stats) {
218            x[i] = i;
219            y1[i] = s.min();
220            y2[i] = s.max();
221            y3[i] = s.avg();
222            i++;
223        }
224
225        for (int j = 0; j < xl.length; j++) {
226            xl[j] = Integer.toString(j * dx);
227        }
228
229        chart.drawChart(xl, x, y1, y2, y3);
230    }
231}
232