Main orchestrator

class network_watcher.AttentionLayer(*args, **kwargs)

Bases: Layer

Custom attention layer for LSTM model. This layer computes the attention weights and applies them to the input sequence.

Args
  • Layer (tf.keras.layers.Layer): Base class for all layers in Keras.

Returns

None

build(input_shape)

Create the attention weights and bias.

Args
  • input_shape (tuple): Shape of the input tensor.

Returns

None

call(x)

Calculate the attention weights and apply them to the input sequence.

Args
  • x (tensor): Input tensor of shape (batch_size, sequence_length, features).

Returns
  • tensor: Output tensor of shape (batch_size, features).

compute_output_shape(input_shape)

Compute the output shape of the layer.

Args
  • input_shape (tuple): Shape of the input tensor.

Returns
  • tuple: Shape of the output tensor.

network_watcher.clean_old_models(directory='/app/data/Model', keep_last_n=7, pattern='Model_bn_*.keras')

Keep only the last keep_last_n saved model files and delete older ones.

Args
  • directory (str): Directory containing the model files.

  • keep_last_n (int): Number of recent models to keep.

  • pattern (str): Pattern to match model files.

Returns

None

network_watcher.load_last_sequence(csv_path, selected_features, sequence_length=60)

Load the last sequence of records from CSV, ensuring all required features and labels are present.

Args
  • csv_path (str): Path to the CSV file.

  • selected_features (list): List of features to select from the DataFrame.

  • sequence_length (int): Length of the sequence to load.

Returns
  • tuple: DataFrame with selected features and the correct labels.

Raises
  • ValueError: If any of the selected features are missing in the DataFrame.

network_watcher.main_loop(interval=1, prometheus_port=9000)

Main loop that monitors UE activity, parses logs, updates Prometheus metrics, and fine-tunes the model in real-time.

Args
  • interval (int): Time interval for monitoring and updating metrics.

  • prometheus_port (int): Port for Prometheus metrics.

Returns

None

network_watcher.parse_amf(lines, previous_state)

Parse AMF log lines to extract UE registration and deregistration events, update UE states, and compute registration/session durations.

Args
  • lines (list): List of log lines to parse.

  • previous_state (dict): Previous state of UE details and durations.

Returns
  • tuple: Updated UE details, new registration durations, and new session durations.

network_watcher.predict_current_uc(latest_window_df)

Predict the current use case (UC) using the loaded LSTM model based on the latest data window.

Args
  • latest_window_df (pd.DataFrame): DataFrame containing the latest data window.

Returns
  • tuple: Predicted UC class and confidence score.

network_watcher.remove_offset()

Remove the offset file used by Pygtail to start reading the log file from the beginning.

Args

None

Returns

None

network_watcher.run_main_notebook_with_backup()

Execute the main.ipynb notebook, log its execution, truncate CSV, and periodically create CSV backups.

Args

None

Returns

None

network_watcher.run_notebook_in_thread()

Run the main.ipynb notebook in a separate thread to avoid blocking the main loop.

Args

None

Returns

None

network_watcher.save_model_with_date(model, path_prefix='/app/data/Model/Model_bn_')

Save the current model to disk with the current date as part of the filename.

Args
  • model (tf.keras.Model): The model to save.

  • path_prefix (str): Prefix for the filename.

Returns

None

network_watcher.truncate_running_data(csv_path, keep_last_n=60)

Truncate the CSV file to keep only the last keep_last_n records.

Args
  • csv_path (str): Path to the CSV file.

  • keep_last_n (int): Number of records to keep.

Returns

None