summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/solver/utils/utils.py
blob: c995eecea39026c388edb077285e89006519d10b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2017 AT&T Intellectual Property
#
#   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.
#
# -------------------------------------------------------------------------
#

import math
from oslo_log import log


LOG = log.getLogger(__name__)


OPERATIONS = {'gte': lambda x, y: x >= y,
              'lte': lambda x, y: x <= y,
              'gt': lambda x, y: x > y,
              'lt': lambda x, y: x < y,
              'eq': lambda x, y: x == y
              }


def compute_air_distance(_src, _dst):
    """Compute Air Distance

    based on latitude and longitude
    input: a pair of (lat, lon)s
    output: air distance as km
    """
    distance = 0.0
    latency_score = 0.0

    if _src == _dst:
        return distance

    radius = 6371.0  # km


    dlat = math.radians(_dst[0] - _src[0])
    dlon = math.radians(_dst[1] - _src[1])
    a = math.sin(dlat / 2.0) * math.sin(dlat / 2.0) + \
        math.cos(math.radians(_src[0])) * \
        math.cos(math.radians(_dst[0])) * \
        math.sin(dlon / 2.0) * math.sin(dlon / 2.0)
    c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a))
    distance = radius * c

    return distance


def compute_latency_score(_src,_dst, _region_group):
    """Compute the Network latency score between src and dst"""
    earth_half_circumference = 20000
    region_group_weight = _region_group.get(_dst[2])

    if region_group_weight == 0 or region_group_weight is None :
        LOG.debug("Computing the latency score based on distance between : ")
        latency_score = compute_air_distance(_src,_dst)
    elif _region_group > 0 :
        LOG.debug("Computing the latency score ")
        latency_score = compute_air_distance(_src, _dst) + region_group_weight * earth_half_circumference
    LOG.debug("Finished Computing the latency score: "+str(latency_score))
    return latency_score


def convert_km_to_miles(_km):
    return _km * 0.621371


def convert_miles_to_km(_miles):
    return _miles / 0.621371