Skip to content
Snippets Groups Projects
bootstrap.js 6.19 KiB
Newer Older
  • Learn to ignore specific revisions
  • "use strict"
    
    // Copyright (c) Alex Ellis 2017. All rights reserved.
    // Licensed under the MIT license. See LICENSE file in the project root for full license information.
    
    
    var app = angular.module('faasGateway', ['ngMaterial']);
    
    Alex Ellis's avatar
    Alex Ellis committed
    
    
    app.controller("home", ['$scope', '$log', '$http', '$location', '$timeout', '$mdDialog', '$mdToast',
            function($scope, $log, $http, $location, $timeout, $mdDialog, $mdToast) {
    
        $scope.functions = [];
    
    Alex Ellis's avatar
    Alex Ellis committed
        $scope.invocationRequest = "";
        $scope.invocationResponse = "";
        $scope.invocationStatus = "";
        $scope.invocation = {
    
    Alex Ellis's avatar
    Alex Ellis committed
            contentType: "text"
    
    Alex Ellis's avatar
    Alex Ellis committed
        };
    
    Alex's avatar
    Alex committed
        $scope.functionTemplate = {
    
            image: "",
            envProcess: "",
            network: "",
            service: ""
    
    Alex's avatar
    Alex committed
        };
    
    Alex Ellis's avatar
    Alex Ellis committed
        $scope.invocation.request = ""
    
    Alex Ellis's avatar
    Alex Ellis committed
        setInterval(function() {
    
    Alex Ellis's avatar
    Alex Ellis committed
            refreshData();
    
    Alex Ellis's avatar
    Alex Ellis committed
        }, 1000);
    
    
        $scope.showPostInvokedToast = function(val) {
            $mdToast.show(
                $mdToast.simple()
                    .textContent(val)
                    .position("top right")
                    .hideDelay(500)
            );
        };
    
    
    Alex Ellis's avatar
    Alex Ellis committed
        $scope.fireRequest = function() {
    
    Alex Ellis's avatar
    Alex Ellis committed
    
            var options = {
                url: "/function/" + $scope.selectedFunction.name,
                data: $scope.invocation.request,
                method: "POST",
                headers: { "Content-Type": $scope.invocation.contentType == "json" ? "application/json" : "text/plain" },
                responseType: $scope.invocation.contentType
            };
    
            $scope.invocationResponse = "";
            $scope.invocationStatus = null;
    
    Alex Ellis's avatar
    Alex Ellis committed
    
            $http(options)
                .then(function(response) {
    
                    if($scope.invocation && $scope.invocation.contentType == "json") {
                        $scope.invocationResponse = JSON.stringify(response.data, -1, "  ");
    
                    } else {
    
                        $scope.invocationResponse = response.data;    
    
    Alex Ellis's avatar
    Alex Ellis committed
                    $scope.invocationStatus = response.status;
    
                    $scope.showPostInvokedToast("Success");
    
    Alex Ellis's avatar
    Alex Ellis committed
                }).catch(function(error1) {
    
                    $scope.invocationResponse = error1.statusText + "\n" + error1.data;
                    $scope.invocationStatus = error1.status;
    
    
                    $scope.showPostInvokedToast("Error");
    
    Alex Ellis's avatar
    Alex Ellis committed
                });
    
    Alex Ellis's avatar
    Alex Ellis committed
        var refreshData = function() {
    
    Alex Ellis's avatar
    Alex Ellis committed
            var previous = $scope.functions;
    
            var cl = function(previousItems) {
                $http.get("/system/functions").then(function(response) {
    
    Alex Ellis's avatar
    Alex Ellis committed
                    if (response && response.data) {
                        if (previousItems.length != response.data.length) {
    
    Alex Ellis's avatar
    Alex Ellis committed
                            $scope.functions = response.data;
                        } else {
    
    Alex Ellis's avatar
    Alex Ellis committed
                            for (var i = 0; i < $scope.functions.length; i++) {
                                for (var j = 0; j < response.data.length; j++) {
                                    if ($scope.functions[i].name == response.data[j].name) {
                                        $scope.functions[i].replicas = response.data[j].replicas;
                                        $scope.functions[i].invocationCount = response.data[j].invocationCount;
    
    Alex Ellis's avatar
    Alex Ellis committed
        var fetch = function() {
            $http.get("/system/functions").then(function(response) {
                $scope.functions = response.data;
            });
        };
    
    
        $scope.showFunction = function(fn) {
    
    Alex Ellis's avatar
    Alex Ellis committed
            if ($scope.selectedFunction != fn) {
    
    Alex Ellis's avatar
    Alex Ellis committed
                $scope.selectedFunction = fn;
                $scope.invocation.request = "";
                $scope.invocationResponse = "";
                $scope.invocationStatus = "";
    
                $scope.invocation.contentType = "text";
    
    Alex Ellis's avatar
    Alex Ellis committed
            }
    
    Alex's avatar
    Alex committed
        var showDialog=function($event) {
           var parentEl = angular.element(document.body);
           $mdDialog.show({
             parent: parentEl,
             targetEvent: $event,
             templateUrl: "newfunction.html",
             locals: {
               item: $scope.functionTemplate
             },
             controller: DialogController
          });
        };
    
        var DialogController = function($scope, $mdDialog, item) {
            $scope.item = item;
            $scope.closeDialog = function() {
                $mdDialog.hide();
            };
    
            $scope.createFunc = function() {
    
    Alex's avatar
    Alex committed
                var options = {
                    url: "/system/functions",
                    data: $scope.item,
                    method: "POST",
                    headers: { "Content-Type": "application/json"},
                    responseType: "json"
                };
    
                $http(options)
                    .then(function(response) {
                        $scope.invocationResponse = response.data;
                        $scope.invocationStatus = response.status;
                    }).catch(function(error1) {
                        $scope.invocationResponse = error1;
                        $scope.invocationStatus = null;
                    });
    
    Alex's avatar
    Alex committed
                console.log($scope.item);
                $scope.closeDialog();
            };
    
        $scope.newFunction = function() {
    
    Alex's avatar
    Alex committed
            showDialog();
    
        $scope.deleteFunction = function($event) {
            var confirm = $mdDialog.confirm()
                .title('Delete Function')
                .textContent('Are you sure you want to delete ' + $scope.selectedFunction.name + '?')
                .ariaLabel('Delete function')
                .targetEvent($event)
                .ok('OK')
                .cancel('Cancel');
    
            $mdDialog.show(confirm)
                .then(function() {
                    var options = {
                        url: "/system/functions",
                        data: {
                            functionName: $scope.selectedFunction.name
                        },
                        method: "DELETE",
                        headers: { "Content-Type": "application/json"},
                        responseType: "json"
                    };
    
                    return $http(options);
                }).then(function(){
                    $scope.showPostInvokedToast("Success");
                }).catch(function(err) {
                    if (err) {
                        // show error toast only if there actually is an err.
                        // because hitting 'Cancel' also rejects the promise.
                      $scope.showPostInvokedToast("Error");
                    }
                });
        };
    
    
    Alex Ellis's avatar
    Alex Ellis committed
        fetch();
    
    Alex's avatar
    Alex committed
    }]);