diff --git a/README.md b/README.md index 3d3b50c..dedb2a6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ This repository houses various projects. All are listed below. - Two pistache REST HTTP APIs, [documentation](docs/SERVER.md) - A solar panel log server, [documentation](docs/SOLAR_API.md) - An electricity log server, [documentation](docs/ELECTRICITY_API.md) -- A migrator to convert and build data structures yielded by the loggers, [documentation](docs/MIGRATOR.md) ## Project Directory @@ -22,7 +21,6 @@ This repository houses various projects. All are listed below. |- `src` => A folder with all the source files of the different projects that require compilation
|....|- `electricity-logger` => The Landis Gyr E350 logger source files
|....|- `electricity-server` => The pistache REST API sources for serving the electricity logs
-|....|- `migrator ` => The migrator source files
|....|- `solar-logger` => The Zeverlution S3000 logger source files
|....|- `solar-server` => The pistache REST API sources for serving the solar panel logs
|- `systemd` => A folder with example systemd service files for the servers
diff --git a/include/migrator/migrations.hpp b/include/migrator/migrations.hpp deleted file mode 100644 index 96ee54b..0000000 --- a/include/migrator/migrations.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include - -namespace Migrations -{ - int EpochToDateTime(std::string const & sourceDatabase, std::string const & destinationDatabase); - - int UpdateSummaryTable(std::string const & sourceDatabase, std::string const & destinationDatabase); -} \ No newline at end of file diff --git a/include/migrator/transaction.hpp b/include/migrator/transaction.hpp deleted file mode 100644 index 518f354..0000000 --- a/include/migrator/transaction.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include -#include - -class Transaction { -private: - sqlite3 * const destination; - unsigned queryCount; - std::stringstream queryStream; - - void Reset(); - -public: - void AddStatement(std::string const & statement); - - unsigned StatementCount() const; - - // Runs the statements inserted as a single transaction - // Returns a SQLite status code (0 = OK) and then resets - // to a new transaction - int Execute(); - - Transaction(sqlite3 * const databaseToInsertIn); -}; \ No newline at end of file diff --git a/makefile b/makefile index 5f41bf9..26b6027 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,6 @@ CFLAGS = -DSPDLOG_FMT_EXTERNAL -Wall -Wextra -std=c++20 -O2 -Iinclude/ -MMD ELECTRICITY_LOGGER_LFLAGS = -lsqlite3 -lspdlog -lfmt SOLAR_LOGGER_LFLAGS = -lsqlite3 -lcurl SERVER_LFLAGS = -lpistache -lsqlite3 -lstdc++fs -lspdlog -lfmt -MIGRATOR_LFLAGS = -lsqlite3 SOLAR_LOGGER_CPPS = $(shell find src/solar-logger/ -name *.cpp) SOLAR_LOGGER_OBJS = $(patsubst src/%.cpp, build/%.o, ${SOLAR_LOGGER_CPPS}) @@ -13,10 +12,6 @@ SOLAR_SRV_CPPS = $(shell find src/solar-server/ -name *.cpp) SOLAR_SRV_OBJS = $(patsubst src/%.cpp, build/%.o, ${SOLAR_SRV_CPPS}) SOLAR_SRV_DEPS = $(patsubst src/%.cpp, build/%.d, ${SOLAR_SRV_CPPS}) -MIGRATOR_CPPS = $(shell find src/migrator/ -name *.cpp) -MIGRATOR_OBJS = $(patsubst src/%.cpp, build/%.o, ${MIGRATOR_CPPS}) -MIGRATOR_DEPS = $(patsubst src/%.cpp, build/%.d, ${MIGRATOR_CPPS}) - ELECT_LOGGER_CPPS = $(shell find src/electricity-logger/ -name *.cpp) ELECT_LOGGER_OBJS = $(patsubst src/%.cpp, build/%.o, ${ELECT_LOGGER_CPPS}) ELECT_LOGGER_DEPS = $(patsubst src/%.cpp, build/%.d, ${ELECT_LOGGER_CPPS}) @@ -27,7 +22,6 @@ ELECT_SRV_DEPS = $(patsubst src/%.cpp, build/%.d, ${ELECT_SRV_CPPS}) SOLAR_LOGGER_BINARY_PATH = bin/solar-logger SOLAR_SRV_BINARY_PATH = bin/solar-server -MIGRATOR_BINARY_PATH = bin/migrator ELECT_LOGGER_BINARY_PATH = bin/electricity-logger ELECT_SRV_BINARY_PATH = bin/electricity-server @@ -36,7 +30,7 @@ INSTALL_PATH = /usr/local/bin/ # Test variables SOLAR_TEST_DATABASE = solarpaneloutput.db -all: ${SOLAR_LOGGER_BINARY_PATH} ${SOLAR_SRV_BINARY_PATH} ${MIGRATOR_BINARY_PATH} ${ELECT_LOGGER_BINARY_PATH} ${ELECT_SRV_BINARY_PATH} +all: ${SOLAR_LOGGER_BINARY_PATH} ${SOLAR_SRV_BINARY_PATH} ${ELECT_LOGGER_BINARY_PATH} ${ELECT_SRV_BINARY_PATH} clean: -rm -rf bin/* build/* @@ -86,10 +80,6 @@ ${SOLAR_SRV_BINARY_PATH}: ${SOLAR_SRV_OBJS} mkdir -p ${@D} ${CC} $^ ${SERVER_LFLAGS} -o $@ -${MIGRATOR_BINARY_PATH}: ${MIGRATOR_OBJS} - mkdir -p ${@D} - ${CC} $^ ${MIGRATOR_LFLAGS} -o $@ - ${ELECT_LOGGER_BINARY_PATH}: ${ELECT_LOGGER_OBJS} mkdir -p ${@D} ${CC} $^ ${ELECTRICITY_LOGGER_LFLAGS} -o $@ diff --git a/src/migrator/main.cpp b/src/migrator/main.cpp deleted file mode 100644 index 63a1df0..0000000 --- a/src/migrator/main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -int main(int argc, char ** argv) -{ - TCLAP::CmdLine cmd("migrator is a small program designed to run migrations", ' ', "1.0"); - - TCLAP::ValueArg migrationArg("m", "migration", "", true, "", "Name of the migration to run"); - cmd.add(migrationArg); - - TCLAP::ValueArg srcArg("s", "source", "", false, "", "Source file or directory to use"); - cmd.add(srcArg); - - TCLAP::ValueArg dstArg("d", "destination", "", false, "", "Destination file or directory to use"); - cmd.add(dstArg); - - cmd.parse(argc, argv); - - auto const & migrationToRun = migrationArg.getValue(); - if(migrationToRun == "epochToDateTime") - { - return Migrations::EpochToDateTime(srcArg.getValue(), dstArg.getValue()); - } - if(migrationToRun == "updateSummaryTable") - { - return Migrations::UpdateSummaryTable(srcArg.getValue(), dstArg.getValue()); - } - - std::printf("Unknown migration %s, aborting.\n", migrationToRun.c_str()); - return 1; -} \ No newline at end of file diff --git a/src/migrator/migrations/epochtodatetime.cpp b/src/migrator/migrations/epochtodatetime.cpp deleted file mode 100644 index 133a20a..0000000 --- a/src/migrator/migrations/epochtodatetime.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include -#include -#include -#include -#include - -std::string ZeroPadTwoDigitNumber(int number) -{ - auto result = std::to_string(number); - if(result.size() == 1) - { - return "0" + result; - } - - return result; -} - -int SourceCallback(void * data, int argc, char ** argv, char ** columnNames) -{ - if(argc != 3) - { - std::puts("Expected source database to have 3 columns: DateTimeUtc, Watt and KilowattHour."); - return 1; - } - - Transaction * const transactionPtr = reinterpret_cast(data); - auto const epochTime = std::atol(argv[0]); - auto const dateTime = *std::gmtime(&epochTime); - - std::stringstream insertStream; - insertStream << "INSERT INTO SolarPanelOutput VALUES(" << '\'' << (dateTime.tm_year + 1900) << '-' - << ZeroPadTwoDigitNumber(dateTime.tm_mon + 1) << '-' << ZeroPadTwoDigitNumber(dateTime.tm_mday) - << "','" << ZeroPadTwoDigitNumber(dateTime.tm_hour) << ':' << ZeroPadTwoDigitNumber(dateTime.tm_min) - << ':' << ZeroPadTwoDigitNumber(dateTime.tm_sec) << "'," << argv[1] << ',' << argv[2] << ");"; - - transactionPtr->AddStatement(insertStream.str()); - - if(transactionPtr->StatementCount() > 1000) - { - return transactionPtr->Execute(); - } - - return 0; -} - -namespace Migrations -{ - int EpochToDateTime(std::string const & sourceDatabase, std::string const & destinationDatabase) - { - if(sourceDatabase == destinationDatabase) - { - std::puts("The EpochToDateTime is not meant to be run on the same database."); - return 1; - } - - sqlite3 * source; - if(sqlite3_open(sourceDatabase.c_str(), &source)) - { - std::printf("Error opening source database %s\n", sourceDatabase.c_str()); - return 1; - } - - sqlite3 * destination; - if(sqlite3_open(destinationDatabase.c_str(), &destination)) - { - std::printf("Error opening destination database %s\n", destinationDatabase.c_str()); - return 1; - } - - Transaction transaction(destination); - auto const sqlResult - = sqlite3_exec(source, "SELECT * FROM SolarPanelOutput;", SourceCallback, &transaction, nullptr); - if(sqlResult) - { - std::printf("Error %i during insertion of records into destination database\n", sqlResult); - return 1; - } - - auto const commitResult = transaction.Execute(); - if(commitResult) - { - std::printf("Error %i when committing last transaction\n", commitResult); - return 1; - } - - sqlite3_close(source); - sqlite3_close(destination); - - return 0; - } -} diff --git a/src/migrator/migrations/updatesummarytable.cpp b/src/migrator/migrations/updatesummarytable.cpp deleted file mode 100644 index 1165185..0000000 --- a/src/migrator/migrations/updatesummarytable.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include - -namespace Migrations -{ - int SourceCallback(void * data, int argc, char ** argv, char ** columnNames) - { - // Expect Date and KilowattHour - if(argc != 2) - { - std::puts("Wrong number of columns received"); - return 1; - } - - Transaction * transactionPtr = reinterpret_cast(data); - std::stringstream insertStream; - insertStream << "INSERT INTO SolarPanelSummary(Date,KilowattHour) VALUES(" << '\'' << argv[0] << "'," << argv[1] - << ')' << "ON CONFLICT(Date) DO UPDATE SET KilowattHour = excluded.KilowattHour;"; - - transactionPtr->AddStatement(insertStream.str()); - - return 0; - } - - int UpdateSummaryTable(std::string const & sourceDatabase, std::string const & destinationDatabase) - { - sqlite3 * source; - if(sqlite3_open(sourceDatabase.c_str(), &source)) - { - std::printf("Error opening source database %s\n", sourceDatabase.c_str()); - return 1; - } - - sqlite3 * destination; - if(sqlite3_open(destinationDatabase.c_str(), &destination)) - { - std::printf("Error opening destination database %s\n", destinationDatabase.c_str()); - return 1; - } - - Transaction transaction(destination); - auto const sqlResult = sqlite3_exec( - source, - "SELECT Date, MAX(KilowattHour) AS KilowattHour FROM SolarPanelOutput GROUP BY Date;", - SourceCallback, - &transaction, - nullptr); - if(sqlResult) - { - std::printf("Error %i during fetching of source database records\n", sqlResult); - return 1; - } - - std::printf("UpdateSummaryTable: Upserting %u records...\n", transaction.StatementCount()); - auto const commitResult = transaction.Execute(); - if(commitResult) - { - std::printf("Error %i when committing transaction\n", commitResult); - return 1; - } - - sqlite3_close(source); - sqlite3_close(destination); - - return 0; - } -} \ No newline at end of file diff --git a/src/migrator/transaction.cpp b/src/migrator/transaction.cpp deleted file mode 100644 index 523ccf7..0000000 --- a/src/migrator/transaction.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -void Transaction::Reset() -{ - queryStream.clear(); - queryStream.str(std::string()); - queryCount = 0u; - - queryStream << "PRAGMA journal_mode = OFF;" - << "BEGIN TRANSACTION;"; -} - -void Transaction::AddStatement(std::string const & statement) -{ - ++queryCount; - queryStream << statement; -} - -unsigned Transaction::StatementCount() const { return queryCount; } - -int Transaction::Execute() -{ - queryStream << "COMMIT;"; - - auto const result = sqlite3_exec(destination, queryStream.str().c_str(), nullptr, nullptr, nullptr); - Reset(); - - return result; -} - -Transaction::Transaction(sqlite3 * const databaseToInsertIn) - : destination(databaseToInsertIn), queryCount(0u), queryStream() -{ - Reset(); -} \ No newline at end of file