Skip to content

Commit

Permalink
Add more call stats
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomb committed Aug 5, 2023
1 parent 056eda3 commit 509e7f8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
13 changes: 13 additions & 0 deletions pipeline/Time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,16 @@ export const formatDatetime = (format: TimeFormat, datetime?: Datetime) => {

return formatTime(format, Day.fromKey(datetime.key), datetime.secondOfDay);
};

/** Finds the time difference in seconds between two Datetimes */
export const diffDatetime = (a: Datetime, b: Datetime): number => {
// Probably this can be done more efficient and be reused (also see formatTime)

const aDate = Day.fromKey(a.key).toDate();
if (a.secondOfDay !== undefined) aDate.setSeconds(a.secondOfDay);

const bDate = Day.fromKey(b.key).toDate();
if (b.secondOfDay !== undefined) bDate.setSeconds(b.secondOfDay);

return (bDate.getTime() - aDate.getTime()) / 1000;
};
68 changes: 61 additions & 7 deletions pipeline/aggregate/blocks/calls/CallsStats.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,84 @@
import { Datetime, diffDatetime } from "@pipeline/Time";
import { BlockDescription, BlockFn } from "@pipeline/aggregate/Blocks";
import { filterMessages } from "@pipeline/aggregate/Helpers";
import { MessageView } from "@pipeline/serialization/MessageView";

interface CallDuration {
duration: number;
start: Datetime;
}

export interface CallsStats {
/** Total number of calls */
total: number;
/** Total number of seconds spent in calls */
secondsInCall: number;
medianDuration: number;
averageDuration: number;

longestCall?: CallDuration;
}

const fn: BlockFn<CallsStats> = (database, filters, common, args) => {
const stats: CallsStats = {
total: 0,
secondsInCall: 0,
};
const { dateKeys } = common.timeKeys;

let total = 0;
let secondsInCall = 0;
let longestCall: CallDuration | undefined = undefined;
let lastCall: Datetime | undefined = undefined;

const durations: number[] = [];
const timesBetween: number[] = [];

for (const call of database.calls) {
if (!filters.inTime(call.start.dayIndex)) continue;
if (!filters.hasChannel(call.channelIndex)) continue;
if (!filters.hasAuthor(call.authorIndex)) continue;

stats.total++;
stats.secondsInCall += call.duration;
const startDatetime = {
key: dateKeys[call.start.dayIndex],
secondOfDay: call.start.secondOfDay,
};
const endDatetime = {
key: dateKeys[call.end.dayIndex],
secondOfDay: call.end.secondOfDay,
};

total++;
secondsInCall += call.duration;

if (longestCall === undefined || call.duration > longestCall.duration) {
longestCall = {
duration: call.duration,
start: startDatetime,
};
}

durations.push(call.duration);

if (lastCall !== undefined) {
// compute time difference between calls
const diff = diffDatetime(lastCall, startDatetime);
if (diff < 0) throw new Error("Time difference between calls is negative, diff=" + diff);
timesBetween.push(diff);
}
lastCall = endDatetime;
}

return stats;
durations.sort((a, b) => b - a);
const medianIndex = Math.floor(durations.length / 2);
const medianDuration = durations[medianIndex];

console.log(timesBetween);

return {
total,
secondsInCall,
medianDuration,
averageDuration: secondsInCall / total,
longestCall,
// timesBetween
};
};

export default {
Expand Down
26 changes: 26 additions & 0 deletions report/components/cards/calls/CallStatsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ const CallsStatsTable = () => {
label: "Time spent on calls",
value: stats?.secondsInCall,
},
{
type: "number",
formatter: "time",
label: "Average call duration",
value: stats?.averageDuration,
},
{
type: "number",
formatter: "time",
label: "Median call duration",
value: stats?.medianDuration,
},
{
type: "separator",
},
{
type: "number",
formatter: "time",
label: "Longest call",
value: stats?.longestCall?.duration,
tooltip: (
<>
The call was initiated on <b>{formatDatetime("ymdhm", stats?.longestCall?.start)}</b>
</>
),
},
];

return <DottedTable lines={lines} />;
Expand Down

0 comments on commit 509e7f8

Please sign in to comment.