pub struct Frame {
    pub name: String,
    pub meta: Option<Metadata>,
    /* private fields */
}
Expand description

A structured, two-dimensional data frame.

Frames 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 Fields.

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.

Frames 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, Notice, Severity};

let mut notice = Notice::new("read this notice".to_string());
notice.severity = Some(Severity::Warning);
let mut metadata = Metadata::default();
metadata.path = Some("a path".to_string());
metadata.notices = Some(vec![notice]);
let frame = Frame::new("frame").with_metadata(metadata);
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

Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Create a Frame with the given name from fields.
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Wrap the input message T in a tonic::Request
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
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