Skip to content

Commit 964733d

Browse files
enhance metrics tab ui
Signed-off-by: IrushaBasukala <[email protected]>
1 parent dc979b5 commit 964733d

17 files changed

+1497
-85
lines changed

alembic.ini

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts.
5+
# this is typically a path given in POSIX (e.g. forward slashes)
6+
# format, relative to the token %(here)s which refers to the location of this
7+
# ini file
8+
script_location = %(here)s/alembic
9+
10+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
11+
# Uncomment the line below if you want the files to be prepended with date and time
12+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
13+
# for all available tokens
14+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
15+
16+
# sys.path path, will be prepended to sys.path if present.
17+
# defaults to the current working directory. for multiple paths, the path separator
18+
# is defined by "path_separator" below.
19+
prepend_sys_path = .
20+
21+
22+
# timezone to use when rendering the date within the migration file
23+
# as well as the filename.
24+
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
25+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
26+
# string value is passed to ZoneInfo()
27+
# leave blank for localtime
28+
# timezone =
29+
30+
# max length of characters to apply to the "slug" field
31+
# truncate_slug_length = 40
32+
33+
# set to 'true' to run the environment during
34+
# the 'revision' command, regardless of autogenerate
35+
# revision_environment = false
36+
37+
# set to 'true' to allow .pyc and .pyo files without
38+
# a source .py file to be detected as revisions in the
39+
# versions/ directory
40+
# sourceless = false
41+
42+
# version location specification; This defaults
43+
# to <script_location>/versions. When using multiple version
44+
# directories, initial revisions must be specified with --version-path.
45+
# The path separator used here should be the separator specified by "path_separator"
46+
# below.
47+
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
48+
49+
# path_separator; This indicates what character is used to split lists of file
50+
# paths, including version_locations and prepend_sys_path within configparser
51+
# files such as alembic.ini.
52+
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
53+
# to provide os-dependent path splitting.
54+
#
55+
# Note that in order to support legacy alembic.ini files, this default does NOT
56+
# take place if path_separator is not present in alembic.ini. If this
57+
# option is omitted entirely, fallback logic is as follows:
58+
#
59+
# 1. Parsing of the version_locations option falls back to using the legacy
60+
# "version_path_separator" key, which if absent then falls back to the legacy
61+
# behavior of splitting on spaces and/or commas.
62+
# 2. Parsing of the prepend_sys_path option falls back to the legacy
63+
# behavior of splitting on spaces, commas, or colons.
64+
#
65+
# Valid values for path_separator are:
66+
#
67+
# path_separator = :
68+
# path_separator = ;
69+
# path_separator = space
70+
# path_separator = newline
71+
#
72+
# Use os.pathsep. Default configuration used for new projects.
73+
path_separator = os
74+
75+
# set to 'true' to search source files recursively
76+
# in each "version_locations" directory
77+
# new in Alembic version 1.10
78+
# recursive_version_locations = false
79+
80+
# the output encoding used when revision files
81+
# are written from script.py.mako
82+
# output_encoding = utf-8
83+
84+
# database URL. This is consumed by the user-maintained env.py script only.
85+
# other means of configuring database URLs may be customized within the env.py
86+
# file.
87+
sqlalchemy.url = driver://user:pass@localhost/dbname
88+
89+
90+
[post_write_hooks]
91+
# post_write_hooks defines scripts or Python functions that are run
92+
# on newly generated revision scripts. See the documentation for further
93+
# detail and examples
94+
95+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
96+
# hooks = black
97+
# black.type = console_scripts
98+
# black.entrypoint = black
99+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
100+
101+
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
102+
# hooks = ruff
103+
# ruff.type = module
104+
# ruff.module = ruff
105+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
106+
107+
# Alternatively, use the exec runner to execute a binary found on your PATH
108+
# hooks = ruff
109+
# ruff.type = exec
110+
# ruff.executable = ruff
111+
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
112+
113+
# Logging configuration. This is also consumed by the user-maintained
114+
# env.py script only.
115+
[loggers]
116+
keys = root,sqlalchemy,alembic
117+
118+
[handlers]
119+
keys = console
120+
121+
[formatters]
122+
keys = generic
123+
124+
[logger_root]
125+
level = WARNING
126+
handlers = console
127+
qualname =
128+
129+
[logger_sqlalchemy]
130+
level = WARNING
131+
handlers =
132+
qualname = sqlalchemy.engine
133+
134+
[logger_alembic]
135+
level = INFO
136+
handlers =
137+
qualname = alembic
138+
139+
[handler_console]
140+
class = StreamHandler
141+
args = (sys.stderr,)
142+
level = NOTSET
143+
formatter = generic
144+
145+
[formatter_generic]
146+
format = %(levelname)-5.5s [%(name)s] %(message)s
147+
datefmt = %H:%M:%S

mcpgateway/alembic/README.md renamed to alembic/README

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Alembic Migration Guide for `mcpgateway`
1+
Generic single-database configuration.# Alembic Migration Guide for `mcpgateway`
22

33
> Creating, applying, and managing schema migrations with Alembic.
44

@@ -169,4 +169,4 @@ make db-history
169169
* Official docs: [https://alembic.sqlalchemy.org](https://alembic.sqlalchemy.org)
170170
* Autogenerate docs: [https://alembic.sqlalchemy.org/en/latest/autogenerate.html](https://alembic.sqlalchemy.org/en/latest/autogenerate.html)
171171

172-
---
172+
---

alembic/env.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# -*- coding: utf-8 -*-
2+
"""Alembic environment configuration for database migrations.
3+
4+
Copyright 2025
5+
SPDX-License-Identifier: Apache-2.0
6+
Authors: Mihai Criveti, Madhav Kandukuri
7+
8+
This module configures the Alembic migration environment for the MCP Gateway
9+
application. It sets up both offline and online migration modes, configures
10+
logging, and establishes the database connection parameters.
11+
12+
The module performs the following key functions:
13+
- Configures Alembic to locate migration scripts in the mcpgateway package
14+
- Sets up Python logging based on the alembic.ini configuration
15+
- Imports the SQLAlchemy metadata from the application models
16+
- Configures the database URL from application settings
17+
- Provides functions for running migrations in both offline and online modes
18+
19+
Offline mode generates SQL scripts without connecting to the database, while
20+
online mode executes migrations directly against a live database connection.
21+
22+
Attributes:
23+
config (Config): The Alembic configuration object loaded from alembic.ini.
24+
target_metadata (MetaData): SQLAlchemy metadata object containing all
25+
table definitions from the application models.
26+
27+
Examples:
28+
Running migrations in offline mode::
29+
30+
alembic upgrade head --sql
31+
32+
Running migrations in online mode::
33+
34+
alembic upgrade head
35+
36+
The module is typically not imported directly but is used by Alembic
37+
when executing migration commands.
38+
39+
Note:
40+
This file is automatically executed by Alembic and should not be
41+
imported or run directly by application code.
42+
"""
43+
44+
# Standard
45+
from importlib.resources import files
46+
from logging.config import fileConfig
47+
48+
# Third-Party
49+
from alembic import context
50+
51+
# this is the Alembic Config object, which provides
52+
# access to the values within the .ini file in use.
53+
from alembic.config import Config
54+
from sqlalchemy import engine_from_config, pool
55+
56+
# First-Party
57+
from mcpgateway.config import settings
58+
from mcpgateway.db import Base
59+
60+
# from mcpgateway.db import get_metadata
61+
# target_metadata = get_metadata()
62+
63+
64+
# Create config object - this is the standard way in Alembic
65+
config = getattr(context, "config", None) or Config()
66+
67+
68+
def _inside_alembic() -> bool:
69+
"""Detect if this module is being executed by the Alembic CLI.
70+
71+
This function checks whether the current execution context is within
72+
an Alembic migration environment. It's used to prevent migration code
73+
from running when this module is imported for other purposes (e.g.,
74+
during testing or when importing models).
75+
76+
The detection works by checking for the presence of the '_proxy' attribute
77+
on the alembic.context object. This attribute is set internally by Alembic
78+
when it loads and executes the env.py file during migration operations.
79+
80+
Returns:
81+
bool: True if running under Alembic CLI (e.g., during 'alembic upgrade',
82+
'alembic downgrade', etc.), False if imported normally by Python
83+
code or during testing.
84+
85+
Examples:
86+
When running migrations::
87+
88+
$ alembic upgrade head
89+
# _inside_alembic() returns True
90+
91+
When importing in tests or application code::
92+
93+
from mcpgateway.alembic.env import target_metadata
94+
# _inside_alembic() returns False
95+
96+
Note:
97+
This guard is crucial to prevent the migration execution code at the
98+
bottom of this module from running during normal imports. Without it,
99+
importing this module would attempt to run migrations every time.
100+
"""
101+
return getattr(context, "_proxy", None) is not None
102+
103+
104+
config.set_main_option("script_location", str(files("mcpgateway").joinpath("alembic")))
105+
106+
# Interpret the config file for Python logging.
107+
# This line sets up loggers basically.
108+
if config.config_file_name is not None:
109+
fileConfig(
110+
config.config_file_name,
111+
disable_existing_loggers=False,
112+
)
113+
114+
# First-Party
115+
# add your model's MetaData object here
116+
# for 'autogenerate' support
117+
# from myapp import mymodel
118+
119+
target_metadata = Base.metadata
120+
121+
# other values from the config, defined by the needs of env.py,
122+
# can be acquired:
123+
# my_important_option = config.get_main_option("my_important_option")
124+
# ... etc.
125+
126+
config.set_main_option(
127+
"sqlalchemy.url",
128+
settings.database_url,
129+
)
130+
131+
132+
def run_migrations_offline() -> None:
133+
"""Run migrations in 'offline' mode.
134+
135+
This configures the context with just a URL
136+
and not an Engine, though an Engine is acceptable
137+
here as well. By skipping the Engine creation
138+
we don't even need a DBAPI to be available.
139+
140+
Calls to context.execute() here emit the given string to the
141+
script output.
142+
143+
"""
144+
url = config.get_main_option("sqlalchemy.url")
145+
context.configure(
146+
url=url,
147+
target_metadata=target_metadata,
148+
literal_binds=True,
149+
dialect_opts={"paramstyle": "named"},
150+
)
151+
152+
with context.begin_transaction():
153+
context.run_migrations()
154+
155+
156+
def run_migrations_online() -> None:
157+
"""Run migrations in 'online' mode.
158+
159+
In this scenario we need to create an Engine
160+
and associate a connection with the context.
161+
162+
"""
163+
connection = config.attributes.get("connection")
164+
if connection is None:
165+
connectable = engine_from_config(
166+
config.get_section(config.config_ini_section, {}),
167+
prefix="sqlalchemy.",
168+
poolclass=pool.NullPool,
169+
)
170+
171+
with connectable.connect() as connection:
172+
context.configure(connection=connection, target_metadata=target_metadata)
173+
174+
with context.begin_transaction():
175+
context.run_migrations()
176+
else:
177+
context.configure(connection=connection, target_metadata=target_metadata)
178+
179+
with context.begin_transaction():
180+
context.run_migrations()
181+
182+
183+
if _inside_alembic():
184+
if context.is_offline_mode():
185+
run_migrations_offline()
186+
else:
187+
run_migrations_online()
File renamed without changes.

0 commit comments

Comments
 (0)