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§

source§

impl Frame

source

pub fn new(name: impl Into<String>) -> Self

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");
source

pub fn add_field(&mut self, field: Field)

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");
source

pub fn fields(&self) -> &[Field]

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");
source

pub fn fields_mut(&mut self) -> &mut [Field]

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());
source

pub fn check(&self) -> Result<CheckedFrame<'_>, Error>

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()
);
source

pub fn with_name(self, name: impl Into<String>) -> Self

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");
source

pub fn with_metadata(self, metadata: impl Into<Option<Metadata>>) -> Self

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()));
source

pub fn with_field(self, field: Field) -> Self

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");
source

pub fn with_fields(self, fields: impl IntoIterator<Item = Field>) -> Self

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");
source

pub fn set_channel(&mut self, channel: Channel)

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§

source§

impl Debug for Frame

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Frame

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: IntoFrame> FromFields<T> for Frame

source§

fn from_fields(name: impl Into<String>, fields: T) -> Frame

Create a Frame with the given name from fields.
source§

impl Index<&str> for Frame

§

type Output = Field

The returned type after indexing.
source§

fn index(&self, name: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<usize> for Frame

§

type Output = Field

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<&str> for Frame

source§

fn index_mut(&mut self, name: &str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<usize> for Frame

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl Serialize for Frame

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Frame

§

impl Send for Frame

§

impl Sync for Frame

§

impl Unpin for Frame

§

impl !UnwindSafe for Frame

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,