<commit_before>/*
Copyright (c) 2014 kimmoli <EMAIL> @likimmo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "logger.h"

#include <QCoreApplication>
#include <QCryptographicHash>
#include <QtSql>
#include <QTime>
#include <QColor>
#include <QFile>
#include <QLocale>

const QString Logger::DB_NAME = "";

Logger::Logger(QObject *parent) :
    QObject(parent)
{
    /* Initialise random number generator */
    qsrand( QDateTime::currentDateTime().toTime_t() );

    /* Open the SQLite database */

    QDir dbdir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));

    if (!dbdir.exists())
    {
        dbdir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
    }

    db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));

    db->setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/valueLoggerDb.sqlite");

    qDebug()  << QStandardPaths::writableLocation(QStandardPaths::DataLocation);


    if (db->open())
    {
        qDebug() << "Open Success";
    }
    else
    {
        qDebug() << "Open error";
        qDebug() << " " << db->lastError().text();
    }

    createParameterTable();
}

QString Logger::readVersion()
{
    return APPVERSION;
}


/*
 * Create table for data storage, each parameter has its own table
 * table name is prefixed with _ to allow number-starting tables
 */

void Logger::createDataTable(QString table)
{
    QSqlQuery query;

    if (query.exec("CREATE TABLE IF NOT EXISTS _" + table + " (key TEXT PRIMARY KEY, timestamp TEXT, value TEXT, annotation TEXT)"))
    {
        qDebug() << "datatable created _" << table;
    }
    else
    {
        qDebug() << "datatable not created _" << table << " : " << query.lastError();
    }
}

/*
 * When parameter is deleted, we need also to drop the datatable connected to it
 */

void Logger::dropDataTable(QString table)
{
    QSqlQuery query;

    if (query.exec("DROP TABLE IF EXISTS _" + table))
    {
        qDebug() << "datatable dropped _" << table;
    }
    else
    {
        qDebug() << "datatable not dropped _" << table << " : " << query.lastError();
    }
}

/*
 * Paramtere table collects parameters to which under data is being logged
 */

void Logger::createParameterTable()
{
    QSqlQuery query;

    if (query.exec("CREATE TABLE IF NOT EXISTS parameters (parameter TEXT, description TEXT, visualize INTEGER, plotcolor TEXT, datatable TEXT PRIMARY KEY)"))
    {
        qDebug() << "parameter table created";
    }
    else
    {
        qDebug() << "parameter table not created : " << query.lastError();
    }
}

/*
 * Add new data entry to a parameter
 * key = "" to generate new
 */

QString Logger::addData(QString table, QString key, QString value, QString annotation, QString timestamp)
{
    qDebug() << "Adding " << value << " (" << timestamp << ") to " << table;

    QString objHash = ( (key.length() > 0) ? key : generateHash(value));

    QSqlQuery query = QSqlQuery("INSERT OR REPLACE INTO _" + table + " (key,timestamp,value,annotation) VALUES (?,?,?,?)", *db);

    query.addBindValue(objHash);
    query.addBindValue(timestamp);<commit_msg>add annotations column if it does not exist<commit_after>/*
Copyright (c) 2014 kimmoli <EMAIL> @likimmo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "logger.h"

#include <QCoreApplication>
#include <QCryptographicHash>
#include <QtSql>
#include <QTime>
#include <QColor>
#include <QFile>
#include <QLocale>

const QString Logger::DB_NAME = "";

Logger::Logger(QObject *parent) :
    QObject(parent)
{
    /* Initialise random number generator */
    qsrand( QDateTime::currentDateTime().toTime_t() );

    /* Open the SQLite database */

    QDir dbdir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));

    if (!dbdir.exists())
    {
        dbdir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
    }

    db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));

    db->setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/valueLoggerDb.sqlite");

    qDebug()  << QStandardPaths::writableLocation(QStandardPaths::DataLocation);


    if (db->open())
    {
        qDebug() << "Open Success";
    }
    else
    {
        qDebug() << "Open error";
        qDebug() << " " << db->lastError().text();
    }

    createParameterTable();
}

QString Logger::readVersion()
{
    return APPVERSION;
}


/*
 * Create table for data storage, each parameter has its own table
 * table name is prefixed with _ to allow number-starting tables
 */

void Logger::createDataTable(QString table)
{
    QSqlQuery query;

    if (query.exec("CREATE TABLE IF NOT EXISTS _" + table + " (key TEXT PRIMARY KEY, timestamp TEXT, value TEXT, annotation TEXT)"))
    {
        qDebug() << "datatable created _" << table;
    }
    else
    {
        qDebug() << "datatable not created _" << table << " : " << query.lastError();
    }
}

/*
 * When parameter is deleted, we need also to drop the datatable connected to it
 */

void Logger::dropDataTable(QString table)
{
    QSqlQuery query;

    if (query.exec("DROP TABLE IF EXISTS _" + table))
    {
        qDebug() << "datatable dropped _" << table;
    }
    else
    {
        qDebug() << "datatable not dropped _" << table << " : " << query.lastError();
    }
}

/*
 * Paramtere table collects parameters to which under data is being logged
 */

void Logger::createParameterTable()
{
    QSqlQuery query;

    if (query.exec("CREATE TABLE IF NOT EXISTS parameters (parameter TEXT, description TEXT, visualize INTEGER, plotcolor TEXT, datatable TEXT PRIMARY KEY)"))
    {
        qDebug() << "parameter table created";
    }
    else
    {
        qDebug() << "parameter table not created : " << query.lastError();
    }
}

/*
 * Add new data entry to a parameter
 * key = "" to generate new
 */

QString Logger::addData(QString table, QString key, QString value, QString annotation, QString timestamp)
{
    qDebug() << "Adding " << value << " (" << timestamp << ") " << annotation << " to " << table;

    QString objHash = ( (key.length() > 0) ? key : generateHash(value));

    QSqlQuery addColQuery;

    if (addColQuery.exec("ALTER TABLE _" + table + " ADD COLUMN annotation TEXT"))
        qDebug() << "coiumn added succesfully";
    else
        qDebug() << "coiumn add failed";

    QSqlQuery query = QSqlQuery("INSERT OR REPLACE INTO _" + table + " (key,timestamp,value,annotation) VALUES (?,?,?,?)", *db);

    query.addBindValue(objHash);
    query.addBindValue(timestamp);
{'fraction_non_alphanumeric': 0.11472904666745033, 'fraction_numerical': 0.0011755025273304338, 'mean_word_length': 3.713573407202216, 'pattern_counts': {'":': 0, '<': 131, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6764020', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2343, 'n_words': 844}