etc/cli-scripts/measurement_utils.js | 134 +++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+)
New commits: commit bcf70dce12b481f4f0876a9d87eb3f7781df9497 Merge: ce3f841... 256cc0e... Author: John Sanda jsanda@redhat.com Date: Wed Sep 8 13:38:17 2010 -0400
Merge branch 'master' of ssh://git.fedorahosted.org/git/rhq/rhq
commit ce3f841a30ea1f4d56055c42e252acad575043bb Author: John Sanda jsanda@redhat.com Date: Wed Sep 8 13:36:49 2010 -0400
Initial commit for measurement_utils.js
Defines the class MeausrementModule which provides some properties and methods for working with measurement schedules.
diff --git a/etc/cli-scripts/measurement_utils.js b/etc/cli-scripts/measurement_utils.js new file mode 100644 index 0000000..261aa8e --- /dev/null +++ b/etc/cli-scripts/measurement_utils.js @@ -0,0 +1,134 @@ +testUpdates = { + context: 'Resource', + id: 10033, + schedules: { + 'Late Collections': interval(15, minutes), + 'Failed Collections per Minute': interval(15, minutes), + 'Currently Schedule Measurements': 'enabled' + } +} + +testGroupUpdates = { + context: 'Group', + id: 10031, + schedules: { + 'Used Swap Space': 'disabled', + 'Total Memory': interval(3, minutes) + } +} + +/** + * MeasurementModule is a class that provides properties and methods for working with + * measurements. + */ +function MeasurementModule() { + + /** + * Performs the actual updates, calling MeasurementScheduleManager. + * + * @param updates + * @param criteria + * @param enableSchedules + * @param disableSchedules + * @param updateSchedules + */ + function doScheduleUpdates(updates, criteria, enableSchedules, disableSchedules, updateSchedules) { + criteria.fetchDefinition(true); + var schedules = MeasurementScheduleManager.findSchedulesByCriteria(criteria); + + foreach(schedules, function (schedule) { + var measurementName = schedule.definition.displayName; + + if (updates.schedules[measurementName]) { + switch (updates.schedules[measurementName]) { + case 'enabled': + MeasurementScheduleManager[enableSchedules](updates.id, [schedule.definition.id]); + break; + case 'disabled': + MeasurementScheduleManager[disableSchedules](updates.id, + [schedule.definition.id]); + break; + default: + var interval = updates.schedules[measurementName]; + MeasurementScheduleManager[updateSchedules](updates.id, + [schedule.definition.id], interval); + } + } + }); + + } + + /** + * Intended for use with the interval method. + */ + this.time = { + seconds: 1000, + minutes: 60 * seconds, + hours: 60 * minutes + } + + /** + * A helper method that calculates a shedule's interval in milliseconds + * @param num + * @param time + */ + this.interval = function (num, time) { + return num * time; + } + + /** + * Updates the metric schedule as specified in the updates object. The object is expected + * to contain three properties or keys. The first of these required keys is <context>. + * Accepted values are the strings 'Resource' or 'Group'. + * + * The next required key is <id>, and its values is expected to be an integer. The + * interpretation of its values is dependent on the value of <context>. When the value of + * <context> is 'Resource', then <id> is treated as a resource id. If the value of + * <context> is 'Group', then <id> is treated as a compatible group id. + * + * The third required key is <schedules>, and it a nested object that specifies the + * schedules to be updated. The keys of <schedules> are the measurement display names. + * Expected values are 'enabled', 'disabled', or an integer which specifies the + * collection interval in milliseconds. + * + * Here is an example to illustrate what the updates object should look like: + * + * resourceSchedulesUpdates = { + * context: 'Resource', + * id: 123, + * schedules: { + * 'Measurement A': 'enabled', + * 'Measurement B': 'disabled, + * 'Measurement C': interval(20, time.minutes) + * } + * } + * + * @param updates The updates to perform + */ + this.updateSchedules = function (updates) { + if (!updates.id) { + throw '<id> is a required property'; + } + + if (!updates.schedules) { + throw '<schedules> is a required property'; + } + + if (updates.context == 'Resource') { + var criteria = MeasurementScheduleCriteria(); + criteria.addFilterResourceId(updates.id); + doScheduleUpdates(updates, criteria, 'enableSchedulesForResource', + 'disableSchedulesForResource', 'updateSchedulesForResource'); + } + else if (updates.context == 'Group') { + var criteria = MeasurementScheduleCriteria(); + criteria.addFilterResourceGroupId(updates.id); + doScheduleUpdates(updates, criteria, 'enableSchedulesForCompatibleGroup', + 'disableSchedulesForCompatibleGroup', 'updateSchedulesForCompatibleGroup'); + } + else { + throw "Unrecognized value for context: " + updates.context + " - expected either " + + "<Resource> or <Group>"; + } + } +}