<commit_before># -*- coding: utf-8 -*-

# Focus
# Copyright (C) 2010-2012 Grid Dynamics Consulting Services, Inc
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.


"""Show load history to global admins.

CPU/Load Avg./Mem/free space/io-wait history of every compute node.
"""

import contextlib
import datetime
import json
import re
import urllib
import urlparse

import flask

from flask import blueprints
from focus.models import orm
from focus.views import environments


bp = environments.admin(blueprints.Blueprint('load_history', __name__))


def baseurl():
    url = flask.current_app.config['ZABBIX_PROXY_BASEURL']
    if not url.endswith('/'):
        url += '/'
    return url


@bp.context_processor
def titles():
    return {
        'title': 'Load history',
        'subtitle': 'CPU/Load Avg./Mem/free space/io-wait history of every compute node'
        }

COMPUTE_ON = 0
COMPUTE_OFF = 1
COMPUTE_NOT_RESPONDING = 2


def total_seconds(td):
    return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6


def compute_statuses((host, updated_at, created_at, disabled)):
    AVAILABLE_DUE = 60 # seconds compute service is alive since last check
    # NOTE(apugachev) we'd survive doing it in this cycle
    NOW = datetime.datetime.now()
    if disabled:
        status = COMPUTE_OFF
    elif total_seconds(((updated_at or created_at) - NOW) > AVAILABLE_DUE:
        status = COMPUTE_NOT_RESPONDING
    else:
        status = COMPUTE_ON
    return host, status


def get_compute_nodes_with_statuses():
    """Return list of compute nodes with statuses.

    (('n005', 1), ('n007', 0), ('n008', 2))
    """
    db = orm.get_store('NOVA_RO')
    data = db.execute("SELECT host, updated_at, created_at, disabled FROM services WHERE deleted = 0 AND topic LIKE 'compute'").get_all()
    NOW = datetime.datetime.now()
    return map(compute_statuses, data)


def get_api_data(path):
    content = _proxy(path)
    return json.loads(content)
<commit_msg>fixed mistype<commit_after># -*- coding: utf-8 -*-

# Focus
# Copyright (C) 2010-2012 Grid Dynamics Consulting Services, Inc
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.


"""Show load history to global admins.

CPU/Load Avg./Mem/free space/io-wait history of every compute node.
"""

import contextlib
import datetime
import json
import re
import urllib
import urlparse

import flask

from flask import blueprints
from focus.models import orm
from focus.views import environments


bp = environments.admin(blueprints.Blueprint('load_history', __name__))


def baseurl():
    url = flask.current_app.config['ZABBIX_PROXY_BASEURL']
    if not url.endswith('/'):
        url += '/'
    return url


@bp.context_processor
def titles():
    return {
        'title': 'Load history',
        'subtitle': 'CPU/Load Avg./Mem/free space/io-wait history of every compute node'
        }

COMPUTE_ON = 0
COMPUTE_OFF = 1
COMPUTE_NOT_RESPONDING = 2


def total_seconds(td):
    return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6


def compute_statuses((host, updated_at, created_at, disabled)):
    AVAILABLE_DUE = 60 # seconds compute service is alive since last check
    # NOTE(apugachev) we'd survive doing it in this cycle
    NOW = datetime.datetime.now()
    if disabled:
        status = COMPUTE_OFF
    elif total_seconds((updated_at or created_at) - NOW) > AVAILABLE_DUE:
        status = COMPUTE_NOT_RESPONDING
    else:
        status = COMPUTE_ON
    return host, status


def get_compute_nodes_with_statuses():
    """Return list of compute nodes with statuses.

    (('n005', 1), ('n007', 0), ('n008', 2))
    """
    db = orm.get_store('NOVA_RO')
    data = db.execute("SELECT host, updated_at, created_at, disabled FROM services WHERE deleted = 0 AND topic LIKE 'compute'").get_all()
    NOW = datetime.datetime.now()
    return map(compute_statuses, data)


def get_api_data(path):
    content = _proxy(path)
    return json.loads(content)

{'fraction_non_alphanumeric': 0.09634169699291323, 'fraction_numerical': 0.015705803485922237, 'mean_word_length': 3.6625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3001107', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1554, 'n_words': 577}