|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ |
| 25 | +/*global define, $*/ |
| 26 | + |
| 27 | +/** |
| 28 | + * Utilities functions related to Health Data logging |
| 29 | + */ |
| 30 | +define(function (require, exports, module) { |
| 31 | + "use strict"; |
| 32 | + |
| 33 | + var PreferencesManager = require("preferences/PreferencesManager"), |
| 34 | + LanguageManager = require("language/LanguageManager"), |
| 35 | + FileUtils = require("file/FileUtils"), |
| 36 | + PerfUtils = require("utils/PerfUtils"), |
| 37 | + |
| 38 | + HEALTH_DATA_STATE_KEY = "HealthData.Logs", |
| 39 | + logHealthData = true; |
| 40 | + |
| 41 | + /** |
| 42 | + * Init: creates the health log preference keys in the state.json file |
| 43 | + */ |
| 44 | + function init() { |
| 45 | + PreferencesManager.stateManager.definePreference(HEALTH_DATA_STATE_KEY, "object", {}); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * All the logging functions should be disabled if this returns false |
| 50 | + * @returns {boolean} true if health data can be logged |
| 51 | + */ |
| 52 | + function shouldLogHealthData() { |
| 53 | + return logHealthData; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Return the Performance related data |
| 58 | + * @returns {Object} Performance Data aggregated till now |
| 59 | + */ |
| 60 | + function getPerformanceData() { |
| 61 | + return PerfUtils.getHealthReport(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Return all health data logged till now stored in the state prefs |
| 66 | + * @returns {Object} Health Data aggregated till now |
| 67 | + */ |
| 68 | + function getStoredHealthData() { |
| 69 | + return PreferencesManager.getViewState(HEALTH_DATA_STATE_KEY); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Return the aggregate of all health data logged till now from all sources |
| 74 | + * @returns {Object} Health Data aggregated till now |
| 75 | + */ |
| 76 | + function getAggregatedHealthData() { |
| 77 | + var healthData = getStoredHealthData(); |
| 78 | + $.extend(healthData, getPerformanceData()); |
| 79 | + return healthData; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sets the health data |
| 84 | + * @param {Object} dataObject The object to be stored as health data |
| 85 | + */ |
| 86 | + function setHealthData(dataObject) { |
| 87 | + if (!shouldLogHealthData()) { |
| 88 | + return; |
| 89 | + } |
| 90 | + PreferencesManager.setViewState(HEALTH_DATA_STATE_KEY, dataObject); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns health data logged for the given key |
| 95 | + * @returns {Object} Health Data object for the key or undefined if no health data stored |
| 96 | + */ |
| 97 | + function getHealthDataLog(key) { |
| 98 | + var healthData = getStoredHealthData(); |
| 99 | + return healthData[key]; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets the health data for the given key |
| 104 | + * @param {Object} dataObject The object to be stored as health data for the key |
| 105 | + */ |
| 106 | + function setHealthDataLog(key, dataObject) { |
| 107 | + var healthData = getStoredHealthData(); |
| 108 | + healthData[key] = dataObject; |
| 109 | + setHealthData(healthData); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Clears all the health data recorded till now |
| 114 | + */ |
| 115 | + function clearHealthData() { |
| 116 | + PreferencesManager.setViewState(HEALTH_DATA_STATE_KEY, {}); |
| 117 | + //clear the performance related health data also |
| 118 | + PerfUtils.clear(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Enable or disable health data logs |
| 123 | + * @param {boolean} enabled true to enable health logs |
| 124 | + */ |
| 125 | + function setHealthLogsEnabled(enabled) { |
| 126 | + logHealthData = enabled; |
| 127 | + if (!enabled) { |
| 128 | + clearHealthData(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Whenever a file is opened call this function. The function will record the number of times |
| 134 | + * the standard file types have been opened. We only log the standard filetypes |
| 135 | + * @param {String} filePath The path of the file to be registered |
| 136 | + * @param {boolean} addedToWorkingSet set to true if extensions of files added to the |
| 137 | + * working set needs to be logged |
| 138 | + */ |
| 139 | + function fileOpened(filePath, addedToWorkingSet) { |
| 140 | + if (!shouldLogHealthData()) { |
| 141 | + return; |
| 142 | + } |
| 143 | + var fileExtension = FileUtils.getFileExtension(filePath), |
| 144 | + language = LanguageManager.getLanguageForPath(filePath), |
| 145 | + healthData = getStoredHealthData(), |
| 146 | + fileExtCountMap = []; |
| 147 | + healthData.fileStats = healthData.fileStats || { |
| 148 | + openedFileExt : {}, |
| 149 | + workingSetFileExt : {} |
| 150 | + }; |
| 151 | + if (language.getId() !== "unknown") { |
| 152 | + fileExtCountMap = addedToWorkingSet ? healthData.fileStats.workingSetFileExt : healthData.fileStats.openedFileExt; |
| 153 | + if (!fileExtCountMap[fileExtension]) { |
| 154 | + fileExtCountMap[fileExtension] = 0; |
| 155 | + } |
| 156 | + fileExtCountMap[fileExtension]++; |
| 157 | + setHealthData(healthData); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Define public API |
| 162 | + exports.getHealthDataLog = getHealthDataLog; |
| 163 | + exports.setHealthDataLog = setHealthDataLog; |
| 164 | + exports.getAggregatedHealthData = getAggregatedHealthData; |
| 165 | + exports.clearHealthData = clearHealthData; |
| 166 | + exports.fileOpened = fileOpened; |
| 167 | + exports.setHealthLogsEnabled = setHealthLogsEnabled; |
| 168 | + exports.shouldLogHealthData = shouldLogHealthData; |
| 169 | + exports.init = init; |
| 170 | +}); |
0 commit comments