All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restcomm.connect.dao.mybatis.MybatisUsageDao Maven / Gradle / Ivy

There is a newer version: 8.4.0-227
Show newest version
/*
 * TeleStax, Open Source Cloud Communications
 * Copyright 2011-2014, Telestax Inc and individual contributors
 * by the @authors tag.
 *
 * This program is free software: you can redistribute it and/or modify
 * under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see 
 *
 */
package org.restcomm.connect.dao.mybatis;

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.restcomm.connect.dao.DaoUtils;
import org.restcomm.connect.dao.UsageDao;
import org.restcomm.connect.commons.dao.Sid;
import org.restcomm.connect.dao.entities.Usage;
import org.restcomm.connect.commons.annotations.concurrency.ThreadSafe;

import java.math.BigDecimal;
import java.net.URI;
import java.sql.Date;
import java.util.Currency;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author [email protected] (Alexandre Mendonca)
 */
@ThreadSafe
public final class MybatisUsageDao implements UsageDao {

  private static final String namespace = "org.mobicents.servlet.sip.restcomm.dao.UsageDao.";
  private final SqlSessionFactory sessions;

  public MybatisUsageDao(final SqlSessionFactory sessions) {
    super();
    this.sessions = sessions;
  }

  @Override
  public List getUsage(final Sid accountSid) {
    return getUsageCalls(accountSid, null, null, null, "getAllTimeCalls");
  }

  @Override
  public List getUsageDaily(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getDailyCalls");
  }

  @Override
  public List getUsageMonthly(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getMonthlyCalls");
  }

  @Override
  public List getUsageYearly(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getYearlyCalls");
  }

  @Override
  public List getUsageAllTime(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getAllTimeCalls");
  }

  /*
  @Override
  public List getUsageToday(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getTodayCalls");
  }

  @Override
  public List getUsageYesterday(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getYesterdayCalls");
  }

  @Override
  public List getUsageThisMonth(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getThisMonthCalls");
  }

  @Override
  public List getUsageLastMonth(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate) {
    return getUsageCalls(accountSid, category, startDate, endDate, "getLastMonthCalls");
  }
  */
  private List getUsageCalls(final Sid accountSid, Usage.Category category, DateTime startDate, DateTime endDate, final String queryName) {
    long startTime = System.currentTimeMillis();
    final SqlSession session = sessions.openSession();
    Map params = new HashMap();
    params.put("sid", accountSid.toString());
    params.put("startDate", new Date(startDate.getMillis()));
    params.put("endDate", new Date(endDate.getMillis()));
    fillParametersByCategory(category, params);
    try {
      final List> results = session.selectList(namespace + queryName, params);
      final List usageRecords = new ArrayList();
      if (results != null && !results.isEmpty()) {
        for (final Map result : results) {
          usageRecords.add(toUsageRecord(accountSid, result));
        }
      }
      return usageRecords;
    } finally {
      session.close();
    }
  }

  private Usage toUsageRecord(final Sid accountSid, final Map map) {
    final Usage.Category category = Usage.Category.CALLS;
    final String description = "Total Calls";
    final DateTime startDate = DateTimeFormat.forPattern("yyyyy-MM-dd").parseDateTime(map.get("start_date").toString());
    final DateTime endDate = DateTimeFormat.forPattern("yyyyy-MM-dd").parseDateTime(map.get("end_date").toString());

    final Long usage = DaoUtils.readLong(map.get("usage"));
    final String usageUnit = "minutes";

    final Long count = DaoUtils.readLong(map.get("count"));
    final String countUnit = "calls";

    /* FIXME: readBigDecimal should take Double instead of String ? */
    final BigDecimal price = DaoUtils.readBigDecimal(map.get("price").toString());
    final Currency priceUnit = Currency.getInstance(Locale.US);

    final URI uri = DaoUtils.readUri(map.get("uri"));

    return new Usage(category, description, accountSid, startDate, endDate, usage, usageUnit, count, countUnit, price, priceUnit, uri);
  }

  private Map fillParametersByCategory(Usage.Category category, Map params) {
    // FIXME: handle no category, meaning all
    if (category == null) category = Usage.Category.CALLS;
    switch (category) {
      case CALLS:
      case CALLS_INBOUND:
      case CALLS_INBOUND_LOCAL:
      case CALLS_INBOUND_TOLLFREE:
      case CALLS_OUTBOUND:
      case CALLS_CLIENT:
      case CALLS_SIP:
        params.put("tableName", "restcomm_call_detail_records");
        params.put("usageExprPre", "COALESCE(SUM(");
        params.put("usageExprCol", "duration");
        params.put("usageExprSuf", "),0)");
        break;
      case SMS:
      case SMS_INBOUND:
      case SMS_INBOUND_SHORTCODE:
      case SMS_INBOUND_LONGCODE:
      case SMS_OUTBOUND:
      case SMS_OUTBOUND_SHORTCODE:
      case SMS_OUTBOUND_LONGCODE:
        params.put("tableName", "restcomm_sms_messages");
        params.put("usageExprPre", "COUNT(");
        params.put("usageExprCol", "sid");
        params.put("usageExprSuf", ")");
        break;
      case PHONENUMBERS:
      case PHONENUMBERS_TOLLFREE:
      case PHONENUMBERS_LOCAL:
      case SHORTCODES:
      case SHORTCODES_VANITY:
      case SHORTCODES_RANDOM:
      case SHORTCODES_CUSTOMEROWNED:
      case CALLERIDLOOKUPS:
      case RECORDINGS:
      case TRANSCRIPTIONS:
      case RECORDINGSTORAGE:
      case TOTALPRICE:
      default:
        params.put("tableName", "restcomm_call_detail_records");
        params.put("usageExprPre", "COALESCE(SUM(");
        params.put("usageExprCol", "duration");
        params.put("usageExprSuf", "),0)");
        break;
    }
    return params;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy