<commit_before>/*globals: require, console*/
require([
    "jquery", 
    "urlutils", 
    "dialogs",
    "analytics",
    "home/models", 
    "home/views/application_list_view",
    "home/views/application_view",
    "jsapi/v1/resources",
    "handlebars",
    "underscore"
], function(
    $, 
    urlutils, 
    dialogs, 
    analytics, 
    models, 
    application_list_view,
    application_view, 
    resources,
    hb,
    _) {
    "use strict";

    var ga = analytics.init();
    
    hb.registerHelper('icon_src', function(app_data) {
            var icon_data = app_data.image.icon_128;
            return (icon_data ? "data:image/png;base64,"+icon_data :
                urlutils.path_join(this.base_url, "static", "images", "generic_appicon_128.png"));
        }
    );
    hb.registerHelper('image_name', function(app_data) {
        return (app_data.image.ui_name ? app_data.image.ui_name : app_data.image.name);
    });

    // This model keeps the retrieved content from the REST query locally.
    // It is only synchronized at initial load.
    var model = new models.ApplicationListModel();
    var app_list_view = new application_list_view.ApplicationListView(model);
    var app_view = new application_view.ApplicationView(model);

    app_list_view.entry_clicked = function (index) {
        model.selected_index = index;
        app_list_view.update_entry(index);
        app_list_view.update_selected();
        app_view.render();
    };
    
    app_list_view.stop_button_clicked = function(index) {
        if (model.status[index] === models.Status.STOPPING) {
            return;
        }
        model.status[index] = models.Status.STOPPING;
        
        var app_info = model.app_data[index];
        var url_id = app_info.container.url_id;

        app_list_view.update_entry(index);
        
        resources.Container.delete(url_id)
            .done(function () {
                model.update_idx(index)
                    .always(function() {
                        app_list_view.update_entry(index);
                        app_view.render(true);
                    })
                    .fail(function(error) {
                        dialogs.webapi_error_dialog(error);
                    });
                })
            .fail(
                function (error) {
                    app_list_view.update_entry(index);
                    app_view.render(true);
                    dialogs.webapi_error_dialog(error);
                });
    };

    app_view.start_button_clicked = function (index) {
        // The container is not running. This is a start button.
        var mapping_id = model.app_data[index].mapping_id;
        var image_name = model.app_data[index].image.name;

        if (model.status[index] === models.Status.STARTING) {
            return;
        }
        
        model.status[index] = models.Status.STARTING;
        app_view.update();
        app_list_view.update_entry(index);

        var configurables_data = {};
        var configurables = model.configurables[index];
        configurables_data = {};

        Object.getOwnPropertyNames(configurables).forEach(
            function(val, idx, array) {  // jshint ignore:line
                var configurable = configurables[val];
                var tag = configurable.tag;
                configurables_data[tag] = configurable.as_config_dict();
            }
        );
       
        resources.Container.create({
            mapping_id: mapping_id,
            configurables: configurables_data
        }).done(function() {
            ga("send", "event", {
                eventCategory: "Application",
                eventAction: "start",
                eventLabel: image_name
            });

            model.update_idx(index)
                .always(function() {
                    app_list_view.update_entry(index);
                    app_list_view.update_selected();
                    app_view.render(true);
                })
                .fail(function(error) {
                    dialogs.webapi_error_dialog(error);
                });
        }).fail(function(error) {
            app_list_view.update_entry(index);
            app_list_view.update_selected();
            app_view.render(true);
            dialogs.webapi_error_dialog(error);
        });
    };

    $.when(model.update()).done(function () { 
        app_list_view.render(); 
        app_view.render();
    });

});<commit_msg>Fixed incorrect reset at error<commit_after>/*globals: require, console*/
require([
    "jquery", 
    "urlutils", 
    "dialogs",
    "analytics",
    "home/models", 
    "home/views/application_list_view",
    "home/views/application_view",
    "jsapi/v1/resources",
    "handlebars",
    "underscore"
], function(
    $, 
    urlutils, 
    dialogs, 
    analytics, 
    models, 
    application_list_view,
    application_view, 
    resources,
    hb,
    _) {
    "use strict";

    var ga = analytics.init();
    
    hb.registerHelper('icon_src', function(app_data) {
            var icon_data = app_data.image.icon_128;
            return (icon_data ? "data:image/png;base64,"+icon_data :
                urlutils.path_join(this.base_url, "static", "images", "generic_appicon_128.png"));
        }
    );
    hb.registerHelper('image_name', function(app_data) {
        return (app_data.image.ui_name ? app_data.image.ui_name : app_data.image.name);
    });

    // This model keeps the retrieved content from the REST query locally.
    // It is only synchronized at initial load.
    var model = new models.ApplicationListModel();
    var app_list_view = new application_list_view.ApplicationListView(model);
    var app_view = new application_view.ApplicationView(model);

    app_list_view.entry_clicked = function (index) {
        model.selected_index = index;
        app_list_view.update_entry(index);
        app_list_view.update_selected();
        app_view.render();
    };
    
    app_list_view.stop_button_clicked = function(index) {
        if (model.status[index] === models.Status.STOPPING) {
            return;
        }
        model.status[index] = models.Status.STOPPING;
        
        var app_info = model.app_data[index];
        var url_id = app_info.container.url_id;

        app_list_view.update_entry(index);
        
        resources.Container.delete(url_id)
            .done(function () {
                model.update_idx(index)
                    .done(function() {
                        app_list_view.update_entry(index);
                        app_view.render(true);
                    })
                    .fail(function(error) {
                        model.status[index] = models.Status.STOPPED;
                        app_list_view.update_entry(index);
                        app_view.render(true);
                        dialogs.webapi_error_dialog(error);
                    });
                })
            .fail(
                function (error) {
                    model.status[index] = models.Status.STOPPED;
                    app_list_view.update_entry(index);
                    app_view.render(true);
                    dialogs.webapi_error_dialog(error);
                });
    };

    app_view.start_button_clicked = function (index) {
        // The container is not running. This is a start button.
        var mapping_id = model.app_data[index].mapping_id;
        var image_name = model.app_data[index].image.name;

        if (model.status[index] === models.Status.STARTING) {
            return;
        }
        
        model.status[index] = models.Status.STARTING;
        app_view.update();
        app_list_view.update_entry(index);

        var configurables_data = {};
        var configurables = model.configurables[index];
        configurables_data = {};

        Object.getOwnPropertyNames(configurables).forEach(
            function(val, idx, array) {  // jshint ignore:line
                var configurable = configurables[val];
                var tag = configurable.tag;
                configurables_data[tag] = configurable.as_config_dict();
            }
        );
       
        resources.Container.create({
            mapping_id: mapping_id,
            configurables: configurables_data
        }).done(function() {
            ga("send", "event", {
                eventCategory: "Application",
                eventAction: "start",
                eventLabel: image_name
            });

            model.update_idx(index)
                .always(function() {
                    app_list_view.update_entry(index);
                    app_list_view.update_selected();
                    app_view.render(true);
                })
                .fail(function(error) {
                    model.status[index] = models.Status.STOPPED;
                    app_list_view.update_entry(index);
                    app_view.render(true);
                    dialogs.webapi_error_dialog(error);
                });
        }).fail(function(error) {
            model.status[index] = models.Status.STOPPED;
            app_list_view.update_entry(index);
            app_list_view.update_selected();
            app_view.render(true);
            dialogs.webapi_error_dialog(error);
        });
    };

    $.when(model.update()).done(function () { 
        app_list_view.render(); 
        app_view.render();
    });

});
{'fraction_non_alphanumeric': 0.10948982852273938, 'fraction_numerical': 0.001917137075300884, 'mean_word_length': 1.7805744743855494, 'pattern_counts': {'":': 0, '<': 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': '2410', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2588, 'n_words': 394}