timeseries_shaper.loader.metadata.metadata_api_loader

  1import requests
  2import pandas as pd
  3import json
  4from typing import List, Dict
  5
  6
  7class DatapointAPI:
  8    """
  9    Class for accessing datapoints for multiple devices via an API.
 10    """
 11
 12    def __init__(self, device_names: List[str], base_url: str, api_token: str, output_path: str = "data", required_uuid_list: List[str] = None, filter_enabled: bool = True):
 13        """
 14        Initialize the DatapointAPI class.
 15
 16        :param device_names: List of device names to retrieve metadata for.
 17        :param base_url: Base URL of the API.
 18        :param api_token: API token for authentication.
 19        :param output_path: Directory to save the data points JSON files.
 20        :param required_uuid_list: Mixed list of UUIDs to filter the metadata across devices (optional).
 21        :param filter_enabled: Whether to filter metadata by "enabled == True" (default is True).
 22        """
 23        self.device_names = device_names
 24        self.base_url = base_url
 25        self.api_token = api_token
 26        self.output_path = output_path
 27        self.required_uuid_list = required_uuid_list or []  # Defaults to an empty list if None
 28        self.filter_enabled = filter_enabled
 29        self.device_metadata: Dict[str, pd.DataFrame] = {}  # Store metadata for each device
 30        self.device_uuids: Dict[str, List[str]] = {}  # Store UUIDs for each device
 31        self._api_access()
 32
 33    def _api_access(self) -> None:
 34        """Connect to the API and retrieve metadata for the specified devices."""
 35        headers = {
 36            "Content-Type": "application/json",
 37            "Authorization": f"Bearer {self.api_token}",
 38        }
 39
 40        for device_name in self.device_names:
 41            metadata = []
 42            devices_found = []
 43
 44            for datatron in requests.get(f"{self.base_url}", headers=headers).json():
 45                for device in requests.get(f"{self.base_url}/{datatron['id']}/devices", headers=headers).json():
 46                    if device["name"] == device_name:
 47                        datapoints = requests.get(
 48                            f"{self.base_url}/{datatron['id']}/devices/{device['id']}/data_points",
 49                            headers=headers,
 50                        ).json()
 51                        metadata += datapoints
 52                        devices_found.append(device["name"])
 53                    if devices_found:
 54                        break
 55                if devices_found:
 56                    break
 57
 58            # Process metadata for the current device
 59            metadata_df = pd.DataFrame(metadata)
 60            if not metadata_df.empty:
 61                if self.filter_enabled:
 62                    metadata_df = metadata_df[metadata_df["enabled"] == True]
 63
 64                metadata_df = metadata_df[["uuid", "label", "config"]]
 65
 66                # Filter metadata by required UUIDs, if any
 67                if self.required_uuid_list:
 68                    metadata_df = metadata_df[metadata_df["uuid"].isin(self.required_uuid_list)]
 69
 70                # Store processed metadata and UUIDs
 71                self.device_metadata[device_name] = metadata_df
 72                self.device_uuids[device_name] = metadata_df["uuid"].tolist()
 73
 74                # Export JSON file for this device
 75                self._export_json(metadata_df.to_dict(orient="records"), device_name)
 76
 77    def _export_json(self, data_points: List[Dict[str, str]], device_name: str) -> None:
 78        """Export data points to a JSON file for the specified device."""
 79        file_name = f"{self.output_path}/{device_name.replace(' ', '_')}_data_points.json"
 80        with open(file_name, 'w') as f:
 81            json.dump(data_points, f, indent=2)
 82
 83    def get_all_uuids(self) -> Dict[str, List[str]]:
 84        """Return a dictionary of UUIDs for each device."""
 85        return self.device_uuids
 86
 87    def get_all_metadata(self) -> Dict[str, List[Dict[str, str]]]:
 88        """Return a dictionary of metadata for each device."""
 89        return {device: metadata.to_dict(orient="records") for device, metadata in self.device_metadata.items()}
 90
 91    def display_dataframe(self, device_name: str = None) -> None:
 92        """
 93        Print the metadata DataFrame for a specific device or all devices.
 94
 95        :param device_name: Name of the device to display metadata for (optional).
 96                            If None, displays metadata for all devices.
 97        """
 98        if device_name:
 99            # Display metadata for a specific device
100            if device_name in self.device_metadata:
101                print(f"Metadata for device: {device_name}")
102                print(self.device_metadata[device_name])
103            else:
104                print(f"No metadata found for device: {device_name}")
105        else:
106            # Display metadata for all devices
107            for device, metadata in self.device_metadata.items():
108                print(f"\nMetadata for device: {device}")
109                print(metadata)
class DatapointAPI:
  8class DatapointAPI:
  9    """
 10    Class for accessing datapoints for multiple devices via an API.
 11    """
 12
 13    def __init__(self, device_names: List[str], base_url: str, api_token: str, output_path: str = "data", required_uuid_list: List[str] = None, filter_enabled: bool = True):
 14        """
 15        Initialize the DatapointAPI class.
 16
 17        :param device_names: List of device names to retrieve metadata for.
 18        :param base_url: Base URL of the API.
 19        :param api_token: API token for authentication.
 20        :param output_path: Directory to save the data points JSON files.
 21        :param required_uuid_list: Mixed list of UUIDs to filter the metadata across devices (optional).
 22        :param filter_enabled: Whether to filter metadata by "enabled == True" (default is True).
 23        """
 24        self.device_names = device_names
 25        self.base_url = base_url
 26        self.api_token = api_token
 27        self.output_path = output_path
 28        self.required_uuid_list = required_uuid_list or []  # Defaults to an empty list if None
 29        self.filter_enabled = filter_enabled
 30        self.device_metadata: Dict[str, pd.DataFrame] = {}  # Store metadata for each device
 31        self.device_uuids: Dict[str, List[str]] = {}  # Store UUIDs for each device
 32        self._api_access()
 33
 34    def _api_access(self) -> None:
 35        """Connect to the API and retrieve metadata for the specified devices."""
 36        headers = {
 37            "Content-Type": "application/json",
 38            "Authorization": f"Bearer {self.api_token}",
 39        }
 40
 41        for device_name in self.device_names:
 42            metadata = []
 43            devices_found = []
 44
 45            for datatron in requests.get(f"{self.base_url}", headers=headers).json():
 46                for device in requests.get(f"{self.base_url}/{datatron['id']}/devices", headers=headers).json():
 47                    if device["name"] == device_name:
 48                        datapoints = requests.get(
 49                            f"{self.base_url}/{datatron['id']}/devices/{device['id']}/data_points",
 50                            headers=headers,
 51                        ).json()
 52                        metadata += datapoints
 53                        devices_found.append(device["name"])
 54                    if devices_found:
 55                        break
 56                if devices_found:
 57                    break
 58
 59            # Process metadata for the current device
 60            metadata_df = pd.DataFrame(metadata)
 61            if not metadata_df.empty:
 62                if self.filter_enabled:
 63                    metadata_df = metadata_df[metadata_df["enabled"] == True]
 64
 65                metadata_df = metadata_df[["uuid", "label", "config"]]
 66
 67                # Filter metadata by required UUIDs, if any
 68                if self.required_uuid_list:
 69                    metadata_df = metadata_df[metadata_df["uuid"].isin(self.required_uuid_list)]
 70
 71                # Store processed metadata and UUIDs
 72                self.device_metadata[device_name] = metadata_df
 73                self.device_uuids[device_name] = metadata_df["uuid"].tolist()
 74
 75                # Export JSON file for this device
 76                self._export_json(metadata_df.to_dict(orient="records"), device_name)
 77
 78    def _export_json(self, data_points: List[Dict[str, str]], device_name: str) -> None:
 79        """Export data points to a JSON file for the specified device."""
 80        file_name = f"{self.output_path}/{device_name.replace(' ', '_')}_data_points.json"
 81        with open(file_name, 'w') as f:
 82            json.dump(data_points, f, indent=2)
 83
 84    def get_all_uuids(self) -> Dict[str, List[str]]:
 85        """Return a dictionary of UUIDs for each device."""
 86        return self.device_uuids
 87
 88    def get_all_metadata(self) -> Dict[str, List[Dict[str, str]]]:
 89        """Return a dictionary of metadata for each device."""
 90        return {device: metadata.to_dict(orient="records") for device, metadata in self.device_metadata.items()}
 91
 92    def display_dataframe(self, device_name: str = None) -> None:
 93        """
 94        Print the metadata DataFrame for a specific device or all devices.
 95
 96        :param device_name: Name of the device to display metadata for (optional).
 97                            If None, displays metadata for all devices.
 98        """
 99        if device_name:
100            # Display metadata for a specific device
101            if device_name in self.device_metadata:
102                print(f"Metadata for device: {device_name}")
103                print(self.device_metadata[device_name])
104            else:
105                print(f"No metadata found for device: {device_name}")
106        else:
107            # Display metadata for all devices
108            for device, metadata in self.device_metadata.items():
109                print(f"\nMetadata for device: {device}")
110                print(metadata)

Class for accessing datapoints for multiple devices via an API.

DatapointAPI( device_names: List[str], base_url: str, api_token: str, output_path: str = 'data', required_uuid_list: List[str] = None, filter_enabled: bool = True)
13    def __init__(self, device_names: List[str], base_url: str, api_token: str, output_path: str = "data", required_uuid_list: List[str] = None, filter_enabled: bool = True):
14        """
15        Initialize the DatapointAPI class.
16
17        :param device_names: List of device names to retrieve metadata for.
18        :param base_url: Base URL of the API.
19        :param api_token: API token for authentication.
20        :param output_path: Directory to save the data points JSON files.
21        :param required_uuid_list: Mixed list of UUIDs to filter the metadata across devices (optional).
22        :param filter_enabled: Whether to filter metadata by "enabled == True" (default is True).
23        """
24        self.device_names = device_names
25        self.base_url = base_url
26        self.api_token = api_token
27        self.output_path = output_path
28        self.required_uuid_list = required_uuid_list or []  # Defaults to an empty list if None
29        self.filter_enabled = filter_enabled
30        self.device_metadata: Dict[str, pd.DataFrame] = {}  # Store metadata for each device
31        self.device_uuids: Dict[str, List[str]] = {}  # Store UUIDs for each device
32        self._api_access()

Initialize the DatapointAPI class.

Parameters
  • device_names: List of device names to retrieve metadata for.
  • base_url: Base URL of the API.
  • api_token: API token for authentication.
  • output_path: Directory to save the data points JSON files.
  • required_uuid_list: Mixed list of UUIDs to filter the metadata across devices (optional).
  • filter_enabled: Whether to filter metadata by "enabled == True" (default is True).
device_names
base_url
api_token
output_path
required_uuid_list
filter_enabled
device_metadata: Dict[str, pandas.core.frame.DataFrame]
device_uuids: Dict[str, List[str]]
def get_all_uuids(self) -> Dict[str, List[str]]:
84    def get_all_uuids(self) -> Dict[str, List[str]]:
85        """Return a dictionary of UUIDs for each device."""
86        return self.device_uuids

Return a dictionary of UUIDs for each device.

def get_all_metadata(self) -> Dict[str, List[Dict[str, str]]]:
88    def get_all_metadata(self) -> Dict[str, List[Dict[str, str]]]:
89        """Return a dictionary of metadata for each device."""
90        return {device: metadata.to_dict(orient="records") for device, metadata in self.device_metadata.items()}

Return a dictionary of metadata for each device.

def display_dataframe(self, device_name: str = None) -> None:
 92    def display_dataframe(self, device_name: str = None) -> None:
 93        """
 94        Print the metadata DataFrame for a specific device or all devices.
 95
 96        :param device_name: Name of the device to display metadata for (optional).
 97                            If None, displays metadata for all devices.
 98        """
 99        if device_name:
100            # Display metadata for a specific device
101            if device_name in self.device_metadata:
102                print(f"Metadata for device: {device_name}")
103                print(self.device_metadata[device_name])
104            else:
105                print(f"No metadata found for device: {device_name}")
106        else:
107            # Display metadata for all devices
108            for device, metadata in self.device_metadata.items():
109                print(f"\nMetadata for device: {device}")
110                print(metadata)

Print the metadata DataFrame for a specific device or all devices.

Parameters
  • device_name: Name of the device to display metadata for (optional). If None, displays metadata for all devices.