240717班级,工业化控制系统,煤矿相关行业,昆仑系统
lzz
2024-11-07 edd4466fdad20fcf50a7c30569ba7ce11f262914
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include "LogSearch.h"
#include <iostream>
#include "MysqlConn.h"
using namespace std;
LogSearch::LogSearch()
{
}
 
 
LogSearch::~LogSearch()
{
}
 
bool LogSearch::searchLogBusiness(SOCKET client, LogQueryReq * req)
{
    //// ³õʼ»¯ SQL ²éѯÓï¾ä
 
    string sql = "SELECT * FROM parsed_logs WHERE";
 
    if (*(req->startTime) && *(req->endTime)) {
        sql += " time_stamp BETWEEN '" + string(req->startTime) + "' AND '" + string(req->endTime) + "'";
    }
 
    if (*(req->level)) {
        sql += " level LIKE '%" + string(req->level) + "%'";
    }
 
    if (*(req->deviceId)) {
        sql += " device_id LIKE '%" + string(req->deviceId) + "%'";
    }
 
    if ((*req->content)) {
        sql += " content LIKE '%" + string(req->content) + "%'";
    }
 
    // ´òÓ¡¹¹½¨µÄ SQL Óï¾ä£¬·½±ãµ÷ÊÔ
    cout << "SQL: " << sql << endl;
 
    // ÉèÖàMySQL Á¬½Ó²¢Ö´Ðвéѯ
 
 
        MysqlConn conn;
        vector<vector<string>> result;
        bool querySuccess = conn.query(sql, result);
        if (!querySuccess) {
            cout << "²éѯʧ°Ü" << endl;
            return false;
        }
 
        int len = sizeof(LogQueryRes) + result.size() * sizeof(ParsedLog);  // ¶¯Ì¬¼ÆËãÊý¾Ý³¤¶È
        LogQueryRes* res = (LogQueryRes*)malloc(len);  // Îª LogQueryRes ½á¹¹Ìå¼°ÆäÈáÐÔÊý×é·ÖÅäÄÚ´æ
 
                                                       // ³õʼ»¯½á¹¹Ìå
        res->head.type = LOGSEARCH_RES;
        res->head.len = len;
        res->status = (result.empty()) ? 0 : 1; // Èç¹û½á¹ûΪ¿Õ£¬ÉèÖÃ״̬Ϊ 0
 
                                                // ½«²éѯ½á¹ûÌî³äµ½ parsedLog Êý×éÖÐ
        for (const auto& row : result) {
            cout << "ÐУº" << row.size() << endl;
            for (const auto& item : row) {
                cout << item << " ";
 
            }
        }
            cout << endl;
        for (size_t i = 0; i < result.size(); ++i) {
            const auto& row = result[i];
            ParsedLog parlog;
            if (row.size() >= 9) {
                parlog.id = stoi(row[0]);
                strncpy(parlog.timeStamp, row[1].c_str(), sizeof(parlog.timeStamp));
                strncpy(parlog.deviceId, row[2].c_str(), sizeof(parlog.deviceId));
                strncpy(parlog.level, row[3].c_str(), sizeof(parlog.level));
                strncpy(parlog.content, row[4].c_str(), sizeof(parlog.content));
                strncpy(parlog.userId, row[5].c_str(), sizeof(parlog.userId));
                strncpy(parlog.fileName, row[6].c_str(), sizeof(parlog.fileName));
                parlog.problemLine = stoi(row[7]);
                strncpy(parlog.functionName, row[8].c_str(), sizeof(parlog.functionName));
                res->parsedLog[i] = parlog;  // Ìî³äÊý¾Ý
            }
        }
 
        // ·¢ËÍÏìÓ¦
        cout << __FUNCTION__ << send(client, (char*)res, res->head.len, 0) << endl;
 
        // ÊÍ·ÅÄÚ´æ
        free(res);  // ÊÍ·Å·ÖÅäµÄÄÚ´æ
 
}