def import_external_grouped_data(data_source, data_type): LOGGER.info('Loading working data') query = QUERIES[data_source][data_type]['query'] group_by = QUERIES[data_source][data_type]['group_by'] LOGGER.info('Importing ' + str(data_type) + ' from ' + str(data_source)) LOGGER.info('Using query:' + str(query)) results = dbutils.query_to_dicts(query, data_source) database = getattr(client, data_source) all_values = {} for result in results: keys = [] for key in group_by: if isinstance(result[key], datetime.date): keys.append(epoch_time(dt.combine(result[key],dt.min.time()))) else: keys.append(result[key].replace('.','___')) # Clean the data new_entry = convert_to_mongo(result) current_values = all_values for key in keys[:-1]: if not current_values.has_key(key): current_values[key] = {} current_values = current_values[key] if not current_values.has_key(keys[-1]): current_values[keys[-1]] = [] current_values[keys[-1]].append(new_entry) for key in all_values.keys(): LOGGER.info("Inserting " + data_type + " with _id: " + str(key)) database[data_type].remove({'_id': key}) database[data_type].insert({'_id': key, 'values': all_values[key]})
def import_external_tracks(data_source): nav_value = Attributes.objects.get(identifier='NUM_TYPE_NAV', active=True) final_status = Attributes.objects.get(identifier='NUM_STATUS_FINAL', active=True) official_type = Attributes.objects.get(identifier='PRICE_TYPE_OFFICIAL', active=True) daily = Attributes.objects.get(identifier='FREQ_DAILY', active=True) data_provider = Attributes.objects.get(identifier='SCR_DP', active=True) provider = CompanyContainer.objects.get(short_name__icontains=data_source) external_provider = RelatedCompany.objects.get(company=provider, role=data_provider) query = QUERIES[data_source]['tracks']['query'] securities = SecurityContainer.objects.filter(aliases__alias_type__short_name=data_source.upper()).distinct() for security in securities: LOGGER.info("Working on " + security.name) if not TrackContainer.objects.filter(effective_container_id = security.id).exists(): # Switch to new data provider if security.associated_companies.filter(role=data_provider).exists(): security.associated_companies.remove(security.associated_companies.get(role=data_provider)) security.save() security.associated_companies.add(external_provider) security.save() try: track = TrackContainer.objects.get( effective_container_id=security.id, type__id=nav_value.id, quality__id=official_type.id, source__id=provider.id, frequency__id=daily.id, status__id=final_status.id) LOGGER.info("\tTrack already exists") except: track = TrackContainer() track.effective_container = security track.type = nav_value track.quality = official_type track.source = provider track.status = final_status track.frequency = daily track.frequency_reference = None track.save() all_values = [] # TODO: Change while import of universe is correct alias = security.aliases.filter(alias_type__short_name=data_source.upper()) alias = alias[0] results = dbutils.query_to_dicts(query%alias.alias_value, data_source) for result in results: if result['prezzo']!='' and result['prezzo']!=None: all_values.append({'date': dt.combine(result['data_ins'], dt.min.time()), 'value': float(result['prezzo'])}) else: LOGGER.warning("\tInvalid price value " + unicode(result)) set_track_content(track, all_values, True) if len(all_values)>0 and security.associated_companies.filter(company=provider, role=data_provider).exists(): populate_perf(track.effective_container, track) populate_monthly_track_from_track(track.effective_container, track) populate_weekly_track_from_track(track.effective_container, track) LOGGER.info("\tFound " + str(len(all_values)) + " tokens in external track [" + str(alias.alias_value) + "]")
def import_external_data_sequence(data_source, data_type): LOGGER.info('Loading working data') query = QUERIES[data_source][data_type]['query'] group_by = QUERIES[data_source][data_type]['group_by'] LOGGER.info('Importing ' + str(data_type) + ' from ' + str(data_source)) LOGGER.info('Using query:' + str(query)) results = dbutils.query_to_dicts(query, data_source) database = getattr(client, data_source) database[data_type].drop() all_data = {} for result in results: key = result[group_by] new_entry = convert_to_mongo(result) if not all_data.has_key(key): all_data[key] = {'_id': key, 'data': []} all_data[key]['data'].append(new_entry) [database[data_type].save(e) for e in all_data.values()]
def import_external_data(data_source, data_type): LOGGER.info('Loading working data') query = QUERIES[data_source][data_type]['query'] group_by = QUERIES[data_source][data_type]['group_by'] external_alias = Attributes.objects.get(identifier='ALIAS_' + data_source.upper()) if data_type=='securities': bloomberg_alias = Attributes.objects.get(identifier='ALIAS_BLOOMBERG') isin_alias = Attributes.objects.get(identifier='ALIAS_ISIN') sec_container_type = Attributes.objects.get(identifier='CONT_SECURITY') fund_container_type = Attributes.objects.get(identifier='CONT_FUND') bond_container_type = Attributes.objects.get(identifier='CONT_BOND') stock_container_type = Attributes.objects.get(identifier='CONT_SECURITY') security_type = Attributes.objects.get(identifier='SECTYP_SECURITY') fund_type = Attributes.objects.get(identifier='SECTYP_FUND') bond_type = Attributes.objects.get(identifier='SECTYP_BOND') stock_type = Attributes.objects.get(identifier='SECTYP_STOCK') daily = Attributes.objects.get(identifier='FREQ_DAILY', active=True) bloomberg_provider = get_bloomberg_provider() universe = get_universe_from_datasource(data_source) LOGGER.info('Cleaning already imported aliases for ' + data_source) securities = SecurityContainer.objects.filter(aliases__alias_type__short_name=data_source.upper()).distinct() for security in securities: for alias in security.aliases.all(): if alias.alias_type.short_name==data_source.upper(): security.aliases.remove(alias) security.save() LOGGER.info('\tCleaning done') LOGGER.info('Importing ' + str(data_type) + ' from ' + str(data_source)) LOGGER.info('Using query:' + str(query)) results = dbutils.query_to_dicts(query, data_source) database = getattr(client, data_source) database[data_type].drop() all_tickers = [] for result in results: # Clean the data new_entry = convert_to_mongo(result) # LOGGER.info('Removing entries with name ' + new_entry[QUERIES[data_source][data_type]['name']]) # SecurityContainer.objects.filter(name=new_entry[QUERIES[data_source][data_type]['name']]).delete() for group_id in group_by: if new_entry[group_id]!=None and new_entry[group_id]!='': # LOADING INTO MONGO LOGGER.info('Adding entry [' + group_id + '=' + str(new_entry[group_id]) + "]") new_entry['_id'] = new_entry[group_id] if QUERIES[data_source][data_type].has_key('joins'): for join_info in QUERIES[data_source][data_type]['joins']: values = [new_entry[identifier] for identifier in join_info['on']] underlying_query = join_info['query'] for value in values: underlying_query = underlying_query%value new_entry[join_info['name']] = [] under_results = dbutils.query_to_dicts(underlying_query, data_source) for under_result in under_results: LOGGER.info('\tAdding underlying [' + join_info['name'] + ', ' + group_id + '=' + str(new_entry[group_id]) + "]") # Clean the data under_entry = convert_to_mongo(under_result) new_entry[join_info['name']].append(under_entry) try: database[data_type].save(new_entry) except DuplicateKeyError: LOGGER.error("The following entry already exists:") # CONVERTING TO FINALE if group_id==QUERIES[data_source][data_type]['EXTERNAL'] and data_type=='securities': name_field = QUERIES[data_source][data_type]['name'] short_name_field = QUERIES[data_source][data_type]['short_name'] bloomberg_ticker = result[QUERIES[data_source][data_type]['BLOOMBERG']] isin_code = result[QUERIES[data_source][data_type]['ISIN']] currency = result[QUERIES[data_source][data_type]['currency']] currency = Attributes.objects.filter(type='currency', short_name=currency) if currency.exists(): currency = currency[0] else: currency = None security = SecurityContainer.objects.filter(aliases__alias_type__short_name=data_source.upper(), aliases__alias_value=result[group_id]) external_append = False additional = '' if not security.exists(): security = SecurityContainer.objects.filter(aliases__alias_type__id=bloomberg_alias.id, aliases__alias_value=bloomberg_ticker) additional = result[name_field] external_append = True if is_fund(data_source, result): container = fund_container_type stype = fund_type elif is_bond(data_source, result): container = bond_container_type stype = bond_type elif is_stock(data_source, result): container = stock_container_type stype = stock_type else: container = sec_container_type stype = security_type if not security.exists(): LOGGER.info("Creating security with " + data_source + " id [" + str(result[group_id]) + "]") security = SecurityContainer() security.name = result[name_field] security.short_name = result[short_name_field] if result[short_name_field]!=None and result[short_name_field]!='' else result[name_field] security.save() security.associated_companies.add(bloomberg_provider) security.save() else: LOGGER.info("Security with " + data_source + " id [" + str(result[group_id]) + "] already exists.") security = security[0] if not security.associated_companies.filter(role__identifier='SCR_DP').exists(): security.associated_companies.add(bloomberg_provider) security.save() security.update_alias(external_alias, result[group_id], additional, external_append) security.currency = currency security.frequency = daily security.type = container security.security_type = stype security.save() if bloomberg_ticker!=None and bloomberg_ticker!='': all_tickers.append(bloomberg_ticker) elif isin_code!=None and isin_code!='': all_tickers.append(isin_code) if bloomberg_ticker!=None and bloomberg_ticker!='': security.update_alias(bloomberg_alias, bloomberg_ticker) if isin_code!=None and isin_code!='': security.update_alias(isin_alias, isin_code) universe.members.add(security) if data_type=='securities': universe.save() all_containers = {} for member in universe.members.all(): member = get_effective_instance(member) if not all_containers.has_key(member.type.identifier): all_containers[member.type.identifier] = [] try: all_containers[member.type.identifier].append(member.aliases.get(alias_type__name='BLOOMBERG').alias_value) except: try: all_containers[member.type.identifier].append(member.aliases.get(alias_type__name='ISIN').alias_value) except: LOGGER.info("There is no Bloomberg nor ISIN code available for this security [" + member.name + "]") for key in all_containers.keys(): fields = BloombergTrackContainerMapping.objects.filter(Q(container__identifier='CONT_SECURITY') | Q(container__identifier=key), Q(active=True)).values_list('short_name__code', flat=True) all_containers[key] = [to_bloomberg_code(ticker,True) for ticker in all_containers[key]] history_key = uuid.uuid4().get_hex() bb_thread = threading.Thread(None, bloomberg_history_query, history_key, (history_key, all_containers[key], fields, True)) bb_thread.start() bb_thread.join()
def get_unique_id(self): with self.lock: results = dbutils.query_to_dicts("SELECT nextval('edm_sequence')") for result in results: return str(result['nextval'])