//////////////////////////////////////////////////////////////////////////// // Rev: 2010.1 (P4JsApi 1.0 Example) // // NOTE: This is an example only, and should be modified to work in // a production environment! No warranty is expressed or implied. // Scripts should be tested thoroughly on a test server before // using in a production environment. // //////////////////////////////////////////////////////////////////////////// // Copyright (c) 2010, Perforce Software, Inc. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. //////////////////////////////////////////////////////////////////////////// // // An example of a P4JsApi P4 Admin alert. // // Fires an alert message to the main P4 Admin tab if drives are over a // given capacity (75% in this example). // // Note that this requires an trigger to run "df" to track hard drive space: // // df archive //depot/triggers/df df // // Create the "df" file in your workspace: // // touch /workspace/depot/triggers/df // // Add and submit the file to the depot as an archive trigger file type: // // p4 add -t +X /workspace/depot/triggers/df // p4 submit -d 'Added df trigger file' // // You can test this by running 'p4 print': // // p4 print //depot/triggers/df // // You should see the same output as if you had run the 'df' command on // the Perforce server. // //////////////////////////////////////////////////////////////////////////// // To keep track of the current alerts, set this value. Global variables are // shared between alerts in the alerts context, so you can also detect the // presence of other alerts, or determine if another alert is still running: var diskAlertID = diskAlertID || undefined; function processDriveCapacityAlert() { try { // Refresh time in seconds: var refreshTime = 300; // Percentage of capacity drives should not exceed (in percent): var thresholdValue = 75; // Location in the depot of df command: var dfLocation = "//depot/triggers/df"; // Fire off the alerts: displayAlerts(dfLocation, thresholdValue); P4JsApi.startAlertRefreshTimer(refreshTime); } catch(e) { alert(e); } } function displayAlerts(dfLocation, thresholdValue) { try { // Assume all the drives are fine: var overDrives = false; // Set the error message header: var alertText = "\nThe following drives are over " + thresholdValue + "% capacity:\n"; // run 'p4 print' to determine the current drive stats: var df = P4JsApi.p4('print -q ' + dfLocation); // Parse through the df output to get the values returned: var rows = df.text.split("\n"); var regex = /[ ]+/g; var values = []; // Here we look at each row to get the drive labels and capacity: for (var i = 0; i < rows.length; i++) { values.push(rows[i].split(regex)); var capacityStr = values[i][4]; if (capacityStr && capacityStr.indexOf('%') != -1) { var capacity = parseInt(capacityStr.substring(0, capacityStr.length - 1)); // Compare the reported capacity against the threshold: if (capacity > thresholdValue) { // We found drives over capacity: overDrives = true; // Add the drive label and current capacity percentage to // the alert string: alertText += "- " + values[i][5] + ": " + capacityStr + "\n"; } } } // If we found drives over capacity: if (overDrives) { // If there is already an alert: if (diskAlertID) { // Update the alert with the latest alert string: P4JsApi.updateAlert(diskAlertID, alertText); } else { // Otherwise, create a new alert with alert string: diskAlertID = P4JsApi.addAlert(alertText); } } else { // If all the drives are now fine, check to see if there's an // existing alert, and delete it if it's found: if (diskAlertID) { P4JsApi.deleteAlert(diskAlertID); diskAlertID = undefined; } } // We wrap everything in a try statement to catch unexpected errors: } catch(e) { alert(e); } } processDriveCapacityAlert();
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 7663 | dscheirer | Rollback //public/perforce/p4jsapi to changelist 7643 | ||
#3 | 7660 | jhalbig |
As per meeting to resolve issue with sync calls made from within async calls hanging P4V (job039138) pulling example code from Public Depot until it can be re-worked and confirmed to function correctly with pending P4JsApi changes. |
||
#2 | 7643 | jhalbig |
Cleaned up any remaining discrepancies in the code. Removed all internal references and debugging code. Cleaned up formatting, added more commenting for submit dialog code. Ready for 2010.1 Beta. |
||
#1 | 7638 | jhalbig | Initial Addition of P4JsApi samples for 2010.1 Beta |