Repository navigation
feat(backend, http): add HTTP API for ADJ catalogs and WebSocket upgrade support #676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
4a36bb2
build(backend, http): add axum, and cargo update
pminmic 56d4aef
feat(backend, http): add datapacket structure response
pminmic dfbc4a6
feat(backend, http): add orderpacket structure response and separate …
pminmic 77dd811
feat(backend, http): add protectionpacket response structure and some…
pminmic 56e4d73
schema(backend, http): some simple example responses to understand t…
pminmic 86726e5
fix(backend, http): delete unimportant fields in datapackets
pminmic c5c3562
feat(backend, http): setup functions for responses
pminmic 7bfb433
chore(backend, http): move response to new submodule http of module adj
pminmic 48f4abd
chore(backend, http): move http to station, change http dependencies
pminmic 8f3c53d
feat(backend, http): handlers for each endpoint
pminmic d5174f6
feat(backend, http): basic server with 3 enpoints
pminmic 8c666f7
fix(backend, http): efficient responses for endpoint (no clone)
pminmic caee95d
test(backend, http): add tests to server
pminmic e484ad3
docs(backend, http): docs in adj module
pminmic 05d2783
refactor(backend, http): implementation in struct Adj of setups
pminmic 441ddf1
feat(backend, http): websocket support
pminmic 6a3d2a8
todo(backend, http): everything related with configuration (future mo…
pminmic b074dd4
feat(backend, http): small test of the endpoints in main
pminmic 69af8eb
feat(backend, http): add HTTP server configuration with CORS and cach…
pminmic 7f8a380
docs(backend, http): docs HTTP endpoints, configuration and ws handoff
pminmic 8896a8e
refactor(backend, http): move http to model and simplify adj.rs movin…
pminmic e30d059
fix(backend, http): problems with time and other minor fixes
pminmic 9b5045e
fix(backend, http): add comments
pminmic 2817ae7
fix(backend, http): simplification of server configuration
pminmic 69eaf1b
docs(backend, http): update readme and correct errors
pminmic dbef896
fix(backend, http): update main
pminmic ee2e354
fix(backend, git): clippy check
pminmic 62c9ea5
fix(backend, http): clippy warings solved
pminmic 2fdd73d
fix(backend, http): types in all the structures
pminmic ad236e0
docs(backend, http): comment some more
pminmic f6d632d
fix(backend, http): comment little things
pminmic f5e9ee6
fix(backend, http): revert changes to main.rs
pminmic 8f2b89c
fix(backend, http): fix feature imports and usage
pminmic eabf892
Merge branch 'new-backend' into backend/http
pminmic ada7456
fix(backend, Cargo.lock): fix duplicated dependencies
pminmic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,230 @@ | ||
| use crate::{ | ||
| adj::Adj, | ||
| model::{ | ||
| AdjId, MeasurementKind, PacketType, | ||
| http::{ | ||
| data::{ | ||
| DataBoardResponse, DataMeasurementKind, DataMeasurementResponse, | ||
| DataPacketResponse, DataResponse, | ||
| }, | ||
| order::{ | ||
| OrderBoardResponse, OrderMeasurementResponse, OrderPacketResponse, | ||
| OrderProtectionResponse, OrderResponse, | ||
| }, | ||
| protection::{ProtectionInfoResponse, ProtectionResponse, ProtectionTypeResponse}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| impl Adj { | ||
| /// Builds the data catalog without modifying or consuming the ADJ. | ||
| /// | ||
| /// Boards and data packets are sorted by id. Boards without data packets are | ||
| /// retained with an empty list. Measurements keep their wire order and omit | ||
| /// protection metadata. | ||
| pub fn data_response(&self) -> DataResponse { | ||
| let mut boards: Vec<DataBoardResponse> = self | ||
| .pod_data | ||
| .boards | ||
| .values() | ||
| // For each board | ||
| .map(|board| { | ||
| let mut packets: Vec<DataPacketResponse> = board | ||
| .packets | ||
| .values() | ||
| // For each packet of this board | ||
| .filter_map(|packet| { | ||
| // If the PacketType is Data, return Some(DataPacketResponse) -> Will be a Vec<DataPacketResponse> | ||
| let PacketType::Data(measurements) = &packet.packet_type else { | ||
| return None; | ||
| }; | ||
|
|
||
| Some(DataPacketResponse { | ||
| id: packet.id, | ||
| name: packet.name.clone(), | ||
| packet_type: "data".to_string(), | ||
| measurements: measurements | ||
| .iter() | ||
| .map(|measurement| DataMeasurementResponse { | ||
| id: measurement.id, | ||
| alias: measurement.alias.clone(), | ||
| name: measurement.name.clone(), | ||
| display_units: measurement.display_units.clone(), | ||
| kind: match_measurement_kind(&measurement.kind), | ||
| }) | ||
| .collect(), | ||
| }) | ||
| }) | ||
| .collect(); | ||
| packets.sort_unstable_by_key(|packet| packet.id.0); | ||
|
|
||
| // Set up each packet | ||
| DataBoardResponse { | ||
| id: board.id, | ||
| name: board.name.clone(), | ||
| packets, | ||
| } | ||
| }) | ||
| // Obtain a Vec<> of all the packets | ||
| .collect(); | ||
|
|
||
| // Sort the responses by board.id | ||
| boards.sort_unstable_by_key(|board| board.id.0); | ||
|
|
||
| DataResponse { boards } | ||
| } | ||
|
|
||
| /// Builds the order catalog without modifying or consuming the ADJ. | ||
| /// | ||
| /// Boards and orders are sorted by id. Boards without orders are retained with | ||
| /// an empty list. Parameters and their protections keep their ADJ order. | ||
| pub fn order_response(&self) -> OrderResponse { | ||
| let mut boards: Vec<OrderBoardResponse> = self | ||
| .pod_data | ||
| .boards | ||
| .values() | ||
| // For each board | ||
| .map(|board| { | ||
| let mut orders: Vec<OrderPacketResponse> = board | ||
| .packets | ||
| .values() | ||
| // For each order of this board | ||
| .filter_map(|packet| { | ||
| // If the packet is an Order... | ||
| let PacketType::Order(measurements) = &packet.packet_type else { | ||
| return None; | ||
| }; | ||
|
|
||
| // Setup of the response | ||
| Some(OrderPacketResponse { | ||
|
pminmic marked this conversation as resolved.
Resolved
|
||
| id: packet.id, | ||
| name: packet.name.clone(), | ||
| packet_type: "order".to_string(), | ||
| measurements: measurements | ||
| .iter() | ||
| // Convert each parameter into its HTTP response representation | ||
| .map(|measurement| OrderMeasurementResponse { | ||
| id: measurement.id, | ||
| alias: measurement.alias.clone(), | ||
| name: measurement.name.clone(), | ||
| display_units: measurement.display_units.clone(), | ||
| kind: match_measurement_kind(&measurement.kind), | ||
| protections: measurement | ||
| .protections | ||
| .iter() | ||
| // Convert each protection | ||
| .map(|protection| OrderProtectionResponse { | ||
| id: AdjId((protection.id.0 << 13) | measurement.id.0), | ||
| kind: protection.kind, | ||
| severity: protection.severity, | ||
| time: format_protection_time(protection.time), | ||
| }) | ||
| .collect(), | ||
| }) | ||
| .collect(), | ||
| }) | ||
| }) | ||
| // Collect all the OrderPacketResponses | ||
| .collect(); | ||
| orders.sort_unstable_by_key(|order| order.id.0); | ||
|
|
||
| OrderBoardResponse { | ||
| id: board.id, | ||
| name: board.name.clone(), | ||
| orders, | ||
| } | ||
| }) | ||
| .collect(); | ||
| boards.sort_unstable_by_key(|board| board.id.0); | ||
|
|
||
| OrderResponse { boards } | ||
| } | ||
|
|
||
| /// Builds the flat protection catalog without modifying or consuming the ADJ. | ||
| /// | ||
| /// Protection packet ids and names are reused from the loader. The position in | ||
| /// each compound id selects its definition within the watched measurement. | ||
| /// Packets with no definition at that position are omitted. The resulting list | ||
| /// is sorted by packet id, then board id to break ties. | ||
| pub fn protection_response(&self) -> ProtectionResponse { | ||
| let mut protections: Vec<ProtectionInfoResponse> = self | ||
| .pod_data | ||
| .boards | ||
| .values() | ||
| // For each board | ||
| .flat_map(|board| board.packets.values()) | ||
| .filter_map(|packet| { | ||
| // If the packet is of type Protection... | ||
| let PacketType::Protection(measurement) = &packet.packet_type else { | ||
| return None; | ||
| }; | ||
|
|
||
| let index = usize::from(packet.id.protection_position()).checked_sub(1)?; | ||
| let protection = measurement.protections.get(index)?; | ||
|
|
||
| Some(ProtectionInfoResponse { | ||
| id: packet.id, | ||
| name: packet.name.clone(), | ||
| board_id: packet.board, | ||
| measurement_id: measurement.id, | ||
| kind: protection.kind, | ||
| severity: protection.severity, | ||
| time: format_protection_time(protection.time), | ||
| }) | ||
| }) | ||
| .collect(); | ||
| protections.sort_unstable_by_key(|protection| (protection.id.0, protection.board_id.0)); | ||
|
|
||
| let info = self | ||
| .info | ||
| .protection_types | ||
| .iter() | ||
| .map(|(name, metadata)| { | ||
| ( | ||
| name.clone(), | ||
| ProtectionTypeResponse { | ||
| is_range: metadata.is_range, | ||
| text: metadata.text.clone(), | ||
| text_time: metadata.text_time.clone(), | ||
| }, | ||
| ) | ||
| }) | ||
| .collect(); | ||
|
|
||
| ProtectionResponse { protections, info } | ||
| } | ||
| } | ||
|
|
||
| // Returns Some("(time)xs") if the time is not 0 | ||
| fn format_protection_time(time: Duration) -> Option<String> { | ||
| if time == Duration::new(0, 0) { | ||
| None | ||
| } else { | ||
| // If the duration is in nanoseconds (default) | ||
| let ns = time.as_nanos(); | ||
|
|
||
| // Adjust the result | ||
| if ns.is_multiple_of(1_000_000) { | ||
| Some(format!("{}ms", ns / 1_000_000)) | ||
| } else if ns.is_multiple_of(1_000) { | ||
| Some(format!("{}us", ns / 1_000)) | ||
| } else { | ||
| Some(format!("{}ns", ns)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Matches the kind of measurement and returns the specific type/structure | ||
| fn match_measurement_kind(measurement: &MeasurementKind) -> DataMeasurementKind { | ||
| match measurement { | ||
| MeasurementKind::Numeric { wire_type } => DataMeasurementKind::Numeric { | ||
| wire_type: *wire_type, | ||
| }, | ||
| MeasurementKind::Boolean => DataMeasurementKind::Boolean, | ||
| MeasurementKind::Enum { options } => DataMeasurementKind::Enum { | ||
| options: options.clone(), | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.