def accounts(): session.forget(response) client = db.client(request.args(0)) if not accessible_client(client.id): redirect(URL('user', 'not_autorized')) r = accurate.call("ApiV1.SimpleAccountGet", dict(Tenant = client.unique_code, Account = client.unique_code)) return __err_or_res(r)
def metrics(): session.forget(response) client = db.client(request.args(0)) if not accessible_client(client.id): redirect(URL('user', 'not_autorized')) monitors = db(db.monitor.client == client.id).select(db.monitor.unique_code) metrics = {} for mon in monitors: r = accurate.call("CDRStatsV1.GetMetrics", dict(Tenant = client.unique_code, ID = mon.unique_code)) metrics[mon.unique_code] = __err_or_res(r) return metrics
def invoice(): invoice_uuid = request.args(0) or redirect(URL('default', 'index')) invoice = db(db.invoice.uuid == invoice_uuid).select().first() if not invoice: raise HTTP(404, "Not found") to_be_paid_date = invoice.created_on + timedelta( days=(invoice.to_client.payment_period or 1)) # take only a page of cdrs at a time page = 0 items_per_page = 100 params = { "run_ids": ["*default"], "tenants": [invoice.to_client.reseller.unique_code], "accounts": [invoice.to_client.unique_code], "answer_time_start": invoice.start_time.isoformat(), "answer_time_end": invoice.end_time.isoformat(), 'limit': items_per_page + 1, 'offset': 0, } codes = {} if invoice.body or request.vars['force']: body = json.loads(invoice.body) else: while True: cdrs = [] r = accurate.call("GetCdrs", params) if r['error']: response.flash = r['error'] return dict(invoice=invoice, to_be_paid_date=to_be_paid_date, body=body) else: cdrs = r['result'] for cdr in cdrs: cdr = json.loads(cdr['CostDetails']) if len(cdr['Timespans']) == 0 or cdr['Cost'] <= 0: continue # should it be logged? prefix = cdr['Timespans'][0]['MatchedPrefix'] if prefix not in codes: codes[prefix] = { 'dest_id': '', 'rate': 0, 'calls': 0, 'seconds': 0, 'cost': 0 } codes[prefix]['dest_id'] = cdr['Timespans'][0]['MatchedDestId'] codes[prefix]['rate'] = cdr['Timespans'][0]['RateInterval'][ 'Rating']['Rates'][0]['Value'] codes[prefix]['calls'] += 1 codes[prefix]['seconds'] += cdr['RatedUsage'] codes[prefix]['cost'] += cdr['Cost'] page += 1 params['offset'] = page * items_per_page if len(cdrs) <= items_per_page: break total = 0 for code, scdr in codes.iteritems(): total += scdr['cost'] body = dict(total=total, codes=codes) invoice.update_record(body=json.dumps(body)) return dict(invoice=invoice, to_be_paid_date=to_be_paid_date, body=body)
def status(): session.forget(response) return accurate.call("Responder.Status", dict(ShowBytes=True))
def cdrs(): client_id = request.args(0) if not accessible_client(client_id): redirect(URL('user', 'not_autorized')) client = db.client[client_id] or redirect(URL('user', 'not_autorized')) field_dict = OrderedDict() field_dict['cgr_ids'] = 'list:string' field_dict['not_cgr_ids'] = 'list:string' field_dict['origin_hosts'] = 'list:string' field_dict['not_origin_hosts'] = 'list:string' field_dict['sources'] = 'list:string' field_dict['not_sources'] = 'list:string' #field_dict['tors'] = 'list:string' #field_dict['not_tors'] = 'list:string' #field_dict['request_types'] = 'list:string' #field_dict['not_request_types'] = 'list:string' #field_dict['directions'] = 'list:string' #field_dict['not_directions'] = 'list:string' #field_dict['categories'] = 'list:string' #field_dict['not_categories'] = 'list:string' field_dict['destination_prefixes'] = 'list:string' field_dict['not_destination_prefixes'] = 'list:string' field_dict['suppliers'] = 'list:string' field_dict['not_suppliers'] = 'list:string' field_dict['disconnect_causes'] = 'list:string' field_dict['not_disconnect_causes'] = 'list:string' field_dict['costs'] = 'list:string' field_dict['not_costs'] = 'list:string' field_dict['extra_fields'] = 'string' field_dict['not_extra_fields'] = 'string' field_dict['order_id_start'] = 'integer' field_dict['order_id_end'] = 'integer' field_dict['setup_time_start'] = 'string' field_dict['setup_time_end'] = 'string' field_dict['answer_time_start'] = 'string' field_dict['answer_time_end'] = 'string' field_dict['created_at_start'] = 'string' field_dict['created_at_end'] = 'string' field_dict['updated_at_start'] = 'string' field_dict['updated_at_end'] = 'string' field_dict['min_usage'] = 'string' field_dict['max_usage'] = 'string' field_dict['min_pdd'] = 'string' field_dict['max_pdd'] = 'string' field_dict['min_cost'] = 'float' field_dict['max_cost'] = 'float' if auth.has_membership('admin'): field_dict['run_ids'] = 'list:string' field_dict['not_run_ids'] = 'list:string' field_dict['tenants'] = 'list:string' field_dict['not_tenants'] = 'list:string' field_dict['accounts'] = 'list:string' field_dict['not_accounts'] = 'list:string' field_dict['subjects'] = 'list:string' field_dict['not_subjects'] = 'list:string' params = {} fields = [] for key, value in field_dict.iteritems(): field = (key,value) fields.append(Field(*field)) form=SQLFORM.factory(*fields, formstyle='bootstrap3_stacked') if form.process(keepvalues=True).accepted: for key, value in form.vars.iteritems(): if field_dict[key] == 'list:string' and value != '': if isinstance(value, basestring): params[key.replace('_','')] = [value] else: params[key.replace('_','')] = value continue if key in ('extra_fields', 'not_extra_fields') and value != '': params[key.replace('_','')] = json.loads(value) continue if field_dict[key] in ('integer', 'float', 'string') and value != '': params[key.replace('_','')] = value continue elif form.errors: response.flash = 'form has errors' page = int(request.args(1)) if request.args(1) else 0 items_per_page=myconf.get('accurate.items_per_page') params['offset'], params['limit'] = page*items_per_page, items_per_page+1 params['runids'] = ['*default'] params['tenants'] = [client.unique_code] params['accounts'] = [client.unique_code] params['subjects'] = [client.unique_code] cdrs = [] r = accurate.call("GetCdrs", params) if r['error']: response.flash = r['error'] else: cdrs = r['result'] # prepare the params for show if 'runids' in params: del params['runids'] if 'tenants' in params: del params['tenants'] if 'subjects' in params: del params['subjects'] if 'accounts' in params: del params['accounts'] if 'limit' in params: del params['limit'] if 'offset' in params: del params['offset'] return dict(form=form, cdrs=cdrs, page=page, items_per_page=items_per_page, client=client, params=params)