-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint that returns the lineageDefinitions for the reques…
…ted column (#689)
- Loading branch information
Showing
7 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expectHeaderToHaveDataVersion, server } from './common.js'; | ||
import { describe, it } from 'node:test'; | ||
import { expect } from 'chai'; | ||
|
||
describe('The /lineageDefinition endpoint', () => { | ||
it('should return the lineage index', async () => { | ||
let searchString = `A: \{\} | ||
A.1: | ||
parents: | ||
- A | ||
A.11: | ||
parents: | ||
- A | ||
`; | ||
await server | ||
.get('/lineageDefinition/pango_lineage') | ||
.expect(200) | ||
.expect('Content-Type', 'application/yaml') | ||
.expect(expectHeaderToHaveDataVersion) | ||
.then(response => { | ||
expect(response.text).to.match(new RegExp(`^${searchString}`)); | ||
}); | ||
}); | ||
|
||
it('should return an error for column name that is not present', async () => { | ||
await server | ||
.get('/lineageDefinition/columnThatIsNotPresent') | ||
.expect(400) | ||
.expect('Content-Type', 'application/json') | ||
.expect(expectHeaderToHaveDataVersion) | ||
.then(response => { | ||
expect(response.body.message).to.equal( | ||
'The column columnThatIsNotPresent does not exist in this instance.' | ||
); | ||
}); | ||
}); | ||
|
||
it('should return an error for column name that does not have a lineage index', async () => { | ||
await server | ||
.get('/lineageDefinition/country') | ||
.expect(400) | ||
.expect('Content-Type', 'application/json') | ||
.expect(expectHeaderToHaveDataVersion) | ||
.then(response => { | ||
expect(response.body.message).to.equal('The column country does not have a lineageIndex defined.'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <Poco/Net/HTTPServerRequest.h> | ||
#include <Poco/Net/HTTPServerResponse.h> | ||
|
||
#include "database_mutex.h" | ||
#include "rest_resource.h" | ||
|
||
namespace silo::api { | ||
|
||
class LineageDefinitionHandler : public RestResource { | ||
private: | ||
DatabaseMutex& database; | ||
std::string column_name; | ||
|
||
public: | ||
explicit LineageDefinitionHandler(DatabaseMutex& database, std::string column_name); | ||
|
||
void get(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) | ||
override; | ||
}; | ||
|
||
} // namespace silo::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "silo/api/lineage_definition_handler.h" | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include <Poco/Net/HTTPServerRequest.h> | ||
#include <Poco/Net/HTTPServerResponse.h> | ||
#include <Poco/URI.h> | ||
#include <nlohmann/json.hpp> | ||
|
||
#include "silo/api/database_mutex.h" | ||
#include "silo/api/error_request_handler.h" | ||
|
||
namespace silo::api { | ||
|
||
LineageDefinitionHandler::LineageDefinitionHandler(DatabaseMutex& database, std::string column_name) | ||
: database(database), | ||
column_name(std::move(column_name)) {} | ||
|
||
void LineageDefinitionHandler::get( | ||
Poco::Net::HTTPServerRequest& request, | ||
Poco::Net::HTTPServerResponse& response | ||
) { | ||
const auto fixed_database = database.getDatabase(); | ||
|
||
response.set("data-version", fixed_database->getDataVersionTimestamp().value); | ||
|
||
auto column_metadata = | ||
std::ranges::find_if(fixed_database->columns.metadata, [&](const auto& metadata) { | ||
return metadata.name == column_name; | ||
}); | ||
if (column_metadata == fixed_database->columns.metadata.end()) { | ||
response.setContentType("application/json"); | ||
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST); | ||
std::ostream& out_stream = response.send(); | ||
out_stream << nlohmann::json(ErrorResponse{ | ||
.error = "Bad request", | ||
.message = fmt::format("The column {} does not exist in this instance.", column_name) | ||
}); | ||
} | ||
// TODO(#691) Change this check for containment to a selection of the correct lineage system | ||
else if(column_metadata->type != config::ColumnType::INDEXED_STRING || !fixed_database->columns.indexed_string_columns.at(column_name).hasLineageTree()){ | ||
response.setContentType("application/json"); | ||
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST); | ||
std::ostream& out_stream = response.send(); | ||
out_stream << nlohmann::json(ErrorResponse{ | ||
.error = "Bad request", | ||
.message = fmt::format("The column {} does not have a lineageIndex defined.", column_name) | ||
}); | ||
} else { | ||
const std::string lineage_definition_yaml = fixed_database->lineage_tree.file; | ||
response.setContentType("application/yaml"); | ||
std::ostream& out_stream = response.send(); | ||
out_stream << lineage_definition_yaml; | ||
} | ||
} | ||
} // namespace silo::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters