(source_data: List, drop_service=(), drop_fault_type=())
| 24 | |
| 25 | |
| 26 | def encoding_data(source_data: List, drop_service=(), drop_fault_type=()): |
| 27 | def pair2index(s_t): |
| 28 | return SERVICE2IDX.get(simple_name(s_t[1])) |
| 29 | |
| 30 | if ENABLE_ALL_FEATURES: |
| 31 | _data = np.ones((len(source_data), len(INVOLVED_SERVICES), 9), dtype=np.float32) * -1 |
| 32 | else: |
| 33 | _data = np.ones((len(source_data), len(INVOLVED_SERVICES), 2), dtype=np.float32) * -1 |
| 34 | |
| 35 | _labels = np.zeros((len(source_data),), dtype=np.bool) |
| 36 | _trace_ids = [""] * len(source_data) |
| 37 | _service_mask = np.zeros((len(source_data), len(INVOLVED_SERVICES)), dtype=np.bool) |
| 38 | _root_causes = np.zeros((len(source_data), len(INVOLVED_SERVICES)), dtype=np.bool) |
| 39 | for trace_idx, trace in enumerate(source_data): |
| 40 | if 'fault_type' in trace and trace['fault_type'] in drop_fault_type: |
| 41 | continue |
| 42 | if 'root_cause' in trace and any(_ in drop_service for _ in trace['root_cause']): |
| 43 | continue |
| 44 | indices = np.asarray([idx for idx, (source, target) in enumerate(trace['s_t']) if source != target]) |
| 45 | if len(indices) <= 0: |
| 46 | continue |
| 47 | for key, item in trace.items(): |
| 48 | if isinstance(item, list) and key != 'root_cause' and key != 'fault_type': |
| 49 | trace[key] = np.asarray(item)[indices] |
| 50 | service_idx = np.asarray(list(map(pair2index, (trace['s_t'])))) |
| 51 | _service_mask[trace_idx, service_idx] = True |
| 52 | # assert all(np.diff(trace['endtime']) <= 0), f'end time is not sorted: {trace["endtime"]}' |
| 53 | |
| 54 | if ENABLE_ALL_FEATURES: |
| 55 | _data[trace_idx, service_idx, 0] = np.asarray(trace['latency']) / 1e6 |
| 56 | _data[trace_idx, service_idx, 1] = np.asarray(trace['cpu_use']) / 100 |
| 57 | _data[trace_idx, service_idx, 2] = np.asarray([round(_, 2) for _ in trace['mem_use_percent']]) |
| 58 | _data[trace_idx, service_idx, 3] = np.asarray(trace['mem_use_amount']) / 1e9 # 1000M |
| 59 | _data[trace_idx, service_idx, 4] = np.asarray(trace['file_write_rate']) / 1e8 |
| 60 | _data[trace_idx, service_idx, 5] = np.asarray(trace['file_read_rate']) / 1e8 |
| 61 | _data[trace_idx, service_idx, 6] = np.asarray(trace['net_send_rate']) / 1e8 |
| 62 | _data[trace_idx, service_idx, 7] = np.asarray(trace['net_receive_rate']) / 1e8 |
| 63 | _data[trace_idx, service_idx, 8] = list(map(lambda x: x // 100 if x != 0 else 9, (trace['http_status']))) |
| 64 | else: |
| 65 | _data[trace_idx, service_idx, 0] = np.asarray(trace['latency']) |
| 66 | _data[trace_idx, service_idx, 1] = list(map(lambda x: int(x) // 100 if x != 0 else 9, (trace['http_status']))) |
| 67 | |
| 68 | _labels[trace_idx] = trace['label'] |
| 69 | _trace_ids[trace_idx] = trace['trace_id'] |
| 70 | _trace_root_causes = trace['root_cause'] if 'root_cause' in trace else [] |
| 71 | for _root_cause in _trace_root_causes: |
| 72 | _root_causes[trace_idx, SERVICE2IDX[_root_cause]] = True |
| 73 | _mask = np.tile(_service_mask[:, :, np.newaxis], (1, 1, 9)) |
| 74 | return _data, _labels, _mask, _trace_ids, _root_causes |
| 75 | |
| 76 | |
| 77 | @click.command('trace-encoding') |
no outgoing calls
no test coverage detected