# -*- coding: utf-8 -*-
import mao, sys
from PyQt5.QtChart import QDateTimeAxis, QValueAxis, QSplineSeries, QChartView
from PyQt5.QtCore import QDateTime, Qt, QTimer
from PyQt5 import QtCore, QtGui, QtWidgets
import re
from PyQt5.QtGui import QPainter
import requests, json, time, datetime
class Ui_Form(QtWidgets.QMainWindow, QChartView):
def __init__(self):
super().__init__()
self.tes = []
self.costs = []
self.names = []
self.names1 = []
self.names2 = []
self.ns = []
self.limitminute = 60 # 设置显示多少分钟内的活动
self.maxspeed = 600 # 预设y轴最大值
self.headers = [
]
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(960, 558)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/新前缀/mao.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit.setObjectName("plainTextEdit")
self.horizontalLayout.addWidget(self.plainTextEdit, 0, QtCore.Qt.AlignLeft)
self.charView = QChartView(Form)
self.charView.setRenderHint(QPainter.Antialiasing)
self.charView.setObjectName("chart")
self.horizontalLayout.addWidget(self.charView)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.timer_init()
self.main()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "数据监控"))
self.plainTextEdit.setPlaceholderText(_translate("Form", "账号,密码,token"))
def main(self):
self.dtaxisX = QDateTimeAxis()
self.vlaxisY = QValueAxis()
self.dtaxisX.setMin(QDateTime.currentDateTime().addSecs(-self.limitminute * 60))
self.dtaxisX.setMax(QDateTime.currentDateTime().addSecs(0))
self.vlaxisY.setMin(0)
self.vlaxisY.setMax(self.maxspeed)
self.dtaxisX.setFormat("hh:mm")
self.dtaxisX.setTickCount(10)
self.vlaxisY.setTickCount(10)
self.dtaxisX.setTitleText("时间")
self.vlaxisY.setTitleText("消费")
self.vlaxisY.setGridLineVisible(True)
self.vlaxisY.setGridLineColor(Qt.gray)
self.dtaxisX.setGridLineVisible(True)
self.dtaxisX.setGridLineColor(Qt.gray)
self.charView.chart().setAxisX(self.dtaxisX)
self.charView.chart().setAxisY(self.vlaxisY)
def ds(self):
ts = self.plainTextEdit.toPlainText().split()
try:
for t in ts:
t = re.split(',|,', t)
hss = {
"token": t[2],
"username": t[0],
"password": t[1],
"action": "API-PYTHON",
}
if hss in self.headers:
pass
else:
self.headers.append(hss)
except:
pass
for he in self.headers:
if he['username'] in self.names:
pass
else:
self.ns.append(QSplineSeries())
self.names.append(he['username'])
for n, he in zip(self.ns, self.headers):
if he['username'] in self.names2:
pass
else:
n.setName(he['username'])
self.charView.chart().addSeries(n)
self.names1.append(he['username'])
for n, he in zip(self.ns, self.headers):
if he['username'] in self.names2:
pass
else:
n.attachAxis(self.dtaxisX)
n.attachAxis(self.vlaxisY)
self.names2.append(he['username'])
bjtime = QDateTime.currentDateTime()
self.dtaxisX.setMin(bjtime.addSecs(-self.limitminute * 60))
self.dtaxisX.setMax(bjtime.addSecs(0))
sts = int(time.time()) * 1000
x = []
for he, n in zip(self.headers, self.ns):
url = "https://api.baidu.com/json/sms/service/OpenApiReportService/getReportData"
data = {
"header": he,
"body": {
"reportType": 2208157,
"startDate": datetime.datetime.now().strftime("%Y-%m-%d"),
"endDate": datetime.datetime.now().strftime("%Y-%m-%d"),
"timeUnit": "DAY",
"columns": ["date", "userName", "impression", "click", "cost"],
"sorts": [],
"filters": [],
"startRow": 0,
"rowCount": 200,
"needSum": False,
},
}
data = json.dumps(data)
req = requests.post(url=url, data=data)
req = req.json()
try:
cost = float(req["body"]["data"][0]["rows"][0]["cost"])
x.append(cost)
n.append(sts, cost)
except:
pass
if x:
self.vlaxisY.setMax(max(x) * 1.2)
def timer_init(self):
self.timer = QTimer(self)
self.timer.timeout.connect(self.ds)
self.timer.start(60000)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())