<commit_before># -*- coding: utf-8 -*-

#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2012 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#
from django.conf import settings
from django.core.management.base import BaseCommand
from random import choice
from uuid import uuid1
from pymongo.connection import Connection
from pymongo.errors import ConnectionFailure
import sys
import random
import datetime
import time

random.seed()

HANGUP_CAUSE = ['NORMAL_CLEARING', 'NORMAL_CLEARING', 'NORMAL_CLEARING',
                'NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER', 'CALL_REJECTED',
                'INVALID_NUMBER_FORMAT']
HANGUP_CAUSE_Q850 = ['16', '17', '18', '19', '20', '21']

#list of exit code : http://www.howtocallabroad.com/codes.html
COUNTRY_PREFIX = ['0034', '011346', '+3465',  # Spain
                  '3912', '39',  '+3928',  # Italy
                  '15', '17',  # US
                  '16', '1640',  # Canada
                  '44', '441', '00442',  # UK
                  '45', '451', '00452',  # Denmark
                  '32', '321', '0322',  # Belgium
                  '91', '919', '0911',  # India
                  '53', '531', '00532',  # Cuba
                  '55', '551', '552',  # Brazil
                  ]


def NumberLong(var):
    return var


def generate_cdr_data(day_delta_int):
    """
    TODO:Add function documentation
    """

    digit = '1234567890'

    delta_days = random.randint(0, day_delta_int)
    delta_minutes = random.randint(1, 1440)
    answer_stamp = datetime.datetime.now() \
        - datetime.timedelta(minutes=delta_minutes) \
        - datetime.timedelta(days=delta_days)

    # convert answer_stamp into milliseconds
    start_uepoch = int(time.mktime(answer_stamp.timetuple()))

    caller_id = ''.join([choice(digit) for i in range(8)])
    channel_name = 'sofia/internal/' + caller_id + '@127.0.0.1'
    destination_number = ''.join([choice(digit) for i in range(8)])

    destination_number = choice(COUNTRY_PREFIX) + destination_number

    hangup_cause = choice(HANGUP_CAUSE)
    hangup_cause_q850 = choice(HANGUP_CAUSE_Q850)
    if hangup_cause == 'NORMAL_CLEARING':
        duration = random.randint(1, 200)
        billsec = random.randint(1, 200)
    else:
        duration = 0
        billsec = 0

    end_stamp = str(datetime.datetime.now()
                    - datetime.timedelta(minutes=delta_minutes)
                    - datetime.timedelta(days=delta_days)
                    + datetime.timedelta(seconds=duration))
    uuid = str(uuid1())

    return (
        answer_stamp,
        start_uepoch,
        caller_id,
        channel_name,
        destination_number,
        hangup_cause,
        hangup_cause_q850,
        duration,
        billsec,
        end_stamp,
        uuid,
        )


class Command(BaseCommand):

    # Usage : generate_cdr 500
    args = ' no_of_record, delta_day '
    help = '''Generate random CDRs
---------------------------------
''' \
        + 'python manage.py generate_cdr <NUMBER_OF_CDR> <DELTA_DAYS>'

    def handle(self, *args, **options):
        """
        Note that subscriber created this way are only for devel purposes
        """

        if not args or len(args) != 2:
            print self.help
            # print >> sys.stderr
            raise SystemExit

        no_of_record = args[0]
        day_delta = args[1]
        try:
            day_delta_int = int(day_delta)
        except ValueError:
            day_delta_int = 30

        # Retrieve the field collection in the mongo_import list
        ipaddress = settings.MG_IMPORT.items()[0][0]

        # Connect on MongoDB Database
        host = settings.MG_IMPORT[ipaddress]['host']
        port = settings.MG_IMPORT[ipaddress]['port']
        db_name = settings.MG_IMPORT[ipaddress]['db_name']
        try:
            connection = Connection(host, port)
            DBCON = connection[db_name]
        except ConnectionFailure, e:
            sys.stderr.write('Could not connect to MongoDB: %s - %s' % (e,
                             ipaddress))
            sys.exit(1)

        for i in range(1, int(no_of_record) + 1):

            (
                answer_stamp,
                start_uepoch,
                caller_id,
                channel_name,
                destination_number,
                hangup_cause,
                hangup_cause_q850,
                duration,
                billsec,
                end_stamp,
                uuid,
                ) = generate_cdr_data(day_delta_int)

            if i % 100 == 0:
                print '%d CDRs created...' % i
<commit_msg>add option to set duration on call generator<commit_after># -*- coding: utf-8 -*-

#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2012 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#
from django.conf import settings
from django.core.management.base import BaseCommand
from random import choice
from uuid import uuid1
from pymongo.connection import Connection
from pymongo.errors import ConnectionFailure
import sys
import random
import datetime
import time

random.seed()

HANGUP_CAUSE = ['NORMAL_CLEARING', 'NORMAL_CLEARING', 'NORMAL_CLEARING',
                'NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER', 'CALL_REJECTED',
                'INVALID_NUMBER_FORMAT']
HANGUP_CAUSE_Q850 = ['16', '17', '18', '19', '20', '21']

#list of exit code : http://www.howtocallabroad.com/codes.html
COUNTRY_PREFIX = ['0034', '011346', '+3465',  # Spain
                  '3912', '39',  '+3928',  # Italy
                  '15', '17',  # US
                  '16', '1640',  # Canada
                  '44', '441', '00442',  # UK
                  '45', '451', '00452',  # Denmark
                  '32', '321', '0322',  # Belgium
                  '91', '919', '0911',  # India
                  '53', '531', '00532',  # Cuba
                  '55', '551', '552',  # Brazil
                  ]


def NumberLong(var):
    return var


def generate_cdr_data(day_delta_int):
    """
    TODO:Add function documentation
    """
    digit = '1234567890'

    delta_days = random.randint(0, day_delta_int)
    delta_minutes = random.randint(1, 1440)
    answer_stamp = datetime.datetime.now() \
        - datetime.timedelta(minutes=delta_minutes) \
        - datetime.timedelta(days=delta_days)

    # convert answer_stamp into milliseconds
    start_uepoch = int(time.mktime(answer_stamp.timetuple()))

    caller_id = ''.join([choice(digit) for i in range(8)])
    channel_name = 'sofia/internal/' + caller_id + '@127.0.0.1'
    destination_number = ''.join([choice(digit) for i in range(8)])

    destination_number = choice(COUNTRY_PREFIX) + destination_number

    hangup_cause = choice(HANGUP_CAUSE)
    hangup_cause_q850 = choice(HANGUP_CAUSE_Q850)
    if hangup_cause == 'NORMAL_CLEARING':
        duration = random.randint(1, 200)
        billsec = random.randint(1, 200)
    else:
        duration = 0
        billsec = 0

    end_stamp = str(datetime.datetime.now()
                    - datetime.timedelta(minutes=delta_minutes)
                    - datetime.timedelta(days=delta_days)
                    + datetime.timedelta(seconds=duration))
    uuid = str(uuid1())

    return (
        answer_stamp,
        start_uepoch,
        caller_id,
        channel_name,
        destination_number,
        hangup_cause,
        hangup_cause_q850,
        duration,
        billsec,
        end_stamp,
        uuid,
        )


class Command(BaseCommand):

    # Usage : generate_cdr 500
    args = ' no_of_record, delta_day '
    help = '''Generate random CDRs
---------------------------------
python manage.py generate_cdr <NUMBER_OF_CDR> <DELTA_DAYS> [DURATION]
'''

    def handle(self, *args, **options):
        """
        Note that subscriber created this way are only for devel purposes
        """
        if not args or len(args) < 2:
            print self.help
            # print >> sys.stderr
            raise SystemExit

        no_of_record = args[0]
        day_delta = args[1]
        try:
            day_delta_int = int(day_delta)
        except ValueError:
            day_delta_int = 30
        try:
            arg_duration = args[2]
            arg_duration = int(arg_duration)
        except ValueError:
            arg_duration = 0
        except IndexError:
            arg_duration = False

        # Retrieve the field collection in the mongo_import list
        ipaddress = settings.MG_IMPORT.items()[0][0]

        # Connect on MongoDB Database
        host = settings.MG_IMPORT[ipaddress]['host']
        port = settings.MG_IMPORT[ipaddress]['port']
        db_name = settings.MG_IMPORT[ipaddress]['db_name']
        try:
            connection = Connection(host, port)
            DBCON = connection[db_name]
        except ConnectionFailure, e:
            sys.stderr.write('Could not connect to MongoDB: %s - %s' % (e,
                             ipaddress))
            sys.exit(1)

        for i in range(1, int(no_of_record) + 1):

            (
                answer_stamp,
                start_uepoch,
                caller_id,
                channel_name,
                destination_number,
                hangup_cause,
                hangup_cause_q850,
                duration,
                billsec,
                end_stamp,
                uuid,
                ) = generate_cdr_data(day_delta_int)

            if type(arg_duration) == int:
                duration = arg_duration

            if i % 100 == 0:
                print '%d CDRs created...' % i

{'fraction_non_alphanumeric': 0.1145760685463784, 'fraction_numerical': 0.03686360466274783, 'mean_word_length': 1.9636846767050486, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6195536', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3033, 'n_words': 730}