Skip to content
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

Optimize connection #35

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions milvus_cli/Cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from Connection import MilvusConnection
from Database import MilvusDatabase
from pymilvus import __version__
from Types import ParameterException

def getPackageVersion():
import pkg_resources # part of setuptools
try:
version = pkg_resources.require("milvus_cli")[0].version
except Exception as e:
raise ParameterException(
"Could not get version under single executable file mode.")
return version

class MilvusCli(object):
connection = MilvusConnection()
database = MilvusDatabase()


15 changes: 5 additions & 10 deletions milvus_cli/Connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pymilvus import __version__,connections,list_collections
from pymilvus import connections,list_collections
from Types import ConnectException
from tabulate import tabulate

class MilvusConnection(object):
uri = "127.0.0.1:19530"
Expand Down Expand Up @@ -39,17 +38,13 @@ def showConnection(self, alias=None, showAll=False):
allConnections = connections.list_connections()

if showAll:
return tabulate(allConnections,
headers=["Alias"],
tablefmt="pretty")
return allConnections;

aliasList = map(lambda x: x[0], allConnections)

if tempAlias in aliasList:
address, user = connections.get_connection_addr(tempAlias).values()
return tabulate(
[["Address", address], ["User", user], ["Alias", tempAlias]],
tablefmt="pretty",
)
response = connections.get_connection_addr(tempAlias).values()
return response
else:
return "Connection not found!"

Expand Down
4 changes: 1 addition & 3 deletions milvus_cli/Database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from pymilvus import db
from Types import ConnectException
from tabulate import tabulate

class Database():
class MilvusDatabase():
alias = "default"
def create_database(self,dbName=None,alias=None):
tempAlias = alias if alias else self.alias
Expand Down
108 changes: 108 additions & 0 deletions milvus_cli/scripts/connection_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from tabulate import tabulate
from helper_cli import show
from init_cli import cli
import click


@cli.command(no_args_is_help=False)
@click.option(
"-a",
"--alias",
"alias",
help="[Optional] - Milvus link alias name, default is `default`.",
default="default",
type=str,
)
@click.option(
"-uri",
"--uri",
"uri",
help="[Optional] - uri, default is `http://127.0.0.1:19530`.",
default="http://127.0.0.1:19530",
type=str,
)
@click.option(
"-u",
"--username",
"username",
help="[Optional] - Username , default is `None`.",
default=None,
type=str,
)
@click.option(
"-pwd",
"--password",
"password",
help="[Optional] - Password , default is `None`.",
default=None,
type=str,
)
@click.pass_obj
def connect(obj, alias, uri, username, password):
"""
Connect to Milvus.

Example:

milvus_cli > connect -h 127.0.0.1 -p 19530 -a default
"""
try:
obj.connection.connect(alias, uri, username, password)
except Exception as e:
click.echo(message=e, err=True)
else:
click.echo("Connect Milvus successfully.")
address, username = obj.connection.showConnection(alias)
click.echo(
tabulate(
[["Address", address], ["User", username], ["Alias", alias]],
tablefmt="pretty",
)
)


@show.command("connection")
@click.option(
"-a",
"--all",
"showAll",
help="[Optional, Flag] - Show all connections.",
default=False,
is_flag=True,
type=bool,
)
@click.option(
"-n",
"--name",
"name",
help="[Optional, Flag] - Show one connection by name.",
default=None,
type=str,
)
@click.pass_obj
def connection(obj, showAll=True, name=None):
"""Show current/all connection details"""
try:
if showAll:
allConnections = obj.connection.showConnection(showAll=True)
click.echo(
tabulate(
allConnections,
headers=["Alias", "Instance"],
tablefmt="pretty",
)
)
else:
result = obj.connection.showConnection(showAll=False, alias=name)
if isinstance(result, str):
click.echo(result)
return
address, user = result
click.echo(
tabulate(
[["Address", address], ["User", user], ["Alias", name]],
tablefmt="pretty",
)
)
except Exception as e:
click.echo(message=e, err=True)
87 changes: 87 additions & 0 deletions milvus_cli/scripts/helper_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from Cli import getPackageVersion
from init_cli import cli
from Types import ConnectException, ParameterException
from utils import WELCOME_MSG, EXIT_MSG, Completer
import sys
import os
import click

current_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)


def print_help_msg(command):
with click.Context(command) as ctx:
click.echo(command.get_help(ctx))


@cli.command()
def help():
"""Show help messages."""
click.echo(print_help_msg(cli))


@cli.command()
def version():
"""Get Milvus_CLI version."""
click.echo(f"Milvus_CLI v{getPackageVersion()}")


@cli.command()
def clear():
"""Clear screen."""
click.clear()


@cli.group(no_args_is_help=False)
@click.pass_obj
def show(obj):
"""Show connection, loading_progress and index_progress."""
pass


@cli.command("exit")
def quit_app():
"""Exit the CLI."""
global quit_app
quit_app = True


quit_app = False # global flag
comp = Completer()


def runCliPrompt():
args = sys.argv
if args and (args[-1] == "--version"):
print(f"Milvus_CLI v{getPackageVersion()}")
return
try:
print(WELCOME_MSG)
while not quit_app:
import readline

readline.set_completer_delims(" \t\n;")
readline.parse_and_bind("tab: complete")
readline.set_completer(comp.complete)
astr = input("milvus_cli > ")
try:
cli(astr.split())
except SystemExit:
# trap argparse error message
# print('error', SystemExit)
continue
except ParameterException as pe:
click.echo(message=f"{str(pe)}", err=True)
except ConnectException as ce:
click.echo(
message="Connect to milvus Error!\nPlease check your connection.",
err=True,
)
except Exception as e:
click.echo(message=f"Error occurred!\n{str(e)}", err=True)
print(EXIT_MSG)
except (KeyboardInterrupt, EOFError):
print(EXIT_MSG)
sys.exit(0)
16 changes: 16 additions & 0 deletions milvus_cli/scripts/init_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys
import os
import click

current_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)

from Cli import MilvusCli


@click.group(no_args_is_help=False, add_help_option=False, invoke_without_command=True)
@click.pass_context
def cli(ctx):
"""Milvus_CLI"""
ctx.obj = MilvusCli()
Loading