Metric APIs

A Block developer may want to publish certain metrics associated with the blocks. These metrics can either be simply stored to use in some computations or can be displayed using charts on the platform. Metrics can be divided into below categories:
Hardware Metrics: Hardware Metrics contains information regarding Memory and CPU usage of a block.
Queue Metrics: Queue Metrics provides information regarding amount of data produced to or consumed from a queue for each block in terms of number of records read/written over time.
Data Metrics: Data Metrics is used to show block specific data as mentioned by the user. Data Metrics can be either time relative or not. The former shows the data v/s time graph whereas only the last updated data is shown in the latter.

The user need not provide data for creating Hardware and Queue metrics as these are in-built.

from pipelineblocksdk.api import BBSDK as sdk
metrics_handler = sdk.metrics_api()	
metrics = dict()
metrics["appId"] = "BLOCKNAME"		 # this is name of the block for which metrics are shown.
metrics["keys"] = {"runId": block_params["pipelineRunID"]}  # keys is passed with the runID of the pipeline  							   # in which the block is running.
metrics["metricId"] = "BLOCK_READ_TIME"	 # this is label name given for the metric.  
metrics["name"] = "LINE_GRAPH_v1.1"

metrics["metrics"] = {
   str(int(time.time())): {
       "x": int(time.time()),
       "y": [records_read],
       "x_label": "TIME",
       "y_label": ["LINES READ"],
       "x_type": "DATE",
       "y_type": "INT"
   }
}
self.metrics_handler.save_metrics_time(metrics)

The above code adds metrics for the block ‘BLOCKNAME’ to the Data Metrics to be shown when the block is run. The metricId should be given with the name that is to be displayed on top of the graph/metric. Here metrics_handler is an instance of the class MetricsAPI(), which uses the save_metrics_time() method to save metrics to server. Metrics may not necessarily be a graph. It can also be any stats, or map etc. The type of metric is based on the value given to metrics[“name”]. Based on this the schema has to be set for metrics[“metrics”]. Some of the metrics available are - LINE_GRAPH_v1.1, BAR_GRAPH, PIE_CHART, HEAT_MAP, SCATTER_GRAPH.

Following are the schemas available for different graphs we support -

LINEGRAPH_V1.1:

{
  'x': integer,
  'y': [number, number],
  'x_label': 'string',              
  'y_label': ['string1', 'string2'],
  'x_type': 'DATE',
  'y_type': 'INTEGER'
}

PIE_CHART:

{
  'data': [ {
        'values': [integer, integer],
        'labels': ['string1', 'string2'],
        'domain': {
           'x': [integer, integer],
           'y': [integer, integer]
        }
        'name': 'string',
        'marker': {
           'color': ['rgb(24, 56, 67)', 'string2']
        }
     }
  ]
}

HEAT_MAP:

{
   'data': [
       {
           'z': [[integer, integer], 
   [integer, integer]],
           'x': ['string1', 'string2'],
           'y': ['string1', 'string2'],
           'colorscale': [[integer, integer]]
       }
   ],
   'layout': {
       'xaxis': {
           'title': 'string'
       },
       'yaxis': {
           'title': 'string'
       }
   }
}

SCATTER_GRAPH:

{
   'data': [
       {
           'fill': 'string1',
           'x': [['string', number],
                ['string', number]],
           'y': [number, number],
           'name': 'string'
       }
   ],
   'layout': {
       'xaxis': {
           'title': 'string'
       },
       'yaxis': {
           'title': 'string'
       },
       'showlegend': 'false'
   }
}

BAR_GRAPH:

{
   'data': [ {
           'x': [['string', number],
                ['string', number]],  
           'y': [number, number],
           'name': 'string',
           	
           'marker': {
               'color': ['rgb(204, 201, 132, 1)', 'string2']
           },
           'base': [integer, integer],
           'width': [number, number],
           'text': ['string', 'string']
       }
   ],
   'layout': {
       'barmode': 'stack',     		# alternate_values = 'group', 'relative'
       'xaxis': {
           'title': 'string'
       },
       'yaxis':{
           'title': 'string'
       }
   }
}