Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup SMPP Message ID correlation for messages waiting for DLR #2965

Merged
merged 16 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,19 @@
AND date_created >= #{startTime}
</if>
<if test="endTime != null">
AND date_created &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
AND date_created &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
</if>
<if test="smppMessageId != null">
AND "smpp_message_id"=#{smppMessageId}
</if>
order by date_created
LIMIT #{limit} OFFSET #{offset}
</select>

<select id="findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc" parameterType="map" resultType="hashmap">
SELECT * FROM "restcomm_sms_messages" AS "restcomm_sms_messages"
WHERE "smpp_message_id" = #{smppMessageId}
AND "date_created" &gt;= #{startDate}
ORDER BY "date_created" DESC
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,19 @@
AND "date_created" &gt;= #{startTime}
</if>
<if test="endTime != null">
AND "date_created" &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
AND "date_created" &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
</if>
<if test="smppMessageId != null">
AND "smpp_message_id"=#{smppMessageId}
</if>
order by "date_created"
LIMIT #{limit} OFFSET #{offset}
</select>
<select id="findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc" parameterType="map" resultType="hashmap">
SELECT * FROM "restcomm_sms_messages" AS "restcomm_sms_messages"
WHERE "smpp_message_id" = #{smppMessageId}
AND "date_created" &gt;= #{startDate}
ORDER BY "date_created" DESC
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.text.ParseException;
import java.util.List;

import org.joda.time.DateTime;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.entities.SmsMessage;
import org.restcomm.connect.dao.entities.SmsMessageFilter;
Expand Down Expand Up @@ -53,4 +54,6 @@ public interface SmsMessagesDao {
// Support for filtering of message list result, Issue 1395
Integer getTotalSmsMessage(SmsMessageFilter filter);
List<SmsMessage> getSmsMessages(SmsMessageFilter filter);

List<SmsMessage> findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc(String smppMessageId, DateTime startDate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public class SmsMessageFilter {
private final String instanceid;

public SmsMessageFilter(String accountSid, List<String> accountSidSet, String recipient, String sender, String startTime, String endTime,
String body, Integer limit, Integer offset) throws ParseException {
String body, Integer limit, Integer offset) throws ParseException {
this(accountSid, accountSidSet, recipient,sender,startTime,endTime, body, limit,offset,null);
}

public SmsMessageFilter(String accountSid, List<String> accountSidSet, String recipient, String sender, String startTime, String endTime,
String body, Integer limit, Integer offset, String instanceId) throws ParseException {
String body, Integer limit, Integer offset, String instanceId) throws ParseException {
this.accountSid = accountSid;
this.accountSidSet = accountSidSet;

Expand Down Expand Up @@ -124,4 +124,4 @@ public int getOffset() {
}

public String getInstanceid() { return instanceid; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.restcomm.connect.commons.annotations.concurrency.ThreadSafe;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.SmsMessagesDao;
Expand All @@ -38,17 +40,7 @@
import java.util.List;
import java.util.Map;

import static org.restcomm.connect.dao.DaoUtils.readBigDecimal;
import static org.restcomm.connect.dao.DaoUtils.readCurrency;
import static org.restcomm.connect.dao.DaoUtils.readDateTime;
import static org.restcomm.connect.dao.DaoUtils.readSid;
import static org.restcomm.connect.dao.DaoUtils.readString;
import static org.restcomm.connect.dao.DaoUtils.readUri;
import static org.restcomm.connect.dao.DaoUtils.writeBigDecimal;
import static org.restcomm.connect.dao.DaoUtils.writeCurrency;
import static org.restcomm.connect.dao.DaoUtils.writeDateTime;
import static org.restcomm.connect.dao.DaoUtils.writeSid;
import static org.restcomm.connect.dao.DaoUtils.writeUri;
import static org.restcomm.connect.dao.DaoUtils.*;

/**
* @author quintana.thomas@gmail.com (Thomas Quintana)
Expand Down Expand Up @@ -205,6 +197,28 @@ public int getSmsMessagesPerAccountLastPerMinute(String accountSid) throws Parse
}
}

@Override
public List<SmsMessage> findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc(String smppMessageId, DateTime startDate) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
final Map<String, Object> parameters = new HashMap<>(2);
parameters.put("smppMessageId", smppMessageId);
parameters.put("startDate", formatter.print(startDate.withTime(0, 0, 0, 0)));

final SqlSession session = this.sessions.openSession();

try {
final List<Map<String, Object>> results = session.selectList(namespace + "findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc", parameters);
final List<SmsMessage> messages = new ArrayList<>(results.size());

for (Map<String, Object> result: results) {
messages.add(toSmsMessage(result));
}
return messages;
} finally {
session.close();
}
}

private Map<String, Object> toMap(final SmsMessage smsMessage) {
final Map<String, Object> map = new HashMap<String, Object>();
map.put("sid", writeSid(smsMessage.getSid()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<mapper namespace="org.mobicents.servlet.sip.restcomm.dao.SmsMessagesDao">
<insert id="addSmsMessage" parameterType="map">
INSERT INTO "restcomm_sms_messages" ("sid", "date_created", "date_updated", "date_sent", "account_sid", "sender", "recipient", "body", "status", "direction", "price",
"api_version", "uri") VALUES (#{sid}, #{date_created}, #{date_updated}, #{date_sent}, #{account_sid}, #{sender}, #{recipient}, #{body},
#{status}, #{direction}, #{price}, #{api_version}, #{uri});
"api_version", "uri", "smpp_message_id") VALUES (#{sid}, #{date_created}, #{date_updated}, #{date_sent}, #{account_sid}, #{sender}, #{recipient}, #{body},
#{status}, #{direction}, #{price}, #{api_version}, #{uri}, #{smpp_message_id});
</insert>

<select id="getSmsMessage" parameterType="string" resultType="hashmap">
Expand Down Expand Up @@ -117,10 +117,21 @@
AND "date_created" &gt;= #{startTime}
</if>
<if test="endTime != null">
AND "date_created" &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
AND "date_created" &lt;= DATE_ADD(#{endTime},INTERVAL 1 DAY)
</if>
<if test="smppMessageId != null">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to make this change for sms-messages.xml files in restcomm.application module as they are the ones that are packaged and used on production

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, agree

AND "smpp_message_id"=#{smppMessageId}
</if>

order by "date_created"
LIMIT #{limit} OFFSET #{offset}
</select>

<select id="findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc" parameterType="map" resultType="hashmap">
SELECT * FROM "restcomm_sms_messages" AS "restcomm_sms_messages"
WHERE "smpp_message_id" = #{smppMessageId}
AND "date_created" &gt;= #{startDate}
ORDER BY "date_created" DESC
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@
*/
package org.restcomm.connect.dao.mybatis;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.text.ParseException;
import java.util.Currency;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
Expand All @@ -39,6 +30,16 @@
import org.restcomm.connect.dao.SmsMessagesDao;
import org.restcomm.connect.dao.entities.SmsMessage;

import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.text.ParseException;
import java.util.Currency;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author quintana.thomas@gmail.com (Thomas Quintana)
*/
Expand Down Expand Up @@ -120,24 +121,28 @@ private SmsMessage createSms() {
return createSms(Sid.generate(Sid.Type.ACCOUNT), SmsMessage.Direction.OUTBOUND_API, 0, DateTime.now());
}

private SmsMessage createSms(Sid account, SmsMessage.Direction direction, int i, DateTime date) {
private SmsMessage createSms(Sid account, SmsMessage.Direction direction, int index, DateTime date) {
return createSms(account, direction, index, date, null);
}

private SmsMessage createSms(Sid account, SmsMessage.Direction direction, int index, DateTime date, String smppMessageId) {
final Sid sid = Sid.generate(Sid.Type.SMS_MESSAGE);
final URI url = URI.create("2012-04-24/Accounts/Acoount/SMS/Messages/unique-id.json");
final SmsMessage.Builder builder = SmsMessage.builder();
builder.setSid(sid);
builder.setAccountSid(account);
builder.setApiVersion("2012-04-24");
builder.setRecipient("+12223334444");
builder.setSender("+17778889999");
builder.setBody("Hello World - "+i);
builder.setStatus(SmsMessage.Status.SENDING);
builder.setDirection(direction);
builder.setPrice(new BigDecimal("0.00"));
builder.setPriceUnit(Currency.getInstance("USD"));
builder.setUri(url);
builder.setDateCreated(date);
SmsMessage message = builder.build();
return message;
return SmsMessage.builder()
.setSid(sid)
.setAccountSid(account)
.setApiVersion("2012-04-24")
.setRecipient("+12223334444")
.setSender("+17778889999")
.setBody("Hello World - "+index)
.setStatus(SmsMessage.Status.SENDING)
.setDirection(direction)
.setPrice(new BigDecimal("0.00"))
.setPriceUnit(Currency.getInstance("USD"))
.setUri(url)
.setDateCreated(date)
.setSmppMessageId(smppMessageId)
.build();
}

@Test
Expand Down Expand Up @@ -234,4 +239,47 @@ public void testUpdateSmsMessageDateSentAndStatusAndGetBySmppMsgId(){
assertEquals(smsMessage.getDateSent(), resultantSmsMessage.getDateSent());
assertEquals(smsMessage.getStatus(), resultantSmsMessage.getStatus());
}

@Test
public void testFindBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc() {
// given
final SmsMessagesDao smsMessagesDao = manager.getSmsMessagesDao();
final Sid accountSid = Sid.generate(Sid.Type.ACCOUNT);
final String smppMessageId = "12345";

final DateTime fourDaysAgo = DateTime.now().minusDays(4);
final SmsMessage smsMessage1 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 0, fourDaysAgo, smppMessageId);
final SmsMessage smsMessage2 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 1, fourDaysAgo, smppMessageId);

final DateTime threeDaysAgo = DateTime.now().minusDays(3);
final SmsMessage smsMessage3 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 2, threeDaysAgo, smppMessageId);
final SmsMessage smsMessage4 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 3, threeDaysAgo, null);

final DateTime yesterday = DateTime.now().minusDays(1);
final SmsMessage smsMessage5 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 4, yesterday, smppMessageId);

final DateTime today = DateTime.now();
final SmsMessage smsMessage6 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 5, today, smppMessageId);
final SmsMessage smsMessage7 = createSms(accountSid, SmsMessage.Direction.OUTBOUND_API, 6, today, null);

// when
smsMessagesDao.addSmsMessage(smsMessage1);
smsMessagesDao.addSmsMessage(smsMessage2);
smsMessagesDao.addSmsMessage(smsMessage3);
smsMessagesDao.addSmsMessage(smsMessage4);
smsMessagesDao.addSmsMessage(smsMessage5);
smsMessagesDao.addSmsMessage(smsMessage6);
smsMessagesDao.addSmsMessage(smsMessage7);

final List<SmsMessage> messages = smsMessagesDao.findBySmppMessageIdAndDateCreatedGreaterOrEqualThanOrderedByDateCreatedDesc(smppMessageId, threeDaysAgo);

try {
assertEquals(3, messages.size());
assertEquals(smsMessage6.getSid(), messages.get(0).getSid());
assertEquals(smsMessage5.getSid(), messages.get(1).getSid());
assertEquals(smsMessage3.getSid(), messages.get(2).getSid());
} finally {
smsMessagesDao.removeSmsMessages(accountSid);
}
}
}