import statistics
from db06 import DBAccess

#分析機能画面作成
def makepage_analyze():

  #色設定
  POSI_BG_COLOR = '#fff2f2'
  POSI_COLOR    = '#e65050'
  NEGA_BG_COLOR = '#fafafa'
  NEGA_COLOR    = '#3d3838'

  #評価の中央値
  boundary = statistics.median([1,2,3,4,5,6])

  #埋め込み：集計テーブル
  total_list_content = ''
  for res in DBAccess.get_record_totalization():
    # 色変更用変数の設定
    tr_bg_color = POSI_BG_COLOR if res[1] >= boundary else NEGA_BG_COLOR
    tr_color   = POSI_COLOR    if res[1] >= boundary else NEGA_COLOR

    total_list_content += '''
      <tr style="background-color:{0};color:{1};">
          <td>{2}</td>
          <td class="analysis-r">{3}</td>
          <td class="analysis-r">{4}</td>
      </tr>
    '''
    total_list_content = total_list_content.format(tr_bg_color ,tr_color, res[0], res[1], res[2])

  #埋め込み：集計グラフ
  chart_datasets = ''
  chart_config = ''
  chart_doughnut = ''

  #HTMLファイルを開く
  with open('sub_analyze.html', mode='r', encoding='utf-8') as f:
    file = f.read()

  #<body>内容を出力
  html_Text = file.format( 
    embed_total_list = total_list_content,
    embed_chart_script_01 = chart_datasets,
    embed_chart_script_02 = chart_config,
    embed_chart_script_03 = chart_doughnut
  )

  return html_Text