Skip to content

Commit

Permalink
Merge pull request apache#1 from vitalybe/dashboard-config-state
Browse files Browse the repository at this point in the history
feature: json templates
  • Loading branch information
nir-sopher authored Feb 11, 2018
2 parents 269bfcd + 52fbfb6 commit 9b622b4
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

var FormEditDeliveryServiceController = function(deliveryService, type, types, $scope, $controller, $uibModal, $anchorScroll, locationUtils, deliveryServiceService) {
var FormEditDeliveryServiceController = function(deliveryService, type, types, $scope, $controller, $uibModal, $anchorScroll, locationUtils, deliveryServiceService, stringUtils) {

// extends the FormDeliveryServiceController to inherit common methods
angular.extend(this, $controller('FormDeliveryServiceController', { deliveryService: deliveryService, type: type, types: types, $scope: $scope }));
Expand All @@ -30,13 +30,20 @@ var FormEditDeliveryServiceController = function(deliveryService, type, types, $
};

$scope.deliveryServiceName = angular.copy(deliveryService.displayName);
var result = stringUtils.extractJsonFromRemapText(deliveryService.remapText);
$scope.remapTextReal = result[0];
$scope.remapTextJsonData = result[1];
$scope.hasAdditionalJson = !!($scope.remapTextJsonData && Object.keys($scope.remapTextJsonData).length > 0);

$scope.settings = {
isNew: false,
saveLabel: 'Update'
};


$scope.save = function(deliveryService) {
deliveryService.remapText = stringUtils.combineToRemapText($scope.remapTextReal, $scope.remapTextJsonData);

deliveryServiceService.updateDeliveryService(deliveryService).
then(function() {
$scope.deliveryServiceName = angular.copy(deliveryService.displayName);
Expand Down Expand Up @@ -68,5 +75,5 @@ var FormEditDeliveryServiceController = function(deliveryService, type, types, $

};

FormEditDeliveryServiceController.$inject = ['deliveryService', 'type', 'types', '$scope', '$controller', '$uibModal', '$anchorScroll', 'locationUtils', 'deliveryServiceService'];
FormEditDeliveryServiceController.$inject = ['deliveryService', 'type', 'types', '$scope', '$controller', '$uibModal', '$anchorScroll', 'locationUtils', 'deliveryServiceService', "stringUtils"];
module.exports = FormEditDeliveryServiceController;
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@
</div>
</div>

<div class="form-group" ng-class="{'has-error': hasError(deliveryServiceForm.geolimitRedirectUrl), 'has-feedback': hasError(deliveryServiceForm.geolimitRedirectUrl)}">
<div class="form-group" ng-class="{'has-error': hasError(deliveryServiceForm.geoLimitRedirectURL), 'has-feedback': hasError(deliveryServiceForm.geoLimitRedirectURL)}">
<label class="control-label col-md-2 col-sm-2 col-xs-12">Geo Limit Redirect URL</label>
<div class="col-md-10 col-sm-10 col-xs-12">
<input id="geolimitRedirectUrl" name="geolimitRedirectUrl" type="text" class="form-control" ng-model="deliveryService.geolimitRedirectUrl" ng-maxlength="255" ng-pattern="/^(https?:\/\/)/" autofocus>
<small class="input-error" ng-show="hasPropertyError(deliveryServiceForm.geolimitRedirectUrl, 'maxlength')">Too Long</small>
<small class="input-error" ng-show="hasPropertyError(deliveryServiceForm.geolimitRedirectUrl, 'pattern')">Must start with http:// or https://</small>
<span ng-show="hasError(deliveryServiceForm.geolimitRedirectUrl)" class="form-control-feedback"><i class="fa fa-times"></i></span>
<input id="geoLimitRedirectURL" name="geoLimitRedirectURL" type="text" class="form-control" ng-model="deliveryService.geoLimitRedirectURL" ng-maxlength="255" ng-pattern="/^(https?:\/\/)/" autofocus>
<small class="input-error" ng-show="hasPropertyError(deliveryServiceForm.geoLimitRedirectURL, 'maxlength')">Too Long</small>
<small class="input-error" ng-show="hasPropertyError(deliveryServiceForm.geoLimitRedirectURL, 'pattern')">Must start with http:// or https://</small>
<span ng-show="hasError(deliveryServiceForm.geoLimitRedirectURL)" class="form-control-feedback"><i class="fa fa-times"></i></span>
</div>
</div>

Expand Down Expand Up @@ -357,12 +357,18 @@
<div class="form-group" ng-class="{'has-error': hasError(deliveryServiceForm.remapText), 'has-feedback': hasError(deliveryServiceForm.remapText)}">
<label class="control-label col-md-2 col-sm-2 col-xs-12">Raw remap text</label>
<div class="col-md-10 col-sm-10 col-xs-12">
<input id="remapText" name="remapText" type="text" class="form-control" ng-model="deliveryService.remapText" ng-maxlength="2048" autofocus>
<input id="remapText" name="remapText" type="text" class="form-control" ng-model="remapTextReal" ng-maxlength="2048" autofocus>
<small class="input-error" ng-show="hasPropertyError(deliveryServiceForm.remapText, 'maxlength')">Too Long</small>
<span ng-show="hasError(deliveryServiceForm.remapText)" class="form-control-feedback"><i class="fa fa-times"></i></span>
</div>
</div>

<div class="form-group">
<label class="control-label col-md-2 col-sm-2 col-xs-12">Has additional JSON: </label>
<div class="col-md-10 col-sm-10 col-xs-12">
<input id="checkBox" type="checkbox" disabled ng-model="hasAdditionalJson">
</div>
</div>
<div class="form-group" ng-class="{'has-error': hasError(deliveryServiceForm.longDesc), 'has-feedback': hasError(deliveryServiceForm.longDesc)}">
<label class="control-label col-md-2 col-sm-2 col-xs-12">Long Description</label>
<div class="col-md-10 col-sm-10 col-xs-12">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
var TableDeliveryServiceServersController = function(deliveryService, deliveryServiceService, $scope, messageModel) {
var TableDeliveryServiceServersController = function(
deliveryService,
deliveryServiceService,
$scope,
messageModel,
stringUtils,
$http,
ENV
) {
var EASY_MODE = "easy";
var FULL_MODE = "full";

Expand All @@ -7,38 +15,43 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
{
from: "subitem[0].operation.subitemManipulation.minStepSizeForChunking",
to: "subitem.minStepSizeForChunking",
default: 1,
schema: { type: "integer", minimum: 1 },
default: null,
discardIf: isNull,
schema: { oneOf: [{ type: "integer", minimum: 1 }, { type: "null" }] },
},
{
from: "subitem[0].operation.subitemManipulation.maxSegmentSecViewTime",
to: "subitem.maxSegmentSecViewTime",
default: 7,
default: null,
discardIf: isNull,
schema: { oneOf: [{ type: "integer", minimum: 1 }, { type: "null" }] },
},
{
from: "subitem[0].operation.subitemManipulation.requiredChunkSecViewTime",
to: "subitem.requiredChunkSecViewTime",
default: 3600,
schema: { type: "integer", minimum: 1 },
default: null,
discardIf: isNull,
schema: { oneOf: [{ type: "integer", minimum: 1 }, { type: "null" }] },
},
{
from: "subitem[0].elements[0].priority",
to: "subitem.priority",
default: 1,
schema: { type: "integer", minimum: 1 },
default: null,
discardIf: isNull,
schema: { oneOf: [{ type: "integer", minimum: 1 }, { type: "null" }] },
},
{
from: "subitem[0].elements[0].match",
to: "subitem.match",
default: "kMatchOnlyOne",
schema: { type: "string", enum: ["kMatchAll", "kMatchOnlyOne"] },
default: null,
discardIf: isNull,
schema: { oneOf: [{ type: "string", enum: ["kMatchAll", "kMatchOnlyOne"] }, { type: "null" }] },
},
{
from: "subitem[0].elements[0].tokens",
to: "subitem.tokens",
default: [],
discardIf: isEmpty,
transformFullToPartialFunc: tokenTransformFullToPartialFunc,
transformPartialToFullFunc: tokenTransformPartialToFullFunc,
schema: {
Expand Down Expand Up @@ -267,34 +280,6 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
},
];

function extractJsonFromRemapText(deliveryService) {
var regex = /^#\s*?config=(.+$)/;
var data = {};

if (!deliveryService) {
throw new Error("expected a deliveryService, got: " + deliveryService);
}

var remapText = deliveryService.remapText;
try {
if (remapText) {
var match = remapText.match(regex);
if (match) {
data = JSON.parse(match[1]);
}
}
} catch (e) {
_friendlyExceptionNotification(
e,
"Failed to find a valid JSON object in 'Raw remap text', assuming no existing JSON object. Found value: \"" +
remapText +
'"'
);
}

return data;
}

function createSchemaFromMappings(mappings) {
const schema = {
title: "Validation schema",
Expand Down Expand Up @@ -322,9 +307,13 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
}

function tokenTransformFullToPartialFunc(tokens) {
return tokens.filter(function(token) {
return "pathSegment" in token;
});
if (tokens) {
return tokens.filter(function(token) {
return "pathSegment" in token;
});
} else {
return [];
}
}

function tokenTransformPartialToFullFunc(currentTokens, newTokens) {
Expand Down Expand Up @@ -419,7 +408,9 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
}

function _friendlyExceptionNotification(e, customMessage) {
console.error("Message: " + (customMessage || "None"));
if (customMessage) {
console.error("Message: " + customMessage);
}
console.error(e);

var friendlyMessage =
Expand All @@ -431,19 +422,6 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
messageModel.setMessages([{ level: "error", text: friendlyMessage }], false);
}

var schema = createSchemaFromMappings(mappings);

try {
$scope.deliveryService = deliveryService;
$scope.originalFullData = extractJsonFromRemapText(deliveryService);
$scope.fullData = angular.copy($scope.originalFullData);
$scope.jsonEdtiorConfig = getJsonEditorEasyDataConfig($scope.fullData, mappings, schema);
$scope.selectedMode = EASY_MODE;
$scope.isUpdateInProgress = false;
} catch (e) {
_friendlyExceptionNotification(e);
}

$scope.onModeChange = function() {
try {
if ($scope.selectedMode === FULL_MODE) {
Expand All @@ -458,7 +436,7 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
$scope.onUpdate = function() {
try {
updateFullDataFromJsonEditor();
deliveryService.remapText = "# config=" + JSON.stringify($scope.fullData);
deliveryService.remapText = stringUtils.combineToRemapText($scope.originalBeforeData, $scope.fullData);

$scope.isUpdateInProgress = true;
deliveryServiceService
Expand All @@ -478,13 +456,24 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe

$scope.onRevert = function() {
try {
$scope.selectedMode = FULL_MODE;
$scope.fullData = $scope.originalFullData;
updateJsonEditorFromFullData();
} catch (e) {
_friendlyExceptionNotification(e);
}
};

$scope.updateFromTemplate = function(template) {
try {
$scope.selectedMode = FULL_MODE;
$scope.fullData = template;
updateJsonEditorFromFullData();
} catch (e) {
_friendlyExceptionNotification(e);
}
}

$scope.$watch(
"jsonEdtiorConfig.json",
function(newValue, oldValue) {
Expand All @@ -496,7 +485,46 @@ var TableDeliveryServiceServersController = function(deliveryService, deliverySe
},
true
);

var schema = createSchemaFromMappings(mappings);

try {
$scope.deliveryService = deliveryService;

var result = stringUtils.extractJsonFromRemapText(deliveryService.remapText);
$scope.originalBeforeData = result[0];
$scope.originalFullData = result[1];

$scope.fullData = angular.copy($scope.originalFullData);
$scope.jsonEdtiorConfig = getJsonEditorEasyDataConfig($scope.fullData, mappings, schema);
$scope.selectedMode = EASY_MODE;
$scope.isUpdateInProgress = false;
} catch (e) {
_friendlyExceptionNotification(e);
}

$scope.templates = []
try {
$http
.get(ENV.api['root'] + 'msp-templates/dump')
.then(function(result) {
$scope.templates = result.data.response.templates;
})
.catch(function(e) {
_friendlyExceptionNotification("Failed to load templates");
});
} catch (e) {
_friendlyExceptionNotification("Failed to load templates");
}
};

TableDeliveryServiceServersController.$inject = ["deliveryService", "deliveryServiceService", "$scope", "messageModel"];
TableDeliveryServiceServersController.$inject = [
"deliveryService",
"deliveryServiceService",
"$scope",
"messageModel",
"stringUtils",
"$http",
"ENV"
];
module.exports = TableDeliveryServiceServersController;
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
display: flex;

div.editing-container {
flex: 1;
width: 50%;
padding-right: 5px;

div.editing-header {
Expand All @@ -51,7 +51,7 @@
}

div.preview-container {
flex: 1;
width: 50%;
padding-left: 5px;

.preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
</ol>
<div class="pull-right">
<button type="button" class="btn btn-danger" ng-click="onRevert()" ng-disabled="isUpdateInProgress">Revert</button>
<div class="btn-group" uib-dropdown is-open="status.isopen">
<button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle ng-disabled="disabled">
Revert to template&nbsp<span class="caret"></span>
</button>
<ul ng-if="templates.length" class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
<li role="menuitem" ng-repeat="template in templates"><a ng-click="updateFromTemplate(template)">{{template.siteName}}</a></li>
</ul>
</div>
<button type="button" class="btn btn-primary" ng-click="onUpdate()" ng-disabled="isUpdateInProgress">Update</button>
</div>
<div class="clearfix"></div>
Expand Down
33 changes: 30 additions & 3 deletions traffic_portal/app/src/common/service/utils/StringUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@
*/

var FormUtils = function() {
this.labelize = function(string) {
return string.replace(/([A-Z])/g, " $1").replace(/^./, function(str) {
return str.toUpperCase();
});
};

this.labelize = function(string) {
return string.replace(/([A-Z])/g, ' $1').replace(/^./, function(str){ return str.toUpperCase(); });
};
this.extractJsonFromRemapText = function(remapText) {
var regex = /^(.*?)#\s*?config=(.+$)$/;
var original = "";
var data = {};

try {
if (remapText) {
var match = remapText.match(regex);
if (match) {
original = match[1];
data = JSON.parse(match[2]);
} else {
original = remapText;
}
}
} catch (e) {
original = remapText;
console.error("exception during remap parsing:", e);
}

return [original, data];
};

this.combineToRemapText = function(remapTextReal, jsonData) {
return remapTextReal + "# config=" + JSON.stringify(jsonData)
}
};

FormUtils.$inject = [];
Expand Down

0 comments on commit 9b622b4

Please sign in to comment.