Skip to content

Commit

Permalink
Substitued ntp with regular machine utc date time, as it was consider…
Browse files Browse the repository at this point in the history
…ed an overkill
  • Loading branch information
Pablo Rodríguez Flores committed Jun 11, 2024
1 parent f4d7561 commit 4f580f4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 143 deletions.
16 changes: 7 additions & 9 deletions resources/src/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ outliers_server_port=39091

[Outliers]
metric=bytes
schedule_hour=* * * * *
epochs=20
epochs=1000
batch_size=32
backup_path=resources/src/ai/backups/
#target_sensors=FlowSensor
model_names=traffic

[Druid]
druid_endpoint=http://x.x.x.x:8080/druid/v2/

[Logger]
log_file=./outliers.log

[NTP]
ntp_server=x.x.x.x
[Rails]
endpoint=https://x.x.x.x
auth_token=my_token

[AWS]
s3_hostname=http://x.x.x.x:9000
Expand All @@ -34,7 +32,7 @@ s3_region=es
s3_bucket=my_bucket

[ZooKeeper]
zk_sleep_time=28800
zk_hosts=localhost:2181,localhost:2182,localhost:2183
zk_sleep_time=86400
zk_hosts=hosts
zk_sync_path=/rb-aioutliers
zk_name=client
zk_name=name
71 changes: 0 additions & 71 deletions resources/src/rbntp/ntplib.py

This file was deleted.

38 changes: 19 additions & 19 deletions resources/src/redborder/async_jobs/train_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
# You should have received a copy of the GNU Affero General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.

import os, json, shutil
import os
import json
import shutil
from datetime import datetime, timezone, timedelta
from resources.src.redborder.s3 import S3
from resources.src.ai.trainer import Trainer
from resources.src.server.rest import config
from resources.src.logger.logger import logger
from resources.src.rbntp.ntplib import NTPClient
from resources.src.druid.client import DruidClient
from resources.src.druid.query_builder import QueryBuilder
from resources.src.redborder.rb_ai_outliers_filters import RbAIOutliersFilters
Expand Down Expand Up @@ -112,6 +114,17 @@ def upload_model_to_s3(self, model_name):
s3_path = f'rbaioutliers/latest/{filename}'
self.upload_file(local_path, s3_path)

def get_iso_time(self):
"""Returns a string with the current timestamp in ISO format."""
dt_utc = datetime.now(timezone.utc).replace(microsecond=0)
return dt_utc.isoformat

def subtract_one_day(self, iso_time_str):
"""Given a timestamp string in ISO format, returns timestamp of one day before"""
dt = datetime.fromisoformat(iso_time_str)
dt_minus_one_day = dt - timedelta(days=1)
return dt_minus_one_day.isoformat()

def train_job(self, model_name):
"""
Start the Outliers training job.
Expand All @@ -122,9 +135,7 @@ def train_job(self, model_name):
logger.logger.info("Getting model files from S3")
self.download_model_from_s3(model_name)
logger.info("Starting Outliers Train Job")
redborder_ntp = self.initialize_ntp_client()
druid_client = self.initialize_druid_client()
manager_time = redborder_ntp.get_ntp_time()
traffic_query = self.load_traffic_query()
self.query_builder = QueryBuilder(
self.get_aggregation_config_path(),
Expand All @@ -135,16 +146,7 @@ def train_job(self, model_name):
os.path.join(self.main_dir, "ai", f"{model_name}.keras"),
os.path.join(self.main_dir, "ai", f"{model_name}.ini"),
)
self.process_model_data(model_name, query, redborder_ntp, manager_time, druid_client)

def initialize_ntp_client(self):
"""
Initialize the NTP client.
Returns:
NTPClient: The initialized NTP client.
"""
return NTPClient(config.get("NTP", "ntp_server"))
self.process_model_data(model_name, query,druid_client)

def initialize_druid_client(self):
"""
Expand Down Expand Up @@ -184,22 +186,20 @@ def get_post_aggregations_config_path(self):
"""
return os.path.join(self.main_dir, "druid", "data", "postAggregations.json")

def process_model_data(self, model_name, query, redborder_ntp, manager_time, druid_client):
def process_model_data(self, model_name, query, druid_client):
"""
Process data and train the model.
Args:
model_name (str): Model identifier.
query (dict): The query to be modified.
redborder_ntp (NTPClient): The NTP client.
manager_time (datetime): The manager time.
druid_client (DruidClient): The Druid client.
This function processes data, modifies the query, and trains the model.
"""
rb_granularities=["pt1m", "pt2m", "pt5m", "pt15m", "pt30m", "pt1h", "pt2h", "pt8h"]
start_time = redborder_ntp.time_to_iso8601_time(redborder_ntp.get_substracted_day_time(manager_time))
end_time = redborder_ntp.time_to_iso8601_time(manager_time)
end_time = self.get_iso_time()
start_time = self.subtract_one_day(end_time)
model_filter = RbAIOutliersFilters().get_filtered_data(model_name)
query = self.query_builder.modify_filter(query, model_filter)
query = self.query_builder.set_time_origin(query, start_time)
Expand Down
44 changes: 0 additions & 44 deletions resources/tests/test_ntp.py

This file was deleted.

0 comments on commit 4f580f4

Please sign in to comment.