-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from sonnhfit/feature/add-immune-system
immune system
- Loading branch information
Showing
6 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Dev | ||
- fix coding convention | ||
``` | ||
ruff check --fix . | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
from datetime import datetime | ||
from typing import Any, Dict, Optional | ||
|
||
from sonagent.loggers import bufferHandler | ||
from sonagent.utils.datetime_helpers import format_date | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ImmuneSystem: | ||
""" | ||
Class for Self Immune System that allows self-monitoring and self-recovery of the system. | ||
""" | ||
error_logs = [] | ||
|
||
@staticmethod | ||
def get_logs(limit: Optional[int]) -> Dict[str, Any]: | ||
"""Returns the last X logs""" | ||
if limit: | ||
buffer = bufferHandler.buffer[-limit:] | ||
else: | ||
buffer = bufferHandler.buffer | ||
records = [[format_date(datetime.fromtimestamp(r.created)), | ||
r.created * 1000, r.name, r.levelname, | ||
r.message + ('\n' + r.exc_text if r.exc_text else '')] | ||
for r in buffer] | ||
return {'log_count': len(records), 'logs': records} | ||
|
||
def immune_scan(self) -> None: | ||
""" | ||
Scan the system for any anomalies. | ||
""" | ||
logs = ImmuneSystem.get_logs(5) | ||
|
||
# Check for any errors in the logs | ||
errors = [log for log in logs['logs'] if log[3] == 'ERROR'] | ||
|
||
errors_detected = [] | ||
for error in errors: | ||
if error not in ImmuneSystem.error_logs: | ||
ImmuneSystem.error_logs.append(error) | ||
errors_detected.append(error) | ||
|
||
if len(errors_detected) > 0: | ||
logger.info(f"Errors detected: {errors_detected}") | ||
else: | ||
logger.debug("No errors detected.") | ||
|
||
def immune_recover(self) -> None: | ||
""" | ||
Recover the system from any anomalies. | ||
""" | ||
if ImmuneSystem.error_logs: | ||
print(f"Recovering from errors: {ImmuneSystem.error_logs}") | ||
logger.info(f"Recovering from errors: {ImmuneSystem.error_logs}") | ||
ImmuneSystem.error_logs = [] | ||
else: | ||
print("No errors detected.") | ||
logger.info("No errors detected.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters