Skip to content

Commit

Permalink
feat(tui): show history of json selection
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Nov 15, 2020
1 parent fdd3805 commit 33e262d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/interactive/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn draw_details<B>(
chunks[1]
};

history::draw(f, history_area, topic_history);
history::draw(f, history_area, topic_history, &json_view_state.selected());
}

fn draw_payload_string<B>(f: &mut Frame<B>, area: Rect, bytes: usize, payload: &str)
Expand Down
38 changes: 24 additions & 14 deletions src/interactive/ui/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::Ordering;

use chrono::{DateTime, Local};
use json::JsonValue;
use rumqttc::QoS;
use tui::backend::Backend;
use tui::layout::{Constraint, Layout, Rect};
Expand All @@ -10,6 +11,7 @@ use tui::widgets::{Axis, Block, Borders, Chart, Dataset, GraphType, Row, Table,
use tui::{symbols, Frame};

use crate::format::{self};
use crate::json_view;
use crate::mqtt_history::HistoryEntry;

#[derive(Debug, PartialEq)]
Expand All @@ -24,8 +26,15 @@ pub struct DataPoint {
pub value: Result<String, String>,
}

fn stringify_jsonlike_string(source: &str, selection: &[usize]) -> Result<String, String> {
let root = json::parse(source).map_err(|err| format!("invalid json: {}", err))?;
json_view::get_selected_subvalue(&root, selection)
.ok_or_else(|| String::from("selection is not possible in json"))
.map(JsonValue::dump)
}

impl DataPoint {
pub fn parse_from_history_entry(entry: &HistoryEntry) -> Self {
pub fn parse_from_history_entry(entry: &HistoryEntry, json_selector: &[usize]) -> Self {
let time = if entry.packet.retain {
PacketTime::Retained
} else {
Expand All @@ -34,16 +43,10 @@ impl DataPoint {

let qos = entry.packet.qos;
let value = String::from_utf8(entry.packet.payload.to_vec())
.map_err(|err| format!("invalid UTF8: {}", err));
DataPoint { time, qos, value }
}
.map_err(|err| format!("invalid UTF8: {}", err))
.map(|string| stringify_jsonlike_string(&string, json_selector).map_or(string, |s| s));

pub fn parse_from_history_entries(entries: &[HistoryEntry]) -> Vec<Self> {
let mut data = Vec::new();
for entry in entries {
data.push(DataPoint::parse_from_history_entry(entry));
}
data
DataPoint { time, qos, value }
}

fn optional_time(&self) -> Option<DateTime<Local>> {
Expand Down Expand Up @@ -143,11 +146,18 @@ impl GraphDataPoints {
}
}

pub fn draw<B>(f: &mut Frame<B>, area: Rect, topic_history: &[HistoryEntry])
where
pub fn draw<B>(
f: &mut Frame<B>,
area: Rect,
topic_history: &[HistoryEntry],
json_selector: &[usize],
) where
B: Backend,
{
let data = DataPoint::parse_from_history_entries(&topic_history);
let mut data = Vec::new();
for entry in topic_history {
data.push(DataPoint::parse_from_history_entry(entry, json_selector));
}

let table_area = GraphDataPoints::parse_from_datapoints(&data).map_or(area, |points| {
let chunks = Layout::default()
Expand Down Expand Up @@ -199,7 +209,7 @@ where
}
title += ")";

let header = ["Time", "QoS", "Payload"];
let header = ["Time", "QoS", "Value"];

let mut rows_content: Vec<Vec<String>> = Vec::new();
for entry in topic_history {
Expand Down
7 changes: 5 additions & 2 deletions src/json_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ fn get_nth_subvalue(root: &JsonValue, select: usize) -> Option<&JsonValue> {
}
}

pub fn get_selected_subvalue<'a>(root: &'a JsonValue, selection: &[u8]) -> Option<&'a JsonValue> {
pub fn get_selected_subvalue<'a>(
root: &'a JsonValue,
selection: &[usize],
) -> Option<&'a JsonValue> {
let mut current = root;
for select in selection {
current = get_nth_subvalue(current, select.to_owned() as usize)?;
current = get_nth_subvalue(current, select.to_owned())?;
}

Some(current)
Expand Down

0 comments on commit 33e262d

Please sign in to comment.