|
| 1 | +/* eslint-disable camelcase */ |
| 2 | +import type {Reducer} from 'redux'; |
| 3 | +import {createSelector, Selector} from 'reselect'; |
| 4 | + |
| 5 | +import type { |
| 6 | + IConsumerAction, |
| 7 | + IConsumerRootStateSlice, |
| 8 | + IConsumerState, |
| 9 | + IPreparedPartitionData, |
| 10 | +} from '../../types/store/consumer'; |
| 11 | + |
| 12 | +import '../../services/api'; |
| 13 | + |
| 14 | +import {convertBytesObjectToSpeed} from '../../utils/bytesParsers'; |
| 15 | +import {parseLag, parseTimestampToIdleTime} from '../../utils/timeParsers'; |
| 16 | +import {isNumeric} from '../../utils/utils'; |
| 17 | + |
| 18 | +import {createRequestActionTypes, createApiRequest} from '../utils'; |
| 19 | + |
| 20 | +export const FETCH_CONSUMER = createRequestActionTypes('consumer', 'FETCH_CONSUMER'); |
| 21 | + |
| 22 | +const SET_DATA_WAS_NOT_LOADED = 'consumer/SET_DATA_WAS_NOT_LOADED'; |
| 23 | + |
| 24 | +const initialState = { |
| 25 | + loading: false, |
| 26 | + wasLoaded: false, |
| 27 | + data: {}, |
| 28 | +}; |
| 29 | + |
| 30 | +const consumer: Reducer<IConsumerState, IConsumerAction> = (state = initialState, action) => { |
| 31 | + switch (action.type) { |
| 32 | + case FETCH_CONSUMER.REQUEST: { |
| 33 | + return { |
| 34 | + ...state, |
| 35 | + loading: true, |
| 36 | + }; |
| 37 | + } |
| 38 | + case FETCH_CONSUMER.SUCCESS: { |
| 39 | + // On older version it can return HTML page of Internal Viewer with an error |
| 40 | + if (typeof action.data !== 'object') { |
| 41 | + return {...state, loading: false, error: {}}; |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + ...state, |
| 46 | + data: action.data, |
| 47 | + loading: false, |
| 48 | + wasLoaded: true, |
| 49 | + error: undefined, |
| 50 | + }; |
| 51 | + } |
| 52 | + case FETCH_CONSUMER.FAILURE: { |
| 53 | + if (action.error?.isCancelled) { |
| 54 | + return state; |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + ...state, |
| 59 | + error: action.error, |
| 60 | + loading: false, |
| 61 | + }; |
| 62 | + } |
| 63 | + case SET_DATA_WAS_NOT_LOADED: { |
| 64 | + return { |
| 65 | + ...state, |
| 66 | + wasLoaded: false, |
| 67 | + }; |
| 68 | + } |
| 69 | + default: |
| 70 | + return state; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +export const setDataWasNotLoaded = () => { |
| 75 | + return { |
| 76 | + type: SET_DATA_WAS_NOT_LOADED, |
| 77 | + } as const; |
| 78 | +}; |
| 79 | + |
| 80 | +export function getConsumer(path?: string, consumerName?: string) { |
| 81 | + return createApiRequest({ |
| 82 | + request: window.api.getConsumer({path, consumer: consumerName}), |
| 83 | + actions: FETCH_CONSUMER, |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +export const selectPartitions = (state: IConsumerRootStateSlice) => state.consumer.data?.partitions; |
| 88 | + |
| 89 | +export const selectPreparedPartitionsData: Selector< |
| 90 | + IConsumerRootStateSlice, |
| 91 | + IPreparedPartitionData[] | undefined |
| 92 | +> = createSelector([selectPartitions], (partitions) => { |
| 93 | + return partitions?.map((partition) => { |
| 94 | + // describe_consumer endpoint doesn't return zero values, so some values will be initialized with 0 |
| 95 | + const {partition_id = '0', partition_stats, partition_consumer_stats} = partition; |
| 96 | + |
| 97 | + const { |
| 98 | + partition_offsets, |
| 99 | + store_size_bytes = '0', |
| 100 | + last_write_time: partition_last_write_time, |
| 101 | + max_write_time_lag: partition_write_lag, |
| 102 | + bytes_written, |
| 103 | + partition_node_id = 0, |
| 104 | + } = partition_stats || {}; |
| 105 | + |
| 106 | + const {start: start_offset = '0', end: end_offset = '0'} = partition_offsets || {}; |
| 107 | + |
| 108 | + const { |
| 109 | + last_read_offset = '0', |
| 110 | + committed_offset = '0', |
| 111 | + read_session_id, |
| 112 | + last_read_time: consumer_last_read_time, |
| 113 | + max_read_time_lag: consumer_read_lag, |
| 114 | + max_write_time_lag: consumer_write_lag, |
| 115 | + bytes_read, |
| 116 | + reader_name, |
| 117 | + connection_node_id = 0, |
| 118 | + } = partition_consumer_stats || {}; |
| 119 | + |
| 120 | + const uncommitedMessages = |
| 121 | + isNumeric(end_offset) && isNumeric(committed_offset) |
| 122 | + ? Number(end_offset) - Number(committed_offset) |
| 123 | + : 0; |
| 124 | + |
| 125 | + const unreadMessages = |
| 126 | + isNumeric(end_offset) && isNumeric(last_read_offset) |
| 127 | + ? Number(end_offset) - Number(last_read_offset) |
| 128 | + : 0; |
| 129 | + |
| 130 | + return { |
| 131 | + partitionId: partition_id, |
| 132 | + storeSize: store_size_bytes, |
| 133 | + |
| 134 | + writeSpeed: convertBytesObjectToSpeed(bytes_written), |
| 135 | + readSpeed: convertBytesObjectToSpeed(bytes_read), |
| 136 | + |
| 137 | + partitionWriteLag: parseLag(partition_write_lag), |
| 138 | + partitionWriteIdleTime: parseTimestampToIdleTime(partition_last_write_time), |
| 139 | + |
| 140 | + consumerWriteLag: parseLag(consumer_write_lag), |
| 141 | + consumerReadLag: parseLag(consumer_read_lag), |
| 142 | + consumerReadIdleTime: parseTimestampToIdleTime(consumer_last_read_time), |
| 143 | + |
| 144 | + uncommitedMessages, |
| 145 | + unreadMessages, |
| 146 | + |
| 147 | + startOffset: start_offset, |
| 148 | + endOffset: end_offset, |
| 149 | + commitedOffset: committed_offset, |
| 150 | + |
| 151 | + readSessionId: read_session_id, |
| 152 | + readerName: reader_name, |
| 153 | + |
| 154 | + partitionNodeId: partition_node_id, |
| 155 | + connectionNodeId: connection_node_id, |
| 156 | + }; |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +export default consumer; |
0 commit comments