Skip to content

Commit 505ef19

Browse files
Merge pull request #791 from motech-implementations/obd_file_bifurcation
Bifurcation of obd files to separate priority subscribers to enable b…
2 parents 87a81d4 + be31ab7 commit 505ef19

File tree

8 files changed

+331
-169
lines changed

8 files changed

+331
-169
lines changed

imi/src/main/java/org/motechproject/nms/imi/service/TargetFileService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.motechproject.nms.imi.web.contract.FileProcessedStatusRequest;
55
import org.motechproject.nms.kilkari.domain.SubscriptionOrigin;
66

7+
import java.util.HashMap;
8+
79
/**
810
* Creating the targetFile: a csv file containing all the phone numbers to be called by the IVR system
911
*/
@@ -12,9 +14,7 @@ public interface TargetFileService {
1214
* Probably only to be called by an IT. This service's constructor sets the repeating
1315
* nms.obd.generate_target_file MOTECH event which triggers the daily generation of the targetFile.
1416
*/
15-
TargetFileNotification generateTargetFile();
16-
17-
17+
HashMap<String, TargetFileNotification> generateTargetFile(boolean split);
1818
/**
1919
* The IVR system invoked the obdFileProcessedStatusNotification http endpoint signalling the completion of the
2020
* processing of the targetFile we generated
@@ -32,4 +32,6 @@ public interface TargetFileService {
3232
* @return the IMI ServiceID
3333
*/
3434
String serviceIdFromOrigin(boolean freshCall, SubscriptionOrigin origin);
35+
36+
String serviceIdFromOriginJh(boolean freshCall, SubscriptionOrigin origin);
3537
}

imi/src/main/java/org/motechproject/nms/imi/service/impl/TargetFileServiceImpl.java

Lines changed: 239 additions & 124 deletions
Large diffs are not rendered by default.

imi/src/main/java/org/motechproject/nms/imi/web/ImiController.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.web.bind.annotation.ResponseStatus;
3636

3737
import javax.servlet.http.HttpServletRequest;
38+
import java.util.HashMap;
3839
import java.util.regex.Pattern;
3940

4041
/**
@@ -48,6 +49,7 @@ public class ImiController {
4849
public static final String INVALID_STATUS_ENUM = "Can not construct instance of " +
4950
"org.motechproject.nms.imi.domain.FileProcessedStatus from String value";
5051
public static final Pattern TARGET_FILENAME_PATTERN = Pattern.compile("OBD_NMS_[0-9]{14}\\.csv");
52+
public static final Pattern TARGET_FILENAME_PATTERN_JH = Pattern.compile("OBD_NMS_[0-9]{14}JH\\.csv");
5153
public static final String IVR_INTERACTION_LOG = "IVR INTERACTION: %s";
5254

5355
private static final Logger LOGGER = LoggerFactory.getLogger(ImiController.class);
@@ -59,6 +61,8 @@ public class ImiController {
5961
private FileAuditRecordDataService fileAuditRecordDataService;
6062
private AlertService alertService;
6163

64+
public static final String generateJhFile = "imi.obd_bifurcate";
65+
6266

6367
@Autowired
6468
public ImiController(SettingsFacade settingsFacade, CdrFileService cdrFileService, AlertService alertService,
@@ -88,7 +92,8 @@ private static boolean validateFieldPresent(StringBuilder errors, String fieldNa
8892
// verify the passed targetFileName is valid
8993
private static boolean validateTargetFileName(StringBuilder errors, String targetFileName) {
9094
if (validateFieldPresent(errors, "fileName", targetFileName)) {
91-
if (TARGET_FILENAME_PATTERN.matcher(targetFileName).matches()) {
95+
if (TARGET_FILENAME_PATTERN.matcher(targetFileName).matches() ||
96+
TARGET_FILENAME_PATTERN_JH.matcher(targetFileName).matches() ) {
9297
return true;
9398
} else {
9499
errors.append(String.format(INVALID, "fileName"));
@@ -148,13 +153,13 @@ public String generateTargetFile() {
148153

149154
LOGGER.debug("/generateTargetFile (GET)");
150155
try {
151-
TargetFileNotification tfn = targetFileService.generateTargetFile();
152-
LOGGER.debug("targetFileService.generateTargetFile() done");
156+
HashMap<String, TargetFileNotification> tfn = targetFileService.generateTargetFile(Boolean.parseBoolean(settingsFacade.getProperty(generateJhFile)));
157+
LOGGER.debug("targetFileService.generateTargetFile() done");
153158

154-
return tfn == null ? "null" : tfn.getFileName();
155-
} catch(Exception e) {
156-
LOGGER.error(e.getMessage(),e);
157-
throw e;
159+
return tfn == null ? "null" : tfn.values().toString();
160+
}catch(Exception e){
161+
LOGGER.error(e.getMessage(), e);
162+
throw e;
158163
}
159164

160165
}

imi/src/main/resources/imi.properties

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ imi.target_file_time=05:00
55
imi.target_file_sec_interval=86400
66

77
#local directory where target files should be generated
8-
imi.local_obd_dir=obd-files-local
8+
imi.local_obd_dir=/usr/local/imi/obd
99

1010
#remote directory where target files should be copied to
11-
imi.remote_obd_dir=obd-files-remote
11+
imi.remote_obd_dir=/usr/local/imi/obd-remote
1212

1313
#local directory where CDR files should be copied to
1414
imi.local_cdr_dir=cdr-files-local
@@ -44,6 +44,11 @@ imi.fresh_no_check_dnd=Retryonroute1noDND_1444132196827
4444
imi.retry_check_dnd=Retryonroute2noDND_1444132213332
4545
imi.retry_no_check_dnd= Retryonroute2noDND_1444132213332
4646

47+
imi.fresh_check_dnd_jh= jhrRetryonroute1noDND_1560947525746
48+
imi.fresh_no_check_dnd_jh=jhrRetryonroute1noDND_1560947525746
49+
imi.retry_check_dnd_jh=jhrRetryonroute2noDND_1560947563925
50+
imi.retry_no_check_dnd_jh= jhrRetryonroute2noDND_1560947563925
51+
4752
#maximum number of errors allowed in a CDR file after which all errors are ignored so as to not overwhelm the
4853
#audit table & the tomcat log file
4954
imi.max_cdr_error_count=100
@@ -85,4 +90,7 @@ imi.distributed_csr_processing=false
8590

8691
# CSR chunk size - any number {n} greater than one will enable chunk processing, where sets (or chunks) of {n} CSRs are
8792
# distributed for processing, regardless of the value of imi.distributed_csr_processing
88-
imi.csr_chunk_size=1000
93+
imi.csr_chunk_size=1000
94+
95+
#set to false if bifurcation of obd files is not required
96+
imi.obd_bifurcate=true

kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Subscription createSubscription(Subscriber subscriber, long callingNumber, Langu
150150
*/
151151
List<Subscription> findActiveSubscriptionsForDay(DayOfTheWeek dayOfTheWeek, long offset, int rowCount);
152152

153+
List<String> findJhSubscriptionIds();
153154

154155
/**
155156
* Get the list of pending subscriptions that starts after the specified date.

kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,31 @@ public List<Subscription> execute(Query query) {
10111011
return subscriptions;
10121012
}
10131013

1014+
@Override
1015+
public List<String> findJhSubscriptionIds() {
1016+
SqlQueryExecution<List<String>> queryExecution = new SqlQueryExecution<List<String>>() {
1017+
@Override
1018+
public String getSqlQuery() {
1019+
String query = "select subscriptionId from (select subscriptionId from nms_mcts_mothers a LEFT JOIN " +
1020+
"nms_subscribers b on b.mother_id_oid=a.id LEFT JOIN nms_subscriptions c on c.subscriber_id_oid = b.id where rchId like 'JH%' " +
1021+
"and subscriptionPack_id_OID = 1 UNION ALL select subscriptionId from nms_mcts_children a LEFT JOIN " +
1022+
"nms_subscribers b on b.child_id_oid=a.id LEFT JOIN nms_subscriptions c on c.subscriber_id_oid = b.id " +
1023+
"where rchId like 'JH%' and subscriptionPack_id_OID = 2) as a;";
1024+
LOGGER.debug(KilkariConstants.SQL_QUERY_LOG, query);
1025+
return query;
1026+
}
1027+
1028+
@Override
1029+
public List<String> execute(Query query) {
1030+
List<String> ids = (List<String>) query.execute();
1031+
return ids;
1032+
}
1033+
};
1034+
List<String> subscriptionIds = subscriptionDataService.executeSQLQuery(queryExecution);
1035+
return subscriptionIds;
1036+
}
1037+
1038+
10141039

10151040
public List<Subscription> findPendingSubscriptionsFromDate(DateTime startDate, int page, int pageSize) {
10161041
return subscriptionDataService.findByStatusAndStartDate(SubscriptionStatus.PENDING_ACTIVATION, startDate, new QueryParams(page, pageSize));

testing/src/test/java/org/motechproject/nms/testing/it/imi/ImiControllerCdrBundleIT.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.io.IOException;
5353
import java.nio.file.Files;
5454
import java.security.NoSuchAlgorithmException;
55+
import java.util.HashMap;
5556

5657
import static org.junit.Assert.assertEquals;
5758
import static org.junit.Assert.assertNotNull;
@@ -100,6 +101,8 @@ public class ImiControllerCdrBundleIT extends BasePaxIT {
100101
private String localObdDirBackup;
101102
private String remoteObdDirBackup;
102103
private CdrHelper helper;
104+
private static final String Jh = "JH";
105+
private static final String non_Jh = "NON-JH";
103106

104107

105108
@Before
@@ -511,12 +514,12 @@ public void verifyTargetFileNameRoundTrip() throws IOException, InterruptedExcep
511514
SubscriptionOrigin.MCTS_IMPORT);
512515
transactionManager.commit(status);
513516

514-
TargetFileNotification tfn = targetFileService.generateTargetFile();
515-
assertNotNull(tfn);
517+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
518+
assertNotNull(tfn.get(non_Jh));
516519

517520
CdrHelper cdrHelper = new CdrHelper(settingsService, subscriberService,subscriptionService, subscriberDataService,
518521
subscriptionPackDataService, languageDataService, languageService, circleDataService, stateDataService,
519-
districtDataService, fileAuditRecordDataService, districtService, tfn.getFileName());
522+
districtDataService, fileAuditRecordDataService, districtService, tfn.get(non_Jh).getFileName());
520523

521524
helper.makeCdrs(1, 0, 0, 0);
522525
File remoteCdrFile = helper.makeRemoteCdrFile();

testing/src/test/java/org/motechproject/nms/testing/it/imi/TargetFileServiceBundleIT.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.nio.file.Files;
5656
import java.security.NoSuchAlgorithmException;
5757
import java.util.ArrayList;
58+
import java.util.HashMap;
5859
import java.util.List;
5960

6061
import static org.junit.Assert.assertEquals;
@@ -70,6 +71,8 @@ public class TargetFileServiceBundleIT extends BasePaxIT {
7071

7172
private static final String LOCAL_OBD_DIR = "imi.local_obd_dir";
7273
private static final String REMOTE_OBD_DIR = "imi.remote_obd_dir";
74+
private static final String Jh = "JH";
75+
private static final String non_Jh = "NON-JH";
7376

7477

7578
private String localObdDirBackup;
@@ -175,9 +178,9 @@ public void createLargeFile() {
175178
));
176179
}
177180

178-
TargetFileNotification tfn = targetFileService.generateTargetFile();
179-
assertNotNull(tfn);
180-
getLogger().debug("Generated {}", tfn.getFileName());
181+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
182+
assertNotNull(tfn.get(non_Jh));
183+
getLogger().debug("Generated {}", tfn.values().toString());
181184
}
182185

183186
// To check that target file should contain correct weekID according to LMP of the subscriber.
@@ -198,10 +201,10 @@ public void verifyFT151() throws NoSuchAlgorithmException, IOException {
198201
List<String> contents = new ArrayList<>();
199202
String line;
200203

201-
TargetFileNotification tfn = targetFileService.generateTargetFile();
204+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
202205

203206
File targetDir = new File(settingsService.getSettingsFacade().getProperty("imi.local_obd_dir"));
204-
File targetFile = new File(targetDir, tfn.getFileName());
207+
File targetFile = new File(targetDir, tfn.get(non_Jh).getFileName());
205208
int recordCount = 0;
206209
boolean header = true;
207210
try (InputStream is = Files.newInputStream(targetFile.toPath());
@@ -218,8 +221,8 @@ public void verifyFT151() throws NoSuchAlgorithmException, IOException {
218221

219222
String checksum = ChecksumHelper.checksum(targetFile);
220223

221-
assertEquals((int)tfn.getRecordsCount(), recordCount);
222-
assertEquals(tfn.getChecksum(), checksum);
224+
assertEquals((int)tfn.get(non_Jh).getRecordsCount(), recordCount);
225+
assertEquals(tfn.get(non_Jh).getChecksum(), checksum);
223226
assertTrue("w6_1".equals(contents.get(0)));
224227
assertEquals(1, recordCount);
225228
}
@@ -242,10 +245,10 @@ public void verifyFT152() throws NoSuchAlgorithmException, IOException {
242245
List<String> contents = new ArrayList<>();
243246
String line;
244247

245-
TargetFileNotification tfn = targetFileService.generateTargetFile();
248+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
246249

247250
File targetDir = new File(settingsService.getSettingsFacade().getProperty("imi.local_obd_dir"));
248-
File targetFile = new File(targetDir, tfn.getFileName());
251+
File targetFile = new File(targetDir, tfn.get(non_Jh).getFileName());
249252
int recordCount = 0;
250253
boolean header = true;
251254
try (InputStream is = Files.newInputStream(targetFile.toPath());
@@ -261,8 +264,8 @@ public void verifyFT152() throws NoSuchAlgorithmException, IOException {
261264
}
262265

263266
String checksum = ChecksumHelper.checksum(targetFile);
264-
assertEquals((int)tfn.getRecordsCount(), recordCount);
265-
assertEquals(tfn.getChecksum(), checksum);
267+
assertEquals((int)tfn.get(non_Jh).getRecordsCount(), recordCount);
268+
assertEquals(tfn.get(non_Jh).getChecksum(), checksum);
266269
assertTrue("w5_1".equals(contents.get(0)));
267270

268271
//update the date of birth of the subscriber
@@ -272,9 +275,9 @@ public void verifyFT152() throws NoSuchAlgorithmException, IOException {
272275
subscriberService.updateStartDate(subscriber2);
273276

274277
// again generate the target file to check correct weekId is picked after DOB is changed.
275-
tfn = targetFileService.generateTargetFile();
278+
tfn = targetFileService.generateTargetFile(false);
276279
contents.clear();
277-
targetFile = new File(targetDir, tfn.getFileName());
280+
targetFile = new File(targetDir, tfn.get(non_Jh).getFileName());
278281
recordCount = 0;
279282
header = true;
280283
try (InputStream is = Files.newInputStream(targetFile.toPath());
@@ -289,8 +292,8 @@ public void verifyFT152() throws NoSuchAlgorithmException, IOException {
289292
}
290293
}
291294
checksum = ChecksumHelper.checksum(targetFile);
292-
assertEquals((int)tfn.getRecordsCount(), recordCount);
293-
assertEquals(tfn.getChecksum(), checksum);
295+
assertEquals((int)tfn.get(non_Jh).getRecordsCount(), recordCount);
296+
assertEquals(tfn.get(non_Jh).getChecksum(), checksum);
294297
assertTrue("w4_1".equals(contents.get(0)));
295298
assertEquals(1, recordCount);
296299
}
@@ -313,10 +316,10 @@ public void verifyFT190() throws NoSuchAlgorithmException, IOException {
313316
List<String> contents = new ArrayList<>();
314317
String line;
315318

316-
TargetFileNotification tfn = targetFileService.generateTargetFile();
319+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
317320

318321
File targetDir = new File(settingsService.getSettingsFacade().getProperty("imi.local_obd_dir"));
319-
File targetFile = new File(targetDir, tfn.getFileName());
322+
File targetFile = new File(targetDir, tfn.get(non_Jh).getFileName());
320323
int recordCount = 0;
321324
boolean header = true;
322325
try (InputStream is = Files.newInputStream(targetFile.toPath());
@@ -332,8 +335,8 @@ public void verifyFT190() throws NoSuchAlgorithmException, IOException {
332335
}
333336

334337
String checksum = ChecksumHelper.checksum(targetFile);
335-
assertEquals((int)tfn.getRecordsCount(), recordCount);
336-
assertEquals(tfn.getChecksum(), checksum);
338+
assertEquals((int)tfn.get(non_Jh).getRecordsCount(), recordCount);
339+
assertEquals(tfn.get(non_Jh).getChecksum(), checksum);
337340
assertTrue("w1_1.wav".equals(contents.get(0)));
338341
}
339342

@@ -355,10 +358,10 @@ public void verifyFT191() throws NoSuchAlgorithmException, IOException {
355358
List<String> contents = new ArrayList<>();
356359
String line;
357360

358-
TargetFileNotification tfn = targetFileService.generateTargetFile();
361+
HashMap<String,TargetFileNotification> tfn = targetFileService.generateTargetFile(false);
359362

360363
File targetDir = new File(settingsService.getSettingsFacade().getProperty("imi.local_obd_dir"));
361-
File targetFile = new File(targetDir, tfn.getFileName());
364+
File targetFile = new File(targetDir, tfn.get(non_Jh).getFileName());
362365
int recordCount = 0;
363366
boolean header = true;
364367
try (InputStream is = Files.newInputStream(targetFile.toPath());
@@ -374,8 +377,8 @@ public void verifyFT191() throws NoSuchAlgorithmException, IOException {
374377
}
375378

376379
String checksum = ChecksumHelper.checksum(targetFile);
377-
assertEquals((int)tfn.getRecordsCount(), recordCount);
378-
assertEquals(tfn.getChecksum(), checksum);
380+
assertEquals((int)tfn.get(non_Jh).getRecordsCount(), recordCount);
381+
assertEquals(tfn.get(non_Jh).getChecksum(), checksum);
379382
assertTrue("w1_1.wav".equals(contents.get(0)));
380383

381384
}
@@ -395,9 +398,9 @@ public void testChecksumsVaryWithFileContent() throws NoSuchAlgorithmException,
395398

396399
transactionManager.commit(status);
397400

398-
TargetFileNotification tfn1 = targetFileService.generateTargetFile();
399-
assertNotNull(tfn1);
400-
getLogger().debug(tfn1.toString());
401+
HashMap<String,TargetFileNotification> tfn1 = targetFileService.generateTargetFile(false);
402+
assertNotNull(tfn1.get(non_Jh));
403+
getLogger().debug(tfn1.get(non_Jh).toString());
401404

402405
// Sleep two seconds so the file names are different
403406
Thread.sleep(2000L);
@@ -413,11 +416,11 @@ public void testChecksumsVaryWithFileContent() throws NoSuchAlgorithmException,
413416

414417
transactionManager.commit(status);
415418

416-
TargetFileNotification tfn2 = targetFileService.generateTargetFile();
417-
assertNotNull(tfn2);
419+
HashMap<String,TargetFileNotification> tfn2 = targetFileService.generateTargetFile(false);
420+
assertNotNull(tfn2.get(non_Jh));
418421
getLogger().debug(tfn2.toString());
419422

420-
assertFalse(tfn1.getChecksum().equals(tfn2.getChecksum()));
423+
assertFalse(tfn1.get(non_Jh).getChecksum().equals(tfn2.get(non_Jh).getChecksum()));
421424
}
422425

423426
}

0 commit comments

Comments
 (0)