def to_csvdb(self, serialized_report) -> Tuple[List[str], Dict]: """ Return raw data from serialized report :return: Header, Dict(str, List(Dict)) """ basic_csv_row = { key: serialized_report[key] for key in CSV_HEADER_COMMON } # timestamp basic_csv_row['timestamp'] = datetime_to_timestamp( basic_csv_row['timestamp']) final_dict = {} try: for group_name, sockets in serialized_report['groups'].items(): new_csv_row = basic_csv_row.copy() for socket_name, cpus in sockets.items(): new_csv_row['socket'] = socket_name for cpu_name, events in cpus.items(): new_csv_row['cpu'] = cpu_name for event_name, value in events.items(): new_csv_row[event_name] = value # add in final dict if group_name in final_dict: final_dict[group_name].append(new_csv_row.copy()) else: final_dict[group_name] = [new_csv_row.copy()] except KeyError: raise BadInputData() return CSV_HEADER_HWPC, final_dict
def from_csvdb(self, file_name, row) -> Dict: """ Get the csvdb report { 'timestamp': ... 'sensor': ... 'target': ... 'power': ... 'metadata': { ... } } """ final_dict: Dict = {} try: final_dict = {key: row[key] for key in CSV_HEADER_POWER} final_dict['timestamp'] = timestamp_to_datetime( int(row['timestamp'])) final_dict['power'] = float(row['power']) final_dict['metadata'] = {} for key in row.keys(): if key not in CSV_HEADER_POWER: final_dict['metadata'][key] = row[key] except KeyError: raise BadInputData() return final_dict
def to_csvdb(self, serialized_report) -> Tuple[List[str], Dict]: """ Return raw data from serialized report { 'PowerReport' : [{ 'timestamp': ... 'sensor': ... 'target': ... 'power': ... 'metadata': { ... } }] } """ final_dict = {} try: for field in CSV_HEADER_POWER: final_dict[field] = serialized_report[field] final_dict['timestamp'] = datetime_to_timestamp( serialized_report['timestamp']) for key, val in serialized_report['metadata'].items(): final_dict[key] = val except KeyError: raise BadInputData() final_dict = {self.get_type().__name__: [final_dict]} return CSV_HEADER_POWER, final_dict
def to_influxdb(self, serialized_report) -> Dict: """ Return raw data from serialized report { 'measurement' : 'power_consumption' 'timestamp': ... 'tags' : { ... } 'fields': { 'power': ... } } """ if ('sensor' not in serialized_report or 'target' not in serialized_report or 'metadata' not in serialized_report or 'timestamp' not in serialized_report or 'power' not in serialized_report): raise BadInputData() tags = {tag: serialized_report[tag] for tag in self.get_tags()} for metadata_name in self._influxdb_keept_metadata(): if metadata_name not in serialized_report['metadata']: pass else: tags[metadata_name] = serialized_report['metadata'][metadata_name] return { 'measurement': 'power_consumption', 'tags': tags, 'time': str(serialized_report['timestamp']), 'fields': { 'power': serialized_report['power'] } }
def to_prometheus(self, serialized_report) -> Dict: if ('sensor' not in serialized_report or 'target' not in serialized_report or 'metadata' not in serialized_report or 'timestamp' not in serialized_report or 'power' not in serialized_report): raise BadInputData() tags = {tag: serialized_report[tag] for tag in self.get_tags()} for metadata_name in self._influxdb_keept_metadata(): if metadata_name not in serialized_report['metadata']: pass else: tags[metadata_name] = serialized_report['metadata'][metadata_name] return { 'tags': tags, 'time': int(serialized_report['timestamp'].timestamp()), 'value': serialized_report['power'] }
def to_influxdb(self, serialized_report) -> Dict: """ Return raw data from serialized report { 'measurement' : 'power_consumption' 'timestamp': ... 'tags' : { ... } 'fields': { 'power': ... } } """ if ('sensor' not in serialized_report or 'target' not in serialized_report or 'metadata' not in serialized_report or 'timestamp' not in serialized_report or 'power' not in serialized_report): raise BadInputData() tags = {'sensor': serialized_report['sensor'], 'target': serialized_report['target']} for name in serialized_report['metadata'].keys(): if name == 'ratio': continue if name == 'predict': continue else: tags[name] = serialized_report['metadata'][name] return { 'measurement': 'power_consumption', 'tags': tags, 'time': str(serialized_report['timestamp']), 'fields': { 'power': serialized_report['power'] } }
def from_csvdb(self, file_name, row) -> Dict: """ Get HWPCReport from a few csv files. """ final_dict = {} try: group_name = file_name[:-4] if file_name[ len(file_name) - 4:] == '.csv' else file_name final_dict = {key: row[key] for key in CSV_HEADER_COMMON} final_dict['timestamp'] = timestamp_to_datetime( int(row['timestamp'])) final_dict['groups'] = {} # If group doesn't exist, create it if group_name not in final_dict: final_dict['groups'][group_name] = {} # If socket doesn't exist, create it if row['socket'] not in final_dict['groups'][group_name]: final_dict['groups'][group_name][row['socket']] = {} # If cpu doesn't exist, create it if row['cpu'] not in final_dict['groups'][group_name][ row['socket']]: final_dict['groups'][group_name][row['socket']][ row['cpu']] = {} # Add events for key, value in row.items(): if key not in CSV_HEADER_HWPC: final_dict['groups'][group_name][row['socket']][ row['cpu']][key] = int(value) except KeyError: raise BadInputData() return final_dict