Skip to content

Commit

Permalink
fix: counts API changes to start and end time
Browse files Browse the repository at this point in the history
  • Loading branch information
praveen5959 committed Jan 17, 2025
1 parent 897d152 commit b384a40
Showing 1 changed file with 35 additions and 44 deletions.
79 changes: 35 additions & 44 deletions src/pages/Stream/components/EventTimeLineGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,59 +79,53 @@ type LogRecord = {

// date_bin removes tz info
// filling data with empty values where there is no rec
const parseGraphData = (
data: LogsResponseWithHeaders | undefined,
avg: number,
startTime: Date,
endTime: Date,
interval: number,
): GraphTickItem[] => {
const parseGraphData = (data: LogsResponseWithHeaders | undefined, avg: number, interval: number): GraphTickItem[] => {
if (!data || !Array.isArray(data?.records)) return [];

const { fields, records } = data;
if (_.isEmpty(records) || !_.includes(fields, 'log_count') || !_.includes(fields, 'counts_timestamp')) return [];
if (
_.isEmpty(records) ||
!_.includes(fields, 'log_count') ||
!_.includes(fields, 'counts_start_timestamp') ||
!_.includes(fields, 'counts_end_timestamp')
)
return [];

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

const isValidRecord = (record: any): record is LogRecord => {
return typeof record.counts_timestamp === 'string' && typeof record.log_count === 'number';
return (
typeof record.counts_start_timestamp === 'string' &&
typeof record.counts_end_timestamp === 'string' &&
typeof record.log_count === 'number' &&
record.counts_start_timestamp &&
record.counts_end_timestamp
);
};

const allTimestamps = Array.from(
{ length: ticksCount },
(_, index) => new Date(startTime.getTime() + index * intervalDuration),
);

const parsedData = allTimestamps.map((ts) => {
const countData = records.find((d) => {
if (!isValidRecord(d)) return false;
const recordTime = new Date(d.counts_timestamp).getTime();
const tsTime = ts.getTime();
return Math.abs(recordTime - tsTime) < intervalDuration / 2;
});

const defaultOpts = {
events: 0,
minute: ts,
aboveAvgPercent: 0,
compactType,
startTime: dayjs(ts),
endTime: dayjs(new Date(ts.getTime() + intervalDuration)),
};
// Filter valid records and sort them by start timestamp
const validRecords = records
.filter(isValidRecord)
.map((record) => ({
...record,
startDate: record.counts_start_timestamp ? new Date(record.counts_start_timestamp) : new Date(),
endDate: record.counts_end_timestamp ? new Date(record.counts_end_timestamp) : new Date(),
}))
.sort((a, b) => a.startDate.getTime() - b.startDate.getTime());

if (!countData || !isValidRecord(countData)) {
return defaultOpts;
}
if (validRecords.length === 0) return [];

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

return {
...defaultOpts,
events: countData.log_count,
events: record.log_count,
minute: record.startDate,
aboveAvgPercent,
compactType,
startTime: dayjs(record.startDate),
endTime: dayjs(record.endDate),
};
});

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

const adjustedStartTime = dayjs(startTime).startOf('minute');
const adjustedEndTime = dayjs(endTime).startOf('minute');

const totalMinutes = interval / (1000 * 60);
const numBins = Math.trunc(totalMinutes < 10 ? totalMinutes : totalMinutes < 60 ? 10 : 60);

const logsQuery = {
stream: localStream,
startTime: adjustedStartTime.toISOString(),
endTime: adjustedEndTime.add(1, 'minute').toISOString(),
startTime: dayjs(startTime).startOf('minute').toISOString(),
endTime: dayjs(endTime).startOf('minute').toISOString(),
numBins,
};

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

0 comments on commit b384a40

Please sign in to comment.