Skip to content

Commit

Permalink
Modify the reset behaviour to work on the same activation, Dont know …
Browse files Browse the repository at this point in the history
…what I was thinking with the other row
  • Loading branch information
Ómar Högni Guðmarsson committed Jun 20, 2024
1 parent 18e00e7 commit 1fb0768
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
27 changes: 18 additions & 9 deletions exes/themis/inc/alarm_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CREATE TABLE IF NOT EXISTS Alarms(
sha1sum TEXT NOT NULL,
alarm_level INTEGER NOT NULL,
alarm_latching BOOLEAN NOT NULL,
inserted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(tfc_id, sha1sum) ON CONFLICT IGNORE
);
)";
Expand All @@ -57,6 +58,7 @@ CREATE TABLE IF NOT EXISTS AlarmTranslations(
locale TEXT NOT NULL,
details TEXT NOT NULL,
description TEXT NOT NULL,
inserted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(sha1sum, locale) ON CONFLICT REPLACE
FOREIGN KEY(alarm_id) REFERENCES Alarms(alarm_id)
);
Expand All @@ -71,7 +73,9 @@ CREATE TABLE IF NOT EXISTS AlarmActivations(
activation_id INTEGER PRIMARY KEY,
alarm_id INTEGER NOT NULL,
activation_time LONG INTEGER NOT NULL,
reset_time LONG INTEGER,
activation_level BOOLEAN NOT NULL,
inserted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(alarm_id) REFERENCES Alarms(alarm_id)
);
)";
Expand Down Expand Up @@ -171,31 +175,36 @@ ON Alarms.sha1sum = AlarmTranslations.sha1sum;
db_ << query;
}

/**
* @brief Set an alarm in the database
* @param alarm_id the id of the alarm
* @param variables the variables for this activation
* @param tp an optional timepoint
* @return the activation_id
*/
auto set_alarm(snitch::api::alarm_id_t alarm_id,
const std::unordered_map<std::string, std::string>& variables,
std::optional<tfc::snitch::api::time_point> tp = {}) -> void {
std::optional<tfc::snitch::api::time_point> tp = {}) -> std::uint64_t {
db_ << "BEGIN;";
std::uint64_t activation_id;
try {
db_ << fmt::format("INSERT INTO AlarmActivations(alarm_id, activation_time, activation_level) VALUES({},{},1)",
alarm_id, milliseconds_since_epoch(tp));
std::int64_t last_insert_rowid = db_.last_insert_rowid();
if (last_insert_rowid < 0) {
throw std::runtime_error("Failed to insert activation into database");
}
activation_id = static_cast<std::uint64_t>(db_.last_insert_rowid());

for (auto& [key, value] : variables) {
db_ << fmt::format("INSERT INTO AlarmVariables(activation_id, variable_key, variable_value) VALUES({},'{}','{}');",
last_insert_rowid, key, value);
activation_id, key, value);
}
} catch (std::exception& e) {
db_ << "ROLLBACK;";
throw e;
}
db_ << "COMMIT;";
return activation_id;
}
auto reset_alarm(snitch::api::alarm_id_t alarm_id, std::optional<tfc::snitch::api::time_point> tp = {}) -> void {
db_ << fmt::format("INSERT INTO AlarmActivations(alarm_id, activation_time, activation_level) VALUES({},{},0)", alarm_id,
milliseconds_since_epoch(tp));
auto reset_alarm(snitch::api::alarm_id_t activation_id, std::optional<tfc::snitch::api::time_point> tp = {}) -> void {
db_ << fmt::format("UPDATE AlarmActivations SET activation_level = 0, reset_time = {} WHERE activation_id = {};", milliseconds_since_epoch(tp), activation_id);
}

[[nodiscard]] auto list_activations(std::string_view locale,
Expand Down
8 changes: 4 additions & 4 deletions exes/themis/tests/themis_database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ auto main(int argc, char** argv) -> int {

// Add some activations
for(int i = 0; i < 10; i++){
alarm_db.set_alarm(insert_id, {});
alarm_db.reset_alarm(insert_id);
auto activation_id = alarm_db.set_alarm(insert_id, {});
alarm_db.reset_alarm(activation_id);
}
// Insert an invalid activation
expect(throws([&]{alarm_db.set_alarm(999, {}); }));
Expand All @@ -104,12 +104,12 @@ auto main(int argc, char** argv) -> int {
"es", 0, 10000, tfc::snitch::level_e::info, tfc::snitch::api::active_e::all,
tfc::themis::alarm_database::timepoint_from_milliseconds(0),
tfc::themis::alarm_database::timepoint_from_milliseconds(std::numeric_limits<std::int64_t>::max()));
expect(activations.size() == 20) << activations.size();
expect(activations.size() == 10) << activations.size();
activations = alarm_db.list_activations(
"es", 0, 10000, tfc::snitch::level_e::info, tfc::snitch::api::active_e::active,
tfc::themis::alarm_database::timepoint_from_milliseconds(0),
tfc::themis::alarm_database::timepoint_from_milliseconds(std::numeric_limits<std::int64_t>::max()));
expect(activations.size() == 10) << activations.size();
expect(activations.size() == 0) << activations.size();
activations = alarm_db.list_activations(
"es", 0, 10000, tfc::snitch::level_e::info, tfc::snitch::api::active_e::inactive,
tfc::themis::alarm_database::timepoint_from_milliseconds(0),
Expand Down

0 comments on commit 1fb0768

Please sign in to comment.