-
Notifications
You must be signed in to change notification settings - Fork 9
Show network usage #36
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
9 commits
Select commit
Hold shift + click to select a range
7835831
Add network_usage and peer_count table
majecty 34af1e9
Write network usage to DB
majecty 956b3da
Change the misused TimeoutError to InternalError
majecty 33b8336
Add "graph_network_out_all_node" API
majecty 1b93d92
Show NetworkOutAll Graph
majecty 785c82b
Add NetworkOutAllAVGGraph
majecty cb39f08
Add style to graphs
majecty 07b4a1a
Update library version
majecty c17ce9c
Make the heap size limit bigger
majecty 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| pub mod agent_extra; | ||
| pub mod config; | ||
| pub mod logs; | ||
| pub mod network_usage; | ||
| pub mod network_usage_graph; | ||
| pub mod peer_count; |
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,47 @@ | ||
| use chrono; | ||
| use postgres; | ||
| use regex::{Captures, Regex}; | ||
|
|
||
| use common_rpc_types::NetworkUsage; | ||
|
|
||
| pub fn insert( | ||
| conn: &postgres::Connection, | ||
| node_name: &str, | ||
| network_usage: NetworkUsage, | ||
| time: chrono::DateTime<chrono::Utc>, | ||
| ) -> postgres::Result<()> { | ||
| ctrace!("Add network usage of {}", node_name); | ||
|
|
||
| if network_usage.is_empty() { | ||
| return Ok(()) | ||
| } | ||
|
|
||
| let stmt = conn | ||
| .prepare("INSERT INTO network_usage (time, name, extension, target_ip, bytes) VALUES ($1, $2, $3, $4, $5)")?; | ||
| for key in network_usage.keys() { | ||
| let parse_result = parse_network_usage_key(key); | ||
| let (extension, ip) = match parse_result { | ||
| Ok((extension, ip)) => (extension, ip), | ||
| Err(err) => { | ||
| cerror!("Network Usage Parse Failed {:?}", err); | ||
| // FIXME: propagate the error | ||
| return Ok(()) | ||
| } | ||
| }; | ||
| let bytes = network_usage[key]; | ||
| stmt.execute(&[&time, &node_name, &extension, &ip, &bytes])?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn parse_network_usage_key(key: &str) -> Result<(String, String), String> { | ||
| // Ex) ::block-propagation@54.180.74.243:3485 | ||
| lazy_static! { | ||
| static ref KEY_REGEX: Regex = Regex::new(r"::(?P<extension>[a-zA-Z\-]*)@(?P<ip>[0-9\.]*)").unwrap(); | ||
| } | ||
|
|
||
| let reg_result: Captures = KEY_REGEX.captures(key).ok_or_else(|| "Parse Error".to_string())?; | ||
|
|
||
| Ok((reg_result["extension"].to_string(), reg_result["ip"].to_string())) | ||
| } |
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,71 @@ | ||
| use common_rpc_types::{GraphCommonArgs, GraphNetworkOutAllRow, GraphPeriod}; | ||
| use postgres; | ||
|
|
||
| pub fn query_network_out_all( | ||
| conn: &postgres::Connection, | ||
| graph_args: GraphCommonArgs, | ||
| ) -> postgres::Result<Vec<GraphNetworkOutAllRow>> { | ||
| let query_stmt = format!( | ||
| "\ | ||
| SELECT \ | ||
| name, \ | ||
| {}, \ | ||
| CAST (AVG(bytes) AS REAL) as value \ | ||
| FROM \"network_usage\" \ | ||
| WHERE \"time\"<$1 and \"time\">$2 \ | ||
| GROUP BY \"name\", \"rounded_time\" \ | ||
| ORDER BY \"name\", \"rounded_time\" ASC", | ||
| get_sql_round_period_expression(graph_args.period) | ||
| ); | ||
|
|
||
| let rows = conn.query(&query_stmt, &[&graph_args.to, &graph_args.from])?; | ||
|
|
||
| Ok(rows | ||
| .into_iter() | ||
| .map(|row| GraphNetworkOutAllRow { | ||
| node_name: row.get("name"), | ||
| time: row.get("rounded_time"), | ||
| value: row.get("value"), | ||
| }) | ||
| .collect()) | ||
| } | ||
|
|
||
| fn get_sql_round_period_expression(period: GraphPeriod) -> &'static str { | ||
| match period { | ||
| GraphPeriod::Minutes5 => { | ||
| "date_trunc('hour', \"network_usage\".time) + INTERVAL '5 min' * ROUND(date_part('minute', \"network_usage\".time) / 5.0) as \"rounded_time\"" | ||
| } | ||
| GraphPeriod::Hour => "date_trunc('hour', \"network_usage\".time) as \"rounded_time\"", | ||
| GraphPeriod::Day => "date_trunc('day', \"network_usage\".time) as \"rounded_time\"", | ||
| } | ||
| } | ||
|
|
||
| pub fn query_network_out_all_avg( | ||
| conn: &postgres::Connection, | ||
| graph_args: GraphCommonArgs, | ||
| ) -> postgres::Result<Vec<GraphNetworkOutAllRow>> { | ||
| let query_stmt = format!( | ||
| "\ | ||
| SELECT \ | ||
| \"network_usage\".name, \ | ||
| {}, \ | ||
| CAST (AVG(bytes/\"peer_count\".\"peer_count\") AS REAL) as value \ | ||
| FROM \"network_usage\" \ | ||
| LEFT JOIN peer_count ON (\"network_usage\".\"time\"=\"peer_count\".\"time\") \ | ||
| WHERE \"network_usage\".\"time\"<$1 and \"network_usage\".\"time\">$2 \ | ||
| GROUP BY \"network_usage\".\"name\", \"rounded_time\" \ | ||
| ORDER BY \"network_usage\".\"name\", \"rounded_time\" ASC", | ||
| get_sql_round_period_expression(graph_args.period) | ||
| ); | ||
|
|
||
| let rows = conn.query(&query_stmt, &[&graph_args.to, &graph_args.from])?; | ||
|
|
||
| Ok(rows | ||
| .into_iter() | ||
| .map(|row| GraphNetworkOutAllRow { | ||
| node_name: row.get("name"), | ||
| time: row.get("rounded_time"), | ||
| value: row.get("value"), | ||
| }) | ||
| .collect()) | ||
| } |
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,18 @@ | ||
| use chrono; | ||
| use postgres; | ||
|
|
||
| pub fn insert( | ||
| conn: &postgres::Connection, | ||
| node_name: &str, | ||
| peer_count: i32, | ||
| time: chrono::DateTime<chrono::Utc>, | ||
| ) -> postgres::Result<()> { | ||
| ctrace!("Add peer count of {}", node_name); | ||
|
|
||
| conn.execute("INSERT INTO peer_count (time, name, peer_count) VALUES ($1, $2, $3)", &[ | ||
| &time, | ||
| &node_name, | ||
| &peer_count, | ||
| ])?; | ||
| Ok(()) | ||
| } |
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.