七牛日志分析

七牛日志格式

116.231.10.133 HIT 0 [07/May/2019:09:04:56 +0800] "GET http://wdl1.cache.wps.cn/per-plugin/dl/addons/pool/win-i386/wpsminisite_3.0.0.37.7z HTTP/1.1" 206 66372 "-" "-"

Qt的相关代码

int getDownloadSize(QString line) {
    int idx = line.indexOf("HTTP/1.1\"");
    if(idx > 0) {
        QString sub = line.mid(idx);
        QStringList sets = sub.split(" ");
        int size = sets.at(2).toInt();
        return size;
    }
    return 0;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString dpath("F:\\日志0508\\18");

    QDir d(dpath);
    QStringList fs = d.entryList();
    qint64 plug_total = 1;
    qint64 other_total = 1;
    for(int i = 0; i < fs.size(); i++) {
        QString f = fs.at(i);
        QFile file(dpath+"\\"+f);
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
            continue;
        }
        int linecnt = 0;
        while(!file.atEnd()) {
            QByteArray line = file.readLine();
            if(line.indexOf("wdl1.cache.wps.cn") < 0) {
                continue;
            }
            int idx = line.indexOf("win-i386");
            int size = getDownloadSize(line);
            qint64 skidx = file.pos();
            if(idx > 0) {
                plug_total += size;
            }else{
                other_total += size;
            }
            linecnt++;
            if(linecnt % 10000 == 0) {
                printf("\r\nlinecnt:%d - skidx:%lld - plug_total:%lld - other_total:%lld - ratio:%5f", linecnt, skidx, plug_total, other_total, double(plug_total) / double(other_total));
            }
        }
        printf("\r\nlinecnt:%d - plug_total:%lld - other_total:%lld - ratio:%5f", linecnt, plug_total, other_total, double(plug_total) / double(other_total));
    }
    printf("\r\nplug_total:%lld - other_total:%lld - ratio:%5f", plug_total, other_total, double(plug_total) / double(other_total));
    return a.exec();
}