Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

Commit

Permalink
Fix diff errors
Browse files Browse the repository at this point in the history
Summary:
Fix the errors that were flagged in D6821787, except for a couple of
'this method is too complex' advisories, and the openbts-python directory, which
is a nightmare but we can probably deprecate anyway.

Reviewed By: jagill

Differential Revision: D6837337

fbshipit-source-id: 8e92c9813ad02040eeb8d4184f10ad201f835041
  • Loading branch information
Steve Muir authored and facebook-github-bot committed Feb 23, 2018
1 parent 25b38f7 commit 48d0bd7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 42 deletions.
9 changes: 5 additions & 4 deletions client/core/fake_phone/_osmocom.py
Expand Up @@ -8,21 +8,22 @@
of patent rights can be found in the PATENTS file in the same directory.
"""

from threading import Thread

from ESL import ESLconnection
from core.config_database import ConfigDB
from core.subscriber import subscriber

from .base import BaseFakePhone

SMPP_PORT = 2775
SMPP_USER = 'OSMPP'
SMPP_PASSWORD = 'etagecom'

class OsmocomFakePhone(BaseFakePhone): # noqa: F821 T25377293 Grandfathered in

class OsmocomFakePhone(BaseFakePhone):

def __init__(self, user, port, call_handler, sms_handler,
self_ip="127.0.0.1", other_ip="127.0.0.1"):
BaseFakePhone.__init__(self, user, port, call_handler, sms_handler, # noqa: F821 T25377293 Grandfathered in
BaseFakePhone.__init__(self, user, port, call_handler, sms_handler,
self_ip=self_ip, other_ip=other_ip)
self.user = user
self.conf = ConfigDB()
Expand Down
6 changes: 5 additions & 1 deletion cloud/ansible/files/monitors/etage_freeswitch_monitor.py
Expand Up @@ -25,6 +25,7 @@
"(\w+)")
SOFIA_PROFILE_STATUS_RE = re.compile("^([\w|/-]+)\s+(\d+)")


def check_sofia_status(dp):
res = subprocess.check_output('/usr/bin/fs_cli -x "sofia status"',
shell=True).split("\n")
Expand All @@ -33,7 +34,6 @@ def check_sofia_status(dp):
if (m):
name = m.group(1)
typee = m.group(2)
addr = m.group(3) # noqa: F841 T25377293 Grandfathered in
status = m.group(4)
dp.append({'entity': 'etagecom.cloud.freeswitch.%s.%s' %
(name, typee),
Expand All @@ -52,7 +52,10 @@ def check_sofia_status(dp):
'key': m.group(1),
'value': int(m.group(2))})


NUM_ACTIVE_CALLS_RE = re.compile("^(\d+) total.")


def count_active_calls(dp):
res = subprocess.check_output('/usr/bin/fs_cli -x "show calls"',
shell=True).split("\n")
Expand All @@ -65,6 +68,7 @@ def count_active_calls(dp):
'value': int(count)})
break


datapoints = []
check_sofia_status(datapoints)
count_active_calls(datapoints)
Expand Down
4 changes: 1 addition & 3 deletions cloud/endagaweb/stats_app/test_stats_app_views.py
Expand Up @@ -8,7 +8,7 @@
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
"""

Expand Down Expand Up @@ -37,8 +37,6 @@ def setUpClass(cls):
cls.user = models.User(username=cls.username, email=email)
cls.user.set_password(cls.password)
cls.user.save()
user_profile = models.UserProfile.objects.get(user=cls.user) # noqa: F841 T25377293 Grandfathered in
# TODO(matt): need to link UserProfile and Network?
# Setup a network.
network_name = 'test-network-name'
cls.network = models.Network(name=network_name)
Expand Down
3 changes: 0 additions & 3 deletions cloud/endagaweb/tests/test_subscriber_registration.py
Expand Up @@ -159,9 +159,6 @@ def test_register_with_preexisting_imsi_original_bts_post(self):
BTS returns the SAME number for that IMSI. Note, this is a change in
behavior from the GET endpoint.
"""
endpoint = self.endpoint # noqa: F841 T25377293 Grandfathered in
(
self.bts.uuid, self.available_number.number, self.subscriber.imsi)
token = self.user_profile.network.api_token
header = {
'HTTP_AUTHORIZATION': 'Token %s' % token
Expand Down
6 changes: 4 additions & 2 deletions cloud/endagaweb/views/stats.py
Expand Up @@ -3,7 +3,7 @@
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
"""

Expand All @@ -17,10 +17,11 @@
from endagaweb.models import Transaction
from endagaweb.models import UserProfile


@login_required(login_url='/login/')
def numbers(request):
user_profile = UserProfile.objects.get(user=request.user)
truncate_date = connection.ops.date_trunc_sql('month','created') # noqa: F841 T25377293 Grandfathered in
connection.ops.date_trunc_sql('month', 'created')

try:
numbers = Number.objects.get(user=user_profile).order_by("-created")
Expand All @@ -30,6 +31,7 @@ def numbers(request):
resp = [n.created for n in numbers]
return HttpResponse(json.dumps(resp), content_type="application/json")


@login_required(login_url='/login/')
def totals(request):
user_profile = UserProfile.objects.get(user=request.user)
Expand Down
2 changes: 1 addition & 1 deletion cloud/endagaweb/views/towers.py
Expand Up @@ -329,7 +329,7 @@ def _handle_request(self, request, uuid=None):
elif request.method == "GET":
page = request.GET.get('page', 1)
else:
return HttpResponseBadRequest() # noqa: F821 T25377293 Grandfathered in
return http.HttpResponseBadRequest()

try:
tower = models.BTS.objects.get(uuid=uuid,
Expand Down
Empty file added common/.python3
Empty file.
32 changes: 23 additions & 9 deletions common/ccm/common/crdt/base.py
Expand Up @@ -15,15 +15,16 @@
try:
import snowflake
NAME = snowflake.snowflake()
except: # noqa: B001 T25377293 Grandfathered in
except Exception:
pass

#snowflake sometimes fails silently, if so
#(or if not loaded) use UUID -kurtis
# snowflake sometimes fails silently, if so
# (or if not loaded) use UUID -kurtis
if not NAME:
import uuid
NAME = str(uuid.uuid4())


class StateCRDT(object):
"""
This class represents a state-based CRDT, or convergent replicated data
Expand Down Expand Up @@ -96,7 +97,7 @@ def value(self):

def is_used(self):
for s in self.state.keys():
if (self.state[s] != 0):
if (self.state[s] != 0):
return True
return False

Expand All @@ -108,17 +109,30 @@ def merge(cls, x, y, name=None):
For each key, in each, return the max value of the two.
"""
keys = set(set(x.state.keys()) | set(y.state.keys()))
z = dict([(k, max(x.state.get(k, 0), y.state.get(k, 0))) for k in keys])
z = {
k: max(x.state.get(k, 0), y.state.get(k, 0)) for k in keys
}
return GCounter.from_state(z, name=name)

@classmethod
def from_state(cls, state, name=None):
def max_int(k, a, b):
""" Get the max value of a key from two counters """
a_val = a.get(k, 0)
if not isinstance(a_val, int):
raise ValueError("expected int, got '%s'" % (a_val, ))
b_val = b.get(k, 0)
if not isinstance(b_val, int):
raise ValueError("expected int, got '%s'" % (b_val, ))
return max(a_val, b_val)

new = GCounter(name=name)
try:
keys = set(set(new.state.keys()) | set(state.keys()))
new.state = dict([(k, max(new.state.get(k, 0), state.get(k, 0))) for k in keys])
[int(_) for _ in new.state.values()] # make sure everything is an int
except: # noqa: B001 T25377293 Grandfathered in
new.state = {
k: max_int(k, new.state, state) for k in keys
}
except Exception:
raise ValueError("Invalid state for GCounter")
return new

Expand Down Expand Up @@ -168,7 +182,7 @@ def from_state(cls, state, name=None):
try:
new.P = GCounter.from_state(state['p'], name=new.name)
new.N = GCounter.from_state(state['n'], name=new.name)
except: # noqa: B001 T25377293 Grandfathered in
except Exception:
raise ValueError("Invalid state for PN counter")
return new

Expand Down
15 changes: 9 additions & 6 deletions common/ccm/common/crdt/tests/crdt_tests.py
Expand Up @@ -3,14 +3,15 @@
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
"""

from unittest import TestCase

from .. import base


class GCounterTestCase(TestCase):
def setUp(self):
self.g = base.GCounter("g1")
Expand All @@ -37,19 +38,21 @@ def test_from_state(self):
self.assertEqual(g.name, "gtest")

def test_from_invalid_state(self):
# note, this is a valid PNCounter state
# note, this is a valid PNCounter state (but an invalid GCounter state)
state = {'p': {'pn4': 4, 'pn5': 5}, 'n': {'pn6': 6, 'pn5': 5}}
with self.assertRaises(ValueError):
g = base.GCounter.from_state(state, name="test")
base.GCounter.from_state(state, name="test")
# this is not a valid state
state = 1
with self.assertRaises(ValueError):
g = base.GCounter.from_state(state, name="test") # noqa: F841 T25377293 Grandfathered in
base.GCounter.from_state(state, name="test")

def test_is_used(self):
self.assertFalse(self.g.is_used())
self.g.increment()
self.assertTrue(self.g.is_used())


class PNCounterTestCase(TestCase):
def setUp(self):
self.pn = base.PNCounter("pn1")
Expand Down Expand Up @@ -87,10 +90,10 @@ def test_from_state(self):
def test_from_invalid_state(self):
state = {'p': {'pn4': 4, 'pn5': 5}}
with self.assertRaises(ValueError):
pn = base.PNCounter.from_state(state)
base.PNCounter.from_state(state)
state = {'a': 1, 'b': 2}
with self.assertRaises(ValueError):
pn = base.PNCounter.from_state(state) # noqa: F841 T25377293 Grandfathered in
base.PNCounter.from_state(state)

def test_is_used(self):
self.assertFalse(self.pn.is_used())
Expand Down
23 changes: 15 additions & 8 deletions common/ccm/common/delta/dictdiff.py
Expand Up @@ -23,14 +23,18 @@

def diff(new, old):
"""
produces a diff (delta) between two dictionaries recursively, returns a tuple of elements to
remove & add from/into 'old' dictionary to get 'new' dictionary
Produces a diff (delta) between two dictionaries recursively, returns a
tuple of elements to remove & add from/into 'old' dictionary to get 'new'
dictionary.
Args:
new: new dictionary which should be a result of applaying delta to old dict
old: old dictionary to apply the returned delta on in order to create the new dict
new: new dictionary which should be a result of applaying delta to old
dict
old: old dictionary to apply the returned delta on in order to create
the new dict
Returns: delta to apply on old dictionary to make the new one ({ '+': ..., '-': ... })
Returns: delta to apply on old dictionary to make the new one
({ '+': ..., '-': ... })
Both, '+' (add) & '-' (remove) keys are optional
if old & new dictionaries are identical {} will be returned
Throws TypeError if new or old are not valid dictionaries
Expand Down Expand Up @@ -79,7 +83,9 @@ def apply_delta(old, delta):
"""
Args:
old: dictionary to apply delta on (old dictionary)
delta: the delta to apply, delta can be 1) empty: {} or 2) { '+': ..., '-': ... }
delta: the delta to apply, delta can be
1) empty: {} or
2) { '+': ..., '-': ... }
Returns:
modified dictionary, modifies old dictionary in place
Expand Down Expand Up @@ -135,8 +141,9 @@ def _diff_lists(new_list, old_list):
def _uni_hash(self):
try:
return json.dumps(self, skipkeys=True, sort_keys=True).__hash__()
except: # noqa: B001 T25377293 Grandfathered in
return "Invalid Object's Hash".__hash__() # all invalid object will share a bucket...
except Exception:
# all invalid objects will share a hash bucket
return "Invalid Object's Hash".__hash__()


def _make_hashable(o):
Expand Down
9 changes: 4 additions & 5 deletions common/ccm/common/delta/optimizer.py
Expand Up @@ -12,7 +12,6 @@
from __future__ import unicode_literals

import time
import types
import copy
from .protocol import DeltaProtocolCtx, DeltaProtocol

Expand Down Expand Up @@ -77,7 +76,7 @@ def process(self, dict_or_delta):

try:
return self.ctx.apply_delta(dict_or_delta)
except: # noqa: B001 T25377293 Grandfathered in
except Exception:
# apply_delta will throw if there is sig mismatch,
# we need to clear state
self.ctx.reset()
Expand Down Expand Up @@ -200,7 +199,7 @@ def _gc(self):
self._last_gc_time = tm
stale_time = tm - self._ttl
# first - delete all stale optimizers
for k, v in self._optimizers.iteritems(): # noqa: B301 T25377293 Grandfathered in
for k, v in self._optimizers.items():
if (not isinstance(v, DeltaProtocolOptimizer) or
v.last_used_ts < stale_time):
self._optimizers.pop(k)
Expand All @@ -210,10 +209,10 @@ def _gc(self):
# to be utilized, we'll have to tune it later...
if len(self._optimizers) > self._max_size:
oldest_sort = sorted(
self._optimizers.iteritems(), # noqa: B301 T25377293 Grandfathered in
self._optimizers.items(),
key=lambda k, v: v.last_used_ts
)
for k, v in oldest_sort[:len(self._optimizers) - self._max_size]: # noqa: B007 T25377293 Grandfathered in
for k, _ in oldest_sort[:len(self._optimizers) - self._max_size]:
self._optimizers.pop(k)

def clear(self): # delete all optimizers
Expand Down

0 comments on commit 48d0bd7

Please sign in to comment.