import csv,datetime,collections,json,math
import matplotlib; matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
from matplotlib.patches import Patch
for f in ['/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc','/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc']: fm.fontManager.addfont(f)
plt.rcParams.update({'font.family':'Noto Sans CJK JP','font.size':10,'axes.edgecolor':'#c9c8c3','axes.labelcolor':'#52514e',
 'xtick.color':'#52514e','ytick.color':'#52514e','text.color':'#0b0b0b','figure.facecolor':'#fcfcfb','axes.facecolor':'#fcfcfb',
 'savefig.facecolor':'#fcfcfb','axes.spines.top':False,'axes.spines.right':False})
BLUE,ORANGE,AQUA,YELLOW='#2a78d6','#eb6834','#1baf7a','#eda100'; MUTED='#8b8a85'; INK2='#52514e'
exec(open('analyze.py').read())
# 並び：Googlebot を最上位、以降は SEO から見た重要度の順（検索 → AI 検索 → AI 学習など → SEO ツール）
ORDER=[('Googlebot','googlebot'),('Bingbot','bingbot'),('GoogleOther','googleother'),('OAI-SearchBot','oai-searchbot'),
       ('ClaudeBot','claudebot'),('GPTBot','gptbot'),('meta-externalagent','meta-externalagent'),('Amazonbot','amazonbot'),
       ('PetalBot','petalbot'),('Bytespider','bytespider'),('MJ12bot（SEO）','other:MJ12bot'),('AhrefsBot（SEO）','ahrefsbot')]
CATS=[('robots.txt',BLUE),('sitemap.xml',ORANGE),('トップ・区などのハブ',AQUA),('丁目ページ',YELLOW)]
def cat(p):
    if p=='/robots.txt': return 0
    if p.startswith('/sitemap'): return 1
    if ischome(p): return 3
    return 2
fig,ax=plt.subplots(figsize=(9,6.4))
for i,(lab,b) in enumerate(ORDER):
    c=[0,0,0,0]
    for r in rows:
        if r['n']==b: c[cat(r['path'])]+=1
    tot=sum(c); left=0
    for k,(cl,col) in enumerate(CATS):
        w=c[k]/tot*100
        if w: ax.barh(i,w,left=left,height=0.62,color=col,edgecolor='#fcfcfb',linewidth=1)
        if w>=9: ax.text(left+w/2,i,str(c[k]),ha='center',va='center',fontsize=8.5,color='#0b0b0b')
        left+=w
    ax.text(101.5,i,f'計 {tot}',va='center',fontsize=9,color=INK2)
ax.set_yticks(range(len(ORDER))); ax.set_yticklabels([o[0] for o in ORDER]); ax.invert_yaxis()
ax.set_xlim(0,100); ax.set_xticks([0,25,50,75,100]); ax.set_xticklabels(['0%','25%','50%','75%','100%'])
ax.tick_params(length=0); ax.spines['left'].set_visible(False); ax.spines['bottom'].set_visible(False)
ax.legend(handles=[Patch(color=c,label=l) for l,c in CATS],loc='upper center',bbox_to_anchor=(0.5,-0.07),ncol=4,frameon=False,fontsize=9)
fig.suptitle('クローラごとの、リクエストの中身',x=0.02,ha='left',fontsize=13)
fig.text(0.02,0.915,'9/13〜10/13 の全リクエストの内訳。棒の中の数字は件数（9%未満は省略）。上から SEO で重要な順',fontsize=8.5,color=INK2,ha='left')
fig.text(0.02,0.01,'herahin.net のアクセスログより作成',fontsize=8,color=MUTED,ha='left')
plt.tight_layout(rect=(0,0.03,0.95,0.9))
fig.savefig('fig/fig6_request_mix.png',dpi=200)
