Untitled

 avatar
unknown
plain_text
2 years ago
4.5 kB
6
Indexable
import pandas as pd;
import smtplib;
import mysql.connector;
from sqlalchemy import create_engine
import urllib.parse
from datetime import datetime
import datetime
from tabulate import tabulate
import json
import codecs
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
from pretty_html_table import build_table

mydb = create_engine('mysql+mysqldb://root:%s@103.82.21.72:3306/data_kiot_viet' % urllib.parse.quote('Datcang1@'), echo = False)

query1 = "SELECT IFNULL(ROUND(SUM(duration_time)/60,2),0) a_1, COUNT(*) a_2, (SELECT COUNT(*) FROM data_kiot_viet.sys_tf_manage_duration_time WHERE status = 'Pass' AND dayid = CURDATE()) a_3 FROM data_kiot_viet.sys_tf_manage_duration_time WHERE dayid = CURDATE();"
query2 = "SELECT IFNULL(ROUND(SUM(duration_time)/60,2),0) b_1, COUNT(*) b_2, (SELECT COUNT(*) FROM data_kiot_viet.sys_bi_manage_duration_time WHERE status = 'Pass' AND dayid = CURDATE()) b_3 FROM data_kiot_viet.sys_bi_manage_duration_time WHERE dayid = CURDATE();"
        
query3 = "SELECT * FROM data_kiot_viet.sys_load_manage_duration_time WHERE DATE(dayid) = CURDATE() ORDER BY datasource, major_type, table_name,last_update_date;"
query4 = "SELECT * FROM data_kiot_viet.sys_tf_manage_duration_time WHERE dayid = CURDATE();"
query5 = "SELECT * FROM data_kiot_viet.sys_bi_manage_duration_time WHERE dayid = CURDATE();"

dfa = pd.read_sql(query1, mydb)
dfb = pd.read_sql(query2, mydb)
c = dfa.iloc[0, 0] + dfb.iloc[0, 0]
d = dfa.iloc[0, 1] + dfb.iloc[0, 1]
e = dfa.iloc[0, 2] + dfb.iloc[0, 2]

output1 = build_table(pd.read_sql(query3, mydb)
            , 'blue_dark'
            , font_size='small'
            , font_family= 'Verdana'
            )
output2 = build_table(pd.read_sql(query4, mydb)
            , 'blue_dark'
            , font_size='small'
            , font_family= 'Verdana'
            )
output3 = build_table(pd.read_sql(query5, mydb)
            , 'blue_dark'
            , font_size='small'
            , font_family= 'Verdana'
            )

me = 'bebubam2022@gmail.com'
password = 'qxtaasdbtgtmpewj'
server = 'smtp.gmail.com:587'
you = 'ha.nguyenngan57@gmail.com'
you2 = 'quangducit@bebubam.vn'

text = """
Dear Bộ phận IT - Phòng Hệ Thống,
Sau đây là báo cáo kết quả dữ liệu đầu ngày:

Quá trình xử lý dữ liệu hết {c1} phút.
Tỷ lệ Pass: {d1}% || Tỷ lệ Fail: {e1}%

Cụ thể kết quả như sau:

1) Dữ liệu từ các nguồn -> Database:
{table1}

2) Kết quả xử lý dữ liệu:
{table2}

3) Kết quả các bảng dữ liệu cho báo cáo:
{table3}

Chúc bạn một ngày làm việc hiệu quả! ^^
Trân trọng,
"""

html = """
<html>
<head>
<style> 
 table, th, td {{ border: 1px solid black; border-collapse: collapse; }}
  th, td {{ padding: 5px; }}
</style>
</head>
<body><h1 style="font-size:15px;">Dear Bộ phận IT - Phòng Hệ Thống, </h1>
<p>Sau đây là báo cáo kết quả dữ liệu đầu ngày:</p>
<p>Quá trình xử lý dữ liệu hết <b style="font-size:15px;color:DarkViolet">{c1}</b> phút.</p>
<p>Tỷ lệ Pass: <b style="font-size:15px;color:green">{d1}%</b> || Tỷ lệ Fail: <b style="font-size:15px;color:red">{e1}%</b></p>
<p>Cụ thể kết quả như sau:</p>
<p style="color:blue;"><b>1) Dữ liệu từ các nguồn -> Database:</b></p>
{table1}
<p style="color:blue;"><b>2) Kết quả xử lý dữ liệu:</b></p>
{table2}
<p style="color:blue;"><b>3) Kết quả các bảng dữ liệu cho báo cáo:</b></p>
{table3}
<p>Chúc bạn một ngày làm việc hiệu quả! ^^</p>
<p><i>Trân trọng,</i></p>
</body></html>
"""

text = text.format(table1=output1, table2=output2,table3=output3, c1=c,d1=e*100/d,e1=(d-e)*100/d,tablefmt="grid")
html = html.format(table1=output1, table2=output2,table3=output3,c1=c,d1=e*100/d,e1=(d-e)*100/d,tablefmt="html")

message = MIMEMultipart(
    "alternative", None, [MIMEText(text), MIMEText(html,'html')])

today = datetime.datetime.now()
date = today.strftime("%Y-%m-%d")

message['Subject'] = f"Báo cáo kết quả dữ liệu đầu ngày \\\ {date}"
message['From'] = me
message['To'] = you
message['To2'] = you2
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.sendmail(me, you2, message.as_string())
server.quit()
Editor is loading...