smart_city_data v3.0.2 SmartCity.Data View Source

Message struct shared amongst all SmartCity microservices.

const DataMessage = {
    "dataset_id": "",        // UUID
    "payload": {},
    "_metadata": {           // cannot be used safely
        "orgName": "",       // ~r/^[a-zA-Z_]+$/
        "dataName": "",      // ~r/^[a-zA-Z_]+$/
        "stream": true
    },
    "operational": {
        "timing": [{
            "startTime": "", // iso8601
            "endTime": "",   // iso8601
            "app": "",       // microservice generating timing data
            "label": ""      // label for this particular timing data
        }]
    }
}

Link to this section Summary

Functions

Adds a SmartCity.Data.Timing to the list of timings in this SmartCity.Data. The timing will be validated to ensure both start and end times have been set.

Encodes SmartCity.Data into JSON. Typically used right before sending as a Kafka message.

Encodes SmartCity.Data into JSON. Typically used right before sending as a Kafka message.

Get all timings on this Data

Returns a new SmartCity.Data struct. SmartCity.Data.Timing structs will be created along the way.

Creates a new SmartCity.Data struct using new/1 and adds timing information to the message.

Transforms the SmartCity.Data payload field with the given unary function and replaces it in the message.

Link to this section Types

Link to this type

t()

View Source
t() :: %SmartCity.Data{
  _metadata: %{org: String.t(), name: String.t(), stream: boolean()},
  dataset_id: String.t(),
  operational: %{timing: [SmartCity.Data.Timing.t()]},
  payload: String.t(),
  version: String.t()
}

Link to this section Functions

Adds a SmartCity.Data.Timing to the list of timings in this SmartCity.Data. The timing will be validated to ensure both start and end times have been set.

Returns a SmartCity.Data struct with new_timing prepended to existing timings list.

Parameters

  • message: A SmartCity.Data
  • new_timing: A timing you want to add. Must have start_time and end_time set

Encodes SmartCity.Data into JSON. Typically used right before sending as a Kafka message.

Encodes SmartCity.Data into JSON. Typically used right before sending as a Kafka message.

Raises an error if it fails to convert to a JSON string.

Get all timings on this Data

Returns a list of SmartCity.Data.Timing structs or []

Parameters

  • data_message: The message to extract timings from
Link to this function

new(msg)

View Source
new(map() | String.t()) :: {:ok, SmartCity.Data.t()} | {:error, String.t()}

Returns a new SmartCity.Data struct. SmartCity.Data.Timing structs will be created along the way.

Can be created from:

  • map with string keys
  • map with atom keys
  • JSON

Examples

iex> SmartCity.Data.new(%{dataset_id: "a_guid", payload: "the_data", _metadata: %{org: "scos", name: "example"}, operational: %{timing: [%{app: "app name", label: "function name", start_time: "2019-05-06T19:51:41+00:00", end_time: "2019-05-06T19:51:51+00:00"}]}})
{:ok, %SmartCity.Data{
    dataset_id: "a_guid",
    payload: "the_data",
    _metadata: %{org: "scos", name: "example"},
    operational: %{
    timing: [%SmartCity.Data.Timing{ app: "app name", end_time: "2019-05-06T19:51:51+00:00", label: "function name", start_time: "2019-05-06T19:51:41+00:00"}]
  }
}}
Link to this function

timed_new(msg, app)

View Source
timed_new(map(), String.t()) ::
  {:ok, SmartCity.Data.t()} | {:error, String.t()}

Creates a new SmartCity.Data struct using new/1 and adds timing information to the message.

Returns a SmartCity.Data struct with new_timing prepended to existing timings list.

Parameters

Link to this function

timed_transform(msg, app, function)

View Source
timed_transform(
  SmartCity.Data.t(),
  String.t(),
  (payload() -> {:ok, term()} | {:error, term()})
) :: {:ok, SmartCity.Data.t()} | {:error, String.t()}

Transforms the SmartCity.Data payload field with the given unary function and replaces it in the message.

Additionally, returns a SmartCity.Data struct with new_timing prepended to existing timings list.

Parameters

  • message: A SmartCity.Data
  • app: The application that is asking to create the new SmartCity.Data. Ex. reaper or voltron
  • function: an arity 1 (/1) function that will transform the payload in the provided message