-
-
Notifications
You must be signed in to change notification settings - Fork 53
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 #178 from netdevopsbr/optimize-performance
Optimize performance
- Loading branch information
Showing
39 changed files
with
1,528 additions
and
135 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,7 @@ | ||
## How it works | ||
|
||
The backend made using the FastAPI framework connects with both Netbox and Proxmox (it can be many different clusters) and exposes the API REST routes that will be consumed by the Netbox Plugin (the Frontend) that is simply a Django App attached to the Netbox Django Project. | ||
|
||
### Proxbox Architecture | ||
|
||
![Proxbox Architecure Image](./proxbox-architecture.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# `NetboxBase` class | ||
|
||
<!-- ::: netbox_proxbox.backend.routes.netbox.generic.NetboxBase | ||
options: | ||
docstring_options: | ||
ignore_init_summary: true | ||
merge_init_into_class: true | ||
show_source: false --> | ||
|
||
|
||
nb is a dependency injection of [NetboxSessionDep][netbox_proxbox.backend.session.netbox.NetboxSessionDep] | ||
|
||
!!! example "Teste" | ||
|
||
=== "1" | ||
``` | ||
teste 01 | ||
``` | ||
|
||
=== "2" | ||
teste 02 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,4 @@ class ProxboxConfig(PluginConfig): | |
|
||
config = ProxboxConfig | ||
|
||
from . import proxbox_api | ||
from . import proxbox_api |
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,9 @@ | ||
from netbox_proxbox.backend.routes.netbox.virtualization.cluster_type import ClusterType | ||
from netbox_proxbox.backend.routes.netbox.virtualization.cluster import Cluster | ||
from netbox_proxbox.backend.routes.netbox.virtualization.virtual_machines import VirtualMachine | ||
|
||
|
||
from netbox_proxbox.backend.routes.netbox.dcim.sites import Site | ||
from netbox_proxbox.backend.routes.netbox.dcim.device_roles import DeviceRole | ||
from netbox_proxbox.backend.routes.netbox.dcim.device_types import DeviceType | ||
from netbox_proxbox.backend.routes.netbox.dcim.devices import Device |
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,26 @@ | ||
from typing import Any | ||
|
||
class Cache: | ||
def __init__(self): | ||
self.cache: dict = {} | ||
|
||
def get(self, key: str): | ||
result = self.cache.get(key) | ||
if result is not None: | ||
return result | ||
|
||
def set(self, key: str, value: Any): | ||
self.cache[key] = value | ||
|
||
def delete(self, key: str): | ||
try: | ||
self.cache.pop(key) | ||
except KeyError: | ||
pass | ||
|
||
cache = Cache() | ||
|
||
|
||
|
||
|
||
|
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,8 @@ | ||
from enum import Enum | ||
|
||
class StatusOptions(str, Enum): | ||
planned = "planned" | ||
staging = "staging" | ||
active = "active" | ||
decommissioning = "decommissioning" | ||
retired = "retired" |
8 changes: 8 additions & 0 deletions
8
netbox_proxbox/backend/enum/netbox/virtualization/__init__.py
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,8 @@ | ||
from enum import Enum | ||
|
||
class ClusterStatusOptions(str, Enum): | ||
planned = "planned" | ||
staging = "staging" | ||
active = "active" | ||
decommissioning = "decommissioning" | ||
offline = "offline" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import logging | ||
from logging.handlers import TimedRotatingFileHandler | ||
|
||
# ANSI escape sequences for colors | ||
class AnsiColorCodes: | ||
BLACK = '\033[30m' | ||
RED = '\033[31m' | ||
GREEN = '\033[32m' | ||
YELLOW = '\033[33m' | ||
BLUE = '\033[34m' | ||
MAGENTA = '\033[35m' | ||
CYAN = '\033[36m' | ||
WHITE = '\033[37m' | ||
RESET = '\033[0m' | ||
DARK_GRAY = '\033[90m' | ||
|
||
class ColorizedFormatter(logging.Formatter): | ||
LEVEL_COLORS = { | ||
logging.DEBUG: AnsiColorCodes.BLUE, | ||
logging.INFO: AnsiColorCodes.GREEN, | ||
logging.WARNING: AnsiColorCodes.YELLOW, | ||
logging.ERROR: AnsiColorCodes.RED, | ||
logging.CRITICAL: AnsiColorCodes.MAGENTA | ||
} | ||
|
||
def format(self, record): | ||
color = self.LEVEL_COLORS.get(record.levelno, AnsiColorCodes.WHITE) | ||
|
||
record.module = f"{AnsiColorCodes.DARK_GRAY}{record.module}{AnsiColorCodes.RESET}" | ||
|
||
record.levelname = f"{color}{record.levelname}{AnsiColorCodes.RESET}" | ||
return super().format(record) | ||
|
||
def setup_logger(): | ||
# Path to log file | ||
log_path = '/var/log/proxbox.log' | ||
|
||
# Create a logger | ||
logger = logging.getLogger('proxbox') | ||
|
||
logger.setLevel(logging.DEBUG) | ||
|
||
# # Create a console handler | ||
console_handler = logging.StreamHandler() | ||
|
||
# Log all messages in the console | ||
console_handler.setLevel(logging.ERROR) | ||
|
||
# Create a formatter with colors | ||
formatter = ColorizedFormatter('%(name)s [%(asctime)s] [%(levelname)-8s] %(module)s: %(message)s') | ||
#formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(module)s: %(message)s') | ||
# Set the formatter for the console handler and file handler | ||
console_handler.setFormatter(formatter) | ||
|
||
# Create a file handler | ||
file_handler = TimedRotatingFileHandler(log_path, when='midnight', interval=1, backupCount=7) | ||
|
||
# Log only WARNINGS and above in the file | ||
file_handler.setLevel(logging.WARNING) | ||
|
||
# Set the formatter for the file handler | ||
file_handler.setFormatter(formatter) | ||
|
||
# Add the handlers to the logger | ||
logger.addHandler(console_handler) | ||
# logger.addHandler(file_handler) | ||
|
||
logger.propagate = False | ||
|
||
return logger | ||
|
||
|
||
logger = setup_logger() |
Oops, something went wrong.