def update_quotes(self, ticker, check_all=True): """ Get all missing quotes through current day for the given stock """ ticker = ticker.lower() stockquotes = None session = self.db.Session() last = session.query(Quote).filter_by(Ticker=ticker).order_by( desc(Quote.Date)).first().Date start_date = last + timedelta(days=1) # Ignore missing quotes for today unless it's after 7, this keeps # us from hitting the yahoo API when we know the data isn't there yet end_date = (date.today() if datetime.datetime.now().time() > datetime.time(19) else date.today() - timedelta(days=1)) if end_date > start_date: stockquotes = self._download_quotes(ticker, start_date, end_date) # Appease the API rate limit gods???? time.sleep(10) if stockquotes is not None: for quote in stockquotes: quote.Features = Indicator(quote.Id) session.add_all(stockquotes) #indicators.update_all(ticker, session, False, check_all) indicators.update_all(ticker, session, True, check_all) session.commit() session.close()
def post(self): data = request.get_json() if Indicator.query.filter(Indicator.object == data['object']).first(): return { 'error': 'indicator {} already exists'.format(data['object']) }, 409 elif not helpers.valid_type(data['type']): return { 'error': 'indicator {} is not of valid type'.format(data['object']) }, 400 elif not helpers.valid_diamond_model(data['diamondmodel']): return { 'error': 'indicator {} has invalid dimond model {}'.format( data['object'], data['diamondmodel']) }, 400 else: indicator = Indicator(data['object'], data['type'], data['firstseen'], data['lastseen'], data['diamondmodel'], data['campaign'], data['confidence'], data['comments'], data['tags'], None) db_session.add(indicator) db_session.commit() indicators = Indicator.query.filter( Indicator.object == data['object']).first() return {'indicator': helpers.row_to_dict(indicators)}, 201
def genIndicatorCard(indicator: Indicator): if indicator.display == "None": indicator.display = "" indicator.unit = "" # Get name of alert by number if indicator.risk == "nan": alert = "" else: alert = loader.config["br"]["farolcovid"]["categories"][int( indicator.risk)] if indicator.right_display == "estabilizando": indicator_right_display = "estabilizando em " + alert else: indicator_right_display = indicator.right_display risk_html_class = "bold white-span p4" return f"""
def add_stock(self, ticker, name=None, exchange=None, sector=None, industry=None): """ Add a stock to the stock database Add the stock to the symbols table and populate quotes table with all available historical quotes. If any of the optional parameters are left out, the corresponding information will be obtained from Yahoo! Finance. :param ticker: Stock ticker symbol :param name: (optional) Company/security name :param exchange: (optional) Exchange on which the security is traded :param sector: (optional) Company/security sector :param Industry (optional) Company/security industry """ ticker = ticker.lower() session = self.db.Session() if self.check_stock_exists(ticker, session): print "Stock %s already exists!" % (ticker.upper()) return if name is None: name = quotes.get_name(ticker) if exchange is None: exchange = quotes.get_stock_exchange(ticker) if sector is None: sector = quotes.get_sector(ticker) if industry is None: industry = quotes.get_industry(ticker) stock = Symbol(ticker, name, exchange, sector, industry) session.add(stock) q = self._download_quotes(ticker, date(1900, 01, 01), date.today()) for quote in q: quote.Features = Indicator(quote.Id) session.add_all(q) session.commit() session.close() self.update_quotes(ticker)
def create(self, request, *args, **kwargs): """ Create Indicator objects via csv upload or json Upload csv with the following POST DATA: city_name: String field with city name to use as a reference in the database for this indicator set source_file: File field with the csv file to import """ # Handle as csv upload if form data present source_file = request.FILES.get('source_file', None) if source_file: city_name = request.DATA.pop('city_name', None) ## Moving city_name to IndicatorJob causes this to serialize to a list if city_name and type(city_name) is list: city_name = city_name.pop(0) load_status = Indicator.load(source_file, city_name, request.user) response_status = status.HTTP_200_OK if load_status.success else status.HTTP_400_BAD_REQUEST return Response(load_status.__dict__, status=response_status) # Fall through to JSON if no form data is present # If this is a post with many indicators, process as many if isinstance(request.DATA, list): many = True else: many = False # Continue through normal serializer save process serializer = self.get_serializer(data=request.DATA, files=request.FILES, many=many) if serializer.is_valid(): self.pre_save(serializer.object) self.object = serializer.save(force_insert=True) self.post_save(self.object, created=True) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): """ Create Indicator objects via csv upload or json Upload csv with the following POST DATA: city_name: String field with city name to use as a reference in the database for this indicator set source_file: File field with the csv file to import """ # Handle as csv upload if form data present source_file = request.FILES.get('source_file', None) if source_file: city_name = request.DATA.pop('city_name', None) ## Moving city_name to IndicatorJob causes this to serialize to a list if city_name and type(city_name) is list: city_name = city_name.pop(0) load_status = Indicator.load(source_file, city_name, request.user) response_status = status.HTTP_200_OK if load_status.success else status.HTTP_400_BAD_REQUEST return Response(load_status.__dict__, status=response_status) # Fall through to JSON if no form data is present # If this is a post with many indicators, process as many if isinstance(request.DATA, list): many=True else: many=False # Continue through normal serializer save process serializer = self.get_serializer(data=request.DATA, files=request.FILES, many=many) if serializer.is_valid(): self.pre_save(serializer.object) self.object = serializer.save(force_insert=True) self.post_save(self.object, created=True) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def indicator_create(request, id=0): """ CREATE AN INDICATOR USING A TEMPLATE FIRST """ getIndicatorTypes = IndicatorType.objects.all() getCountries = Country.objects.all() countries = getCountry(request.user) country_id = Country.objects.get(country=countries[0]).id getPrograms = Program.objects.all().filter(funding_status="Funded",country__in=countries).distinct() getServices = ExternalService.objects.all() program_id = id if request.method == 'POST': #set vars from form and get values from user type = IndicatorType.objects.get(indicator_type="custom") country = Country.objects.get(id=request.POST['country']) program = Program.objects.get(id=request.POST['program']) service = request.POST['services'] level = Level.objects.all()[0] node_id = request.POST['service_indicator'] owner = request.user sector = None name = None source = None definition = None external_service_record = None #import recursive library for substitution import re print node_id #checkfor service indicator and update based on values if node_id != None and int(node_id) != 0: getImportedIndicators = import_indicator(service) for item in getImportedIndicators: if item['nid'] == node_id: getSector, created = Sector.objects.get_or_create(sector=item['sector']) sector=getSector getLevel, created = Level.objects.get_or_create(name=item['level'].title()) level=getLevel name=item['title'] source=item['source'] definition=item['definition'] #replace HTML tags if they are in the string definition = re.sub("<.*?>", "", definition) getService = ExternalService.objects.get(id=service) full_url = getService.url + "/" + item['nid'] external_service_record = ExternalServiceRecord(record_id=item['nid'],external_service=getService,full_url=full_url) external_service_record.save() getType, created = IndicatorType.objects.get_or_create(indicator_type=item['type'].title()) type=getType #save form new_indicator = Indicator(country=country, owner=owner,sector=sector,name=name,source=source,definition=definition, external_service_record=external_service_record) new_indicator.save() new_indicator.program.add(program) new_indicator.indicator_type.add(type) new_indicator.level.add(level) latest = new_indicator.id #redirect to update page messages.success(request, 'Success, Basic Indicator Created!') redirect_url = '/indicators/indicator_update/' + str(latest)+ '/' return HttpResponseRedirect(redirect_url) # send the keys and vars from the json data to the template along with submitted feed info and silos for new form return render(request, "indicators/indicator_create.html", {'country_id': country_id, 'program_id':int(program_id),'getCountries':getCountries, 'getPrograms': getPrograms,'getIndicatorTypes':getIndicatorTypes, 'getServices': getServices})
def indicator_create(request, id=0): """ CREATE AN INDICATOR USING A TEMPLATE FIRST """ getIndicatorTypes = IndicatorType.objects.all() getCountries = Country.objects.all() countries = getCountry(request.user) country_id = Country.objects.get(country=countries[0]).id getPrograms = Program.objects.all().filter( funding_status="Funded", country__in=countries).distinct() getServices = ExternalService.objects.all() program_id = id if request.method == 'POST': #set vars from form and get values from user type = IndicatorType.objects.get(indicator_type="custom") country = Country.objects.get(id=request.POST['country']) program = Program.objects.get(id=request.POST['program']) service = request.POST['services'] level = Level.objects.all()[0] node_id = request.POST['service_indicator'] owner = request.user sector = None name = None source = None definition = None external_service_record = None #import recursive library for substitution import re print node_id #checkfor service indicator and update based on values if node_id != None and int(node_id) != 0: getImportedIndicators = import_indicator(service) for item in getImportedIndicators: if item['nid'] == node_id: getSector, created = Sector.objects.get_or_create( sector=item['sector']) sector = getSector getLevel, created = Level.objects.get_or_create( name=item['level'].title()) level = getLevel name = item['title'] source = item['source'] definition = item['definition'] #replace HTML tags if they are in the string definition = re.sub("<.*?>", "", definition) getService = ExternalService.objects.get(id=service) full_url = getService.url + "/" + item['nid'] external_service_record = ExternalServiceRecord( record_id=item['nid'], external_service=getService, full_url=full_url) external_service_record.save() getType, created = IndicatorType.objects.get_or_create( indicator_type=item['type'].title()) type = getType #save form new_indicator = Indicator( country=country, owner=owner, sector=sector, name=name, source=source, definition=definition, external_service_record=external_service_record) new_indicator.save() new_indicator.program.add(program) new_indicator.indicator_type.add(type) new_indicator.level.add(level) latest = new_indicator.id #redirect to update page messages.success(request, 'Success, Basic Indicator Created!') redirect_url = '/indicators/indicator_update/' + str(latest) + '/' return HttpResponseRedirect(redirect_url) # send the keys and vars from the json data to the template along with submitted feed info and silos for new form return render( request, "indicators/indicator_create.html", { 'country_id': country_id, 'program_id': int(program_id), 'getCountries': getCountries, 'getPrograms': getPrograms, 'getIndicatorTypes': getIndicatorTypes, 'getServices': getServices })
def genIndicatorCard(indicator: Indicator, place_type: str, rt_type: str = "nan"): if indicator.display == "None": indicator.display = "" indicator.unit = "" # Get name of alert by number if indicator.risk == "nan": alert = "" else: alert = loader.config["br"]["farolcovid"]["categories"][int( indicator.risk)] if indicator.right_display == "estabilizando": indicator_right_display = "estabilizando em " + alert else: indicator_right_display = indicator.right_display # TODO: find better palce to save this dic captions_by_place = { "state_num_id": { "SITUAÇÃO DA DOENÇA": "Hoje em seu <b>estado</b> são <b>reportados</b> em média", "CONTROLE DA DOENÇA": "Não há dados abertos sistematizados de testes ou rastreamento de contatos no Brasil. Logo, <b>classificamos pela estimativas de Rt de seu estado.</b>", "CAPACIDADE DO SISTEMA": "Se nada mudar, a capacidade hospitalar de seu <b>estado</b> será atingida em", "CONFIANÇA DOS DADOS": "A cada 10 pessoas infectadas em seu <b>estado</b>,", }, "health_region_id": { "SITUAÇÃO DA DOENÇA": "Hoje em sua <b>regional de saúde</b> são <b>reportados</b> em média", "CONTROLE DA DOENÇA": "Não há dados abertos sistematizados de testes ou rastreamento de contatos no Brasil. Logo, <b>classificamos pela estimativas de Rt de sua regional.</b>", "CAPACIDADE DO SISTEMA": "Se nada mudar, a capacidade hospitalar de sua <b>regional de saúde</b> será atingida em", "CONFIANÇA DOS DADOS": "A cada 10 pessoas infectadas em sua <b>regional de saúde</b>,", }, "city_id": { "SITUAÇÃO DA DOENÇA": "Hoje em seu <b>município</b> são <b>reportados</b> em média", "CONTROLE DA DOENÇA": { "health_region_id": "Não há dados abertos sistematizados de testes ou rastreamento de contatos no Brasil. Logo, <b>classificamos pela estimativas de Rt de sua regional.</b>", "city_id": "Não há dados abertos sistematizados de testes ou rastreamento de contatos no Brasil. Logo, <b>usamos estimativas de Rt de seu município para classificação.</b>", }, "CAPACIDADE DO SISTEMA": "Se nada mudar, a capacidade hospitalar de sua <b>regional de saúde</b> será atingida em", "CONFIANÇA DOS DADOS": "A cada 10 pessoas infectadas em sua <b>regional de saúde</b>,", }, } if place_type == "city_id" and indicator.header == "CONTROLE DA DOENÇA": indicator.caption = captions_by_place[place_type][ indicator.header][rt_type] else: indicator.caption = captions_by_place[place_type][indicator.header] return f"""