def registration_report( request, client, group, territory, platform, start_date, end_date, **kwargs): # use this for asserting whether we respond in json, or use CSV only allow either... response_format = str(request.GET.get('format', 'json')).lower() try: start_date = str(start_date) if start_date else 'all' end_date = str(end_date) if end_date else 'all' except Exception as e: return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=500) try: #if start_date != 'all': # dates.character_checker(start_date) #if end_date != 'all': # dates.character_checker(end_date) #dt = [start_date, end_date] #dt_dates = [datetime.datetime.strptime(d, DT_FORMAT).date() if d != 'all' else d for d in dt] # trace.info('dates: {}'.format(dt_dates)) try: if start_date != 'all': start = datetime.datetime.strptime(start_date, DT_FORMAT).date() else: start = 'all' if end_date != 'all': end = datetime.datetime.strptime(end_date, DT_FORMAT).date() else: end = 'all' # trace.info('s: {} e: {}'.format(start, end)) #if isinstance(start, datetime.date) and isinstance(end, datetime.date): # if end < start: # raise RipeApiException( # *RipeApiException.BAD_DATE_RANGE, # debug_message=u"End date '{}' is before '{}'".format(end, start) # ) except Exception as e: return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=200) group_key = group group = str(group) if group else 'all' params = {'client': client or 'all', 'platform': platform or 'all', 'territory': territory or 'all'} filters = {} # print 'ALL reg PARAMS: {}'.format(params) for k in params: if params[k] != 'all': filters[k] = str(params[k]) trace.info('FILTERS: {}'.format(filters)) trace.info('GROUP: {}'.format(group)) #creates the structure. try: # trace.info('getting DATE RANGE BETWEEN: \ns:{} e: {}\n'.format(start, end)) start, end, date_range = get_date_range( start, end, cube=registration, interval=kwargs.get('interval', None) ) # trace.info('----------s:{} e: {}, range: {} range LEN: {}'.format(start, end, date_range, len(date_range))) if group == 'all': gen = ResponseGenerator('registrations', request.path, REG_ATTRIBUTES, REG_STRUCT, len(date_range) if len(date_range) else 1) else: trace.info('-----GROUPS BY {}------'.format(group)) struct_with_groups = dict() struct_with_groups.update({'start': '', 'end': '', 'name': ''}) grp_struct = {} # trace.info('----before struct {}'.format(grp_struct)) #finds number of groups. groups, group_name = registration.get_groups(group) #FILTERED-GROUPS if group in filters: # trace.info('----filtering groups: {} by {}'.format(group, filters[group])) grp_struct.update({filters[group]: GROUP_SUMMARY_STRUCT}) # trace.info('----after struct FILTERED GRP {}'.format(grp_struct)) else: # trace.info('----all groups: {}'.format()) if groups: trace.info('--creating struct for ALL groups: ---, group-name: {}, groups: {}'.format(group_name, groups)) for i in groups: grp_struct.update({i[group_name]: GROUP_SUMMARY_STRUCT}) # trace.info('----after struct ALL GRP {}'.format(grp_struct)) else: trace.info('{}') # trace.info('---- grp struct {}'.format(grp_struct)) struct_with_groups.update(grp_struct) # trace.info('----curr rep struct {}'.format(struct_with_groups)) gen = ResponseGenerator('registrations', request.path, REG_ATTRIBUTES, struct_with_groups, len(date_range) if len(date_range) else 1) gen.set_report_template() struct = gen.get_dict_response() trace.info('STRUCT created: '.format(struct)) except Exception as e: return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=200) try: m = Mapper(struct) if not date_range: date_range = [(start, end)] i = 0 for s, e in date_range: #if group == 'all': # data = registration.get_total(filters, s, e, group) # name = {'daily': s.strftime("%A"), # 'weekly': s.strftime("%W"), # 'monthly': s.strftime("%b"), # 'total': 'total'} # data.update({'start': s.strftime(DT_FORMAT), # 'end': e.strftime(DT_FORMAT), # 'name': name[kwargs.get('interval', '')]}) # m.map_to_struct(Counter(data), 'current_report', i) #else: #GROUPS ds = dict() ds.update({'start': s.strftime(DT_FORMAT), 'end': e.strftime(DT_FORMAT), 'name': 'test'}) data = registration.get_total(filters, s, e, group) ds.update(data[0]) #return ApiSuccessHttpResponse(body={'status': '{}'.format(ds)}, http_code=200) # trace.info('UPDATING LOC {} OF STRUCT {}'.format(i, struct)) #from collections import Counter ##return ApiSuccessHttpResponse(body={'status': '{}'.format(Counter(ds))}, http_code=200) #ds = Counter(ds) #m.map_to_struct(ds, 'current_report', i) #i += 1 struct = {'current_report': ds} # trace.info(' POPULATED STRUCT : {}'.format(struct)) except Exception as e: return ApiSuccessHttpResponse(body={'status': '{}'.format(e)}, http_code=200) # previous_report = False # if start_date != 'all': # previous_start_date, previous_end_date = dates.previous_report_date(start_date, end_date) # # previous_results = CubeRegistrationsDaily.cube.total( # filters, group, previous_start_date, previous_end_date) # # previous_report = True result = struct if response_format == 'json': # results['attributes'] = { # "client": client, # "groupby": group_key, # "territory": territory, # "platform": platform, # "start_date": start_date, # "end_date": end_date, # } # if previous_report and start != end: # previous_results['attributes'] = { # "client": client, # "groupby": group_key, # "territory": territory, # "platform": platform, # "start_date": previous_start_date, # "end_date": previous_end_date # } pass # if previous_report: # result['user_subscription']['previous_report'] = previous_results elif response_format == 'csv': csv = BuildCsv('registrations') csv.input_json_structure( json=result ) csv.build() return csv.response("registrations") else: raise RipeApiException( *RipeApiException.API_FORMAT, debug_message=u"format selection '{}' is not a valid format type".format(response_format) ) return ApiSuccessHttpResponse(result) except Exception: trace.exception('\n---\n')
def generate(self): GET_ATTRIBUTES = { 'registrations-raw': self.get_registration_path_attributes, 'content': self.get_content_report_attributes, 'license': self.get_license_attributes_and_filters, 'transaction': self.get_transaction_attributes_and_filters, 'subscription_revenue': self.get_subscription_revenue_attributes_and_filters } today = datetime(day=datetime.utcnow().day, month=datetime.utcnow().month, year=datetime.utcnow().year) data = list() cube = '' csvdata = dict() request_path_number = 0 for i in self._config['request_paths']: try: path = i.format(*self._report_params) self._values = list() report_name = path.split('/')[1] attributes, filters = GET_ATTRIBUTES[report_name](path) self._attributes = attributes st_date = attributes.get('start_date', today - timedelta(days=7)) end_date = attributes.get('end_date', today) start = datetime.strptime( st_date, settings.DATE_FORMAT_YMD) if isinstance( st_date, str) else st_date end = datetime.strptime( end_date, settings.DATE_FORMAT_YMD) if isinstance( end_date, str) else end_date start, end, date_range = get_date_range( start, end, cube=self.CUBE_NAMES[report_name], interval=attributes.get('interval', 'weekly')) except Exception: log.exception('\n---\n') raise Exception( 'Error occurred while processing request attributes.') try: if not date_range: date_range = [(start, end)] for s, e in date_range: data = CSVgenerator.REPORT_DATA[report_name]( s, e, filters, attributes.get('group', 'all'), ) if data: if not isinstance(data, list) and \ not isinstance(data, ValuesQuerySet): self.format_data(data, s, e, attributes) elif isinstance(data, list) or isinstance( data, ValuesQuerySet): for rec in data: self.format_data(rec, s, e, attributes) csvdata[request_path_number] = self._values request_path_number += 1 except Exception as e: log.exception( 'Exception occurred while creating data for csv report.') raise Exception( 'Exception occurred while creating data for csv report. {}' .format(e.args)) try: rep_file_name = self._report_settings.get( 'report_filename', '{}_' + self._report_type + '_{}') file_name = rep_file_name.format( self.get_client_name_for_file(), start.strftime(settings.DATE_FORMAT_YMD)) file_path = join(settings.REPORTS_ROOT, file_name) report_file = open(file_path, 'wb') #take headers from config file instead of values. config_headers = self._config['headers'] csv_headers = [str(h['csvfield']) for h in config_headers] myWriter = csv.DictWriter(report_file, fieldnames=csv_headers, dialect='excel') headers = dict((n, str(n).title()) for n in csv_headers) myWriter.writerow(headers) if len(csvdata[0]): for row in range(0, len(csvdata[0])): try: row_list = list() for h in config_headers: t = (h['csvfield'], csvdata[h['repnbr']][row][h['dbfield']] if h['dbfield'] else '') row_list.append(t) csv_row = dict(row_list) except Exception as e: log.exception( 'Exception while writing row to CSV file.') raise Exception( 'EXCEPTION WHILE CREATING CSV ROW FROM GIVEN CONFIG.' ) myWriter.writerow(csv_row) if csv_row else '' report_file.close() except Exception: log.exception('') raise Exception( 'Exception occurred while creating csv report file.') try: self.upload_report(file_path, file_name, start) except Exception: log.exception('') raise Exception('Exception while uploading report.') return cube
def generate(self): GET_ATTRIBUTES = { 'registrations-raw': self.get_registration_path_attributes, 'content': self.get_content_report_attributes, 'license': self.get_license_attributes_and_filters, 'transaction': self.get_transaction_attributes_and_filters, 'subscription_revenue': self.get_subscription_revenue_attributes_and_filters } today = datetime( day=datetime.utcnow().day, month=datetime.utcnow().month, year=datetime.utcnow().year ) data = list() cube = '' csvdata = dict() request_path_number = 0 for i in self._config['request_paths']: try: path = i.format(*self._report_params) self._values = list() report_name = path.split('/')[1] attributes, filters = GET_ATTRIBUTES[report_name](path) self._attributes = attributes st_date = attributes.get('start_date', today - timedelta(days=7)) end_date = attributes.get('end_date', today) start = datetime.strptime(st_date, settings.DATE_FORMAT_YMD) if isinstance(st_date, str) else st_date end = datetime.strptime(end_date, settings.DATE_FORMAT_YMD) if isinstance(end_date, str) else end_date start, end, date_range = get_date_range( start, end, cube=self.CUBE_NAMES[report_name], interval=attributes.get('interval', 'weekly') ) except Exception: log.exception('\n---\n') raise Exception('Error occurred while processing request attributes.') try: if not date_range: date_range = [(start, end)] for s, e in date_range: data = CSVgenerator.REPORT_DATA[report_name]( s, e, filters, attributes.get('group', 'all'), ) if data: if not isinstance(data, list) and \ not isinstance(data, ValuesQuerySet): self.format_data(data, s, e, attributes) elif isinstance(data, list) or isinstance(data, ValuesQuerySet): for rec in data: self.format_data(rec, s, e, attributes) csvdata[request_path_number] = self._values request_path_number += 1 except Exception as e: log.exception('Exception occurred while creating data for csv report.') raise Exception('Exception occurred while creating data for csv report. {}'.format(e.args)) try: rep_file_name = self._report_settings.get('report_filename', '{}_'+self._report_type+'_{}') file_name = rep_file_name.format( self.get_client_name_for_file(), start.strftime(settings.DATE_FORMAT_YMD) ) file_path = join(settings.REPORTS_ROOT, file_name) report_file = open(file_path, 'wb') #take headers from config file instead of values. config_headers = self._config['headers'] csv_headers = [str(h['csvfield'])for h in config_headers] myWriter = csv.DictWriter( report_file, fieldnames=csv_headers, dialect='excel' ) headers = dict( (n, str(n).title()) for n in csv_headers ) myWriter.writerow(headers) if len(csvdata[0]): for row in range(0, len(csvdata[0])): try: row_list = list() for h in config_headers: t = ( h['csvfield'], csvdata[h['repnbr']][row][h['dbfield']] if h['dbfield'] else '' ) row_list.append(t) csv_row = dict(row_list) except Exception as e: log.exception('Exception while writing row to CSV file.') raise Exception('EXCEPTION WHILE CREATING CSV ROW FROM GIVEN CONFIG.') myWriter.writerow(csv_row) if csv_row else '' report_file.close() except Exception: log.exception('') raise Exception('Exception occurred while creating csv report file.') try: self.upload_report(file_path, file_name, start) except Exception: log.exception('') raise Exception('Exception while uploading report.') return cube