HM-SPMS/product/src/tools/workBench/AppMngForm.cpp
2025-03-13 15:21:23 +08:00

385 lines
13 KiB
C++

#include "AppMngForm.h"
#include "ui_AppMngForm.h"
#include "DataMng.h"
#include "../include/sys/sys_login_api/CLoginDlg.h"
#include <QContextMenuEvent>
#include <QMenu>
AppMngForm::AppMngForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::AppMngForm)
{
ui->setupUi(this);
ui->appTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
initAppTree();
connect(ui->appTreeWidget,&QTreeWidget::customContextMenuRequested,this,&AppMngForm::customContext);
connect(ui->appTreeWidget,&QTreeWidget::itemClicked,this,&AppMngForm::appItemClicked);
}
AppMngForm::~AppMngForm()
{
delete ui;
}
void AppMngForm::updateAppTree()
{
std::vector<SApp> runAppVec = DataMng::instance()->getRunAppInfo();
for(size_t i=0;i<runAppVec.size();++i)
{
int topNum = ui->appTreeWidget->topLevelItemCount();
for(int topIndex(0);topIndex<topNum;topIndex++)
{
QTreeWidgetItem *item = ui->appTreeWidget->topLevelItem(topIndex);
if(item->text(0).toStdString() == runAppVec[i].m_nodeName)
{
updateItem(runAppVec[i],item);
break;
}
}
}
ui->appTreeWidget->expandAll();
}
void AppMngForm::customContext(const QPoint &pos)
{
QModelIndex index = ui->appTreeWidget->indexAt(pos);
if(!index.isValid())
{
ui->appTreeWidget->clearSelection();
if(!DataMng::instance()->getSysStatus())
{
QMenu menu(this);
QAction * selectAction = menu.addAction(tr("刷新"));
connect(selectAction, &QAction::triggered, [=](){
emit sigReloadAppAndProc();
});
menu.exec(QCursor::pos());
}
return ;
}
//右键应用
QTreeWidgetItem *item=ui->appTreeWidget->currentItem();
//空指针判断
if (!item)
{
return;
}
//判断是否右键应用
if(item->data(0,Qt::UserRole+5).toInt() == EN_TREE_NODE_IS_APP)
{
//判断是否右键Base应用
if(item->data(0,Qt::UserRole).toInt() == CN_AppId_BASE)
{
return ;
}
//判断是否本节点
if(item->data(0,Qt::UserRole+6).toString() != DataMng::instance()->getHostName())
{
return ;
}
//判断当前是否可以右键操作(防止并行操作)
if(!DataMng::instance()->getAllowedStatus())
{
return ;
}
QMenu menu(this);
QAction * appStart = menu.addAction(tr("启动"));
QAction * appStop = menu.addAction(tr("停止"));
QAction * appRestart = menu.addAction(tr("重启"));
QAction * appUp = menu.addAction(tr("升主"));
QAction * appDown = menu.addAction(tr("降备"));
connect(appStart, &QAction::triggered, [=](){
emit sigStartApp(item->data(0,Qt::UserRole+1).toString());
});
connect(appStop, &QAction::triggered, [=](){
CLoginDlg dlg(this);
QString perm ="FUNC_NOM_SYSTEM_EXIT";
dlg.setCheckPerm(perm);
dlg.setOkButtonText(tr("验证"));
dlg.setTitle(tr("权限验证"));
int res = dlg.exec();
if(res == QDialog::Accepted)
{
emit sigStopApp(item->data(0,Qt::UserRole+1).toString());
}
});
connect(appRestart, &QAction::triggered, [=](){
CLoginDlg dlg(this);
QString perm ="FUNC_NOM_SYSTEM_EXIT";
dlg.setCheckPerm(perm);
dlg.setOkButtonText(tr("验证"));
dlg.setTitle(tr("权限验证"));
int res = dlg.exec();
if(res == QDialog::Accepted)
{
emit sigRestartApp(item->data(0,Qt::UserRole+1).toString());
}
});
if(DataMng::instance()->getBaseStatus()) //基础应用处于启动状态
{
//获取本应用状态
int appStatus = item->data(1,Qt::UserRole).toInt();
if(appStatus == EN_APP_RUNNING)
{
appStart->setEnabled(false);
appStop->setEnabled(true);
appRestart->setEnabled(true);
}else if(appStatus == EN_APP_STOP)
{
if(checkAppStatus(item->data(0,Qt::UserRole).toInt())== EN_APP_HALF)//半运行状态
{
appStart->setEnabled(false);
appStop->setEnabled(true);
appRestart->setEnabled(true);
}else
{
appStart->setEnabled(true);
appStop->setEnabled(false);
appRestart->setEnabled(false);
}
}else if(appStatus == EN_APP_UNKNOW)
{
appStart->setEnabled(false);
appStop->setEnabled(false);
appRestart->setEnabled(false);
}
}else //基础应用处于停止状态
{
appStart->setEnabled(false);
appStop->setEnabled(false);
appRestart->setEnabled(false);
}
appUp->setEnabled(false);
appDown->setEnabled(false);
menu.exec(QCursor::pos());
}
}
void AppMngForm::appItemClicked(QTreeWidgetItem *item, int column)
{
if(!item)
{
return ;
}
if(column != 3)
{
return ;
}
//判断是否点击应用
if(item->data(0,Qt::UserRole+5).toInt() == EN_TREE_NODE_IS_APP)
{
//判断是否点击Base应用
if(item->data(0,Qt::UserRole).toInt() == CN_AppId_BASE)
{
return ;
}
//判断是否本节点
if(item->data(0,Qt::UserRole+6).toString() != DataMng::instance()->getHostName())
{
return ;
}
//判断当前是否可以点击操作(防止并行操作)
if(!DataMng::instance()->getAllowedStatus())
{
return ;
}
if(!DataMng::instance()->getBaseStatus()) //基础应用处于启动状态
{
return ;
}
//获取本应用状态
int appStatus = item->data(1,Qt::UserRole).toInt();
if(appStatus == EN_APP_RUNNING)
{
CLoginDlg dlg(this);
QString perm ="FUNC_NOM_SYSTEM_EXIT";
dlg.setCheckPerm(perm);
dlg.setOkButtonText(tr("验证"));
dlg.setTitle(tr("权限验证"));
int res = dlg.exec();
if(res == QDialog::Accepted)
{
emit sigRestartApp(item->data(0,Qt::UserRole+1).toString());
}
}else if(appStatus == EN_APP_STOP)
{
if(checkAppStatus(item->data(0,Qt::UserRole).toInt())== EN_APP_HALF)//半运行状态
{
CLoginDlg dlg(this);
QString perm ="FUNC_NOM_SYSTEM_EXIT";
dlg.setCheckPerm(perm);
dlg.setOkButtonText(tr("验证"));
dlg.setTitle(tr("权限验证"));
int res = dlg.exec();
if(res == QDialog::Accepted)
{
emit sigRestartApp(item->data(0,Qt::UserRole+1).toString());
}
}else
{
emit sigStartApp(item->data(0,Qt::UserRole+1).toString());
}
}
}
}
void AppMngForm::initAppTree()
{
QMap<QString, QMap<int, SApp> > deployInfo = DataMng::instance()->getDeployInfo();
ui->appTreeWidget->clear();
ui->appTreeWidget->setColumnWidth(0,150);
ui->appTreeWidget->setColumnCount(4);
QMap<QString,QMap<int ,SApp> >::iterator pos = deployInfo.begin();
while (pos != deployInfo.end()) {
QTreeWidgetItem *nodeItem = new QTreeWidgetItem(ui->appTreeWidget);
nodeItem->setText(0,pos.key());
nodeItem->setData(0,Qt::UserRole+5,EN_TREE_NODE_IS_NODE);
nodeItem->setTextColor(0 , "#003764");
ui->appTreeWidget->addTopLevelItem(nodeItem);
QMap<int ,SApp> appMap = pos.value();
QMap<int, SApp>::iterator it = appMap.begin();
while (it != appMap.end()) {
QTreeWidgetItem *appItem = new QTreeWidgetItem();
appItem->setTextColor(0 , "#003764");
appItem->setText(0,QString::fromStdString((*it).m_desc));
appItem->setData(0,Qt::UserRole,(*it).m_appId);
appItem->setData(0,Qt::UserRole+1,QString::fromStdString((*it).m_appName));
appItem->setData(0,Qt::UserRole+5,EN_TREE_NODE_IS_APP);
appItem->setData(0,Qt::UserRole+6,pos.key());
if((*it).m_appId != CN_AppId_BASE && (*it).m_nodeName == DataMng::instance()->getHostName().toStdString())
{
appItem->setIcon(3,QIcon(":/images/restart.png"));
}
if((*it).m_isActive == 1)
{
appItem->setText(1,tr("运行中"));
appItem->setTextColor(1,"#058D02");
appItem->setData(1,Qt::UserRole,EN_APP_RUNNING);
if((*it).m_isMaster == 0 && (*it).m_isSlave==0)
{
appItem->setText(2, tr("异常"));
appItem->setTextColor(2,"#FF0000");
}
else if((*it).m_isMaster == 1 && (*it).m_isSlave==0)
{
appItem->setText(2, tr(""));
appItem->setTextColor(2,"#058D02");
}
else if((*it).m_isMaster == 0 && (*it).m_isSlave==1)
{
appItem->setText(2, tr(""));
appItem->setTextColor(2,"#058D02");
}
else
{
appItem->setText(2, tr("异常"));
appItem->setTextColor(2,"#FF0000");
}
}else if((*it).m_isActive == 0)
{
appItem->setText(1,tr("已停止"));
appItem->setTextColor(1,"#FF0000");
appItem->setData(1,Qt::UserRole,EN_APP_STOP);
appItem->setText(2, "-");
appItem->setTextColor(2,Qt::black);
}else if((*it).m_isActive == 2)
{
appItem->setText(1,tr("未知"));
appItem->setTextColor(1,Qt::black);
appItem->setData(1,Qt::UserRole,EN_APP_UNKNOW);
appItem->setText(2, "-");
appItem->setTextColor(2,Qt::black);
}
nodeItem->addChild(appItem);
it++;
}
pos++;
}
ui->appTreeWidget->expandAll();
return;
}
void AppMngForm::updateItem(const SApp &app, QTreeWidgetItem *item)
{
int num = item->childCount();
for(int i =0;i<num;i++)
{
if(item->child(i)->data(0,Qt::UserRole).toInt() == app.m_appId)
{
if(app.m_isActive == 1)
{
item->child(i)->setText(1,tr("运行中"));
item->child(i)->setTextColor(1,"#058D02");
item->child(i)->setData(1,Qt::UserRole,EN_APP_RUNNING);
if(app.m_isMaster == 0 && app.m_isSlave==0)
{
item->child(i)->setText(2, tr("异常"));
item->child(i)->setTextColor(2,"#FF0000");
}
else if(app.m_isMaster == 1 && app.m_isSlave==0)
{
item->child(i)->setText(2, tr(""));
item->child(i)->setTextColor(2,"#058D02");
}
else if(app.m_isMaster == 0 && app.m_isSlave==1)
{
item->child(i)->setText(2, tr(""));
item->child(i)->setTextColor(2,"#058D02");
}
else
{
item->child(i)->setText(2, tr("异常"));
item->child(i)->setTextColor(2,"#FF0000");
}
}else if(app.m_isActive == 0)
{
item->child(i)->setText(1,tr("已停止"));
item->child(i)->setTextColor(1,"#FF0000");
item->child(i)->setData(1,Qt::UserRole,EN_APP_STOP);
item->child(i)->setText(2, "-");
item->child(i)->setTextColor(2,Qt::black);
}else if(app.m_isActive == 2)
{
item->child(i)->setText(1,tr("未知"));
item->child(i)->setTextColor(1,Qt::black);
item->child(i)->setData(1,Qt::UserRole,EN_APP_UNKNOW);
item->child(i)->setText(2, "-");
item->child(i)->setTextColor(2,Qt::black);
}
break;
}
}
}
void AppMngForm::blockAppSignals(bool b)
{
ui->appTreeWidget->blockSignals(b);
}
int AppMngForm::checkAppStatus(const int appId)
{
float disk = 0.0;
std::vector<SProcess> procVec;
DataMng::instance()->getRunProcInfo(procVec,disk);
foreach (SProcess var, procVec)
{
if( appId == var.m_appId && EN_PROC_RUNNING == var.m_isStartUp)
{
return EN_APP_HALF;
}
}
return EN_APP_STOP;
}