-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathElasticsearchSyncController.java
More file actions
484 lines (398 loc) · 18.3 KB
/
ElasticsearchSyncController.java
File metadata and controls
484 lines (398 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.identity.controller.elasticsearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.iemr.common.identity.data.elasticsearch.ElasticsearchSyncJob;
import com.iemr.common.identity.repo.BenMappingRepo;
import com.iemr.common.identity.service.elasticsearch.ElasticsearchSyncService;
import com.iemr.common.identity.service.elasticsearch.SyncJobService;
import com.iemr.common.identity.service.elasticsearch.ElasticsearchSyncService.SyncStatus;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import com.iemr.common.identity.utils.response.OutputResponse;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.iemr.common.identity.domain.identity.MBeneficiarymapping;
import com.iemr.common.identity.service.elasticsearch.ElasticsearchIndexingService;
/**
* Controller to manage Elasticsearch synchronization operations
* Supports both synchronous and asynchronous sync jobs
*/
@RestController
@RequestMapping("/elasticsearch")
public class ElasticsearchSyncController {
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchSyncController.class);
@Autowired
private ElasticsearchSyncService syncService;
@Autowired
private SyncJobService syncJobService;
@Autowired
private BenMappingRepo mappingRepo;
@Autowired
private ElasticsearchIndexingService indexingService;
@Autowired
private ElasticsearchClient esClient;
@Value("${elasticsearch.index.beneficiary}")
private String beneficiaryIndex;
/**
* Start async full sync (RECOMMENDED for millions of records)
* Returns immediately with job ID for tracking
*
* Usage: POST http://localhost:8094/elasticsearch/start
*/
@PostMapping("/start")
public ResponseEntity<Map<String, Object>> startAsyncFullSync(
@RequestParam(required = false, defaultValue = "API") String triggeredBy) {
logger.info("Received request to start ASYNC full sync");
Map<String, Object> response = new HashMap<>();
try {
ElasticsearchSyncJob job = syncJobService.startFullSyncJob(triggeredBy);
response.put("status", "success");
response.put("message", "Sync job started in background");
response.put("jobId", job.getJobId());
response.put("jobStatus", job.getStatus());
response.put("checkStatusUrl", "/elasticsearch/status/" + job.getJobId());
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
logger.error("Error starting async sync: {}", e.getMessage());
response.put("status", "error");
response.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.CONFLICT).body(response);
} catch (Exception e) {
logger.error("Unexpected error: {}", e.getMessage(), e);
response.put("status", "error");
response.put("message", "Unexpected error: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
/**
* Get job status by ID
*
* Usage: GET http://localhost:8094/elasticsearch/status/{jobid}
*/
@GetMapping("/status/{jobId}")
public ResponseEntity<Map<String, Object>> getAsyncJobStatus(@PathVariable Long jobId) {
logger.info("Checking status for job: {}", jobId);
try {
ElasticsearchSyncJob job = syncJobService.getJobStatus(jobId);
Map<String, Object> response = new HashMap<>();
response.put("jobId", job.getJobId());
response.put("jobType", job.getJobType());
response.put("status", job.getStatus());
response.put("totalRecords", job.getTotalRecords());
response.put("processedRecords", job.getProcessedRecords());
response.put("successCount", job.getSuccessCount());
response.put("failureCount", job.getFailureCount());
response.put("progressPercentage", String.format("%.2f", job.getProgressPercentage()));
response.put("processingSpeed", job.getProcessingSpeed());
response.put("estimatedTimeRemaining", job.getEstimatedTimeRemaining());
response.put("startedAt", job.getStartedAt());
response.put("completedAt", job.getCompletedAt());
response.put("errorMessage", job.getErrorMessage());
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
Map<String, Object> response = new HashMap<>();
response.put("status", "error");
response.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
}
/**
* Get all active jobs
*
* Usage: GET http://localhost:8094/elasticsearch/active
*/
@GetMapping("/active")
public ResponseEntity<List<ElasticsearchSyncJob>> getActiveJobs() {
logger.info("Fetching active jobs");
return ResponseEntity.ok(syncJobService.getActiveJobs());
}
/**
* Get recent jobs
*
* Usage: GET http://localhost:8080/elasticsearch/recent
*/
@GetMapping("/recent")
public ResponseEntity<List<ElasticsearchSyncJob>> getRecentJobs() {
logger.info("Fetching recent jobs");
return ResponseEntity.ok(syncJobService.getRecentJobs());
}
/**
* Resume a failed job
*
* Usage: POST http://localhost:8094/elasticsearch/resume/{jobid}
*/
@PostMapping("/resume/{jobId}")
public ResponseEntity<Map<String, Object>> resumeJob(
@PathVariable Long jobId,
@RequestParam(required = false, defaultValue = "API") String triggeredBy) {
logger.info("Resuming job: {}", jobId);
Map<String, Object> response = new HashMap<>();
try {
ElasticsearchSyncJob job = syncJobService.resumeJob(jobId, triggeredBy);
response.put("status", "success");
response.put("message", "Job resumed");
response.put("jobId", job.getJobId());
response.put("resumedFromOffset", job.getCurrentOffset());
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
response.put("status", "error");
response.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
}
/**
* Cancel a running job
*
* Usage: POST http://localhost:8094/elasticsearch/cancel/{jobid}
*/
@PostMapping("/cancel/{jobId}")
public ResponseEntity<Map<String, Object>> cancelJob(@PathVariable Long jobId) {
logger.info("Cancelling job: {}", jobId);
Map<String, Object> response = new HashMap<>();
boolean cancelled = syncJobService.cancelJob(jobId);
if (cancelled) {
response.put("status", "success");
response.put("message", "Job cancelled");
return ResponseEntity.ok(response);
} else {
response.put("status", "error");
response.put("message", "Could not cancel job. It may not be active.");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
}
/**
* LEGACY: Synchronous full sync(NOT recommended for large datasets)
* Use /start instead
*
* Usage: POST http://localhost:8094/elasticsearch/all
*/
@PostMapping("/all")
public ResponseEntity<Map<String, Object>> syncAllBeneficiaries() {
logger.warn("LEGACY sync endpoint called. Consider using /start instead.");
logger.info("Received request to sync all beneficiaries (BLOCKING)");
Map<String, Object> response = new HashMap<>();
try {
ElasticsearchSyncService.SyncResult result = syncService.syncAllBeneficiaries();
response.put("status", "completed");
response.put("successCount", result.getSuccessCount());
response.put("failureCount", result.getFailureCount());
response.put("error", result.getError());
response.put("warning", "This is a blocking operation. For large datasets, use /start");
if (result.getError() != null) {
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(response);
}
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error in sync all endpoint: {}", e.getMessage(), e);
response.put("status", "error");
response.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
/**
* Sync a single beneficiary by BenRegId
*
* Usage: POST http://localhost:8094/elasticsearch/single/123456
*/
@PostMapping("/single/{benRegId}")
public ResponseEntity<Map<String, Object>> syncSingleBeneficiary(
@PathVariable String benRegId) {
Map<String, Object> response = new HashMap<>();
try {
boolean success = syncService.syncSingleBeneficiary(benRegId);
response.put("status", success ? "success" : "failed");
response.put("benRegId", benRegId);
response.put("synced", success);
if (!success) {
response.put("message", "Beneficiary not found in database or sync failed. Check logs for details.");
} else {
response.put("message", "Beneficiary successfully synced to Elasticsearch");
}
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error syncing single beneficiary: {}", e.getMessage(), e);
response.put("status", "error");
response.put("benRegId", benRegId);
response.put("synced", false);
response.put("message", "Exception occurred: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
/**
* Check sync status - compare DB count vs ES count
*
* Usage: GET http://localhost:8094/elasticsearch/status
*/
@GetMapping("/status")
public ResponseEntity<SyncStatus> checkSyncStatus() {
logger.info("Received request to check sync status");
try {
SyncStatus status = syncService.checkSyncStatus();
return ResponseEntity.ok(status);
} catch (Exception e) {
logger.error("Error checking sync status: {}", e.getMessage(), e);
SyncStatus errorStatus = new SyncStatus();
errorStatus.setError(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorStatus);
}
}
/**
* Health check endpoint
*
* Usage: GET http://localhost:8094/elasticsearch/health
*/
@GetMapping("/health")
public ResponseEntity<Map<String, Object>> healthCheck() {
Map<String, Object> response = new HashMap<>();
response.put("status", "UP");
response.put("service", "Elasticsearch Sync Service");
response.put("asyncJobsRunning", syncJobService.isFullSyncRunning());
response.put("activeJobs", syncJobService.getActiveJobs().size());
return ResponseEntity.ok(response);
}
/**
* Debug endpoint to check if a beneficiary exists in database
*
* Usage: GET http://localhost:8094/elasticsearch/debug/check/123456
*/
@GetMapping("/debug/check/{benRegId}")
public ResponseEntity<Map<String, Object>> checkBeneficiaryExists(
@PathVariable String benRegId) {
Map<String, Object> response = new HashMap<>();
try {
java.math.BigInteger benRegIdBig = new java.math.BigInteger(benRegId);
boolean exists = mappingRepo.existsByBenRegId(benRegIdBig);
response.put("benRegId", benRegId);
response.put("existsInDatabase", exists);
if (exists) {
response.put("message", "Beneficiary found in database");
MBeneficiarymapping mapping =
mappingRepo.findByBenRegId(benRegIdBig);
if (mapping != null) {
response.put("benMapId", mapping.getBenMapId());
response.put("deleted", mapping.getDeleted());
response.put("hasDetails", mapping.getMBeneficiarydetail() != null);
response.put("hasContact", mapping.getMBeneficiarycontact() != null);
}
} else {
response.put("message", "Beneficiary NOT found in database");
}
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error checking beneficiary: {}", e.getMessage(), e);
response.put("status", "error");
response.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
/**
* Create or recreate the Elasticsearch index with proper mapping
* This will DELETE the existing index and create a new one
*
* POST /elasticsearch/index/create
*/
@PostMapping("/index/create")
public ResponseEntity<OutputResponse> createIndex() {
logger.info("API: Create Elasticsearch index request received");
OutputResponse response = new OutputResponse();
try {
indexingService.createIndexWithMapping();
response.setResponse("Index created successfully. Ready for data sync.");
logger.info("Index created successfully");
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error creating index: {}", e.getMessage(), e);
response.setError(5000, "Error creating index: " + e.getMessage());
return ResponseEntity.status(500).body(response);
}
}
/**
* Recreate index and immediately start syncing data
* This is a convenience endpoint that does both operations
*
* POST /elasticsearch/index/recreate-and-sync
*/
@PostMapping("/index/recreate-and-sync")
public ResponseEntity<OutputResponse> recreateAndSync() {
logger.info("API: Recreate index and sync request received");
OutputResponse response = new OutputResponse();
try {
// Step 1: Recreate index
logger.info("Step 1: Recreating index...");
indexingService.createIndexWithMapping();
logger.info("Index recreated successfully");
// Step 2: Start sync
logger.info("Step 2: Starting data sync...");
Map<String, Integer> syncResult = indexingService.indexAllBeneficiaries();
response.setResponse("Index recreated and sync started. Success: " +
syncResult.get("success") + ", Failed: " + syncResult.get("failed"));
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error in recreate and sync: {}", e.getMessage(), e);
response.setError(5000, "Error: " + e.getMessage());
return ResponseEntity.status(500).body(response);
}
}
/**
* Get information about the current index
* Shows mapping, document count, etc.
*
* GET /elasticsearch/index/info
*/
@GetMapping("/index/info")
public ResponseEntity<OutputResponse> getIndexInfo() {
logger.info("API: Get index info request received");
OutputResponse response = new OutputResponse();
try {
// You can add code here to get index stats using esClient
response.setResponse("Index info endpoint - implementation pending");
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Error getting index info: {}", e.getMessage(), e);
response.setError(5000, "Error: " + e.getMessage());
return ResponseEntity.status(500).body(response);
}
}
@PostMapping("/refresh")
public ResponseEntity<Map<String, Object>> refreshIndex() {
Map<String, Object> response = new HashMap<>();
try {
logger.info("Manual refresh requested");
esClient.indices().refresh(r -> r.index(beneficiaryIndex));
response.put("status", "success");
response.put("message", "Index refreshed - all data is now searchable");
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("Refresh failed: {}", e.getMessage(), e);
response.put("status", "error");
response.put("message", "Refresh failed: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
}