Struct grafana_plugin_sdk::data::Frame [−][src]
Expand description
A structured, two-dimensional data frame.
Frame
s can be created manually using Frame::new
if desired.
Alternatively, the IntoFrame
trait (and its reciprocal, FromFields
)
provide a convenient way to create a frame from an iterator of Field
s.
Frames generally can’t be passed back to the SDK without first being checked
that they are valid. The Frame::check
method performs all required checks
(e.g. that all fields have the same length), and returns a [CheckedFrame<'_>
]
which contains a reference to the original frame. This can then be used
throughout the rest of the SDK (e.g. to be sent back to Grafana).
Examples
Creating a Frame using the Frame::new
:
use grafana_plugin_sdk::{
data::{Field, Frame},
prelude::*,
};
let field = [1_u32, 2, 3].into_field("x");
let frame = Frame::new("new")
.with_field(field);
Using the IntoFrame
trait:
use grafana_plugin_sdk::prelude::*;
let frame = [
[1_u32, 2, 3].into_field("x"),
["a", "b", "c"].into_field("y"),
]
.into_frame("super convenient");
Fields can be accessed using either Frame::fields
and Frame::fields_mut
, or
by using the Index
and IndexMut
implementations
with field indexes or names:
use grafana_plugin_sdk::prelude::*;
let frame = [
[1_u32, 2, 3].into_field("x"),
["a", "b", "c"].into_field("y"),
]
.into_frame("frame");
assert_eq!(
frame.fields()[0].name,
"x",
);
assert_eq!(
frame.fields()[1],
frame["y"],
);
Fields
name: String
The name of this frame.
meta: Option<Metadata>
Optional metadata describing this frame.
This can include custom metadata.
Implementations
Create a new, empty Frame
with no fields and no metadata.
Examples
Creating a Frame using the Frame::new
:
use grafana_plugin_sdk::{
data::Frame,
prelude::*,
};
let frame = Frame::new("frame");
assert_eq!(&frame.name, "frame");
Add a field to this frame.
This is similar to Frame::with_field
but takes the frame by mutable reference.
Example
use grafana_plugin_sdk::prelude::*;
let mut frame = [
[1_u32, 2, 3].into_field("x"),
]
.into_frame("frame");
assert_eq!(frame.fields().len(), 1);
frame.add_field(["a", "b", "c"].into_field("y"));
assert_eq!(frame.fields().len(), 2);
assert_eq!(&frame.fields()[1].name, "y");
Get an immutable reference to the the Fields
of this Frame
.
Example
use grafana_plugin_sdk::prelude::*;
let mut frame = [
[1_u32, 2, 3].into_field("x"),
]
.into_frame("frame");
assert_eq!(frame.fields().len(), 1);
assert_eq!(&frame.fields()[0].name, "x");
Get a mutable reference to the the Fields
of this Frame
.
Example
use arrow2::array::{PrimitiveArray, Utf8Array};
use grafana_plugin_sdk::prelude::*;
// Create an initial `Frame`.
let mut frame = [
[1_u32, 2, 3].into_field("x"),
["a", "b", "c"].into_field("y"),
]
.into_frame("frame");
// Update the frame's fields.
frame.fields_mut()[0].set_values(4u32..7).unwrap();
frame.fields_mut()[1].set_values(["d", "e", "f"]).unwrap();
assert_eq!(
frame
.fields()[0]
.values()
.as_any()
.downcast_ref::<PrimitiveArray<u32>>()
.unwrap()
.iter()
.collect::<Vec<_>>(),
vec![Some(&4), Some(&5), Some(&6)],
);
assert_eq!(
frame
.fields()[1]
.values()
.as_any()
.downcast_ref::<Utf8Array<i32>>()
.unwrap()
.iter()
.collect::<Vec<_>>(),
vec![Some("d"), Some("e"), Some("f")],
);
assert!(frame.check().is_ok());
Check this unchecked Frame, returning a CheckedFrame
ready for serializing.
Frame
s may be in an intermediate state whilst being constructed (for example,
their field lengths may differ). Calling this method validates the frame and
returns a CheckedFrame
if successful. Checked frames can then be used
throughout the rest of the SDK (e.g. to be sent back to Grafana).
Errors
Returns an error if the fields of self
do not all have the same length.
Example
use arrow2::array::{PrimitiveArray, Utf8Array};
use grafana_plugin_sdk::prelude::*;
assert!(
[
[1_u32, 2, 3].into_field("x"),
["a", "b", "c"].into_field("y"),
]
.into_frame("frame")
.check()
.is_ok()
);
assert!(
[
[1_u32, 2, 3, 4].into_field("x"),
["a", "b", "c"].into_field("y"),
]
.into_frame("frame")
.check()
.is_err()
);
Return a new frame with the given name.
Example
use grafana_plugin_sdk::data::Frame;
let frame = Frame::new("frame")
.with_name("other name");
assert_eq!(&frame.name, "other name");
Return a new frame with the given metadata.
Example
use grafana_plugin_sdk::data::{Frame, Metadata};
let frame = Frame::new("frame")
.with_metadata(None);
assert_eq!(frame.meta, None);
let frame = Frame::new("frame")
.with_metadata(Metadata {
path: Some("a path".to_string()),
..Metadata::default()
});
assert_eq!(frame.meta.unwrap().path, Some("a path".to_string()));
Return a new frame with an added field.
Example
use grafana_plugin_sdk::{data::Frame, prelude::*};
let frame = Frame::new("frame")
.with_field([1_u32, 2, 3].into_field("x"))
.with_field(["a", "b", "c"].into_field("y"));
assert_eq!(frame.fields().len(), 2);
assert_eq!(&frame.fields()[0].name, "x");
assert_eq!(&frame.fields()[1].name, "y");
Return a new frame with added fields.
Example
use grafana_plugin_sdk::{data::Frame, prelude::*};
let frame = Frame::new("frame")
.with_fields([
[1_u32, 2, 3].into_field("x"),
["a", "b", "c"].into_field("y"),
]);
assert_eq!(frame.fields().len(), 2);
assert_eq!(&frame.fields()[0].name, "x");
assert_eq!(&frame.fields()[1].name, "y");
Set the channel of the frame.
This is used when a frame can be ‘upgraded’ to a streaming response, to tell Grafana that the given channel should be used to subscribe to updates to this frame.
Trait Implementations
Deserialize this value from the given Serde deserializer. Read more
fn from_fields(name: impl Into<String>, fields: T) -> FrameⓘNotable traits for Frameimpl<T: IntoFrame> FromFields<T> for Frame
fn from_fields(name: impl Into<String>, fields: T) -> FrameⓘNotable traits for Frameimpl<T: IntoFrame> FromFields<T> for Frame
impl<T: IntoFrame> FromFields<T> for Frame
Create a Frame
with the given name from fields
.
Auto Trait Implementations
impl !RefUnwindSafe for Frame
impl !UnwindSafe for Frame
Blanket Implementations
Mutably borrows from an owned value. Read more
Wrap the input message T
in a tonic::Request
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more