Skip to content
Snippets Groups Projects
update.js 3.34 KiB
var myApp = angular.module('inventoryApp', []);

myApp.controller('update', function($scope, $http, $timeout) {

    $scope.latch_pending = false;
    $scope.latch_error = false;
    $scope.latch_info = "module version ????";

    $scope.update_request_status = "";
    $scope.update_request_error = false;

    $scope.pending = [];
    $scope.failed = [];
    $scope.errors = [];
    $scope.warnings = [];

    $scope.show_warnings = true;
    $scope.show_errors = true;
    $scope.show_pending = true;

    $scope.check_status = function() {

        $http({
            method: 'GET',
            url: window.location.origin + "/version"
        }).then(
            /* ok response */
            function(rsp) {
                console.log('got version rsp: ' + JSON.stringify(rsp.data));
                $scope.latch_info = "module version " + rsp.data.module;
                $scope.latch_pending = rsp.data.latch.pending;
                $scope.latch_error = rsp.data.latch.failure;
                if (!$scope.latch_pending) {
                    $scope.update_request_status = "";
                }
                $timeout($scope.check_status, 5000);
            },
            /* error response */
            function(rsp) {
                console.log("got error response: " + JSON.stringify(rsp));
                $scope.latch_info = "communication error";
                $scope.latch_pending = false;
                $scope.latch_error = true;
                $timeout($scope.check_status, 5000);
            }
        );
    }

    $scope.refresh_update_status = function() {

        $http({
            method: 'GET',
            url: window.location.origin + "/jobs/log"
        }).then(
            /* ok response */
            function(rsp) {
                console.log('got update status rsp: ' + JSON.stringify(rsp.data).substring(0,30));
                $scope.pending = rsp.data.pending;
                $scope.failed = rsp.data.failed;
                $scope.warnings = rsp.data.warnings;
                $scope.errors = rsp.data.errors;
                $timeout($scope.refresh_update_status, 2000);
            },
            /* error response */
            function(rsp) {
                // assume this is 404 ...?
                $scope.pending = [];
                $scope.failed = [];
                $scope.errors = [];
                $scope.warnings = [];
                $timeout($scope.refresh_update_status, 5000);
            }
        );

    }

    $scope.launch_update = function() {
        $scope.update_request_status = "sending update request";
        $scope.update_request_error = false;
        $scope.latch_pending = true;
        $http({
            method: 'GET',
            url: window.location.origin + "/jobs/update"
        }).then(
            /* ok response */
            function(rsp) {
                $scope.update_request_status = "update is running";
                $scope.update_request_error = false;
                console.log('got update rsp: ' + JSON.stringify(rsp.data));
            },
            /* error response */
            function(rsp) {
                $scope.update_request_status = "update request failed";
                $scope.update_request_error = true;
                console.log("got error response: " + JSON.stringify(rsp));
            }
        );
    }

    $scope.check_status();
    $scope.refresh_update_status();

});