LogCompressor.cc 3.02 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include "LogCompressor.h"

#include <QDebug>

LogCompressor::LogCompressor(QString logFileName, int uasid) :
        logFileName(logFileName),
        uasid(uasid)
{
    start();
}

void LogCompressor::run()
{
    QString separator = "\t";
    QString fileName = logFileName;
    QFile file(fileName);
    QStringList* keys = new QStringList();
    QStringList* times = new QStringList();

    if (!file.exists()) return;
    if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
        return;

    // Find all keys
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        // Accumulate map of keys
        // Data field name is at position 2
        QString key = line.split(separator).at(2);
        if (!keys->contains(key)) keys->append(key);
    }
    keys->sort();

    QString header = "";
    QString spacer = "";
    for (int i = 0; i < keys->length(); i++)
    {
        header += keys->at(i) + separator;
        spacer += " " + separator;
    }

    qDebug() << header;

    qDebug() << "NOW READING TIMES";

    // Find all times
    //in.reset();
    file.reset();
    in.reset();
    in.resetStatus();
    while (!in.atEnd()) {
        QString line = in.readLine();
        // Accumulate map of keys
        // Data field name is at position 2
        QString time = line.split(separator).at(0);
        if (!times->contains(time))
        {
            times->append(time);
        }
    }
    times->sort();

    // Create lines
    QStringList* outLines = new QStringList();
    for (int i = 0; i < times->length(); i++)
    {
        outLines->append(times->at(i) + separator + spacer);
    }

    // Fill in the values for all keys
    file.reset();
    QTextStream data(&file);
    while (!data.atEnd()) {
        QString line = data.readLine();
        QStringList parts = line.split(separator);
        // Get time
        QString time = parts.first();
        QString field = parts.at(2);
        QString value = parts.at(3);
        // Get matching output line
        quint64 index = times->indexOf(time);
        QString outLine = outLines->at(index);
        QStringList outParts = outLine.split(separator);
        // Replace measurement placeholder with current value
        outParts.replace(keys->indexOf(field)+1, value);
        outLine = outParts.join(separator);
        outLines->replace(index, outLine);
    }



    // Add header, write out file
    file.close();
    QFile::remove(file.fileName());
    if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
        return;
    file.write(QString(QString("unix_timestamp") + separator + header.replace(" ", "_") + QString("\n")).toLatin1());
    //QString fileHeader = QString("unix_timestamp") + header.replace(" ", "_") + QString("\n");

    // Debug output
    for (int i = 0; i < outLines->length(); i++)
    {
        //qDebug() << outLines->at(i);
        file.write(QString(outLines->at(i) + "\n").toLatin1());

    }

    delete keys;
    qDebug() << "Done with logfile processing";
}