Skip to content

Commit

Permalink
renaming folders and start to integrate redBorder API
Browse files Browse the repository at this point in the history
  • Loading branch information
malvads committed Sep 29, 2023
1 parent 1c455c9 commit 983011b
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 32 deletions.
2 changes: 1 addition & 1 deletion resources/src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
from Logger.logger import logger
from logger.logger import logger
from server.rest import APIServer, config
from server.production import GunicornApp

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2023 Eneo Tecnologia S.L.
#
# Authors :
# Miguel Álvarez Adsuara <malvarez@redborder.com>
# Pablo Rodriguez Flores <prodriguez@redborder.com>
#
# This program is free software: you can redistribute it and/or modify
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 7 additions & 3 deletions resources/src/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ outliers_binding_address=0.0.0.0
outliers_server_port=39091

[Outliers]
metric = bytes
metric=bytes

[Druid]
druid_endpoint = http://10.0.209.10:8080/druid/v2/
druid_endpoint=http://10.0.209.10:8080/druid/v2/

[Logger]
log_file = ./outliers.log
log_file=./outliers.log

[redBorder]
api_endpoint=http://x.x.x.x/api/v1/
api_oauth_token=abcdabcdabcd
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""

import json

class QueryBuilder:
"""
Class used to modify the manager usual query to exctract data about
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def __init__(self, log_level=logging.INFO, log_file=None): # Accept log_file as
if log_file is None:
log_file = './outliers.log'
try:
from Config import configmanager
from config import configmanager
config = configmanager.ConfigManager(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.ini"))
log_file = config.get('Logger', 'log_file')
except Exception as e:
print("Could not resolve ConfigManager, default set to ./outliers.log")
self.logger = PyLogrus(name="OutliersLogger")
self.logger = PyLogrus(name="Outlierslogger")
log_dir = os.path.dirname(log_file)
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
Expand Down
34 changes: 34 additions & 0 deletions resources/src/redborder/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (C) 2023 Eneo Tecnologia S.L.
#
# Authors :
# Miguel Álvarez Adsuara <malvarez@redborder.com>
# Pablo Rodriguez Flores <prodriguez@redborder.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import requests

class RedBorderAPI:
def __init__(self, endpoint, oauth_token) -> None:
self.api_endpoint = endpoint
self.oauth_token = oauth_token

def request_flow_sensors(self):
response = requests.get(f'{self.api_endpoint}/sensors/flow/?auth_token={self.oauth_token}')

if response.status_code == 200:
response_json = response.json()
return response_json['flow']

raise Exception(f"redBorder api request failed with status code {response.status_code}.")
16 changes: 8 additions & 8 deletions resources/src/server/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import time
import base64
import threading
from IA import outliers
from Druid import client, query_builder
from Logger import logger
from Config import configmanager
from ai import outliers
from druid import client, query_builder
from logger import logger
from config import configmanager
from flask import Flask, jsonify, request

'''
Expand All @@ -36,8 +36,8 @@
config = configmanager.ConfigManager(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.ini"))
druid_client = client.DruidClient(config.get("Druid", "druid_endpoint"))
query_modifier = query_builder.QueryBuilder(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Druid", "aggregations.json"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Druid", "postAggregations.json")
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "druid", "aggregations.json"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "druid", "postAggregations.json")
)

class APIServer:
Expand All @@ -59,8 +59,8 @@ def calculate():
return jsonify(outliers.Autoencoder.execute_prediction_model(
data,
config.get("Outliers", "metric"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "IA", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "IA", "traffic.ini")
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ai", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ai", "traffic.ini")
))
except Exception as e:
logger.logger.error("Error while calculating prediction model -> " + str(e))
Expand Down
2 changes: 1 addition & 1 deletion resources/tests/test_configmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import configparser

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.Config.configmanager import ConfigManager
from src.config.configmanager import ConfigManager

class TestConfigManager(unittest.TestCase):
def setUp(self):
Expand Down
6 changes: 3 additions & 3 deletions resources/tests/test_druid.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.Druid.client import DruidClient
from src.druid.client import DruidClient

class TestDruidClient(unittest.TestCase):
def setUp(self):
Expand Down
5 changes: 2 additions & 3 deletions resources/tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
import os
import sys
import logging
from unittest.mock import patch
import unittest
from io import StringIO

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.Logger.logger import Logger
from src.logger.logger import Logger

class TestLogger(unittest.TestCase):
def setUp(self):
Expand Down
12 changes: 5 additions & 7 deletions resources/tests/test_outliers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import logging
import json

from unittest.mock import patch

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.IA.outliers import Autoencoder
from src.ai.outliers import Autoencoder

class TestAutoencoder(unittest.TestCase):
def setUp(self):
Expand All @@ -41,15 +39,15 @@ def test_model_execution_with_no_data(self):
Autoencoder.execute_prediction_model(
{},
"bytes",
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "IA", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "IA", "traffic.ini")
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "ai", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "ai", "traffic.ini")
)
def test_model_execution_with_sample_data(self):
Autoencoder.execute_prediction_model(
self.sample_data,
"bytes",
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "IA", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "IA", "traffic.ini")
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "ai", "traffic.keras"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "ai", "traffic.ini")
)

if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions resources/tests/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from unittest.mock import MagicMock, patch
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from src.Druid import query_builder
from src.druid import query_builder

class TestQueryBuilder(unittest.TestCase):
def setUp(self) -> None:
aggregations_file = os.path.join(os.getcwd(),"resources", "src", "Druid", "aggregations.json")
post_aggregations_file = os.path.join(os.getcwd(),"resources", "src", "Druid", "postAggregations.json")
aggregations_file = os.path.join(os.getcwd(),"resources", "src", "druid", "aggregations.json")
post_aggregations_file = os.path.join(os.getcwd(),"resources", "src", "druid", "postAggregations.json")
self.builder = query_builder.QueryBuilder(aggregations_file, post_aggregations_file)

def test_known_granularities_granularities_to_seconds(self):
Expand Down
53 changes: 53 additions & 0 deletions resources/tests/test_redborder_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) 2023 Eneo Tecnologia S.L.
#
# Authors :
# Miguel Álvarez Adsuara <malvarez@redborder.com>
# Pablo Rodriguez Flores <prodriguez@redborder.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import unittest
from unittest.mock import Mock

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.redborder import client

class TestRedBorderAPI(unittest.TestCase):
def setUp(self):
self.mock_response = Mock()
self.mock_response.status_code = 200
self.mock_response.json.return_value = {'flow': 'mocked_data'}

def test_request_flow_sensors_success(self):
api = client.RedBorderAPI(endpoint='http://mocked-endpoint', oauth_token='mocked-token')

with unittest.mock.patch('requests.get', return_value=self.mock_response):
result = api.request_flow_sensors()

self.assertEqual(result, 'mocked_data')

def test_request_flow_sensors_failure(self):
api = client.RedBorderAPI(endpoint='http://mocked-endpoint', oauth_token='mocked-token')

self.mock_response.status_code = 404
with unittest.mock.patch('requests.get', return_value=self.mock_response):
with self.assertRaises(Exception) as context:
api.request_flow_sensors()

self.assertIn("redBorder api request failed with status code 404.", str(context.exception))

if __name__ == '__main__':
unittest.main()

0 comments on commit 983011b

Please sign in to comment.