Skip to content

Commit

Permalink
Merge branch 'release/v3.0.0-Beta3'
Browse files Browse the repository at this point in the history
  • Loading branch information
areski committed Sep 2, 2015
2 parents 1a98ccd + e8addf6 commit b23b12c
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 59 deletions.
4 changes: 2 additions & 2 deletions addons/sql/pg_queries.sql
Expand Up @@ -214,7 +214,7 @@ REFRESH MATERIALIZED VIEW matv_voip_cdr_aggr_hour;

-- Create index on Materialized view
CREATE UNIQUE INDEX matv_voip_cdr_aggr_hour_date
ON matv_voip_cdr_aggr_hour (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id);
ON matv_voip_cdr_aggr_hour (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id, user_id);

# Refresh without lock
REFRESH MATERIALIZED VIEW CONCURRENTLY matv_voip_cdr_aggr_hour;
Expand Down Expand Up @@ -243,7 +243,7 @@ CREATE MATERIALIZED VIEW matv_voip_cdr_aggr_min AS

-- Create index on Materialized view
CREATE UNIQUE INDEX matv_voip_cdr_aggr_min_date
ON matv_voip_cdr_aggr_min (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id);
ON matv_voip_cdr_aggr_min (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id, user_id);

-- Refresh without lock
REFRESH MATERIALIZED VIEW CONCURRENTLY matv_voip_cdr_aggr_min;
Expand Down
8 changes: 5 additions & 3 deletions cdr_stats/cdr/admin.py
Expand Up @@ -25,8 +25,10 @@ class HangupCauseAdmin(admin.ModelAdmin):

# CDR
class CDRAdmin(admin.ModelAdmin):
list_display = ('id', 'user', 'starting_date', 'switch', 'destination_number', 'dialcode', 'caller_id_number',
'duration', 'hangup_cause', 'direction', 'country', 'sell_cost',)
search_fields = ('destination_number', 'caller_id_number',)
list_display = ('id', 'user', 'starting_date', 'switch',
'destination_number', 'dialcode', 'caller_id_number',
'accountcode', 'duration', 'hangup_cause', 'direction',
'country', 'sell_cost',)
search_fields = ('destination_number', 'caller_id_number', 'accountcode')

admin.site.register(CDR, CDRAdmin)
2 changes: 1 addition & 1 deletion cdr_stats/cdr/forms.py
Expand Up @@ -183,11 +183,11 @@ def __init__(self, *args, **kwargs):
),
Div(
Div('country_id', css_class=css_class),
Div('records_per_page', css_class=css_class),
css_class='row'
),
)
common_submit_buttons(self.helper.layout, 'search')
self.fields['records_per_page'].widget.attrs['class'] = 'select form-control'


class CountryReportForm(CdrSearchForm):
Expand Down
4 changes: 4 additions & 0 deletions cdr_stats/cdr/import_helper/asterisk_fetcher.py
Expand Up @@ -102,12 +102,16 @@ def push_asterisk_cdr(shell, table_name, db_engine, cursor, cursor_updated, swit
direction = "unknown"

try:
# TODO: Code not valide as accountcode moved to his own model AccountCode
# FYI, asterisk_fetcher.py is not used
voipplan_id = UserProfile.objects.get(accountcode=accountcode).voipplan_id
except:
voipplan_id = False
print_shell(shell, "No VoipPlan created for this user/accountcode")

try:
# TODO: Code not valide as accountcode moved to his own model AccountCode
# FYI, asterisk_fetcher.py is not used
user = UserProfile.objects.get(accountcode=accountcode).user
except:
# Cannot assign accountcode to an existing user
Expand Down
4 changes: 2 additions & 2 deletions cdr_stats/cdr/sql/cdr.sql
Expand Up @@ -20,7 +20,7 @@ CREATE MATERIALIZED VIEW matv_voip_cdr_aggr_hour AS

-- Create index on Materialized view
CREATE UNIQUE INDEX matv_voip_cdr_aggr_hour_date
ON matv_voip_cdr_aggr_hour (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id);
ON matv_voip_cdr_aggr_hour (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id, user_id);

-- Materialized View
CREATE MATERIALIZED VIEW matv_voip_cdr_aggr_min AS
Expand All @@ -43,4 +43,4 @@ CREATE MATERIALIZED VIEW matv_voip_cdr_aggr_min AS

-- Create index on Materialized view
CREATE UNIQUE INDEX matv_voip_cdr_aggr_min_date
ON matv_voip_cdr_aggr_min (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id);
ON matv_voip_cdr_aggr_min (starting_date, country_id, switch_id, cdr_source_type, hangup_cause_id, user_id);
8 changes: 2 additions & 6 deletions cdr_stats/cdr/templates/cdr/list.html
Expand Up @@ -197,7 +197,7 @@ <h1>{% trans "CDR records" %}<small></small></h1>
{% endcache %}
</td>
{% if user.is_superuser %}
<td>{{ row.user_id }}</td>
<td>{{ row.accountcode }}</td>
<td>{{ row.buy_rate|floatformat:2 }}</td>
<td>{{ row.buy_cost|floatformat:2 }}</td>
{% endif %}
Expand Down Expand Up @@ -245,11 +245,6 @@ <h1>{% trans "CDR records" %}<small></small></h1>
</div>
<div class="row">
<div class="col-md-11 text-left">{% trans "total calls"|title %} : {{ cdr_count }}</div>
<div class="col-md-1 text-right">
<div class="form-group">
<div class="controls">{{ form.records_per_page }}</div>
</div>
</div>
</div>
</form><!-- complete search form tag-->
{% if page_cdr_list %}
Expand Down Expand Up @@ -335,6 +330,7 @@ <h1>{% trans "CDR records" %}<small></small></h1>
$("#" + modal_id).load(url);
});
</script>

<!-- modal content-->
<div id="cdr-detail" class="modal fade" role="dialog" aria-labelledby="cdr_detail"></div>
{% endblock %}
10 changes: 6 additions & 4 deletions cdr_stats/cdr/views.py
Expand Up @@ -21,7 +21,7 @@
from django_lets_go.common_functions import int_convert_to_minute,\
percentage, getvar, unset_session_var, ceil_strdate
from django_lets_go.common_functions import get_pagination_vars
from user_profile.models import UserProfile
from user_profile.models import UserProfile, AccountCode
from cdr.functions_def import get_country_name, get_hangupcause_name,\
get_switch_ip_addr, calculate_act_acd
from cdr.forms import CdrSearchForm, CountryReportForm, CdrOverviewForm, \
Expand Down Expand Up @@ -217,9 +217,11 @@ def cdr_view(request):

if request.user.is_superuser and accountcode:
try:
user_prof = UserProfile.objects.get(accountcode=accountcode)
kwargs['user_id'] = user_prof.user.id
except UserProfile.DoesNotExist:
acc = AccountCode.objects.get(accountcode=accountcode)
kwargs['user_id'] = acc.user.id
# on specific accountcode filter let only display that one
kwargs['accountcode'] = accountcode
except AccountCode.DoesNotExist:
# cannot find a user for this accountcode
pass

Expand Down
42 changes: 21 additions & 21 deletions cdr_stats/cdr_alert/management/commands/send_daily_report.py
Expand Up @@ -16,7 +16,6 @@

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Context
Expand All @@ -29,10 +28,7 @@


class Command(BaseCommand):
help = "Send daily report\n"\
"---------------------------------\n"\
"python manage.py send_daily_report\n"\

help = "Send daily report, make sure to have an admin user with email and multiple_email.\n"

def handle(self, *args, **options):
"""to send daily mail report via command line"""
Expand All @@ -42,34 +38,38 @@ def handle(self, *args, **options):
if not c_user.email:
print "User (%s) -> This user doesn't have an email." % c_user.username
continue
from_email = c_user.email
else:
print "Send Report from User (%s - %s)." % (c_user.username, c_user.email)

try:
to = UserProfile.objects.get(user=c_user).multiple_email
to_email = UserProfile.objects.get(user=c_user).multiple_email
except UserProfile.DoesNotExist:
print 'Error: UserProfile not found (user_id:' + str(c_user.id) + ')'
continue

mail_data = get_cdr_mail_report(c_user)
if not to_email:
print 'Error: UserProfile multiple_email not set (user_id:' + str(c_user.id) + ')'
continue

subject = _('CDR Report')
from_email = c_user.email
mail_data = get_cdr_mail_report(c_user)
subject = 'CDR Report'

html_content = get_template('cdr/mail_report_template.html')\
.render(Context({
'yesterday_date': mail_data['yesterday_date'],
'rows': mail_data['rows'],
'total_duration': mail_data['total_duration'],
'total_calls': mail_data['total_calls'],
'ACH': mail_data['ACH'],
'ACD': mail_data['ACD'],
'country_analytic_array': mail_data['country_analytic_array'],
'hangup_analytic_array': mail_data['hangup_analytic_array']
'total_buy_cost': mail_data['total_buy_cost'],
'total_sell_cost': mail_data['total_sell_cost'],
'metric_aggr': mail_data['metric_aggr'],
'country_data': mail_data['country_data'],
'hangup_cause_data': mail_data['hangup_cause_data']
}))

if to:
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
print '\nEmail sent to ' + str(to)
print '-' * 80
msg.content_subtype = 'html'
msg.send()
else:
print "Error: email not sent"
msg = EmailMultiAlternatives(subject, html_content, from_email, [to_email])
print '\nEmail sent to ' + str(to_email)
print '-' * 80
msg.content_subtype = 'html'
msg.send()
42 changes: 26 additions & 16 deletions cdr_stats/cdr_alert/tasks.py
Expand Up @@ -404,7 +404,6 @@ def blacklist_whitelist_notification(notice_type):


# Email previous day's CDR Report

class send_cdr_report(PeriodicTask):

"""A periodic task to send previous day's CDR Report as mail
Expand All @@ -421,33 +420,44 @@ def run(self, **kwargs):
logger = self.get_logger()
logger.info('TASK :: send_cdr_report')

for c_user in User.objects.filter(is_staff=True, is_active=True):
from_email = c_user.email
list_users = User.objects.filter(is_staff=True, is_active=True)
for c_user in list_users:

if not c_user.email:
logger.error("User (%s) -> This user doesn't have an email." % c_user.username)
continue
else:
logger.error("Send Report from User (%s - %s)." % (c_user.username, c_user.email))

try:
to = UserProfile.objects.get(user=c_user).multiple_email
to_email = UserProfile.objects.get(user=c_user).multiple_email
except UserProfile.DoesNotExist:
to = ''
logger.error('Error : UserProfile notfound (user_id:%d)' % c_user.id)
continue

mail_data = get_cdr_mail_report(c_user)
if not to_email:
logger.error('Error: UserProfile multiple_email not set (user_id:' + str(c_user.id) + ')')
continue

subject = _('CDR Report')
from_email = c_user.email
mail_data = get_cdr_mail_report(c_user)
subject = 'CDR Report'

html_content = get_template('cdr/mail_report_template.html')\
.render(Context({
'yesterday_date': mail_data['yesterday_date'],
'rows': mail_data['rows'],
'total_duration': mail_data['total_duration'],
'total_calls': mail_data['total_calls'],
'ACH': mail_data['metric_aggr']['ACH'],
'ACD': mail_data['metric_aggr']['ACD'],
'country_analytic_array': mail_data['country_analytic_array'],
'hangup_analytic_array': mail_data['hangup_analytic_array']
}
))

msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
logger.info('Email sent to %s' % to)
'total_buy_cost': mail_data['total_buy_cost'],
'total_sell_cost': mail_data['total_sell_cost'],
'metric_aggr': mail_data['metric_aggr'],
'country_data': mail_data['country_data'],
'hangup_cause_data': mail_data['hangup_cause_data']
}))

msg = EmailMultiAlternatives(subject, html_content, from_email, [to_email])
logger.info('Email sent to %s' % str(to_email))
msg.content_subtype = 'html'
msg.send()

Expand Down
2 changes: 1 addition & 1 deletion cdr_stats/cdr_stats/__init__.py
Expand Up @@ -16,7 +16,7 @@

# :copyright: (c) 2011-2015 by Arezqui Belaid.
# :license: MPL 2.0, see COPYING for more details.
VERSION = '3.0.0-Beta2'
VERSION = '3.0.0-Beta3'

__version__ = VERSION
__author__ = "Arezqui Belaid"
Expand Down
4 changes: 2 additions & 2 deletions cdr_stats/user_profile/models.py
Expand Up @@ -107,8 +107,8 @@ class UserProfile(models.Model):
note = models.CharField(max_length=250, blank=True, null=True,
verbose_name=_('note'))
multiple_email = models.TextField(blank=True, null=True,
verbose_name=_('report mail list'),
help_text=_('enter a valid e-mail address separated by commas.'))
verbose_name=_('report email list'),
help_text=_('enter valid e-mail addresses separated by commas.'))

created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
Expand Down
3 changes: 3 additions & 0 deletions docs/source/installation/configure-asterisk.rst
Expand Up @@ -143,6 +143,9 @@ edit '/etc/cdr-pusher.yaml'::
- orig_field: "datetime(calldate)"
dest_field: starting_date
type_field: date
- orig_field: accountcode
dest_field: accountcode
type_field: string
- orig_field: channel
dest_field: extradata
type_field: jsonb
Expand Down
2 changes: 1 addition & 1 deletion docs/source/installation/install-cdr-pusher.rst
Expand Up @@ -33,7 +33,7 @@ To install and run the cdr-pusher application, follow these steps::

$ mkdir /opt/app
$ cd /opt/app
$ git clone https://github.com/cdr-stats/cdr-stats.git
$ git clone https://github.com/cdr-stats/cdr-pusher.git
$ cd cdr-pusher
$ export GOPATH=`pwd`
$ make build
Expand Down

0 comments on commit b23b12c

Please sign in to comment.