<commit_before># Namespace view
module 'NamespaceView', ->
    # Hardcoded!
    MAX_SHARD_COUNT = 32

    # All of our rendering code needs a view of available shards
    compute_renderable_shards_array = (namespace_uuid, shards) ->
        ret = []
        for i in [0...shards.length]
            primary_uuid = DataUtils.get_shard_primary_uuid namespace_uuid, shards[i]
            secondary_uuids = DataUtils.get_shard_secondary_uuids namespace_uuid, shards[i]
            ret.push
                name: human_readable_shard shards[i]
                shard: shards[i]
                shard_stats:
                    rows_approx: namespaces.get(namespace_uuid).compute_shard_rows_approximation(shards[i]) if typeof namespaces.get(namespace_uuid).compute_shard_rows_approximation(shards[i]) is 'string'
                notlast: i != shards.length - 1
                index: i
                primary:
                    uuid: primary_uuid
                    # primary_uuid may be null when a new shard hasn't hit the server yet
                    name: if primary_uuid then machines.get(primary_uuid).get('name') else null
                    status: if primary_uuid then DataUtils.get_machine_reachability(primary_uuid) else null
                    backfill_progress: if primary_uuid then DataUtils.get_backfill_progress(namespace_uuid, shards[i], primary_uuid) else null
                secondaries: _.map(secondary_uuids, (machine_uuids, datacenter_uuid) ->
                    datacenter_uuid: datacenter_uuid
                    datacenter_name: datacenters.get(datacenter_uuid).get('name')
                    machines: _.map(machine_uuids, (machine_uuid) ->
                        uuid: machine_uuid
                        name: machines.get(machine_uuid).get('name')
                        status: DataUtils.get_machine_reachability(machine_uuid)
                        backfill_progress: DataUtils.get_backfill_progress(namespace_uuid, shards[i], machine_uuid)
                        )
                    )
        return ret

    class @Sharding extends Backbone.View
        template: Handlebars.compile $('#modify_shards-template').html()
        alert_tmpl: Handlebars.compile $('#modify_shards-alert-template').html()
        invalid_splitpoint_msg: Handlebars.compile $('#namespace_view_invalid_splitpoint_alert-template').html()
        invalid_merge_msg: Handlebars.compile $('#namespace_view_invalid_merge_alert-template').html()
        invalid_shard_msg: Handlebars.compile $('#namespace_view_invalid_shard_alert-template').html()
        error_update_forbidden_msg: Handlebars.compile $('#namespace_view-sharding_alert-template').html()

        class: 'modify-shards'
        events:
            'click #suggest_shards_btn': 'suggest_shards'
            'click #cancel_shards_suggester_btn': 'cancel_shards_suggester_btn'
            'click .btn-compute-shards-suggestion': 'compute_shards_suggestion'
            'click .btn-reset': 'reset_shards'
            'click .btn-primary-commit': 'on_submit'

        initialize: (namespace_id, shard_set) =>
            log_initial '(initializing) view: ModifyShards'
            @namespace_id = @model.get('id')
            @namespace = namespaces.get(@namespace_id)
            shard_set = @namespace.get('shards')

            # Keep an unmodified copy of the shard boundaries with which we compare against when reviewing the changes.
            @original_shard_set = _.map(shard_set, _.identity)
            @shard_set = _.map(shard_set, _.identity)

            # Shard suggester business
            @suggest_shards_view = false

            # We should rerender on key distro updates
            @namespace.on 'change:shards', @refresh_all
            @shard_views = []

            # Forbid changes if issues
            @should_be_hidden = false
            @check_has_unsatisfiable_goals()
            issues.on 'all', @check_has_unsatisfiable_goals

            super

        refresh_all: =>
            shard_set = @namespace.get('shards')
            @original_shard_set = _.map(shard_set, _.identity)
            @shard_set = _.map(shard_set, _.identity)
            @render_only_shards()

        check_has_unsatisfiable_goals: =>
            if @should_be_hidden
                should_be_hidden_new = false
                for issue in issues.models 
                    if issue.get('type') is 'UNSATISFIABLE_GOALS' and issue.get('namespace_id') is @model.get('id') # If unsatisfiable goals, the user should not change shards
                        should_be_hidden_new = true
                        break
                    if issue.get('type') is 'MACHINE_DOWN' # If a machine connected (even with a role nothing) is down, the user should not change shards
                        if machines.get(issue.get('victim')).get('datacenter_uuid') of @model.get('replica_affinities')
                            should_be_hidden_new = true
                            break

                if not should_be_hidden_new
                    @should_be_hidden = false
                    @render()
            else
                for issue in issues.models
                    if issue.get('type') is 'UNSATISFIABLE_GOALS' and issue.get('namespace_id') is @model.get('id')
                        @should_be_hidden = true
                        @render()
                        break
                    if issue.get('type') is 'MACHINE_DOWN'
                        if machines.get(issue.get('victim')).get('datacenter_uuid') of @model.get('replica_affinities')
                            @should_be_hidden = true
                            @render()
                            break

        reset_shards: (e) ->
            e.preventDefault()
            @shard_set = _.map(@original_shard_set, _.identity)
            @render()

        reset_button_enable: ->
            @.$('.btn-reset').button('reset')

        reset_button_disable: ->
            @.$('.btn-reset').button('loading')

        suggest_shards: (e) =>
            e.preventDefault()
            @suggest_shards_view = true
            @render()<commit_msg>Disabled button commit on namespace/shard when nothing has been changed<commit_after># Namespace view
module 'NamespaceView', ->
    # Hardcoded!
    MAX_SHARD_COUNT = 32

    # All of our rendering code needs a view of available shards
    compute_renderable_shards_array = (namespace_uuid, shards) ->
        ret = []
        for i in [0...shards.length]
            primary_uuid = DataUtils.get_shard_primary_uuid namespace_uuid, shards[i]
            secondary_uuids = DataUtils.get_shard_secondary_uuids namespace_uuid, shards[i]
            ret.push
                name: human_readable_shard shards[i]
                shard: shards[i]
                shard_stats:
                    rows_approx: namespaces.get(namespace_uuid).compute_shard_rows_approximation(shards[i]) if typeof namespaces.get(namespace_uuid).compute_shard_rows_approximation(shards[i]) is 'string'
                notlast: i != shards.length - 1
                index: i
                primary:
                    uuid: primary_uuid
                    # primary_uuid may be null when a new shard hasn't hit the server yet
                    name: if primary_uuid then machines.get(primary_uuid).get('name') else null
                    status: if primary_uuid then DataUtils.get_machine_reachability(primary_uuid) else null
                    backfill_progress: if primary_uuid then DataUtils.get_backfill_progress(namespace_uuid, shards[i], primary_uuid) else null
                secondaries: _.map(secondary_uuids, (machine_uuids, datacenter_uuid) ->
                    datacenter_uuid: datacenter_uuid
                    datacenter_name: datacenters.get(datacenter_uuid).get('name')
                    machines: _.map(machine_uuids, (machine_uuid) ->
                        uuid: machine_uuid
                        name: machines.get(machine_uuid).get('name')
                        status: DataUtils.get_machine_reachability(machine_uuid)
                        backfill_progress: DataUtils.get_backfill_progress(namespace_uuid, shards[i], machine_uuid)
                        )
                    )
        return ret

    class @Sharding extends Backbone.View
        template: Handlebars.compile $('#modify_shards-template').html()
        alert_tmpl: Handlebars.compile $('#modify_shards-alert-template').html()
        invalid_splitpoint_msg: Handlebars.compile $('#namespace_view_invalid_splitpoint_alert-template').html()
        invalid_merge_msg: Handlebars.compile $('#namespace_view_invalid_merge_alert-template').html()
        invalid_shard_msg: Handlebars.compile $('#namespace_view_invalid_shard_alert-template').html()
        error_update_forbidden_msg: Handlebars.compile $('#namespace_view-sharding_alert-template').html()

        class: 'modify-shards'
        events:
            'click #suggest_shards_btn': 'suggest_shards'
            'click #cancel_shards_suggester_btn': 'cancel_shards_suggester_btn'
            'click .btn-compute-shards-suggestion': 'compute_shards_suggestion'
            'click .btn-reset': 'reset_shards'
            'click .btn-primary-commit': 'on_submit'

        initialize: (namespace_id, shard_set) =>
            log_initial '(initializing) view: ModifyShards'
            @namespace_id = @model.get('id')
            @namespace = namespaces.get(@namespace_id)
            shard_set = @namespace.get('shards')

            # Keep an unmodified copy of the shard boundaries with which we compare against when reviewing the changes.
            @original_shard_set = _.map(shard_set, _.identity)
            @shard_set = _.map(shard_set, _.identity)

            # Shard suggester business
            @suggest_shards_view = false

            # We should rerender on key distro updates
            @namespace.on 'change:shards', @refresh_all
            @shard_views = []

            # Forbid changes if issues
            @should_be_hidden = false
            @check_has_unsatisfiable_goals()
            issues.on 'all', @check_has_unsatisfiable_goals

            super

        refresh_all: =>
            shard_set = @namespace.get('shards')
            @original_shard_set = _.map(shard_set, _.identity)
            @shard_set = _.map(shard_set, _.identity)
            @render_only_shards()

        check_has_unsatisfiable_goals: =>
            if @should_be_hidden
                should_be_hidden_new = false
                for issue in issues.models 
                    if issue.get('type') is 'UNSATISFIABLE_GOALS' and issue.get('namespace_id') is @model.get('id') # If unsatisfiable goals, the user should not change shards
                        should_be_hidden_new = true
                        break
                    if issue.get('type') is 'MACHINE_DOWN' # If a machine connected (even with a role nothing) is down, the user should not change shards
                        if machines.get(issue.get('victim')).get('datacenter_uuid') of @model.get('replica_affinities')
                            should_be_hidden_new = true
                            break

                if not should_be_hidden_new
                    @should_be_hidden = false
                    @render()
            else
                for issue in issues.models
                    if issue.get('type') is 'UNSATISFIABLE_GOALS' and issue.get('namespace_id') is @model.get('id')
                        @should_be_hidden = true
                        @render()
                        break
                    if issue.get('type') is 'MACHINE_DOWN'
                        if machines.get(issue.get('victim')).get('datacenter_uuid') of @model.get('replica_affinities')
                            @should_be_hidden = true
                            @render()
                            break

        reset_shards: (e) ->
            e.preventDefault()
            @shard_set = _.map(@original_shard_set, _.identity)
            @render()

        reset_button_enable: ->
            @.$('.btn-reset').button 'reset'
            @.$('.btn-primary-commit').removeAttr 'disabled'

        reset_button_disable: ->
            @.$('.btn-reset').button('loading')
            @.$('.btn-primary-commit').attr 'disabled', 'disabled'

        suggest_shards: (e) =>
            e.preventDefault()
            @suggest_shards_view = true
            @render()
{'fraction_non_alphanumeric': 0.0904926464678936, 'fraction_numerical': 0.0006429317688660291, 'mean_word_length': 1.9706373836237765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1205087', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3308, 'n_words': 692}