Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 9d1a9d8

Browse files
feat: new db schema for model and template for engine
1 parent a916ec8 commit 9d1a9d8

File tree

8 files changed

+175
-36
lines changed

8 files changed

+175
-36
lines changed

engine/controllers/models.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,17 @@ void Models::ImportModel(
334334
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
335335
auto yaml_rel_path =
336336
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
337-
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
338-
yaml_rel_path.string(), modelHandle};
337+
cortex::db::ModelEntry model_entry {
338+
modelHandle,
339+
"local",
340+
"imported",
341+
cortex::db::ModelStatus::Downloaded,
342+
"",
343+
"",
344+
"",
345+
yaml_rel_path.string(),
346+
modelHandle
347+
};
339348

340349
std::filesystem::create_directories(
341350
std::filesystem::path(model_yaml_path).parent_path());

engine/database/engines.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "engines.h"
2+
#include "database.h"
3+
4+
namespace cortex::db {
5+
6+
Engines::Engines() : db_(cortex::db::Database::GetInstance().db()) {
7+
db_.exec(
8+
"CREATE TABLE IF NOT EXISTS engines ("
9+
"engine_id TEXT PRIMARY KEY,"
10+
"type TEXT,"
11+
"api_key TEXT,"
12+
"url TEXT,"
13+
"version TEXT,"
14+
"variant TEXT,"
15+
"status TEXT,"
16+
"metadata TEXT);");
17+
}
18+
Engines::~Engines() {}
19+
}

engine/database/engines.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <SQLiteCpp/Database.h>
4+
#include <trantor/utils/Logger.h>
5+
#include <string>
6+
#include <vector>
7+
#include "utils/result.hpp"
8+
9+
namespace cortex::db {
10+
11+
struct EngineEntry {
12+
std::string engine;
13+
};
14+
15+
class Engines {
16+
17+
private:
18+
SQLite::Database& db_;
19+
20+
bool IsUnique(const std::vector<EngineEntry>& entries,
21+
const std::string& model_id,
22+
const std::string& model_alias) const;
23+
24+
cpp::result<std::vector<EngineEntry>, std::string> LoadModelListNoLock() const;
25+
26+
public:
27+
Engines();
28+
Engines(SQLite::Database& db);
29+
~Engines();
30+
};
31+
32+
} // namespace cortex::db

engine/database/models.cc

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Models::Models() : db_(cortex::db::Database::GetInstance().db()) {
1212
db_.exec(
1313
"CREATE TABLE IF NOT EXISTS models ("
1414
"model_id TEXT PRIMARY KEY,"
15+
"model_format TEXT,"
16+
"model_source TEXT,"
17+
"status TEXT,"
18+
"engine TEXT,"
1519
"author_repo_id TEXT,"
1620
"branch_name TEXT,"
1721
"path_to_model_yaml TEXT,"
@@ -22,14 +26,40 @@ Models::Models(SQLite::Database& db) : db_(db) {
2226
db_.exec(
2327
"CREATE TABLE IF NOT EXISTS models ("
2428
"model_id TEXT PRIMARY KEY,"
29+
"model_format TEXT,"
30+
"model_source TEXT,"
31+
"status TEXT,"
32+
"engine TEXT,"
2533
"author_repo_id TEXT,"
2634
"branch_name TEXT,"
2735
"path_to_model_yaml TEXT,"
28-
"model_alias TEXT UNIQUE);");
36+
"model_alias TEXT);");
2937
}
30-
3138
Models::~Models() {}
3239

40+
std::string Models::StatusToString(ModelStatus status) const {
41+
switch (status) {
42+
case ModelStatus::Remote:
43+
return "remote";
44+
case ModelStatus::Downloaded:
45+
return "downloaded";
46+
case ModelStatus::Undownloaded:
47+
return "undownloaded";
48+
}
49+
return "unknown";
50+
}
51+
52+
ModelStatus Models::StringToStatus(const std::string& status_str) const {
53+
if (status_str == "remote") {
54+
return ModelStatus::Remote;
55+
} else if (status_str == "downloaded") {
56+
return ModelStatus::Downloaded;
57+
} else if (status_str == "undownloaded") {
58+
return ModelStatus::Undownloaded;
59+
}
60+
throw std::invalid_argument("Invalid status string");
61+
}
62+
3363
cpp::result<std::vector<ModelEntry>, std::string> Models::LoadModelList()
3464
const {
3565
try {
@@ -57,16 +87,21 @@ cpp::result<std::vector<ModelEntry>, std::string> Models::LoadModelListNoLock()
5787
try {
5888
std::vector<ModelEntry> entries;
5989
SQLite::Statement query(db_,
60-
"SELECT model_id, author_repo_id, branch_name, "
90+
"SELECT model_id, model_format, model_source, "
91+
"status, engine, author_repo_id, branch_name, "
6192
"path_to_model_yaml, model_alias FROM models");
6293

6394
while (query.executeStep()) {
6495
ModelEntry entry;
6596
entry.model = query.getColumn(0).getString();
66-
entry.author_repo_id = query.getColumn(1).getString();
67-
entry.branch_name = query.getColumn(2).getString();
68-
entry.path_to_model_yaml = query.getColumn(3).getString();
69-
entry.model_alias = query.getColumn(4).getString();
97+
entry.model_format = query.getColumn(1).getString();
98+
entry.model_source = query.getColumn(2).getString();
99+
entry.status = StringToStatus(query.getColumn(3).getString());
100+
entry.engine = query.getColumn(4).getString();
101+
entry.author_repo_id = query.getColumn(5).getString();
102+
entry.branch_name = query.getColumn(6).getString();
103+
entry.path_to_model_yaml = query.getColumn(7).getString();
104+
entry.model_alias = query.getColumn(8).getString();
70105
entries.push_back(entry);
71106
}
72107
return entries;
@@ -140,7 +175,8 @@ cpp::result<ModelEntry, std::string> Models::GetModelInfo(
140175
const std::string& identifier) const {
141176
try {
142177
SQLite::Statement query(db_,
143-
"SELECT model_id, author_repo_id, branch_name, "
178+
"SELECT model_id, model_format, model_source, "
179+
"status, engine, author_repo_id, branch_name, "
144180
"path_to_model_yaml, model_alias FROM models "
145181
"WHERE model_id = ? OR model_alias = ?");
146182

@@ -149,10 +185,14 @@ cpp::result<ModelEntry, std::string> Models::GetModelInfo(
149185
if (query.executeStep()) {
150186
ModelEntry entry;
151187
entry.model = query.getColumn(0).getString();
152-
entry.author_repo_id = query.getColumn(1).getString();
153-
entry.branch_name = query.getColumn(2).getString();
154-
entry.path_to_model_yaml = query.getColumn(3).getString();
155-
entry.model_alias = query.getColumn(4).getString();
188+
entry.model_format = query.getColumn(1).getString();
189+
entry.model_source = query.getColumn(2).getString();
190+
entry.status = StringToStatus(query.getColumn(3).getString());
191+
entry.engine = query.getColumn(4).getString();
192+
entry.author_repo_id = query.getColumn(5).getString();
193+
entry.branch_name = query.getColumn(6).getString();
194+
entry.path_to_model_yaml = query.getColumn(7).getString();
195+
entry.model_alias = query.getColumn(8).getString();
156196
return entry;
157197
} else {
158198
return cpp::fail("Model not found: " + identifier);
@@ -164,6 +204,10 @@ cpp::result<ModelEntry, std::string> Models::GetModelInfo(
164204

165205
void Models::PrintModelInfo(const ModelEntry& entry) const {
166206
LOG_INFO << "Model ID: " << entry.model;
207+
LOG_INFO << "Model Format: " << entry.model_format;
208+
LOG_INFO << "Model Source: " << entry.model_source;
209+
LOG_INFO << "Status: " << StatusToString(entry.status);
210+
LOG_INFO << "Engine: " << entry.engine;
167211
LOG_INFO << "Author/Repo ID: " << entry.author_repo_id;
168212
LOG_INFO << "Branch Name: " << entry.branch_name;
169213
LOG_INFO << "Path to model.yaml: " << entry.path_to_model_yaml;
@@ -188,14 +232,18 @@ cpp::result<bool, std::string> Models::AddModelEntry(ModelEntry new_entry,
188232

189233
SQLite::Statement insert(
190234
db_,
191-
"INSERT INTO models (model_id, author_repo_id, "
192-
"branch_name, path_to_model_yaml, model_alias) VALUES (?, ?, "
193-
"?, ?, ?)");
235+
"INSERT INTO models (model_id, model_format, model_source, status, "
236+
"engine, author_repo_id, branch_name, path_to_model_yaml, model_alias) "
237+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
194238
insert.bind(1, new_entry.model);
195-
insert.bind(2, new_entry.author_repo_id);
196-
insert.bind(3, new_entry.branch_name);
197-
insert.bind(4, new_entry.path_to_model_yaml);
198-
insert.bind(5, new_entry.model_alias);
239+
insert.bind(2, new_entry.model_format);
240+
insert.bind(3, new_entry.model_source);
241+
insert.bind(4, StatusToString(new_entry.status));
242+
insert.bind(5, new_entry.engine);
243+
insert.bind(6, new_entry.author_repo_id);
244+
insert.bind(7, new_entry.branch_name);
245+
insert.bind(8, new_entry.path_to_model_yaml);
246+
insert.bind(9, new_entry.model_alias);
199247
insert.exec();
200248

201249
return true;
@@ -215,14 +263,19 @@ cpp::result<bool, std::string> Models::UpdateModelEntry(
215263
try {
216264
SQLite::Statement upd(db_,
217265
"UPDATE models "
218-
"SET author_repo_id = ?, branch_name = ?, "
266+
"SET model_format = ?, model_source = ?, status = ?, "
267+
"engine = ?, author_repo_id = ?, branch_name = ?, "
219268
"path_to_model_yaml = ? "
220269
"WHERE model_id = ? OR model_alias = ?");
221-
upd.bind(1, updated_entry.author_repo_id);
222-
upd.bind(2, updated_entry.branch_name);
223-
upd.bind(3, updated_entry.path_to_model_yaml);
224-
upd.bind(4, identifier);
225-
upd.bind(5, identifier);
270+
upd.bind(1, updated_entry.model_format);
271+
upd.bind(2, updated_entry.model_source);
272+
upd.bind(3, StatusToString(updated_entry.status));
273+
upd.bind(4, updated_entry.engine);
274+
upd.bind(5, updated_entry.author_repo_id);
275+
upd.bind(6, updated_entry.branch_name);
276+
upd.bind(7, updated_entry.path_to_model_yaml);
277+
upd.bind(8, identifier);
278+
upd.bind(9, identifier);
226279
return upd.exec() == 1;
227280
} catch (const std::exception& e) {
228281
return cpp::fail(e.what());
@@ -305,4 +358,5 @@ bool Models::HasModel(const std::string& identifier) const {
305358
return false;
306359
}
307360
}
308-
} // namespace cortex::db
361+
362+
} // namespace cortex::db

engine/database/models.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77
#include "utils/result.hpp"
88

99
namespace cortex::db {
10+
11+
enum class ModelStatus {
12+
Remote,
13+
Downloaded,
14+
Undownloaded
15+
};
16+
1017
struct ModelEntry {
1118
std::string model;
19+
std::string model_format;
20+
std::string model_source;
21+
ModelStatus status;
22+
std::string engine;
1223
std::string author_repo_id;
1324
std::string branch_name;
1425
std::string path_to_model_yaml;
@@ -26,6 +37,9 @@ class Models {
2637

2738
cpp::result<std::vector<ModelEntry>, std::string> LoadModelListNoLock() const;
2839

40+
std::string StatusToString(ModelStatus status) const;
41+
ModelStatus StringToStatus(const std::string& status_str) const;
42+
2943
public:
3044
static const std::string kModelListPath;
3145
cpp::result<std::vector<ModelEntry>, std::string> LoadModelList() const;
@@ -50,4 +64,5 @@ class Models {
5064
const std::string& identifier) const;
5165
bool HasModel(const std::string& identifier) const;
5266
};
53-
} // namespace cortex::db
67+
68+
} // namespace cortex::db

engine/extensions/remote-engine/TemplateRenderer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if defined(_WIN32) || defined(_WIN64)
22
#define NOMINMAX
3+
#undef min
4+
#undef max
35
#endif
46
#include "TemplateRenderer.h"
57
#include <regex>

engine/extensions/remote-engine/TemplateRenderer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// clang-format off
99
#if defined(_WIN32) || defined(_WIN64)
1010
#define NOMINMAX
11+
#undef min
12+
#undef max
1113
#endif
1214
#include <nlohmann/json.hpp>
1315
#include <inja/inja.hpp>

engine/test/components/test_models_db.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace cortex::db {
66
namespace {
77
constexpr const auto kTestDb = "./test.db";
88
}
9+
910
class ModelsTestSuite : public ::testing::Test {
1011
public:
1112
ModelsTestSuite()
@@ -21,9 +22,9 @@ class ModelsTestSuite : public ::testing::Test {
2122
SQLite::Database db_;
2223
cortex::db::Models model_list_;
2324

24-
const cortex::db::ModelEntry kTestModel{"test_model_id", "test_author",
25-
"main", "/path/to/model.yaml",
26-
"test_alias"};
25+
const cortex::db::ModelEntry kTestModel{
26+
"test_model_id", "test_format", "test_source", cortex::db::ModelStatus::Downloaded, "test_engine",
27+
"test_author", "main", "/path/to/model.yaml", "test_alias"};
2728
};
2829

2930
TEST_F(ModelsTestSuite, TestAddModelEntry) {
@@ -33,8 +34,12 @@ TEST_F(ModelsTestSuite, TestAddModelEntry) {
3334
EXPECT_TRUE(retrieved_model);
3435
EXPECT_EQ(retrieved_model.value().model, kTestModel.model);
3536
EXPECT_EQ(retrieved_model.value().author_repo_id, kTestModel.author_repo_id);
37+
EXPECT_EQ(retrieved_model.value().model_format, kTestModel.model_format);
38+
EXPECT_EQ(retrieved_model.value().model_source, kTestModel.model_source);
39+
EXPECT_EQ(retrieved_model.value().status, kTestModel.status);
40+
EXPECT_EQ(retrieved_model.value().engine, kTestModel.engine);
3641

37-
// // Clean up
42+
// Clean up
3843
EXPECT_TRUE(model_list_.DeleteModelEntry(kTestModel.model).value());
3944
}
4045

@@ -59,14 +64,14 @@ TEST_F(ModelsTestSuite, TestUpdateModelEntry) {
5964
EXPECT_TRUE(model_list_.AddModelEntry(kTestModel).value());
6065

6166
cortex::db::ModelEntry updated_model = kTestModel;
67+
updated_model.status = cortex::db::ModelStatus::Downloaded;
6268

6369
EXPECT_TRUE(
6470
model_list_.UpdateModelEntry(kTestModel.model, updated_model).value());
6571

6672
auto retrieved_model = model_list_.GetModelInfo(kTestModel.model);
6773
EXPECT_TRUE(retrieved_model);
68-
EXPECT_TRUE(
69-
model_list_.UpdateModelEntry(kTestModel.model, updated_model).value());
74+
EXPECT_EQ(retrieved_model.value().status, updated_model.status);
7075

7176
// Clean up
7277
EXPECT_TRUE(model_list_.DeleteModelEntry(kTestModel.model).value());
@@ -162,4 +167,5 @@ TEST_F(ModelsTestSuite, TestHasModel) {
162167
// Clean up
163168
EXPECT_TRUE(model_list_.DeleteModelEntry(kTestModel.model).value());
164169
}
165-
} // namespace cortex::db
170+
171+
} // namespace cortex::db

0 commit comments

Comments
 (0)