Skip to content

Commit e7e15b9

Browse files
committed
Remove functionality for deleting inactive contacts which is now handled by RP/mailroom
1 parent b78b579 commit e7e15b9

File tree

5 files changed

+27
-55
lines changed

5 files changed

+27
-55
lines changed

daemon.go

-2
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ func (d *Daemon) reportStats(includeLag bool) {
114114
prev := d.prevStats[ix]
115115

116116
indexedInPeriod := stats.Indexed - prev.Indexed
117-
deletedInPeriod := stats.Deleted - prev.Deleted
118117
elapsedInPeriod := stats.Elapsed - prev.Elapsed
119118
rateInPeriod := float64(0)
120119
if indexedInPeriod > 0 && elapsedInPeriod > 0 {
121120
rateInPeriod = float64(indexedInPeriod) / (float64(elapsedInPeriod) / float64(time.Second))
122121
}
123122

124123
metrics[ix.Name()+"_indexed"] = float64(indexedInPeriod)
125-
metrics[ix.Name()+"_deleted"] = float64(deletedInPeriod)
126124
metrics[ix.Name()+"_rate"] = rateInPeriod
127125

128126
d.prevStats[ix] = stats

indexers/base.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ import (
1818
// indexes a document
1919
const indexCommand = `{ "index": { "_id": %d, "version": %d, "version_type": "external", "routing": %d} }`
2020

21-
// deletes a document
22-
const deleteCommand = `{ "delete" : { "_id": %d, "version": %d, "version_type": "external", "routing": %d} }`
23-
2421
type Stats struct {
2522
Indexed int64 // total number of documents indexed
26-
Deleted int64 // total number of documents deleted
2723
Elapsed time.Duration // total time spent actually indexing (excludes poll delay)
2824
}
2925

@@ -85,12 +81,11 @@ func (i *baseIndexer) log() *slog.Logger {
8581
}
8682

8783
// records indexing activity and updates statistics
88-
func (i *baseIndexer) recordActivity(indexed, deleted int, elapsed time.Duration) {
84+
func (i *baseIndexer) recordActivity(indexed int, elapsed time.Duration) {
8985
i.stats.Indexed += int64(indexed)
90-
i.stats.Deleted += int64(deleted)
9186
i.stats.Elapsed += elapsed
9287

93-
i.log().Info("completed indexing", "indexed", indexed, "deleted", deleted, "elapsed", elapsed)
88+
i.log().Info("completed indexing", "indexed", indexed, "elapsed", elapsed)
9489
}
9590

9691
// our response for figuring out the physical index for an alias
@@ -267,16 +262,16 @@ type indexResponse struct {
267262
}
268263

269264
// indexes the batch of contacts
270-
func (i *baseIndexer) indexBatch(index string, batch []byte) (int, int, int, error) {
265+
func (i *baseIndexer) indexBatch(index string, batch []byte) (int, int, error) {
271266
response := indexResponse{}
272267
indexURL := fmt.Sprintf("%s/%s/_bulk", i.elasticURL, index)
273268

274269
_, err := utils.MakeJSONRequest(http.MethodPut, indexURL, batch, &response)
275270
if err != nil {
276-
return 0, 0, 0, err
271+
return 0, 0, err
277272
}
278273

279-
createdCount, updatedCount, deletedCount, conflictedCount := 0, 0, 0, 0
274+
createdCount, updatedCount, conflictedCount := 0, 0, 0
280275

281276
for _, item := range response.Items {
282277
if item.Index.ID != "" {
@@ -290,21 +285,14 @@ func (i *baseIndexer) indexBatch(index string, batch []byte) (int, int, int, err
290285
} else {
291286
slog.Error("error indexing document", "id", item.Index.ID, "status", item.Index.Status, "result", item.Index.Result)
292287
}
293-
} else if item.Delete.ID != "" {
294-
slog.Debug("delete response", "id", item.Index.ID, "status", item.Index.Status)
295-
if item.Delete.Status == 200 {
296-
deletedCount++
297-
} else if item.Delete.Status == 409 {
298-
conflictedCount++
299-
}
300288
} else {
301289
slog.Error("unparsed item in response")
302290
}
303291
}
304292

305-
slog.Debug("indexed batch", "created", createdCount, "updated", updatedCount, "deleted", deletedCount, "conflicted", conflictedCount)
293+
slog.Debug("indexed batch", "created", createdCount, "updated", updatedCount, "conflicted", conflictedCount)
306294

307-
return createdCount, updatedCount, deletedCount, nil
295+
return createdCount, updatedCount, nil
308296
}
309297

310298
// our response for finding the last modified document

indexers/base_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ func assertIndexesWithPrefix(t *testing.T, cfg *indexer.Config, prefix string, e
7878
assert.Equal(t, expected, actual)
7979
}
8080

81-
func assertIndexerStats(t *testing.T, ix indexers.Indexer, expectedIndexed, expectedDeleted int64) {
81+
func assertIndexerStats(t *testing.T, ix indexers.Indexer, expectedIndexed int64) {
8282
actual := ix.Stats()
8383
assert.Equal(t, expectedIndexed, actual.Indexed, "indexed mismatch")
84-
assert.Equal(t, expectedDeleted, actual.Deleted, "deleted mismatch")
8584
}
8685

8786
func elasticRequest(t *testing.T, cfg *indexer.Config, method, path string, data map[string]any) map[string]any {

indexers/contacts.go

+11-23
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool) (string, error
8989
}
9090

9191
const sqlSelectModifiedContacts = `
92-
SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
92+
SELECT org_id, id, modified_on, row_to_json(t) FROM (
9393
SELECT
9494
id,
9595
org_id,
@@ -98,7 +98,6 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
9898
language,
9999
status,
100100
ticket_count AS tickets,
101-
is_active,
102101
created_on,
103102
modified_on,
104103
last_seen_on,
@@ -143,20 +142,19 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
143142
SELECT array_to_json(array_agg(DISTINCT fr.flow_id)) FROM flows_flowrun fr WHERE fr.contact_id = contacts_contact.id
144143
) AS flow_history_ids
145144
FROM contacts_contact
146-
WHERE modified_on >= $1
145+
WHERE modified_on >= $1 AND is_active
147146
ORDER BY modified_on ASC
148147
LIMIT 100000
149148
) t;
150149
`
151150

152151
// IndexModified queries and indexes all contacts with a lastModified greater than or equal to the passed in time
153152
func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index string, lastModified time.Time, rebuild bool) error {
154-
totalFetched, totalCreated, totalUpdated, totalDeleted := 0, 0, 0, 0
153+
totalFetched, totalCreated, totalUpdated := 0, 0, 0
155154

156155
var modifiedOn time.Time
157156
var contactJSON string
158157
var id, orgID int64
159-
var isActive bool
160158

161159
subBatch := &bytes.Buffer{}
162160
start := time.Now()
@@ -166,20 +164,18 @@ func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index st
166164
batchFetched := 0 // contacts fetched in this batch
167165
batchCreated := 0 // contacts created in ES
168166
batchUpdated := 0 // contacts updated in ES
169-
batchDeleted := 0 // contacts deleted in ES
170167
batchESTime := time.Duration(0) // time spent indexing for this batch
171168

172169
indexSubBatch := func(b *bytes.Buffer) error {
173170
t := time.Now()
174-
created, updated, deleted, err := i.indexBatch(index, b.Bytes())
171+
created, updated, err := i.indexBatch(index, b.Bytes())
175172
if err != nil {
176173
return err
177174
}
178175

179176
batchESTime += time.Since(t)
180177
batchCreated += created
181178
batchUpdated += updated
182-
batchDeleted += deleted
183179
b.Reset()
184180
return nil
185181
}
@@ -198,27 +194,20 @@ func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index st
198194
defer rows.Close()
199195

200196
for rows.Next() {
201-
err = rows.Scan(&orgID, &id, &modifiedOn, &isActive, &contactJSON)
197+
err = rows.Scan(&orgID, &id, &modifiedOn, &contactJSON)
202198
if err != nil {
203199
return err
204200
}
205201

206202
batchFetched++
207203
lastModified = modifiedOn
208204

209-
if isActive {
210-
i.log().Debug("modified contact", "id", id, "modifiedOn", modifiedOn, "contact", contactJSON)
205+
i.log().Debug("modified contact", "id", id, "modifiedOn", modifiedOn, "contact", contactJSON)
211206

212-
subBatch.WriteString(fmt.Sprintf(indexCommand, id, modifiedOn.UnixNano(), orgID))
213-
subBatch.WriteString("\n")
214-
subBatch.WriteString(contactJSON)
215-
subBatch.WriteString("\n")
216-
} else {
217-
i.log().Debug("deleted contact", "id", id, "modifiedOn", modifiedOn)
218-
219-
subBatch.WriteString(fmt.Sprintf(deleteCommand, id, modifiedOn.UnixNano(), orgID))
220-
subBatch.WriteString("\n")
221-
}
207+
subBatch.WriteString(fmt.Sprintf(indexCommand, id, modifiedOn.UnixNano(), orgID))
208+
subBatch.WriteString("\n")
209+
subBatch.WriteString(contactJSON)
210+
subBatch.WriteString("\n")
222211

223212
// write to elastic search in batches
224213
if batchFetched%i.batchSize == 0 {
@@ -239,7 +228,6 @@ func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index st
239228
totalFetched += batchFetched
240229
totalCreated += batchCreated
241230
totalUpdated += batchUpdated
242-
totalDeleted += batchDeleted
243231

244232
totalTime := time.Since(start)
245233
batchTime := time.Since(batchStart)
@@ -265,7 +253,7 @@ func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index st
265253
log.Debug("indexed contact batch")
266254
}
267255

268-
i.recordActivity(batchCreated+batchUpdated, batchDeleted, time.Since(batchStart))
256+
i.recordActivity(batchCreated+batchUpdated, time.Since(batchStart))
269257

270258
// last modified stayed the same and we didn't add anything, seen it all, break out
271259
if lastModified.Equal(queryModified) && batchCreated == 0 {

indexers/contacts_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestContacts(t *testing.T) {
189189
assert.NoError(t, err)
190190
assert.WithinDuration(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), esModified, 0)
191191

192-
assertIndexerStats(t, ix1, 9, 0)
192+
assertIndexerStats(t, ix1, 9)
193193
assertIndexesWithPrefix(t, cfg, cfg.ContactsIndex, []string{expectedIndexName})
194194

195195
for _, tc := range contactQueryTests {
@@ -200,25 +200,24 @@ func TestContacts(t *testing.T) {
200200
assert.NoError(t, err)
201201
assert.Equal(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), lastModified.In(time.UTC))
202202

203-
// now make some contact changes, removing one contact, updating another
203+
// now make some contact changes
204204
_, err = db.Exec(`
205205
DELETE FROM contacts_contactgroup_contacts WHERE id = 3;
206-
UPDATE contacts_contact SET name = 'John Deer', modified_on = '2020-08-20 14:00:00+00' where id = 2;
207-
UPDATE contacts_contact SET is_active = FALSE, modified_on = '2020-08-22 15:00:00+00' where id = 4;`)
206+
UPDATE contacts_contact SET name = 'John Deer', modified_on = '2020-08-20 14:00:00+00' where id = 2;`)
208207
require.NoError(t, err)
209208

210209
// and index again...
211210
indexName, err = ix1.Index(db, false, false)
212211
assert.NoError(t, err)
213212
assert.Equal(t, expectedIndexName, indexName) // same index used
214-
assertIndexerStats(t, ix1, 10, 1)
213+
assertIndexerStats(t, ix1, 10)
215214

216215
time.Sleep(1 * time.Second)
217216

218217
assertIndexesWithPrefix(t, cfg, cfg.ContactsIndex, []string{expectedIndexName})
219218

220-
// should only match new john, old john is gone
221-
assertQuery(t, cfg, elastic.Match("name", "john"), []int64{2})
219+
// still have two johns
220+
assertQuery(t, cfg, elastic.Match("name", "john"), []int64{2, 4})
222221

223222
// 3 is no longer in our group
224223
assertQuery(t, cfg, elastic.Match("group_ids", 4), []int64{1})
@@ -234,7 +233,7 @@ func TestContacts(t *testing.T) {
234233
indexName2, err := ix2.Index(db, true, false)
235234
assert.NoError(t, err)
236235
assert.Equal(t, expectedIndexName+"_1", indexName2) // new index used
237-
assertIndexerStats(t, ix2, 8, 0)
236+
assertIndexerStats(t, ix2, 9)
238237

239238
time.Sleep(1 * time.Second)
240239

@@ -249,7 +248,7 @@ func TestContacts(t *testing.T) {
249248
indexName3, err := ix3.Index(db, true, true)
250249
assert.NoError(t, err)
251250
assert.Equal(t, expectedIndexName+"_2", indexName3) // new index used
252-
assertIndexerStats(t, ix3, 8, 0)
251+
assertIndexerStats(t, ix3, 9)
253252

254253
// check we cleaned up indexes besides the new one
255254
assertIndexesWithPrefix(t, cfg, cfg.ContactsIndex, []string{expectedIndexName + "_2"})

0 commit comments

Comments
 (0)