def sample()

in src/ab/utils/data_source.py [0:0]


    def sample(self):
        '''
        如果配置了redis,会使用redis做缓存

        注意:
        odps里decimal类型的字段会读取为Decimal类型(df里显示为object),
        但to_msgpack无法dump decimal类型,需要手动转成float64,会造成精度丢失。
        如果确实需要精度,需自行实现取数据逻辑
        rds的decimal会自动读取成float64,无需处理,和网上查的不一样,不知道为什么。以防万一依然统一转化下

        TODO 这里没有检查用户名密码是否合法,可能有安全性隐患
        '''

        def value_generator():
            sample_rate, sample_count, sample = super(CachedDataSource, self).sample()
            # redis.exceptions.DataError: Invalid input of type: 'NoneType'. Convert to a byte, string or number first.
            return {'sample_rate': sample_rate if sample_rate is not None else b'',
                    'sample_count': sample_count,
                    'sample_data': self.sample_to_bytes(sample)
                    }

        # invoke cache
        cache_client = cache_plugin.get_cache_client()
        tic = time.time()
        c = cache_client.get_set_cache(self.cache_key, value_generator, redis_type='hash',
                                       expire=app.config.CACHE_TIMEOUT, table_key=self.table_key)
        sample_rate = float(c['sample_rate']) if c['sample_rate'] != b'' else None
        sample_count = int(c['sample_count'])
        sample_data = self.bytes_to_sample(c['sample_data'])
        toc = time.time()
        logger.debug('cache time:', toc - tic)
        return sample_rate, sample_count, sample_data