fix(sla): freeze misses after resolution

This commit is contained in:
Sony Mathew
2026-07-15 13:00:51 +05:30
parent 13db36609d
commit 92d5d5ecff
16 changed files with 598 additions and 66 deletions
@@ -1,6 +1,10 @@
<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';
import { evaluateSLAStatus } from 'dashboard/helper/slaHelper';
import { useI18n } from 'vue-i18n';
import {
evaluateSLAStatus,
shouldRefreshSLAStatus,
} from 'dashboard/helper/slaHelper';
const props = defineProps({
conversation: {
@@ -12,6 +16,7 @@ const props = defineProps({
const REFRESH_INTERVAL = 60000;
const timer = ref(null);
const { t } = useI18n();
const slaStatus = ref({
threshold: null,
isSlaMissed: false,
@@ -24,12 +29,15 @@ const slaEvents = computed(() => props.conversation?.slaEvents);
const isSlaMissed = computed(() => slaStatus.value?.isSlaMissed);
const hasSlaThreshold = computed(() => {
return slaStatus.value?.threshold && appliedSLA.value?.id;
return slaStatus.value?.type && appliedSLA.value?.id;
});
const slaStatusText = computed(() => {
return slaStatus.value?.type?.toUpperCase();
});
const slaValueText = computed(
() => slaStatus.value?.threshold || t('CONVERSATION.HEADER.SLA_STATUS.MISSED')
);
const updateSlaStatus = () => {
slaStatus.value = evaluateSLAStatus({
@@ -39,7 +47,24 @@ const updateSlaStatus = () => {
});
};
const clearTimer = () => {
if (timer.value) {
clearTimeout(timer.value);
timer.value = null;
}
};
const createTimer = () => {
clearTimer();
if (
!shouldRefreshSLAStatus({
appliedSla: appliedSLA.value,
chat: props.conversation,
})
) {
return;
}
timer.value = setTimeout(() => {
updateSlaStatus();
createTimer();
@@ -52,12 +77,16 @@ onMounted(() => {
});
onUnmounted(() => {
if (timer.value) {
clearTimeout(timer.value);
}
clearTimer();
});
watch(() => props.conversation, updateSlaStatus);
watch(
() => props.conversation,
() => {
updateSlaStatus();
createTimer();
}
);
// This expose is to provide context to the parent component, so that it can decided weather
// a new row has to be added to the conversation card or not
@@ -96,7 +125,7 @@ defineExpose({
class="text-sm truncate"
:class="isSlaMissed ? 'text-n-ruby-11' : 'text-n-slate-11'"
>
{{ `${slaStatusText}: ${slaStatus.threshold}` }}
{{ `${slaStatusText}: ${slaValueText}` }}
</span>
</div>
</template>
@@ -1,6 +1,10 @@
<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';
import { evaluateSLAStatus } from 'dashboard/helper/slaHelper';
import { useI18n } from 'vue-i18n';
import {
evaluateSLAStatus,
shouldRefreshSLAStatus,
} from 'dashboard/helper/slaHelper';
import Icon from 'dashboard/components-next/icon/Icon.vue';
import Label from 'dashboard/components-next/label/Label.vue';
@@ -15,6 +19,7 @@ const props = defineProps({
const REFRESH_INTERVAL = 60000;
const timer = ref(null);
const { t } = useI18n();
const slaStatus = ref({
threshold: null,
isSlaMissed: false,
@@ -28,8 +33,18 @@ defineOptions({
const appliedSLA = computed(() => props.chat?.applied_sla);
const slaEvents = computed(() => props.chat?.sla_events);
const hasSlaThreshold = computed(() => slaStatus.value?.threshold);
const hasSlaThreshold = computed(() => slaStatus.value?.type);
const isSlaMissed = computed(() => slaStatus.value?.isSlaMissed);
const slaLabel = computed(() => {
if (slaStatus.value?.threshold) return slaStatus.value.threshold;
const status = t('CONVERSATION.HEADER.SLA_STATUS.MISSED');
return {
FRT: t('CONVERSATION.HEADER.SLA_STATUS.FRT', { status }),
NRT: t('CONVERSATION.HEADER.SLA_STATUS.NRT', { status }),
RT: t('CONVERSATION.HEADER.SLA_STATUS.RT', { status }),
}[slaStatus.value.type];
});
const updateSlaStatus = () => {
slaStatus.value = evaluateSLAStatus({
@@ -39,7 +54,24 @@ const updateSlaStatus = () => {
});
};
const clearTimer = () => {
if (timer.value) {
clearTimeout(timer.value);
timer.value = null;
}
};
const createTimer = () => {
clearTimer();
if (
!shouldRefreshSLAStatus({
appliedSla: appliedSLA.value,
chat: props.chat,
})
) {
return;
}
timer.value = setTimeout(() => {
updateSlaStatus();
createTimer();
@@ -52,12 +84,16 @@ onMounted(() => {
});
onUnmounted(() => {
if (timer.value) {
clearTimeout(timer.value);
}
clearTimer();
});
watch(() => props.chat, updateSlaStatus);
watch(
() => props.chat,
() => {
updateSlaStatus();
createTimer();
}
);
defineExpose({
hasSlaThreshold,
@@ -70,11 +106,7 @@ defineExpose({
v-bind="$attrs"
class="relative flex items-center cursor-pointer min-w-fit group"
>
<Label
:label="slaStatus.threshold"
:color="isSlaMissed ? 'ruby' : 'amber'"
compact
>
<Label :label="slaLabel" :color="isSlaMissed ? 'ruby' : 'amber'" compact>
<template #icon>
<Icon icon="i-lucide-flame" class="flex-shrink-0 size-3.5" />
</template>
@@ -1,7 +1,10 @@
<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { evaluateSLAStatus } from 'dashboard/helper/slaHelper';
import {
evaluateSLAStatus,
shouldRefreshSLAStatus,
} from 'dashboard/helper/slaHelper';
import SLAPopoverCard from './SLAPopoverCard.vue';
const props = defineProps({
@@ -32,7 +35,7 @@ const slaStatus = ref({
const appliedSLA = computed(() => props.chat?.applied_sla);
const slaEvents = computed(() => props.chat?.sla_events);
const hasSlaThreshold = computed(() => slaStatus.value?.threshold);
const hasSlaThreshold = computed(() => slaStatus.value?.type);
const isSlaMissed = computed(() => slaStatus.value?.isSlaMissed);
const slaTextStyles = computed(() =>
isSlaMissed.value ? 'text-n-ruby-11' : 'text-n-amber-11'
@@ -40,12 +43,24 @@ const slaTextStyles = computed(() =>
const slaStatusText = computed(() => {
const upperCaseType = slaStatus.value?.type?.toUpperCase(); // FRT, NRT, or RT
const statusKey = isSlaMissed.value ? 'MISSED' : 'DUE';
const status = isSlaMissed.value
? t('CONVERSATION.HEADER.SLA_STATUS.MISSED')
: t('CONVERSATION.HEADER.SLA_STATUS.DUE');
return t(`CONVERSATION.HEADER.SLA_STATUS.${upperCaseType}`, {
status: t(`CONVERSATION.HEADER.SLA_STATUS.${statusKey}`),
});
return {
FRT: t('CONVERSATION.HEADER.SLA_STATUS.FRT', { status }),
NRT: t('CONVERSATION.HEADER.SLA_STATUS.NRT', { status }),
RT: t('CONVERSATION.HEADER.SLA_STATUS.RT', { status }),
}[upperCaseType];
});
const showFullStatusText = computed(
() => props.showExtendedInfo && props.parentWidth > 650
);
const slaValueText = computed(
() =>
slaStatus.value?.threshold ||
(showFullStatusText.value ? '' : slaStatusText.value)
);
const showSlaPopoverCard = computed(
() => props.showExtendedInfo && slaEvents.value?.length > 0
@@ -65,7 +80,24 @@ const updateSlaStatus = () => {
});
};
const clearTimer = () => {
if (timer.value) {
clearTimeout(timer.value);
timer.value = null;
}
};
const createTimer = () => {
clearTimer();
if (
!shouldRefreshSLAStatus({
appliedSla: appliedSLA.value,
chat: props.chat,
})
) {
return;
}
timer.value = setTimeout(() => {
updateSlaStatus();
createTimer();
@@ -76,6 +108,7 @@ watch(
() => props.chat,
() => {
updateSlaStatus();
createTimer();
}
);
@@ -91,9 +124,7 @@ onMounted(() => {
});
onUnmounted(() => {
if (timer.value) {
clearTimeout(timer.value);
}
clearTimer();
});
</script>
@@ -118,7 +149,7 @@ onUnmounted(() => {
:class="slaTextStyles"
/>
<span
v-if="showExtendedInfo && parentWidth > 650"
v-if="showFullStatusText"
class="text-xs font-medium"
:class="slaTextStyles"
>
@@ -126,10 +157,11 @@ onUnmounted(() => {
</span>
</div>
<span
v-if="slaValueText"
class="text-xs font-medium"
:class="[slaTextStyles, showExtendedInfo && 'ltr:pl-1.5 rtl:pr-1.5']"
>
{{ slaStatus.threshold }}
{{ slaValueText }}
</span>
</div>
<SLAPopoverCard
+63 -34
View File
@@ -47,6 +47,22 @@ const toUnixTimestamp = value => {
: Math.floor(parsedTimestamp / 1000);
};
const isSLACompleted = (sla, conversation) => {
const terminalStatuses = ['hit', 'missed'];
return Boolean(
sla.slaCompletedAt ||
terminalStatuses.includes(sla.slaStatus) ||
conversation.status === 'resolved'
);
};
export const shouldRefreshSLAStatus = ({ appliedSla, chat }) => {
if (!appliedSla || !chat) return false;
return !isSLACompleted(useCamelCase(appliedSla), useCamelCase(chat));
};
/**
* Evaluates SLA status using backend-computed due times
* @param {Object} params - Parameters object
@@ -66,6 +82,9 @@ export const evaluateSLAStatus = ({ appliedSla, chat, slaEvents = [] }) => {
const conversation = useCamelCase(chat);
const events = useCamelCase(slaEvents || []);
const currentTime = Math.floor(Date.now() / 1000);
const completionTime = toUnixTimestamp(sla.slaCompletedAt);
const isCompleted = isSLACompleted(sla, conversation);
const evaluationTime = completionTime || (isCompleted ? null : currentTime);
const slaStatuses = [];
const dueAtByType = {
@@ -84,47 +103,51 @@ export const evaluateSLAStatus = ({ appliedSla, chat, slaEvents = [] }) => {
slaStatuses.push({
type,
threshold: missedAt - currentTime,
threshold: evaluationTime ? missedAt - evaluationTime : null,
icon: 'flame',
isSlaMissed: true,
});
});
const firstReplyCreatedAt = toUnixTimestamp(conversation.firstReplyCreatedAt);
const shouldCheckFirstResponse =
!firstReplyCreatedAt || firstReplyCreatedAt > sla.slaFrtDueAt;
if (!isCompleted) {
const firstReplyCreatedAt = toUnixTimestamp(
conversation.firstReplyCreatedAt
);
const shouldCheckFirstResponse =
!firstReplyCreatedAt || firstReplyCreatedAt > sla.slaFrtDueAt;
// Check FRT - until first reply is made on time
if (sla.slaFrtDueAt && shouldCheckFirstResponse) {
const threshold = sla.slaFrtDueAt - currentTime;
slaStatuses.push({
type: 'FRT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
}
// Check FRT - until first reply is made on time
if (sla.slaFrtDueAt && shouldCheckFirstResponse) {
const threshold = sla.slaFrtDueAt - currentTime;
slaStatuses.push({
type: 'FRT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
}
// Check NRT - only if first reply made and waiting for response
if (sla.slaNrtDueAt && firstReplyCreatedAt && conversation.waitingSince) {
const threshold = sla.slaNrtDueAt - currentTime;
slaStatuses.push({
type: 'NRT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
}
// Check NRT - only if first reply made and waiting for response
if (sla.slaNrtDueAt && firstReplyCreatedAt && conversation.waitingSince) {
const threshold = sla.slaNrtDueAt - currentTime;
slaStatuses.push({
type: 'NRT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
}
// Check RT - only if conversation is unresolved
if (sla.slaRtDueAt && conversation.status !== 'resolved') {
const threshold = sla.slaRtDueAt - currentTime;
slaStatuses.push({
type: 'RT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
// Check RT - only if conversation is unresolved
if (sla.slaRtDueAt) {
const threshold = sla.slaRtDueAt - currentTime;
slaStatuses.push({
type: 'RT',
threshold,
icon: threshold <= 0 ? 'flame' : 'alarm',
isSlaMissed: threshold <= 0,
});
}
}
if (slaStatuses.length === 0) {
@@ -137,13 +160,19 @@ export const evaluateSLAStatus = ({ appliedSla, chat, slaEvents = [] }) => {
return a.isSlaMissed ? -1 : 1;
}
if (a.threshold === null || b.threshold === null) {
if (a.threshold === b.threshold) return 0;
return a.threshold === null ? -1 : 1;
}
return Math.abs(a.threshold) - Math.abs(b.threshold);
});
const mostUrgent = slaStatuses[0];
return {
type: mostUrgent.type,
threshold: formatSLATime(mostUrgent.threshold),
threshold:
mostUrgent.threshold === null ? '' : formatSLATime(mostUrgent.threshold),
icon: mostUrgent.icon,
isSlaMissed: mostUrgent.isSlaMissed,
};
@@ -1,4 +1,4 @@
import { evaluateSLAStatus } from '../slaHelper';
import { evaluateSLAStatus, shouldRefreshSLAStatus } from '../slaHelper';
describe('#SLA Helpers', () => {
const currentTimestamp = 1700000000; // Fixed timestamp for testing
@@ -378,6 +378,109 @@ describe('#SLA Helpers', () => {
});
});
describe('completed SLA misses', () => {
it('freezes a recorded FRT miss at the SLA completion time', () => {
const appliedSla = {
sla_status: 'missed',
sla_completed_at: currentTimestamp - 3600,
sla_frt_due_at: currentTimestamp - 7200,
};
const chat = { status: 'resolved' };
const slaEvents = [
{ event_type: 'frt', created_at: currentTimestamp - 7000 },
];
const result = evaluateSLAStatus({ appliedSla, chat, slaEvents });
expect(result).toMatchObject({
type: 'FRT',
threshold: '1h',
isSlaMissed: true,
});
});
it('freezes a recorded NRT miss at the SLA completion time', () => {
const appliedSla = {
sla_status: 'missed',
sla_completed_at: currentTimestamp - 3600,
};
const chat = { status: 'resolved' };
const slaEvents = [
{ event_type: 'nrt', created_at: currentTimestamp - 5400 },
];
const result = evaluateSLAStatus({ appliedSla, chat, slaEvents });
expect(result).toMatchObject({
type: 'NRT',
threshold: '30m',
isSlaMissed: true,
});
});
it('freezes a recorded RT miss at the SLA completion time', () => {
const appliedSla = {
sla_status: 'missed',
sla_completed_at: currentTimestamp - 3600,
sla_rt_due_at: currentTimestamp - 7200,
};
const chat = { status: 'resolved' };
const slaEvents = [
{ event_type: 'rt', created_at: currentTimestamp - 7000 },
];
const result = evaluateSLAStatus({ appliedSla, chat, slaEvents });
expect(result).toMatchObject({
type: 'RT',
threshold: '1h',
isSlaMissed: true,
});
});
it('returns a static miss for a legacy completed SLA without a timestamp', () => {
const appliedSla = {
sla_status: 'missed',
sla_rt_due_at: currentTimestamp - 7200,
};
const chat = { status: 'resolved' };
const slaEvents = [
{ event_type: 'rt', created_at: currentTimestamp - 7000 },
];
const result = evaluateSLAStatus({ appliedSla, chat, slaEvents });
expect(result).toMatchObject({
type: 'RT',
threshold: '',
isSlaMissed: true,
});
});
});
describe('refresh scheduling', () => {
it('refreshes only active unresolved SLAs', () => {
expect(
shouldRefreshSLAStatus({
appliedSla: { sla_status: 'active' },
chat: { status: 'open' },
})
).toBe(true);
expect(
shouldRefreshSLAStatus({
appliedSla: { sla_status: 'active' },
chat: { status: 'resolved' },
})
).toBe(false);
expect(
shouldRefreshSLAStatus({
appliedSla: { sla_status: 'missed' },
chat: { status: 'open' },
})
).toBe(false);
});
});
describe('time formatting', () => {
it('formats time in days and hours', () => {
const appliedSla = { sla_rt_due_at: currentTimestamp + 90000 }; // 25 hours
@@ -0,0 +1,5 @@
class AddCompletedAtToAppliedSlas < ActiveRecord::Migration[7.1]
def change
add_column :applied_slas, :completed_at, :datetime
end
end
+2 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2026_07_13_184351) do
ActiveRecord::Schema[7.1].define(version: 2026_07_15_000000) do
# These extensions should be enabled to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -178,6 +178,7 @@ ActiveRecord::Schema[7.1].define(version: 2026_07_13_184351) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "sla_status", default: 0
t.datetime "completed_at"
t.index ["account_id", "sla_policy_id", "conversation_id"], name: "index_applied_slas_on_account_sla_policy_conversation", unique: true
t.index ["account_id"], name: "index_applied_slas_on_account_id"
t.index ["conversation_id"], name: "index_applied_slas_on_conversation_id"
+2
View File
@@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# sla_status :integer default("active")
# completed_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
@@ -53,6 +54,7 @@ class AppliedSla < ApplicationRecord
sla_status: sla_status,
created_at: created_at.to_i,
updated_at: updated_at.to_i,
sla_completed_at: completed_at&.to_i,
sla_description: sla_policy.description,
sla_name: sla_policy.name,
sla_first_response_time_threshold: sla_policy.first_response_time_threshold,
@@ -33,6 +33,18 @@ module Enterprise::Conversation
private
def handle_resolved_status_change
super
update_applied_sla_completion
end
def update_applied_sla_completion
return unless saved_change_to_status?
return if applied_sla.blank? || applied_sla.hit? || applied_sla.missed?
applied_sla.update!(completed_at: resolved? ? Time.current : nil)
end
def dispatch_captain_inference_event(event_name)
dispatcher_dispatch(event_name)
end
@@ -0,0 +1,106 @@
class Sla::BackfillAppliedSlaCompletedAtService
DEFAULT_BATCH_SIZE = 500
def initialize(**options)
options.assert_valid_keys(:account_id, :all_accounts, :apply, :batch_size, :after_id, :output)
@account_id = options[:account_id]
@all_accounts = options.fetch(:all_accounts, false)
@apply = options.fetch(:apply, false)
@batch_size = options.fetch(:batch_size, DEFAULT_BATCH_SIZE)
@after_id = options.fetch(:after_id, 0)
@output = options.fetch(:output, $stdout)
end
def perform
validate_options!
scope = candidate_scope
eligible_count = scope.count
counters = { processed: 0, matched: 0, updated: 0, skipped: 0, last_id: @after_id }
print_preflight(eligible_count)
scope.find_in_batches(batch_size: @batch_size, start: @after_id + 1) { |batch| process_batch(batch, counters) }
result = counters.merge(eligible: eligible_count, dry_run: !@apply)
@output.puts "Completed: #{result.inspect}"
result
end
private
def process_batch(batch, counters)
resolution_times = resolution_times_for(batch)
updated_count = @apply ? bulk_update(resolution_times) : 0
counters[:processed] += batch.size
counters[:matched] += resolution_times.size
counters[:updated] += updated_count
counters[:skipped] += batch.size - resolution_times.size
counters[:last_id] = batch.last.id
@output.puts "Processed through applied_sla_id=#{counters[:last_id]} " \
"(matched=#{counters[:matched]}, updated=#{counters[:updated]}, skipped=#{counters[:skipped]})"
end
def validate_options!
account_scope = @account_id.present?
raise ArgumentError, 'Provide exactly one of ACCOUNT_ID or ALL_ACCOUNTS=true' if account_scope == @all_accounts
raise ArgumentError, 'BATCH_SIZE must be greater than zero' unless @batch_size.positive?
raise ArgumentError, 'AFTER_ID must be zero or greater' if @after_id.negative?
Account.find(@account_id) if account_scope
end
def candidate_scope
scope = AppliedSla.where(sla_status: :missed, completed_at: nil).where('applied_slas.id > ?', @after_id)
scope = scope.where(account_id: @account_id) if @account_id.present?
scope
end
def resolution_times_for(batch)
events_by_conversation = ReportingEvent
.where(
account_id: batch.map(&:account_id).uniq,
conversation_id: batch.map(&:conversation_id),
name: 'conversation_resolved'
)
.where.not(event_end_time: nil)
.order(:conversation_id, event_end_time: :desc)
.group_by(&:conversation_id)
batch.each_with_object({}) do |applied_sla, resolution_times|
event = events_by_conversation.fetch(applied_sla.conversation_id, []).find do |reporting_event|
reporting_event.event_end_time.between?(applied_sla.created_at, applied_sla.updated_at)
end
resolution_times[applied_sla.id] = event.event_end_time if event
end
end
def bulk_update(resolution_times)
return 0 if resolution_times.empty?
connection = AppliedSla.connection
values = resolution_times.map do |id, completed_at|
"(#{connection.quote(id)}, #{connection.quote(completed_at)}::timestamp)"
end.join(', ')
statement = <<~SQL.squish
UPDATE #{connection.quote_table_name(AppliedSla.table_name)} AS applied_slas
SET completed_at = backfill.completed_at
FROM (VALUES #{values}) AS backfill(id, completed_at)
WHERE applied_slas.id = backfill.id
AND applied_slas.completed_at IS NULL
SQL
connection.exec_update(statement, 'Backfill applied SLA completed_at')
end
def print_preflight(eligible_count)
scope = @account_id.present? ? "account_id=#{@account_id}" : 'all accounts'
mode = @apply ? 'APPLY' : 'DRY RUN'
@output.puts "Applied SLA completed_at backfill: mode=#{mode}, scope=#{scope}, batch_size=#{@batch_size}, after_id=#{@after_id}"
@output.puts "Eligible missed applied SLAs: #{eligible_count}"
end
end
@@ -3,6 +3,7 @@ json.sla_id resource.sla_policy_id
json.sla_status resource.sla_status
json.created_at resource.created_at.to_i
json.updated_at resource.updated_at.to_i
json.sla_completed_at resource.completed_at&.to_i
json.sla_description resource.sla_policy.description
json.sla_name resource.sla_policy.name
json.sla_first_response_time_threshold resource.sla_policy.first_response_time_threshold
@@ -0,0 +1,30 @@
# Backfill applied_slas.completed_at from conversation resolution reporting events.
#
# Account-scoped dry run:
# ACCOUNT_ID=168154 bundle exec rails runner script/backfill_applied_sla_completed_at.rb
#
# Account-scoped apply:
# ACCOUNT_ID=168154 APPLY=true bundle exec rails runner script/backfill_applied_sla_completed_at.rb
#
# Explicit global apply with resume controls:
# ALL_ACCOUNTS=true APPLY=true BATCH_SIZE=500 AFTER_ID=0 \
# bundle exec rails runner script/backfill_applied_sla_completed_at.rb
begin
account_id = Integer(ENV.fetch('ACCOUNT_ID'), 10) if ENV['ACCOUNT_ID'].present?
all_accounts = ENV['ALL_ACCOUNTS'] == 'true'
apply = ENV['APPLY'] == 'true'
batch_size = Integer(ENV.fetch('BATCH_SIZE', Sla::BackfillAppliedSlaCompletedAtService::DEFAULT_BATCH_SIZE.to_s), 10)
after_id = Integer(ENV.fetch('AFTER_ID', '0'), 10)
Sla::BackfillAppliedSlaCompletedAtService.new(
account_id: account_id,
all_accounts: all_accounts,
apply: apply,
batch_size: batch_size,
after_id: after_id
).perform
rescue ArgumentError, ActiveRecord::RecordNotFound => e
warn "Backfill aborted: #{e.message}"
exit 1
end
@@ -8,13 +8,14 @@ RSpec.describe 'Conversations API', type: :request do
it 'returns SLA data for the conversation if the feature is enabled' do
account.enable_features!('sla')
conversation = create(:conversation, account: account)
applied_sla = create(:applied_sla, conversation: conversation)
applied_sla = create(:applied_sla, conversation: conversation, completed_at: 1.hour.ago)
sla_event = create(:sla_event, conversation: conversation, applied_sla: applied_sla)
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}", headers: administrator.create_new_auth_token
expect(response).to have_http_status(:ok)
expect(response.parsed_body['applied_sla']['id']).to eq(applied_sla.id)
expect(response.parsed_body['applied_sla']['sla_completed_at']).to eq(applied_sla.completed_at.to_i)
expect(response.parsed_body['sla_events'].first['id']).to eq(sla_event.id)
end
@@ -17,6 +17,7 @@ RSpec.describe AppliedSla, type: :model do
sla_status: applied_sla.sla_status,
created_at: applied_sla.created_at.to_i,
updated_at: applied_sla.updated_at.to_i,
sla_completed_at: nil,
sla_description: applied_sla.sla_policy.description,
sla_name: applied_sla.sla_policy.name,
sla_first_response_time_threshold: applied_sla.sla_policy.first_response_time_threshold,
@@ -41,6 +41,37 @@ RSpec.describe Conversation, type: :model do
# end
end
describe 'SLA completion' do
let(:applied_sla) { create(:applied_sla) }
let(:conversation) { applied_sla.conversation }
it 'records the completion time when the conversation is resolved' do
completion_time = Time.zone.parse('2026-07-15 10:00:00')
travel_to(completion_time) { conversation.update!(status: :resolved) }
expect(applied_sla.reload.completed_at).to eq(completion_time)
end
it 'clears the completion time when a nonterminal SLA is reopened' do
conversation.update!(status: :resolved)
conversation.update!(status: :open)
expect(applied_sla.reload.completed_at).to be_nil
end
it 'preserves the completion time when a terminal SLA is reopened' do
conversation.update!(status: :resolved)
completed_at = applied_sla.reload.completed_at
applied_sla.update!(sla_status: :missed)
conversation.update!(status: :open)
expect(applied_sla.reload.completed_at).to eq(completed_at)
end
end
describe 'sla_policy' do
let(:account) { create(:account) }
let(:conversation) { create(:conversation, account: account) }
@@ -0,0 +1,117 @@
require 'rails_helper'
RSpec.describe Sla::BackfillAppliedSlaCompletedAtService do
let(:output) { StringIO.new }
let(:account) { create(:account) }
let(:conversation) { create(:conversation, account: account) }
let(:applied_sla) do
create(
:applied_sla,
account: account,
conversation: conversation,
sla_status: :missed,
created_at: 3.days.ago,
updated_at: 1.day.ago
)
end
let!(:resolution_event) do
create(
:reporting_event,
account: account,
inbox: conversation.inbox,
conversation: conversation,
name: 'conversation_resolved',
event_start_time: applied_sla.created_at,
event_end_time: 2.days.ago
)
end
it 'defaults to a dry run' do
result = described_class.new(account_id: account.id, output: output).perform
expect(result).to include(dry_run: true, eligible: 1, matched: 1, updated: 0, skipped: 0)
expect(applied_sla.reload.completed_at).to be_nil
end
it 'backfills the latest reliable resolution without changing updated_at' do
latest_resolution = create(
:reporting_event,
account: account,
inbox: conversation.inbox,
conversation: conversation,
name: 'conversation_resolved',
event_start_time: applied_sla.created_at,
event_end_time: 36.hours.ago
)
original_updated_at = applied_sla.updated_at
result = described_class.new(account_id: account.id, apply: true, output: output).perform
expect(result).to include(dry_run: false, eligible: 1, matched: 1, updated: 1, skipped: 0)
expect(applied_sla.reload.completed_at).to eq(latest_resolution.event_end_time)
expect(applied_sla.updated_at).to eq(original_updated_at)
end
it 'skips records without a reliable resolution event' do
resolution_event.destroy!
result = described_class.new(account_id: account.id, apply: true, output: output).perform
expect(result).to include(eligible: 1, matched: 0, updated: 0, skipped: 1)
expect(applied_sla.reload.completed_at).to be_nil
end
it 'is idempotent' do
service = described_class.new(account_id: account.id, apply: true, output: output)
service.perform
result = service.perform
expect(result).to include(eligible: 0, matched: 0, updated: 0, skipped: 0)
expect(applied_sla.reload.completed_at).to eq(resolution_event.event_end_time)
end
it 'requires exactly one account scope' do
expect { described_class.new(output: output).perform }
.to raise_error(ArgumentError, 'Provide exactly one of ACCOUNT_ID or ALL_ACCOUNTS=true')
expect { described_class.new(account_id: account.id, all_accounts: true, output: output).perform }
.to raise_error(ArgumentError, 'Provide exactly one of ACCOUNT_ID or ALL_ACCOUNTS=true')
end
it 'limits account runs and requires explicit global scope for other accounts' do
other_account = create(:account)
other_conversation = create(:conversation, account: other_account)
other_applied_sla = create(
:applied_sla,
account: other_account,
conversation: other_conversation,
sla_status: :missed,
created_at: 3.days.ago,
updated_at: 1.day.ago
)
other_resolution_event = create(
:reporting_event,
account: other_account,
inbox: other_conversation.inbox,
conversation: other_conversation,
name: 'conversation_resolved',
event_start_time: other_applied_sla.created_at,
event_end_time: 2.days.ago
)
described_class.new(account_id: account.id, apply: true, output: output).perform
expect(applied_sla.reload.completed_at).to eq(resolution_event.event_end_time)
expect(other_applied_sla.reload.completed_at).to be_nil
described_class.new(all_accounts: true, apply: true, output: output).perform
expect(other_applied_sla.reload.completed_at).to eq(other_resolution_event.event_end_time)
end
it 'resumes after the supplied applied SLA id' do
result = described_class.new(account_id: account.id, after_id: applied_sla.id, output: output).perform
expect(result).to include(eligible: 0, processed: 0, last_id: applied_sla.id)
end
end