All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scripts.sonar-utils.js Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

AJS.$.namespace("AJS.sonar.utils");

/**
 * Translate the measure trend data to a trend image
 * 
 * @param measure the measure containing the trend data
 * @param isSmall flag for small (true) or big (false) images
 * @return the trend image name
 */
AJS.sonar.utils.getTrendImage = function(measure, isSmall) {
	if (measure["trend"] === undefined || measure["var"] === undefined) {
		return "";
	} else if (measure["trend"] === 0 && measure["var"] === 0) {
		return "";
	} else {
		var imageName = "/tendency/" + measure["var"] + "-";
		if (measure["trend"] === 1) {
			imageName = imageName + "green";
		} else {
			imageName = imageName + "black";
		}
		if (isSmall) {
			imageName = imageName + "-small";
		}
		return imageName + ".png";
	}
}

/**
 * Get a Measure from the resource JSON object by the Measure key
 * 
 * @param resource the Resource JSON object
 * @param msrKey the key of the Measure to get
 * @return the Measure of the resource
 */
AJS.sonar.utils.getMeasureFromResource = function(resource, msrKey) {
	for (var index in resource.msr) {
		if (resource.msr[index].key === msrKey) {
			return resource.msr[index];
		}
	}
	return {};
}

/**
 * Get a metric by key from an array of metrics
 * 
 * @param metrics the array of metrics
 * @param metricKey the key of the metric to get
 * @return the metric
 */
AJS.sonar.utils.getMetricFromMetricsArray = function(metrics, metricKey) {
	for (var index in metrics) {
		if (metrics[index].key === metricKey) {
			return metrics[index];
		}
	}
	return {};
}

/**
 * Validate a Metric to see if its a valid one
 * 
 * @param metric the Metric to validate
 * @return true if valid, false otherwise
 */
AJS.sonar.utils.isValidMetric = function(metric) {
	return !(metric != null && metric !== undefined && metric.key !== undefined);
}

/**
 * Validate a Measure to see if its a valid one
 * 
 * @param measure the Measure to validate
 * @return true if valid, false otherwise
 */
AJS.sonar.utils.isValidMeasure = function(measure) {
	return !(measure != null && measure !== undefined && measure.key !== undefined);
}

/**
 * Get all the Metrics used for the Color select box with the treemap
 * 
 * @param metrics Array of all the metrics available
 * @return Array of all color metrics
 */
AJS.sonar.utils.getColorMetrics = function(metrics) {
	var colorMetrics = new Array();
	for(var index in metrics) {
		if (metrics[index].val_type.toUpperCase() === "LEVEL" || metrics[index].val_type.toUpperCase() === "PERCENT") {
			colorMetrics.push(metrics[index]);
		}
	}
	colorMetrics.sort(function(a, b) {
		return (a.name>b.name) - (b.name>a.name);
	});
	return colorMetrics;
}

/**
 * Get all the Metrics used for the Size select box with the treemap
 * 
 * @param metrics Array of all the metrics available
 * @return Array of all size metrics
 */
AJS.sonar.utils.getSizeMetrics = function(metrics) {
	var sizeMetrics = new Array();
	for(var index in metrics) {
		if ((metrics[index].val_type.toUpperCase() === "INT" || metrics[index].val_type.toUpperCase() === "FLOAT"
				|| metrics[index].val_type.toUpperCase() === "MILLISEC")
				&& metrics[index].val_type.toUpperCase() !== "PERCENT" && metrics[index].domain != undefined
				&& metrics[index].domain != null && metrics[index].domain !== "") {
			sizeMetrics.push(metrics[index]);
		}
	}
	sizeMetrics.sort(function(a, b) {
		return (a.name>b.name) - (b.name>a.name);
	});
	return sizeMetrics;
}

/**
 * Get all the Metrics used for the Time Machine
 * 
 * @param metrics Array of all the metrics available
 * @return Array of all time machine metrics
 */
AJS.sonar.utils.getTimeMachineMetrics = function(metrics) {
	var timeMachineMetrics = new Array();
	for(var index in metrics) {
		if ((metrics[index].val_type.toUpperCase() === "INT" || metrics[index].val_type.toUpperCase() === "FLOAT"
				|| metrics[index].val_type.toUpperCase() === "PERCENT"
				|| metrics[index].val_type.toUpperCase() === "MILLISEC"
				|| metrics[index].val_type.toUpperCase() === "RATING")) {
			timeMachineMetrics.push(metrics[index]);
		}
	}
	return timeMachineMetrics;
}

/**
 * Create a Select element for Metrics
 * 
 * @param name the name of the select element
 * @param id the id of the select element
 * @param options the Metric options
 * @param selected the selected metric key
 * @param onChange function to bind to the onChange event of the select element
 * @return the select element
 */
AJS.sonar.utils.createMetricSelectElement = function(name, id, options, selected, onChange) {
	var select = AJS.$("