Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit fd51eee

Browse files
authored
fix: counts API changes to start and end time (#424)
1 parent 897d152 commit fd51eee

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

src/pages/Stream/components/EventTimeLineGraph.tsx

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ const calcAverage = (data: LogsResponseWithHeaders | undefined) => {
5555
if (!data || !Array.isArray(data?.records)) return 0;
5656

5757
const { fields, records } = data;
58-
if (_.isEmpty(records) || !_.includes(fields, 'log_count')) return 0;
58+
if (_.isEmpty(records) || !_.includes(fields, 'count')) return 0;
5959

6060
const total = records.reduce((acc, d) => {
61-
return acc + _.toNumber(d.log_count) || 0;
61+
return acc + _.toNumber(d.count) || 0;
6262
}, 0);
6363
return parseInt(Math.abs(total / records.length).toFixed(0));
6464
};
@@ -74,64 +74,58 @@ type GraphTickItem = {
7474

7575
type LogRecord = {
7676
counts_timestamp: string;
77-
log_count: number;
77+
count: number;
7878
};
7979

8080
// date_bin removes tz info
8181
// filling data with empty values where there is no rec
82-
const parseGraphData = (
83-
data: LogsResponseWithHeaders | undefined,
84-
avg: number,
85-
startTime: Date,
86-
endTime: Date,
87-
interval: number,
88-
): GraphTickItem[] => {
82+
const parseGraphData = (data: LogsResponseWithHeaders | undefined, avg: number, interval: number): GraphTickItem[] => {
8983
if (!data || !Array.isArray(data?.records)) return [];
9084

9185
const { fields, records } = data;
92-
if (_.isEmpty(records) || !_.includes(fields, 'log_count') || !_.includes(fields, 'counts_timestamp')) return [];
86+
if (
87+
_.isEmpty(records) ||
88+
!_.includes(fields, 'count') ||
89+
!_.includes(fields, 'start_time') ||
90+
!_.includes(fields, 'end_time')
91+
)
92+
return [];
9393

9494
const compactType = getCompactType(interval);
95-
const ticksCount = interval < 10 * 60 * 1000 ? interval / (60 * 1000) : interval < 60 * 60 * 1000 ? 10 : 60;
96-
const intervalDuration = (endTime.getTime() - startTime.getTime()) / ticksCount;
9795

9896
const isValidRecord = (record: any): record is LogRecord => {
99-
return typeof record.counts_timestamp === 'string' && typeof record.log_count === 'number';
97+
return (
98+
typeof record.start_time === 'string' &&
99+
typeof record.end_time === 'string' &&
100+
typeof record.count === 'number' &&
101+
record.start_time &&
102+
record.end_time
103+
);
100104
};
101105

102-
const allTimestamps = Array.from(
103-
{ length: ticksCount },
104-
(_, index) => new Date(startTime.getTime() + index * intervalDuration),
105-
);
106-
107-
const parsedData = allTimestamps.map((ts) => {
108-
const countData = records.find((d) => {
109-
if (!isValidRecord(d)) return false;
110-
const recordTime = new Date(d.counts_timestamp).getTime();
111-
const tsTime = ts.getTime();
112-
return Math.abs(recordTime - tsTime) < intervalDuration / 2;
113-
});
114-
115-
const defaultOpts = {
116-
events: 0,
117-
minute: ts,
118-
aboveAvgPercent: 0,
119-
compactType,
120-
startTime: dayjs(ts),
121-
endTime: dayjs(new Date(ts.getTime() + intervalDuration)),
122-
};
106+
// Filter valid records and sort them by start timestamp
107+
const validRecords = records
108+
.filter(isValidRecord)
109+
.map((record) => ({
110+
...record,
111+
startDate: record.start_time ? new Date(record.start_time) : new Date(),
112+
endDate: record.end_time ? new Date(record.end_time) : new Date(),
113+
}))
114+
.sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
123115

124-
if (!countData || !isValidRecord(countData)) {
125-
return defaultOpts;
126-
}
116+
if (validRecords.length === 0) return [];
127117

128-
const aboveAvgCount = countData.log_count - avg;
118+
const parsedData = validRecords.map((record: any) => {
119+
const aboveAvgCount = record.count - avg;
129120
const aboveAvgPercent = avg > 0 ? parseInt(((aboveAvgCount / avg) * 100).toFixed(2)) : 0;
130121

131122
return {
132-
...defaultOpts,
133-
events: countData.log_count,
123+
events: record.count,
124+
minute: record.startDate,
134125
aboveAvgPercent,
126+
compactType,
127+
startTime: dayjs(record.startDate),
128+
endTime: dayjs(record.endDate),
135129
};
136130
});
137131

@@ -179,16 +173,13 @@ const EventTimeLineGraph = () => {
179173
useEffect(() => {
180174
if (!localStream || localStream.length === 0 || !firstEventAt) return;
181175

182-
const adjustedStartTime = dayjs(startTime).startOf('minute');
183-
const adjustedEndTime = dayjs(endTime).startOf('minute');
184-
185176
const totalMinutes = interval / (1000 * 60);
186177
const numBins = Math.trunc(totalMinutes < 10 ? totalMinutes : totalMinutes < 60 ? 10 : 60);
187178

188179
const logsQuery = {
189180
stream: localStream,
190-
startTime: adjustedStartTime.toISOString(),
191-
endTime: adjustedEndTime.add(1, 'minute').toISOString(),
181+
startTime: dayjs(startTime).startOf('minute').toISOString(),
182+
endTime: dayjs(endTime).startOf('minute').toISOString(),
192183
numBins,
193184
};
194185

@@ -205,7 +196,7 @@ const EventTimeLineGraph = () => {
205196
const avgEventCount = useMemo(() => calcAverage(fetchGraphDataMutation?.data), [fetchGraphDataMutation?.data]);
206197
const graphData = useMemo(() => {
207198
if (!firstEventAt) return null;
208-
return parseGraphData(fetchGraphDataMutation?.data, avgEventCount, startTime, endTime, interval);
199+
return parseGraphData(fetchGraphDataMutation?.data, avgEventCount, interval);
209200
}, [fetchGraphDataMutation?.data, interval, firstEventAt]);
210201
const hasData = Array.isArray(graphData) && graphData.length !== 0;
211202
const [, setAppStore] = useAppStore((_store) => null);

0 commit comments

Comments
 (0)