#include "analysisbyrediscache.h"
|
#include "ui_analysisbyrediscache.h"
|
#include <QDateTime>
|
#include <QDebug>
|
#include <QElapsedTimer>
|
|
AnalysisByRedisCache::AnalysisByRedisCache(QWidget *parent) :
|
QMainWindow(parent),
|
ui(new Ui::AnalysisByRedisCache)
|
{
|
ui->setupUi(this);
|
|
ui->dateEdit->setDate(QDate::currentDate().addMonths(-1));
|
|
if(m_dbCache.initMySQL("127.0.0.1",3306,"root","root","stock_plan")){
|
qDebug()<<"mysql conn ok";
|
}else{
|
qDebug()<<"mysql conn fail";
|
}
|
|
if(m_dbCache.initRedis("127.0.0.1",6379)){
|
qDebug()<<"redis conn ok";
|
}else{
|
qDebug()<<"redis conn fail";
|
}
|
|
m_model = new QStandardItemModel(this);
|
ui->tableView->setModel(m_model);
|
|
QStringList labels;
|
labels<<"股票名字"<<"市值"<<"收盘价"<<"涨跌幅"<<"排名"<<"交易时间"<<"排名变化";
|
m_model->setHorizontalHeaderLabels(labels);
|
}
|
|
AnalysisByRedisCache::~AnalysisByRedisCache()
|
{
|
delete ui;
|
}
|
|
void AnalysisByRedisCache::on_pushButton_loadDataSQLRedis_clicked()
|
{
|
QString begin_date = ui->dateEdit->text();
|
QString limit_str = ui->comboBox->currentText();
|
QString sql = QString("select * from stock_day_info where time_trade > '%1' limit %2;").arg(begin_date).arg(limit_str);
|
qDebug()<<"sql:"<<sql;
|
QElapsedTimer timer;
|
timer.start();
|
|
auto result = m_dbCache.query(sql);
|
|
qDebug() << "Query executed in" << timer.elapsed() << "ms";
|
qDebug() << "Retrieved" << result.size() << "rows";
|
|
// 第二次查询应该从缓存获取
|
timer.restart();
|
result = m_dbCache.query(sql);
|
qDebug() << "Cached query executed in" << timer.elapsed() << "ms";
|
m_result = result;
|
}
|
|
// 滑动窗口计算排名变化
|
void AnalysisByRedisCache::calcWindowRank(int start)
|
{
|
|
// 滑动窗口排名,6个要2+的前100
|
QVector<int> ranks;
|
for(int i=start;i<m_model->rowCount();++i){
|
ranks.append(m_model->item(i,4)->text().toInt());
|
if(i >= 6+start){
|
// 先统计排名计数
|
int cntBig=0, cntSmall=0;
|
for(int j=0;j<ranks.size();++j){
|
if(ranks.at(j) > 100){
|
cntBig++;
|
}else{
|
cntSmall++;
|
}
|
}
|
// 统计对比
|
if(cntSmall == 2 && ranks.back() <= 100){
|
// 将当前6个元素定为符合的窗口,背景色设置为紫色
|
for(int k=0;k<6;++k){
|
m_model->item(i-k,4)->setData(QColor("red"),Qt::BackgroundColorRole);
|
}
|
}
|
|
// 清除窗口第一个元素
|
ranks.pop_front();
|
}
|
|
}
|
|
}
|
void AnalysisByRedisCache::searchAndAnalysisData(QString name)
|
{
|
QElapsedTimer timer;
|
timer.start();
|
qDebug()<<"m_result size:"<<m_result.size()<<name;
|
|
int rank_val = 0;
|
int cnt = 0;
|
for(auto it:m_result){
|
if(it["name"].toString() == name){
|
// qDebug()<<it["name"].toString()<<it["close"].toString()<<it["amount_rank"].toString()<<it["time_trade"].toString();
|
cnt++;
|
QList<QStandardItem*> items;
|
items.append(new QStandardItem(it["name"].toString()));
|
double market_capital = it["market_capital"].toDouble() / 100000000;
|
items.append(new QStandardItem(QString::number(market_capital)));
|
items.append(new QStandardItem(it["close"].toString()));
|
|
QStandardItem *percentItem = new QStandardItem(it["percent"].toString());
|
if(percentItem->text().toDouble() > 0){
|
percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加装饰颜色为红色
|
percentItem->setData(QColor("red"),Qt::TextColorRole); // 添加文本颜色为红色
|
items.at(0)->setData(QColor("red"),Qt::TextColorRole); // 设置股票名字为红色
|
|
}else if(percentItem->text().toDouble() < 0){
|
percentItem->setData(QColor("green"),Qt::DecorationRole); // 添加装饰颜色为红色
|
percentItem->setData(QColor("green"),Qt::TextColorRole); // 设置背景颜色为绿色
|
items.at(0)->setData(QColor("green"),Qt::TextColorRole); // 设置股票名字为绿色
|
}
|
items.append(percentItem);
|
//items.append(new QStandardItem(it["percent"].toString()));
|
items.append(new QStandardItem(it["amount_rank"].toString()));
|
items.append(new QStandardItem(it["time_trade"].toString()));
|
QString amount_rank = it["amount_rank"].toString();
|
if(rank_val != 0){
|
double rank_rate = 1 - (amount_rank.toDouble()/rank_val);
|
//items.append(new QStandardItem(QString::number(rank_rate)));
|
rank_val = amount_rank.toInt();
|
QStandardItem * amount_rank_item = new QStandardItem(QString::number(rank_rate));
|
if(rank_rate > 0.9){
|
amount_rank_item->setData(QColor("red"),Qt::BackgroundColorRole);
|
}
|
items.append(amount_rank_item);
|
|
}
|
if(rank_val == 0 && amount_rank.toInt() != 0){
|
rank_val = amount_rank.toInt();
|
}
|
|
m_model->appendRow(items);
|
}
|
|
}
|
qDebug()<<"cnt="<<cnt;
|
qDebug() << "Query executed in" << timer.elapsed() << "ms";
|
qDebug()<<"滑动窗口排名-cnt:"<<m_model->rowCount()<<cnt<<"start:"<<m_model->rowCount() - cnt + 1;
|
calcWindowRank(m_model->rowCount() - cnt + 1);
|
}
|
void AnalysisByRedisCache::on_pushButton_analysisByRedis_clicked()
|
{
|
// 单个分析
|
QString name = ui->lineEdit_name->text();
|
searchAndAnalysisData(name);
|
|
}
|
|
void AnalysisByRedisCache::on_pushButton_batchAnalysis_clicked()
|
{
|
|
// 批量分析
|
// 重设模型的行数为0,清空一下数据
|
m_model->setRowCount(0);
|
|
int batch_num = ui->comboBox_batchNum->currentText().toInt(); // 批量分析个股的数量
|
// 拿出个股交易额排名的前N名,进行缓存分析
|
emit getStockNamesSignal(batch_num);
|
|
}
|