<commit_before>define([
    "jquery",
    "kubernetes/angular",
    "kubernetes/client"
], function($, angular, kubernetes) {
    'use strict';

    function KubernetesService(client) {
        var self = this;

        var version;
        var service = { };
        var calculated = { };

        /* compute current and expected number of containers and return these
           values as a formatted string.
        */
        function calculate() {
            var x = 0;
            var y = 0;
            if (version !== client.resourceVersion) {
                //client.metadata.name != "kubernetes") {
                version = client.resourceVersion;

                calculated.containers = "0";

                if (service.spec && service.spec.selector) {
                    var pods = client.select(service.spec.selector, service.metadata.namespace);

                    /* calculate "x of y" containers, where x is the current
                       number and y is the expected number. If x==y then only
                       show x. The calculation is based on the statuses of the
                       containers within the pod.  Pod states: Pending,
                       Running, Succeeded, Failed, and Unknown. 
                    */
                    angular.forEach(pods, function(pod) {
			if (!pod.status || !pod.status.phase)
                           return;
                        switch (pod.status.phase) {
                        case "Failed":
                            y++;
                            break;
                        case "Pending":
                            y++;
                            break;
                        case "Running":
                            x++; y++;
                            break;
                        case "Succeeded": // don't increment either counter
                            break;
                        case "Unknown":
                            y++;
                            break;
                        default: // assume Failed
                            y++;
                        }
                    });
                    calculated.containers = "" + x;
                    if (x != y)
                        calculated.containers += " of " + y;
                }
            }
            return calculated;
        }

        Object.defineProperty(self, "containers", {
            get: function get() { return calculate().containers; }
        });

        self.apply = function apply(item) {
            var spec = item.spec || { };
            var meta = item.metadata || { };
            self.name = meta.name;
            self.address = spec.portalIP + ":" + spec.port;
            self.namespace = meta.namespace;
            service = item;
        };
    }

    function KubernetesNode(client) {
        var self = this;

        var version;
        var calculated = { };
        var node = { };

        function calculate() {
            var x = 0;
            var y = 0;
            if (version !== client.resourceVersion) {
                version = client.resourceVersion;

                calculated.containers = "0";

                /* TODO: Calculate number of containers */
                if (node.metadata && node.metadata.name) {
                    var pods = client.hosting(node.metadata.name);
                    calculated.containers = "" + pods.length;
                }
            }
            return calculated;
        }

        Object.defineProperty(self, "containers", {
            get: function get() { return calculate().containers; }
        });

        self.apply = function apply(item) {
            var status = item.status || { };
            var meta = item.metadata || { };
            self.name = meta.name;
            self.address = status.hostIP;
            node = item;
        };
    }

    function builder(type, client, Constructor) {
        var objects = { };

        function build() {
            var seen = { };
            angular.forEach(objects, function(value, key) {
                seen[key] = true;
            });
            angular.forEach(client[type], function(item) {
                if (item.metadata.name == "kubernetes" ||
                    item.metadata.name == "kubernetes-ro")
                    return; // skip special pods created for k8 internal usage
                var key = item.metadata ? item.metadata.uid : null;
                if (!key)
                    return;
                delete seen[key];
                var obj = objects[key];
                if (obj === undefined)
                    objects[key] = obj = new Constructor(client, item);
                obj.apply(item);
            });
            angular.forEach(seen, function(value, key) {
                delete objects[key];
            });
            $(objects).triggerHandler("changed");
        }
        $(client).on(type, build);
        build();

        return objects;
    }

    return angular.module('kubernetes', [
            'ngRoute',
            'kubernetes.dashboard'
        ])
        .config(['$routeProvider', function($routeProvider) {
            $routeProvider.otherwise({ redirectTo: '/' });
        }])
        .factory('kubernetesClient', function() {
            return kubernetes.k8client();
        })
        .factory('kubernetesServices', ['kubernetesClient', function(client) {
            return builder('services', client, KubernetesService);
        }])
        .factory('kubernetesNodes', ['kubernetesClient', function(client) {
            return builder('nodes', client, KubernetesNode);
        }]);
});
<commit_msg>kubernetes: Cleanup the calculation of service.containers<commit_after>define([
    "jquery",
    "kubernetes/angular",
    "kubernetes/client"
], function($, angular, kubernetes) {
    'use strict';

    function KubernetesService(client) {
        var self = this;

        var version;
        var service = { };
        var calculated = { };

        /* compute current and expected number of containers and return these
           values as a formatted string.
        */
        function calculate() {
            if (version === client.resourceVersion)
                return calculated;

            var spec = service.spec || { };

            version = client.resourceVersion;

            /* Calculate number of containers */

            var x = 0;
            var y = 0;
            calculated.containers = "0";

            /*
             * Calculate "x of y" containers, where x is the current
             * number and y is the expected number. If x==y then only
             * show x. The calculation is based on the statuses of the
             * containers within the pod.  Pod states: Pending,
             * Running, Succeeded, Failed, and Unknown.
             */
            client.select(service.spec.selector || { },
                          service.metadata.namespace, "Pod").forEach(function(pod) {
                if (!pod.status || !pod.status.phase)
                    return;
                switch (pod.status.phase) {
                case "Failed":
                    y++;
                    break;
                case "Pending":
                    y++;
                    break;
                case "Running":
                    x++; y++;
                    break;
                case "Succeeded": // don't increment either counter
                    break;
                case "Unknown":
                    y++;
                    break;
                default: // assume Failed
                    y++;
                }
            });

            calculated.containers = "" + x;
            if (x != y)
                calculated.containers += " of " + y;

            return calculated;
        }

        Object.defineProperty(self, "containers", {
            get: function get() { return calculate().containers; }
        });

        self.apply = function apply(item) {
            var spec = item.spec || { };
            var meta = item.metadata || { };
            self.name = meta.name;
            self.address = spec.portalIP + ":" + spec.port;
            self.namespace = meta.namespace;
            service = item;
        };
    }

    function KubernetesNode(client) {
        var self = this;

        var version;
        var calculated = { };
        var node = { };

        function calculate() {
            var x = 0;
            var y = 0;
            if (version !== client.resourceVersion) {
                version = client.resourceVersion;

                calculated.containers = "0";

                /* TODO: Calculate number of containers */
                if (node.metadata && node.metadata.name) {
                    var pods = client.hosting(node.metadata.name);
                    calculated.containers = "" + pods.length;
                }
            }
            return calculated;
        }

        Object.defineProperty(self, "containers", {
            get: function get() { return calculate().containers; }
        });

        self.apply = function apply(item) {
            var status = item.status || { };
            var meta = item.metadata || { };
            self.name = meta.name;
            self.address = status.hostIP;
            node = item;
        };
    }

    function builder(type, client, Constructor) {
        var objects = { };

        function build() {
            var seen = { };
            angular.forEach(objects, function(value, key) {
                seen[key] = true;
            });
            angular.forEach(client[type], function(item) {
                if (item.metadata.name == "kubernetes" ||
                    item.metadata.name == "kubernetes-ro")
                    return; // skip special pods created for k8 internal usage
                var key = item.metadata ? item.metadata.uid : null;
                if (!key)
                    return;
                delete seen[key];
                var obj = objects[key];
                if (obj === undefined)
                    objects[key] = obj = new Constructor(client, item);
                obj.apply(item);
            });
            angular.forEach(seen, function(value, key) {
                delete objects[key];
            });
            $(objects).triggerHandler("changed");
        }
        $(client).on(type, build);
        build();

        return objects;
    }

    return angular.module('kubernetes', [
            'ngRoute',
            'kubernetes.dashboard'
        ])
        .config(['$routeProvider', function($routeProvider) {
            $routeProvider.otherwise({ redirectTo: '/' });
        }])
        .factory('kubernetesClient', function() {
            return kubernetes.k8client();
        })
        .factory('kubernetesServices', ['kubernetesClient', function(client) {
            return builder('services', client, KubernetesService);
        }])
        .factory('kubernetesNodes', ['kubernetesClient', function(client) {
            return builder('nodes', client, KubernetesNode);
        }]);
});

{'fraction_non_alphanumeric': 0.11107104219257122, 'fraction_numerical': 0.00144248106743599, 'mean_word_length': 1.2542166226376752, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2810314', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2516, 'n_words': 660}