Exemplo n.º 1
0
    def _resolve_rdates(self, rule_cache: Dict = None):
        # TODO: Thread this...
        rule_cache = rule_cache or {}
        # Default to no calendar for rdate for external and oauth
        calendar = [] if not GsSession.current.is_internal() and isinstance(GsSession.current, OAuth2Session) else None

        for entity_id, rules in self.rdate_entity_map.items():
            entity = self.entity_map.get(entity_id)
            currencies = None
            exchanges = None
            if isinstance(entity, Entity):
                entity_dict = entity.get_entity()
                currency = entity_dict.get("currency")
                exchange = entity_dict.get("exchange")
                currencies = [currency] if currency else None
                exchanges = [exchange] if exchange else None
            for rule_base_date_tuple in rules:
                rule, base_date = rule_base_date_tuple[0], rule_base_date_tuple[1]
                cache_key = get_rdate_cache_key(rule_base_date_tuple[0], rule_base_date_tuple[1], currencies,
                                                exchanges)
                date_value = rule_cache.get(cache_key)
                if date_value is None:
                    if base_date:
                        base_date = dt.datetime.strptime(base_date, "%Y-%m-%d").date()
                    date_value = RelativeDate(rule, base_date).apply_rule(currencies=currencies,
                                                                          exchanges=exchanges,
                                                                          holiday_calendar=calendar)
                    rule_cache[cache_key] = date_value
                self.rule_cache[get_entity_rdate_key(entity_id, rule, base_date)] = date_value
Exemplo n.º 2
0
    def _resolve_queries(self) -> None:
        """ Resolves the dataset_id for each data query
            This is used to query data thereafter
        """
        local_rule_cache = {}
        local_availablility_cache = {}

        for query in self._data_queries:
            entity = query.entity
            query = query.query
            coord = query.coordinate
            entity_dimension = entity.data_dimension
            query_start = query.start
            query_end = query.end
            entity_dict = entity.get_entity()
            if entity_dict is None:
                currencies = None
                exchanges = None
            else:
                currency = entity_dict.get("currency")
                exchange = entity_dict.get("exchange")
                currencies = [currency] if currency else None
                exchanges = [exchange] if exchange else None

            if isinstance(query_start, RelativeDate):
                cache_key = get_rdate_cache_key(query_start.rule, currencies,
                                                exchanges)
                if cache_key in local_rule_cache:
                    query.start = local_rule_cache[cache_key]
                else:
                    query.start = query_start.apply_rule(currencies=currencies,
                                                         exchanges=exchanges)
                    local_rule_cache[cache_key] = query.start

            if isinstance(query_end, RelativeDate):
                cache_key = get_rdate_cache_key(query_end.rule, currencies,
                                                exchanges)
                if cache_key in local_rule_cache:
                    query.end = local_rule_cache[cache_key]
                else:
                    query.end = query_end.apply_rule(currencies=currencies,
                                                     exchanges=exchanges)
                    local_rule_cache[cache_key] = query.end

            if entity_dimension not in coord.dimensions:
                if coord.dataset_id:
                    # don't need to fetch the data set if user supplied it
                    coord.set_dimensions(
                        {entity_dimension: entity.get_marquee_id()})
                    query.coordinate = coord
                else:
                    # Need to resolve the dataset from availability
                    entity_id = entity.get_marquee_id()
                    try:
                        raw_availability = local_availablility_cache.get(
                            entity_id)
                        if raw_availability is None:
                            raw_availability: Dict = GsSession.current._get(
                                f'/data/measures/{entity_id}/availability')
                            local_availablility_cache[
                                entity.get_marquee_id()] = raw_availability
                        query.coordinate = entity.get_data_coordinate(
                            measure=coord.measure,
                            dimensions=coord.dimensions,
                            frequency=coord.frequency,
                            availability=raw_availability)
                    except Exception as e:
                        _logger.info(
                            f'Could not get DataCoordinate with {coord} for entity {entity_id} due to {e}'
                        )