overviewCtrl.js 3.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
module.exports = function ($scope, $location, $interval, $q, Application) {
    var getInfo = function (app) {
        return app.getInfo()
            .success(function (response) {
                app.version = response.version;
                delete response.version;
                app.info = response;
            })
            .error(function () {
                app.version = '---';
            });
    };
    var getHealth = function (app) {
        return app.getHealth()
            .success(function (response) {
                app.status = response.status;
            })
            .error(function (response, httpStatus) {
                if (httpStatus === 503) {
                    app.status = response.status;
                } else if (httpStatus === 404 || httpStatus === 0) {
                    app.status = 'OFFLINE';
                } else {
                    app.status = 'UNKNOWN';
                }
            });
    };
    var getLogfile = function (app) {
        return app.hasLogfile()
            .success(function () {
                app.providesLogfile = true;
            })
            .error(function () {
                app.providesLogfile = false;
            });
    };

55
    $scope.loadData = function () {
56
        Application.query(function (applications) {
57 58 59 60 61 62 63 64 65 66 67 68 69
            function refresh(app) {
                //find application in known applications and copy state --> less flickering
                for (var j = 0; $scope.applications != null && j < $scope.applications
                    .length; j++) {
                    if (app.id === $scope.applications[j].id) {
                        app.info = $scope.applications[j].info;
                        app.version = $scope.applications[j].version;
                        app.status = $scope.applications[j].status;
                        app.providesLogfile = $scope.applications[j].providesLogfile;
                        break;
                    }
                }
                app.refreshing = true;
70
                $q.all(getInfo(app), getHealth(app), getLogfile(app))
71 72 73 74 75 76 77 78 79 80 81 82 83
                    .finally(function () {
                        app.refreshing = false;
                    });
            }

            for (var i = 0; i < applications.length; i++) {
                refresh(applications[i]);
            }
            $scope.applications = applications;
        });
    };

    $scope.remove = function (application) {
84
        application.$remove(function () {
85 86 87 88 89 90 91
            var index = $scope.applications.indexOf(application);
            if (index > -1) {
                $scope.applications.splice(index, 1);
            }
        });
    };

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    $scope.order = {
        column: 'name',
        descending: false
    };

    $scope.orderBy = function (column) {
        if (column === $scope.order.column) {
            $scope.order.descending = !$scope.order.descending;
        } else {
            $scope.order.column = column;
            $scope.order.descending = false;
        }
    };

    $scope.orderByCssClass = function (column) {
        if (column === $scope.order.column) {
            return 'sorted-' + ($scope.order.descending ? 'descending' : 'ascending');
        } else {
            return '';
        }
    };
113 114 115 116 117 118 119 120

    //initial load
    $scope.loadData();

    // reload site every 30 seconds
    $interval(function () {
        $scope.loadData();
    }, 30000);
121
};