<commit_before># Copyright 2016 Pinterest, Inc.
#
# 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.


from django.middleware.csrf import get_token
from django.shortcuts import render, redirect
from django.views.generic import View
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.contrib import messages
import json
import logging
import traceback

from helpers import environs_helper, clusters_helper
from helpers import groups_helper, baseimages_helper
from helpers import specs_helper, autoscaling_groups_helper
from helpers import autoscaling_metrics_helper
from diff_match_patch import diff_match_patch
from deploy_board import settings

log = logging.getLogger(__name__)


DEFAULT_PAGE_SIZE = 50

ScalingType = ["ChangeInCapacity", "ExactCapacity", "PercentChangeInCapacity"]


def group_landing(request):
    index = int(request.GET.get('page_index', '1'))
    size = int(request.GET.get('page_size', DEFAULT_PAGE_SIZE))
    group_names = autoscaling_groups_helper.get_env_group_names(request, index, size)
    return render(request, 'groups/group_landing.html', {
        'group_names': group_names,
        "pageIndex": index,
        "pageSize": DEFAULT_PAGE_SIZE,
        "disablePrevious": index <= 1,
        "disableNext": len(group_names) < DEFAULT_PAGE_SIZE,
    })


def get_system_specs(request):
    instance_types = specs_helper.get_instance_types(request)
    security_groups = specs_helper.get_security_groups(request)
    subnets = specs_helper.get_subnets(request)
    sorted_subnets = sorted(subnets, key=lambda subnet: subnet["info"]["tag"])
    sorted_sgs = sorted(security_groups, key=lambda sg: sg["info"]["name"])
    return instance_types, sorted_subnets, sorted_sgs


def get_launch_config(request, group_name):
    try:
        group_info = autoscaling_groups_helper.get_group_info(request, group_name)
        launch_config = group_info.get("launchInfo")

        if launch_config and launch_config.get("subnets"):
            launch_config["subnetArrays"] = launch_config["subnets"].split(',')
        appNames = baseimages_helper.get_image_names(request, 'AWS')
        appNames = sorted(appNames)
        curr_image = baseimages_helper.get_by_provider_name(request, launch_config["imageId"])
        html = render_to_string('groups/launch_config.tmpl', {
            "group_name": group_name,
            "app_names": appNames,
            "config": launch_config,
            "curr_image": curr_image,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def get_group_config_internal(group_config):
    if group_config:
        if group_config.get("launchLatencyTh"):
            group_config["launchLatencyTh"] = group_config.get("launchLatencyTh") / 60
        if group_config.get("healthcheckPeriod"):
            group_config["healthcheckPeriod"] = group_config.get("healthcheckPeriod") / 60
        if group_config.get("lifecycleTimeout"):
            group_config["lifecycleTimeout"] = group_config.get("lifecycleTimeout") / 60
        return group_config
    else:
        group_config = {}
        group_config["launchLatencyTh"] = 10
        group_config["healthcheckPeriod"] = 10
        group_config["lifecycleTimeout"] = 10
        return group_config


def get_group_config(request, group_name):
    try:
        group_info = autoscaling_groups_helper.get_group_info(request, group_name)
        group_config = group_info.get("groupInfo")
        group_config = get_group_config_internal(group_config)
        is_cmp = False
        envs = environs_helper.get_all_envs_by_group(request, group_name)
        for env in envs:
            basic_cluster_info = clusters_helper.get_cluster(request, env.get('clusterName'))
            if basic_cluster_info:
                is_cmp = True
        html = render_to_string('groups/group_config.tmpl', {
            "group_name": group_name,
            "config": group_config,
            "is_cmp": is_cmp,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def update_pas_config(request, group_name):
    try:
        params = request.POST
        if not params['metric'] or not params['throughput']:
            raise Exception("All fields for Predictive Autoscaling Config Must be specified. ")
        data = {}
        data['group_name'] = group_name
        data["metric"] = params["metric"]
        data["throughput"] = int(params["throughput"])
        if "pas_state" in params:
            data["pas_state"] = "ENABLED"
        else:
            data["pas_state"] = "DISABLED"
        autoscaling_groups_helper.update_pas_config(request, group_name, data)
        return get_pas_config(request, group_name)
    except:
        log.error(traceback.format_exc())
        raise


def get_pas_config(request, group_name):
    try:
        pas_config = autoscaling_groups_helper.get_pas_config(request, group_name)
        html = render_to_string('groups/pase_config.tmpl', {
            "group_name": group_name,
            "pas_config": pas_config,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def update_launch_config(request, group_name):
    try:
        params = request.POST
        launchRequest = {}
        launchRequest["instanceType"] = params["instanceType"]
        launchRequest["securityGroup"] = params["securityGroup"]
        launchRequest["imageId"] = params["imageId"]
        launchRequest["userData"] = params["userData"]
        launchRequest["iamRole"] = params["iamRole"]<commit_msg>PAS Disabled Updated Defined Min Size (#245)<commit_after># Copyright 2016 Pinterest, Inc.
#
# 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.


from django.middleware.csrf import get_token
from django.shortcuts import render, redirect
from django.views.generic import View
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.contrib import messages
import json
import logging
import traceback

from helpers import environs_helper, clusters_helper
from helpers import groups_helper, baseimages_helper
from helpers import specs_helper, autoscaling_groups_helper
from helpers import autoscaling_metrics_helper
from diff_match_patch import diff_match_patch
from deploy_board import settings

log = logging.getLogger(__name__)


DEFAULT_PAGE_SIZE = 50

ScalingType = ["ChangeInCapacity", "ExactCapacity", "PercentChangeInCapacity"]


def group_landing(request):
    index = int(request.GET.get('page_index', '1'))
    size = int(request.GET.get('page_size', DEFAULT_PAGE_SIZE))
    group_names = autoscaling_groups_helper.get_env_group_names(request, index, size)
    return render(request, 'groups/group_landing.html', {
        'group_names': group_names,
        "pageIndex": index,
        "pageSize": DEFAULT_PAGE_SIZE,
        "disablePrevious": index <= 1,
        "disableNext": len(group_names) < DEFAULT_PAGE_SIZE,
    })


def get_system_specs(request):
    instance_types = specs_helper.get_instance_types(request)
    security_groups = specs_helper.get_security_groups(request)
    subnets = specs_helper.get_subnets(request)
    sorted_subnets = sorted(subnets, key=lambda subnet: subnet["info"]["tag"])
    sorted_sgs = sorted(security_groups, key=lambda sg: sg["info"]["name"])
    return instance_types, sorted_subnets, sorted_sgs


def get_launch_config(request, group_name):
    try:
        group_info = autoscaling_groups_helper.get_group_info(request, group_name)
        launch_config = group_info.get("launchInfo")

        if launch_config and launch_config.get("subnets"):
            launch_config["subnetArrays"] = launch_config["subnets"].split(',')
        appNames = baseimages_helper.get_image_names(request, 'AWS')
        appNames = sorted(appNames)
        curr_image = baseimages_helper.get_by_provider_name(request, launch_config["imageId"])
        html = render_to_string('groups/launch_config.tmpl', {
            "group_name": group_name,
            "app_names": appNames,
            "config": launch_config,
            "curr_image": curr_image,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def get_group_config_internal(group_config):
    if group_config:
        if group_config.get("launchLatencyTh"):
            group_config["launchLatencyTh"] = group_config.get("launchLatencyTh") / 60
        if group_config.get("healthcheckPeriod"):
            group_config["healthcheckPeriod"] = group_config.get("healthcheckPeriod") / 60
        if group_config.get("lifecycleTimeout"):
            group_config["lifecycleTimeout"] = group_config.get("lifecycleTimeout") / 60
        return group_config
    else:
        group_config = {}
        group_config["launchLatencyTh"] = 10
        group_config["healthcheckPeriod"] = 10
        group_config["lifecycleTimeout"] = 10
        return group_config


def get_group_config(request, group_name):
    try:
        group_info = autoscaling_groups_helper.get_group_info(request, group_name)
        group_config = group_info.get("groupInfo")
        group_config = get_group_config_internal(group_config)
        is_cmp = False
        envs = environs_helper.get_all_envs_by_group(request, group_name)
        for env in envs:
            basic_cluster_info = clusters_helper.get_cluster(request, env.get('clusterName'))
            if basic_cluster_info:
                is_cmp = True
        html = render_to_string('groups/group_config.tmpl', {
            "group_name": group_name,
            "config": group_config,
            "is_cmp": is_cmp,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def update_pas_config(request, group_name):
    try:
        params = request.POST
        if not params['metric'] or not params['throughput']:
            raise Exception("All fields for Predictive Autoscaling Config Must be specified. ")
        data = {}
        data['group_name'] = group_name
        data["metric"] = params["metric"]
        data["throughput"] = int(params["throughput"])
        if "pas_state" in params:
            data["pas_state"] = "ENABLED"
        else:
            data["pas_state"] = "DISABLED"
            # Reset min size to user defined min size
            pas_config = autoscaling_groups_helper.get_pas_config(request, group_name)
            asg_request = {}
            asg_request['group_name'] = group_name
            asg_request['min_size'] = int(pas_config.get("defined_min_size"))
            autoscaling_groups_helper.update_autoscaling(request, asg_request)
        autoscaling_groups_helper.update_pas_config(request, group_name, data)
        return get_pas_config(request, group_name)
    except:
        log.error(traceback.format_exc())
        raise


def get_pas_config(request, group_name):
    try:
        pas_config = autoscaling_groups_helper.get_pas_config(request, group_name)
        html = render_to_string('groups/pase_config.tmpl', {
            "group_name": group_name,
            "pas_config": pas_config,
            "csrf_token": get_token(request),
        })
    except:
        log.error(traceback.format_exc())
        raise
    return HttpResponse(json.dumps(html), content_type="application/json")


def update_launch_config(request, group_name):
    try:
        params = request.POST
        launchRequest = {}
        launchRequest["instanceType"] = params["instanceType"]
        launchRequest["securityGroup"] = params["securityGroup"]
        launchRequest["imageId"] = params["imageId"]
        launchRequest["userData"] = params["userData"]
        launchRequest["iamRole"] = params["iamRole"]
{'fraction_non_alphanumeric': 0.09802735998790718, 'fraction_numerical': 0.0038545839316756103, 'mean_word_length': 3.5129604365620737, 'pattern_counts': {'":': 32, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '816719', 'n_tokens_mistral': 3887, 'n_tokens_neox': 3775, 'n_words': 819}