1/*******************************************************************************
2 * Copyright (c) 2000, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.test.performance.ui;
12
13import java.io.PrintStream;
14import java.util.ArrayList;
15import java.util.Collections;
16import java.util.List;
17
18import org.eclipse.test.internal.performance.data.Dim;
19import org.eclipse.test.internal.performance.results.db.BuildResults;
20import org.eclipse.test.internal.performance.results.db.ConfigResults;
21import org.eclipse.test.internal.performance.results.db.DB_Results;
22import org.eclipse.test.internal.performance.results.utils.Util;
23
24/**
25 * Class used to fill details file of scenario builds data.
26 * @see ScenarioData
27 */
28public class RawDataTable {
29
30	private ConfigResults configResults;
31	private List buildPrefixes;
32	private PrintStream stream;
33	private Dim[] dimensions = DB_Results.getResultsDimensions();
34	private boolean debug = false;
35
36private RawDataTable(ConfigResults results, PrintStream ps) {
37	this.configResults = results;
38	this.stream = ps;
39}
40
41public RawDataTable(ConfigResults results, List prefixes, PrintStream ps) {
42	this(results, ps);
43	this.buildPrefixes = prefixes;
44}
45public RawDataTable(ConfigResults results, String baselinePrefix, PrintStream ps) {
46	this(results, ps);
47	this.buildPrefixes = new ArrayList();
48	this.buildPrefixes.add(baselinePrefix);
49}
50
51/**
52 * Print all build data to the current stream.
53 */
54public void print(){
55	this.stream.print("<table border=\"1\">");
56	printSummary();
57	printDetails();
58	this.stream.print("</table>\n");
59}
60
61/*
62 * Print table columns headers.
63 */
64private void printColumnHeaders() {
65	StringBuffer buffer = new StringBuffer();
66	int length = this.dimensions.length;
67	for (int i=0; i<length; i++) {
68		buffer.append("<td><b>");
69		buffer.append(this.dimensions[i].getName());
70		buffer.append("</b></td>");
71	}
72	this.stream.print(buffer.toString());
73}
74
75/*
76 * Print all build results in the table.
77 */
78private void printDetails() {
79	this.stream.print("<tr><td><b>Build ID</b></td>");
80	printColumnHeaders();
81	this.stream.print("</tr>\n");
82
83	List builds = this.configResults.getBuildsMatchingPrefixes(this.buildPrefixes);
84	Collections.reverse(builds);
85	int size = builds.size();
86	for (int i=0; i<size; i++) {
87		BuildResults buildResults = (BuildResults) builds.get(i);
88		this.stream.print("<tr><td>");
89		this.stream.print(buildResults.getName());
90		this.stream.print("</td>");
91		int dimLength = this.dimensions.length;
92		for (int d=0; d<dimLength; d++) {
93			Dim dimension = this.dimensions[d];
94			int dim_id = dimension.getId();
95			double value = buildResults.getValue(dim_id);
96			printDimTitle(dimension.getName());
97			String displayValue = dimension.getDisplayValue(value);
98			this.stream.print(displayValue);
99			if (this.debug) System.out.print("\t"+displayValue);
100			this.stream.print("</td>");
101		}
102		if (this.debug) System.out.println();
103		this.stream.print("</tr>\n");
104	}
105	if (this.debug) System.out.println("\n");
106}
107
108/*
109 * Print summary on top of the table.
110 */
111private void printSummary() {
112	this.stream.print("<tr><td><b>Stats</b></td>");
113	printColumnHeaders();
114	this.stream.print("</tr>\n");
115
116	int length = this.dimensions.length;
117	double[][] dimStats = new double[length][];
118	for (int i=0; i<length; i++) {
119		dimStats[i] = this.configResults.getStatistics(this.buildPrefixes, this.dimensions[i].getId());
120	}
121
122	this.stream.print("<tr><td>#BUILDS SAMPLED</td>");
123	for (int i=0; i<length; i++) {
124		String dimName = this.dimensions[i].getName();
125		printDimTitle(dimName);
126		this.stream.print((int)dimStats[i][0]);
127		this.stream.print("</td>");
128	}
129	this.stream.print("</tr>\n");
130	this.stream.print("<tr><td>MEAN</td>");
131	printRowDoubles(dimStats, 1);
132	this.stream.print("</tr>\n");
133	this.stream.print("<tr><td>STD DEV</td>");
134	printRowDoubles(dimStats, 2);
135	this.stream.print("</tr>\n");
136	this.stream.print("<tr><td>COEF. VAR</td>");
137	printRowDoubles(dimStats, 3);
138	this.stream.print("</tr>\n");
139
140	// Blank line
141	this.stream.print("<tr>");
142	for (int i=0; i<length+1;	i++){
143		this.stream.print("<td>&nbsp;</td>");
144	}
145	this.stream.print("</tr>\n");
146}
147
148/*
149 * Print values in table row.
150 */
151private void printRowDoubles(double[][] stats, int idx) {
152	int length = this.dimensions.length;
153	for (int i=0; i<length; i++) {
154		double value = stats[i][idx];
155		String dimName = this.dimensions[i].getName();
156		if (idx == 3) {
157			if (value > 0.1 && value < 0.2) {
158				this.stream.print("<td bgcolor=\"yellow\" title=\"");
159			} else if (value >= 0.2) {
160				this.stream.print("<td bgcolor=\"FF9900\" title=\"");
161			} else {
162				this.stream.print("<td title=\"");
163			}
164			this.stream.print(dimName);
165			this.stream.print("\">");
166			this.stream.print(Util.PERCENTAGE_FORMAT.format(value));
167			this.stream.print("</td>");
168		} else {
169			printDimTitle(dimName);
170			this.stream.print(this.dimensions[i].getDisplayValue(value));
171			this.stream.print("</td>");
172		}
173	}
174}
175
176/*
177 * Print dim title inside value reference.
178 * TODO (frederic) See if this title is really necessary
179 */
180private void printDimTitle(String dimName) {
181    this.stream.print("<td title=\"");
182    this.stream.print(dimName);
183    this.stream.print("\">");
184}
185}
186