🚀 Pythonスクリプトによるデータ分析
このPythonスクリプトは、データベースからデータを取得し、そのデータを分析してグラフ化するためのものです。具体的には、サーバーデバイスのIDと所属グループ番号を取得し、それを元に各サーバーの地域分布を分析し、グラフ化します。
🚀 クイックスタート
データ取得
まず、db_alpha
データベースからサーバーデバイスのIDと所属グループ番号を取得します。
select ex.device_id as id, ex.group_no
from service_config_alpha ex
where ex.is_active = 1
and ex.status = 1
union all
select dv.id, dv.group_no
from device_config_beta dv
where dv.is_active = 1
and dv.status = 1
このクエリで取得したサーバーIDを使って、db_beta
データベースから各サーバーの地域分布を取得します。
select loc.region,
count(1)
from connection_log cnt
left join device_location loc on loc.id = cnt.client_id
where server_id in (
)
group by loc.region
ただし、取得した結果には region
が空の場合があります。これは connection_log
テーブルの client_id
が db_beta
の device_location
テーブルに存在しないためです。そのため、これらの client_id
を db_alpha
の device_location
テーブルで再検索し、結果を結合します。
select loc.region,
count(1)
from device_location loc
where loc.id in (
)
group by loc.region
結果の結合と分析
最後に、上記の2つの結果を結合して、単体のサーバーの地域分布を得ます。そして、地域分布のパーセンテージを計算し、変動係数を算出します。これらの結果を pyecharts
を使ってグラフ化します。
import pymysql
import pandas as pd
from pyecharts.charts import Bar
from pyecharts import options as opts
conn_alpha = pymysql.connect(host='localhost', user='user', password='password', database='db_alpha')
query1 = """
select ex.device_id as id, ex.group_no
from service_config_alpha ex
where ex.is_active = 1
and ex.status = 1
union all
select dv.id, dv.group_no
from device_config_beta dv
where dv.is_active = 1
and dv.status = 1
"""
servers = pd.read_sql(query1, conn_alpha)
conn_alpha.close()
for server_id in servers['id']:
conn_beta = pymysql.connect(host='localhost', user='user', password='password', database='db_beta')
query2 = f"""
select loc.region,
count(1)
from connection_log cnt
left join device_location loc on loc.id = cnt.client_id
where server_id = {server_id}
group by loc.region
"""
result_beta = pd.read_sql(query2, conn_beta)
conn_beta.close()
null_client_ids = result_beta[result_beta['region'].isnull()]['client_id'].tolist()
conn_alpha = pymysql.connect(host='localhost', user='user', password='password', database='db_alpha')
query3 = f"""
select loc.region,
count(1)
from device_location loc
where loc.id in ({','.join(map(str, null_client_ids))})
group by loc.region
"""
result_alpha = pd.read_sql(query3, conn_alpha)
conn_alpha.close()
combined_result = pd.concat([result_beta, result_alpha])
combined_result['percentage'] = combined_result['count(1)'] / combined_result['count(1)'].sum()
cv = combined_result['count(1)'].std() / combined_result['count(1)'].mean()
bar = (
Bar()
.add_xaxis(combined_result['region'].tolist())
.add_yaxis("地域分布パーセンテージ", combined_result['percentage'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(title=f"サーバー {server_id} の地域分布パーセンテージ"))
)
bar.render(f"server_{server_id}_region_distribution.html")
grouped_servers = servers.groupby('group_no')
for group_no, group_servers in grouped_servers:
group_result = pd.DataFrame()
for server_id in group_servers['id']:
pass
group_result['percentage'] = group_result['count(1)'] / group_result['count(1)'].sum()
group_cv = group_result['count(1)'].std() / group_result['count(1)'].mean()
average_group_cv = grouped_servers['count(1)'].std() / grouped_servers['count(1)'].mean()
bar = (
Bar()
.add_xaxis(group_result['region'].tolist())
.add_yaxis("地域分布パーセンテージ", group_result['percentage'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(title=f"グループ {group_no} の地域分布パーセンテージ"))
)
bar.render(f"group_{group_no}_region_distribution.html")
bar_cv = (
Bar()
.add_xaxis(grouped_servers.groups.keys())
.add_yaxis("平均変動係数", average_group_cv.tolist())
.set_global_opts(title_opts=opts.TitleOpts(title="各グループの平均変動係数"))
)
bar_cv.render("group_average_cv.html")
注意事項
- データベース接続情報(ホスト、ユーザー名、パスワードなど)は、実際の環境に合わせて変更してください。
- コード内のSQLクエリは、データベースのテーブル構造に合わせて調整する必要があります。