Verified Commit 695e1115 authored by Štěpán Henek's avatar Štěpán Henek 🐻
Browse files

get_data action added

parent 1b5b2c01
......@@ -17,12 +17,15 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
import json
import logging
from foris_controller.app import app_info
from foris_controller_backends.uci import (
UciBackend, UciTypeException, UciRecordNotFound, get_option_named,
parse_bool, store_bool
)
from foris_controller.utils import RWLock
logger = logging.getLogger(__name__)
......@@ -62,3 +65,32 @@ class NetmetrUci():
backend.replace_list("netmetr", "settings", "hours_to_run", hours_to_run)
return True
class NetmetrDataFile(object):
DATA_FILE_PATH = "/tmp/netmetr-history.json"
data_file_lock = RWLock(app_info["lock_backend"])
def read_records(self):
with self.data_file_lock.readlock:
try:
with open(NetmetrDataFile.DATA_FILE_PATH) as f:
data = json.load(f)
except IOError:
return "missing", []
except ValueError:
return "error", [] # invalid json
if len(data["error"]) > 0:
return "error", [] # error indicator was set
res = []
for record in data["history"]:
res.append({
"speed_download": float(record["speed_download"]),
"speed_upload": float(record["speed_upload"]),
"ping": float(record["ping"]),
"time": int(record["time"]),
"test_uuid": record["test_uuid"],
})
return "ready", res
......@@ -37,10 +37,16 @@ class NetmetrModule(BaseModule):
self.notify("update_settings", data)
return {"result": res}
def action_get_data(self, data):
res = {}
res.update(self.handler.get_data())
return res
@wrap_required_functions([
'get_settings',
'update_settings',
'get_data',
])
class Handler(object):
pass
......@@ -18,6 +18,7 @@
#
import logging
import random
from foris_controller.handler_base import BaseMockHandler
from foris_controller.utils import logger_wrapper
......@@ -45,3 +46,26 @@ class MockNetmetrHandler(Handler, BaseMockHandler):
self.hours_to_run = hours_to_run
self.autostart_enabled = autostart_enabled
return True
@logger_wrapper(logger)
def get_data(self):
return random.choice([
{"status": "error", "performed_tests": []},
{"status": "missing", "performed_tests": []},
{"status": "ready", "performed_tests": [
{
"speed_download": 23,
"speed_upload": 53,
"ping": 0.57,
"time": 1510669694000,
"test_uuid": "563803cc-b43e-4b2e-bf9e-439b098fe35b"
},
{
"speed_download": 88,
"speed_upload": 66,
"ping": 0.81,
"time": 1510669699000,
"test_uuid": "a975e00d-0bca-4871-8ce3-6e33c12311ef"
}
]},
])
......@@ -22,7 +22,7 @@ import logging
from foris_controller.handler_base import BaseOpenwrtHandler
from foris_controller.utils import logger_wrapper
from foris_controller_backends.netmetr import NetmetrUci
from foris_controller_backends.netmetr import NetmetrUci, NetmetrDataFile
from .. import Handler
......@@ -31,6 +31,7 @@ logger = logging.getLogger(__name__)
class OpenwrtNetmetrHandler(Handler, BaseOpenwrtHandler):
uci = NetmetrUci()
data = NetmetrDataFile()
@logger_wrapper(logger)
def get_settings(self):
......@@ -39,3 +40,8 @@ class OpenwrtNetmetrHandler(Handler, BaseOpenwrtHandler):
@logger_wrapper(logger)
def update_settings(self, autostart_enabled, hours_to_run):
return self.uci.update_settings(autostart_enabled, hours_to_run)
@logger_wrapper(logger)
def get_data(self):
status, data = self.data.read_records()
return {"status": status, "performed_tests": data}
......@@ -93,6 +93,50 @@
},
"additionalProperties": false,
"required": ["data"]
},
{
"description": "Get netmetr data request",
"properties": {
"module": {"enum": ["netmetr"]},
"kind": {"enum": ["request"]},
"action": {"enum": ["get_data"]}
},
"additionalProperties": false
},
{
"description": "Get netmetr data reply",
"properties": {
"module": {"enum": ["netmetr"]},
"kind": {"enum": ["reply"]},
"action": {"enum": ["get_data"]},
"data": {
"type": "object",
"properties": {
"status": {"enum": ["ready", "missing", "error"]},
"performed_tests": {
"type": "array",
"items": {
"type": "object",
"properties": {
"speed_download": {"type": "number"},
"speed_upload": {"type": "number"},
"ping": {"type": "number"},
"time": {"type": "integer"},
"test_uuid": {"type": "string"}
},
"additionalProperties": false,
"required": [
"speed_download", "speed_upload", "ping", "test_uuid", "time"
]
}
}
},
"additionalProperties": false,
"required": ["status", "performed_tests"]
}
},
"additionalProperties": false,
"required": ["data"]
}
]
}
......@@ -67,3 +67,12 @@ def test_settings(infrastructure, ubusd_test):
u"hours_to_run": new_hours_to_run
},
}
def test_data(infrastructure, ubusd_test):
res = infrastructure.process_message({
"module": "netmetr",
"action": "get_data",
"kind": "request",
})
assert set(res["data"].keys()) == {"status", "performed_tests"}
......@@ -2,7 +2,7 @@
"user": "stepan",
"access": {
"foris-controller-netmetr": {
"methods": [ "get_settings", "update_settings" ]
"methods": [ "get_settings", "update_settings", "get_data" ]
}
},
"publish": [ "foris-controller-netmetr" ],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment