-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring source code to be fully a bot #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apentori
wants to merge
13
commits into
master
Choose a base branch
from
module-refactoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
387c7da
add refactoring plan
apentori f95224e
refacto v1
apentori 7f045c9
fix code
apentori 541de4d
refacto: extract config and metrics
apentori 9e7cb0f
fix: improve config
apentori ba34006
tmp: adding monitoring as module
apentori 82226ff
fix module loading
apentori 3ff1971
docs: small update
apentori 47a561e
feat: api server
apentori 19091a1
docs: update Documentation
apentori c28a596
api: adding optional API KEY
apentori c5afc4d
api: add wehbook
apentori ffc525f
refactor database and add receiver modules
apentori File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,3 +12,7 @@ bot/docs | |
|
|
||
| config.yaml | ||
| .env | ||
|
|
||
| accounts/ | ||
| backups/ | ||
| data-dir/ | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| from .account import Account | ||
| from .logger import Logger | ||
| from .postgres import Postgres | ||
| from .config import Config |
This file contains hidden or 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
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems an overkill for the project. Looks like a Dagster Config.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well that;s just a proper config management |
This file contains hidden or 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,84 @@ | ||
| from pydantic import BaseModel | ||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
| from pydantic_settings.sources import YamlConfigSettingsSource | ||
|
|
||
|
|
||
| class BackendConfig(BaseModel): | ||
| domain: str = "localhost" | ||
| port: int = 8080 | ||
| is_secure: bool = False | ||
|
|
||
|
|
||
| class BotConfig(BaseModel): | ||
| display_name: str = "" | ||
| public_key: str = "" | ||
| password: str = "" | ||
| mnemonic_phrase: str = "" | ||
| init_account: bool = False | ||
| compressed_key: str = "" | ||
| infura_token: str = "" | ||
| coingecko_api_key: str = "" | ||
|
|
||
| class DatabaseConfig(BaseModel): | ||
| host: str = "database" | ||
| port: int = 5432 | ||
| user: str = "" | ||
| password: str = "" | ||
| name: str = "" | ||
| schema: str = "public" | ||
| tables: dict = {} | ||
|
|
||
|
|
||
| class ApiConfig(BaseModel): | ||
| enable: bool = True | ||
| host: str = "0.0.0.0" | ||
| port: int = 8081 | ||
|
|
||
|
|
||
| class MetricsConfig(BaseModel): | ||
| enabled: bool = False | ||
| host: str = "0.0.0.0" | ||
| port: int = 8000 | ||
|
|
||
|
|
||
| class FilesConfig(BaseModel): | ||
| current_state: str = "dates.pkl" | ||
|
|
||
|
|
||
| class ModulesConfig(BaseModel): | ||
| directories: list[str] = ["./modules", "bot/modules"] | ||
| enabled: list[str] = [] | ||
| settings: dict = {} | ||
|
|
||
|
|
||
| class Config(BaseSettings): | ||
| model_config = SettingsConfigDict( | ||
| env_nested_delimiter="__", | ||
| extra="ignore", | ||
| ) | ||
|
|
||
| sleep: int = 10 | ||
| files: FilesConfig = FilesConfig() | ||
| bot: BotConfig = BotConfig() | ||
| backend: BackendConfig = BackendConfig() | ||
| api: ApiConfig = ApiConfig() | ||
| modules: ModulesConfig = ModulesConfig() | ||
| metrics: MetricsConfig = MetricsConfig() | ||
| database: DatabaseConfig = DatabaseConfig() | ||
|
|
||
| _yaml_file = "./config.yaml" | ||
|
|
||
| @classmethod | ||
| def settings_customise_sources( | ||
| cls, | ||
| settings_cls, | ||
| init_settings, | ||
| env_settings, | ||
| dotenv_settings, | ||
| file_secret_settings, | ||
| ): | ||
| return ( | ||
| YamlConfigSettingsSource(settings_cls, yaml_file=cls._yaml_file), | ||
| env_settings, | ||
| dotenv_settings, | ||
| ) |
This file contains hidden or 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,34 @@ | ||
| import logging | ||
|
|
||
| from bot.config import MetricsConfig | ||
| from bot.modules.manager import ModuleManager | ||
| from prometheus_client import start_http_server, Gauge, Counter | ||
|
|
||
|
|
||
| def start_prometheus(metrics_config: MetricsConfig, manager: ModuleManager, logger: logging.Logger): | ||
| if not metrics_config.enabled: | ||
| logger.info("Prometheus metrics exporter disabled") | ||
| return | ||
|
|
||
| health = Gauge("status_bot_health", "Bot health status") | ||
| version = Gauge("status_bot_version", "Bot version", ["version"]) | ||
| module_loaded = Gauge( | ||
| "status_bot_module_loaded", "Module loaded", ["module"] | ||
| ) | ||
| module_errors = Counter( | ||
| "status_bot_module_errors_total", "Module errors", ["module"] | ||
| ) | ||
| module_restarts = Counter( | ||
| "status_bot_module_restarts_total", "Module restarts", ["module"] | ||
| ) | ||
|
|
||
| health.set(1) | ||
| version.labels(version="0.1.0").set(1) | ||
|
|
||
| for module_name in manager.module_names: | ||
| module_loaded.labels(module=module_name).set(1) | ||
|
|
||
| host = metrics_config.host | ||
| port = metrics_config.port | ||
| start_http_server(port, host) | ||
| logger.info(f"Prometheus metrics exporter server started on {host}:{port}") |
This file contains hidden or 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,4 @@ | ||
| from .base import BaseModule, ModuleType, ModuleConfig, ModuleContext | ||
| from .manager import ModuleManager | ||
|
|
||
| __all__ = ["BaseModule", "ModuleType", "ModuleConfig", "ModuleContext", "ModuleManager"] |
This file contains hidden or 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,43 @@ | ||
| import threading | ||
|
|
||
| import uvicorn | ||
| from fastapi import FastAPI | ||
|
|
||
| from bot.modules.base import BaseModule, ModuleType | ||
|
|
||
|
|
||
| class APIServerModule(BaseModule): | ||
|
|
||
| @property | ||
| def module_type(self) -> ModuleType: | ||
| return ModuleType.SERVICE | ||
|
|
||
| def on_start(self): | ||
| api_config = self.ctx.shared_state["config"].api | ||
| self._host = api_config.host | ||
| self._port = api_config.port | ||
| self._app: FastAPI = self.ctx.shared_state["fastapi_app"] | ||
| self._server = None | ||
|
|
||
| def execute(self): | ||
| if not self.ctx.shared_state["config"].api.enable: | ||
| self.ctx.logger.info("API server is disabled, skipping startup") | ||
| return | ||
|
|
||
| config = uvicorn.Config( | ||
| self._app, | ||
| host=self._host, | ||
| port=self._port, | ||
| log_level="info", | ||
| ) | ||
| server = uvicorn.Server(config) | ||
| self._server = server | ||
| thread = threading.Thread(target=server.run, daemon=True) | ||
| thread.start() | ||
| self.ctx.stop_event.wait() | ||
| server.should_exit = True | ||
| thread.join(timeout=10) | ||
|
|
||
| def on_stop(self): | ||
| if self._server: | ||
| self._server.should_exit = True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.