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

Feature/access for rdb resource #374

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5e2eb15
preparare doundation
aran-nakayama Sep 29, 2024
e25cfe9
working with mock data
aran-nakayama Sep 29, 2024
35b5eae
use vannna to make sql and pass to the dana agent through dbresource
aran-nakayama Oct 1, 2024
001f6d5
make file for making example table and data
aran-nakayama Oct 1, 2024
3c047f4
train vanna for generating SQL from prompt
aran-nakayama Oct 2, 2024
0d63e06
resource data is taple and it seems that openssa has difficulty to un…
aran-nakayama Oct 3, 2024
ffae625
make myvanna file to clean code
aran-nakayama Oct 3, 2024
47ecfe8
TypeError: Can't instantiate abstract class DbResource without an imp…
Oct 11, 2024
86c0840
change how to import from openssa to try to make it work (but it didn't)
Oct 18, 2024
4e9f648
nailcare
aran-nakayama Oct 22, 2024
27d9c37
nailcare
aran-nakayama Oct 22, 2024
39b218b
save tmp (need to be fixed)
aran-nakayama Oct 22, 2024
7227879
style: reformat code
phanhongan Oct 26, 2024
f27c9ed
refactor: replace random.choice by secrets.choice to avoid security r…
phanhongan Oct 26, 2024
2ade37a
Merge from `main`
phanhongan Oct 26, 2024
82432fb
style: add empty lines and space
phanhongan Oct 26, 2024
2ec3e3a
style: add empty lines
phanhongan Oct 26, 2024
5a93152
style: rearrange imports
phanhongan Oct 26, 2024
7f9f044
refactor: replace Exception by ValueError & RuntimeError
phanhongan Oct 26, 2024
08a2170
style: import 3rd party before 1st party
phanhongan Oct 26, 2024
4c51982
fix: add default db port
phanhongan Oct 26, 2024
654b8f7
fix: add default db port as string
phanhongan Oct 26, 2024
c255aa9
build: add pymysql
phanhongan Oct 26, 2024
9bf2495
fix: set default values for DB connection
phanhongan Oct 26, 2024
2030be4
ci: add MySQL to Mac
phanhongan Oct 26, 2024
16220cd
ci: install Docker for pulling MySQL
phanhongan Oct 26, 2024
a5a1a8a
ci: setup Docker Buildx
phanhongan Oct 26, 2024
4921af5
ci: setup MySQL on Ubuntu
phanhongan Oct 26, 2024
9fbf2ce
ci: wait for MySQL to be ready
phanhongan Oct 26, 2024
e9ee662
ci: use random root password for MySQL
phanhongan Oct 26, 2024
965881c
ci: remove MYSQL_USER="root"
phanhongan Oct 26, 2024
78f0789
ci: remove MYSQL_USER="root" in test env
phanhongan Oct 26, 2024
e44b6ab
ci: use empty password for MySQL
phanhongan Oct 26, 2024
0a0e048
ci: remove MYSQL_USER="root" if using MYSQL_ALLOW_EMPTY_PASSWORD
phanhongan Oct 26, 2024
35063d0
ci: run db migrations before testing
phanhongan Oct 26, 2024
7cdbeae
build: add alembic
phanhongan Oct 26, 2024
54135fe
test: add alembic
phanhongan Oct 26, 2024
ff1df64
ci: install alembic
phanhongan Oct 26, 2024
d042cf5
ci: init alembic
phanhongan Oct 26, 2024
e6e6426
ci: install MySQL driver
phanhongan Oct 26, 2024
a2edf10
ci: use pymysql when running db migration
phanhongan Oct 26, 2024
cff10f6
ci: no password in db connection
phanhongan Oct 26, 2024
ef5aa89
ci: add alembic.ini
phanhongan Oct 27, 2024
aaba05d
fix: put alembic.ini in `openssa`
phanhongan Oct 27, 2024
3ea10b7
fix: put alembic.ini in root
phanhongan Oct 27, 2024
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
Prev Previous commit
Next Next commit
make file for making example table and data
  • Loading branch information
aran-nakayama committed Oct 1, 2024
commit 001f6d512c29eb3574ba213129d39a4ff70dd245
118 changes: 118 additions & 0 deletions examples/use-rdb-resource/make_example_table_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from sqlalchemy import Column, Integer, String, Date, create_engine, inspect
from sqlalchemy.orm import sessionmaker, declarative_base
from faker import Faker
import random
import yaml

# ベースモデルを定義
Base = declarative_base()

# sales_data テーブルの定義
class SalesData(Base):
__tablename__ = 'sales_data'

sale_id = Column(Integer, primary_key=True, autoincrement=True)
product_id = Column(Integer)
product_name = Column(String(255))
sale_date = Column(Date)
region = Column(String(255))

# データベースクラス
class MySQLDatabase:
def __init__(self, config_path):
self.config_path = config_path
self.config = self.load_config()
self.engine = self.create_engine()
self.Session = sessionmaker(bind=self.engine)

def load_config(self):
with open(self.config_path, 'r') as file:
return yaml.safe_load(file)['database']['mysql']

def create_engine(self):
username = self.config['username']
password = self.config['password']
host = self.config['host']
port = self.config['port']
database = self.config['database']
connection_string = f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}'
return create_engine(connection_string)

def create_tables(self):
Base.metadata.create_all(self.engine)

def drop_table(self, table_class):
inspector = inspect(self.engine)
if inspector.has_table(table_class.__tablename__):
table_class.__table__.drop(self.engine)

def get_session(self):
return self.Session()


# Fakeデータ生成用のFakerを初期化
fake = Faker()

# ランダムシードを設定(例: 42)
seed_value = 42
random.seed(seed_value)
Faker.seed(seed_value)

# 製品リストを作成
products = [
{"id": 101, "name": "Smartwatch", "price": 150.00},
{"id": 102, "name": "Laptop", "price": 1200.00},
{"id": 103, "name": "Smartphone", "price": 800.00},
{"id": 104, "name": "Tablet", "price": 400.00},
{"id": 105, "name": "Headphones", "price": 100.00}
]

# 地域リストを作成
regions = ["North America", "Europe", "Asia", "South America", "Africa"]

# データを生成して挿入
def generate_sales_data(session, num_records):
sales_data_list = []
for _ in range(num_records):
# ランダムに製品、価格、地域を選択
product = random.choice(products)
region = random.choice(regions)

# ランダムな販売日を生成(過去1年の範囲)
sale_date = fake.date_between(start_date='-1y', end_date='today')

# SalesData インスタンスを作成
sales_data = SalesData(
product_id=product["id"],
product_name=product["name"],
sale_date=sale_date,
region=region
)
sales_data_list.append(sales_data)

# 一括挿入
session.bulk_save_objects(sales_data_list)
session.commit()


# MySQLDatabaseクラスを使用して、テーブル作成とデータ挿入を実行
if __name__ == "__main__":
# 設定ファイルのパス
config_path = 'db_config.yaml'

# MySQLDatabaseインスタンスを初期化
db = MySQLDatabase(config_path)

# 既存のsales_dataテーブルを削除
db.drop_table(SalesData)

# テーブルを作成
db.create_tables()

# セッションを取得
session = db.get_session()

# 20000件のデータを生成
generate_sales_data(session, 20000)

print("20000件のデータがsales_dataテーブルに作成されました。")
1 change: 1 addition & 0 deletions examples/use-rdb-resource/pyproject.toml
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ pymysql = "^1.1.1"
kaleido = "0.2.1"
vanna = "^0.7.3"
chromadb = "^0.5.11"
faker = "^30.1.0"


[build-system]
5 changes: 0 additions & 5 deletions openssa/core/resource/db.py
Original file line number Diff line number Diff line change
@@ -43,11 +43,6 @@ def create_engine(self):
def get_session(self):
return self.Session()

def get_events(self):
session = self.get_session()
result = session.execute(text("SELECT * FROM items")) # TODO: use vanna ai later for the query
return result


@global_register
@dataclass