def __init__(self, redis_client: Redis, key_schema: KeySchema = None, **kwargs) -> None: self.redis = redis_client if key_schema is None: key_schema = KeySchema() self.key_schema = key_schema
def configure(app): key_schema = KeySchema(app.config['REDIS_KEY_PREFIX']) redis_client = get_redis_timeseries_connection(app.config['REDIS_HOST'], app.config['REDIS_PORT']) try: redis_client.ping() except exceptions.AuthenticationError: app.logger.error("Redis authentication failed. Make sure you set " "$REDISOLAR_REDIS_PASSWORD to the correct password " "for your Redis instance. Stopping server.") raise app.do_teardown_appcontext() if app.config.get('USE_GEO_SITE_API'): api.add_resource(SiteGeoListResource, '/sites', resource_class_args=(SiteGeoDaoRedis( redis_client, key_schema), )) api.add_resource(SiteGeoResource, '/sites/<int:site_id>', resource_class_args=(SiteGeoDaoRedis( redis_client, key_schema), )) else: api.add_resource(SiteListResource, '/sites', resource_class_args=(SiteDaoRedis( redis_client, key_schema), )) api.add_resource(SiteResource, '/sites/<int:site_id>', resource_class_args=(SiteDaoRedis( redis_client, key_schema), )) api.add_resource(CapacityReportResource, '/capacity', resource_class_args=(CapacityReportDaoRedis( redis_client, key_schema), )) api.add_resource(GlobalMeterReadingResource, '/meter_readings', resource_class_args=(MeterReadingDaoRedis( redis_client, key_schema), FeedDaoRedis(redis_client, key_schema))) api.add_resource(SiteMeterReadingResource, '/meter_readings/<int:site_id>', resource_class_args=(FeedDaoRedis(redis_client, key_schema), )) api.add_resource(MetricsResource, '/metrics/<int:site_id>', resource_class_args=(MetricDaoRedisTimeseries( redis_client, key_schema), ))
def load(filename, delete_keys): """Load the specified JSON file into Redis""" conf = current_app.config hostname = conf['REDIS_HOST'] port = conf['REDIS_PORT'] key_prefix = conf['REDIS_KEY_PREFIX'] key_schema = KeySchema(key_prefix) client = get_connection(hostname=hostname, port=port) site_dao = SiteDaoRedis(client, key_schema) site_geo_dao = SiteGeoDaoRedis(client, key_schema) if delete_keys: for key in client.scan_iter(f"{key_prefix}:*"): client.delete(key) with open(filename, 'r') as f: sites = [FlatSiteSchema().load(d) for d in json.loads(f.read())] sites_bar = Bar('Loading sites', max=len(sites)) p = client.pipeline(transaction=False) for site in sites: sites_bar.next() site_dao.insert(site, pipeline=p) site_geo_dao.insert(site, pipeline=p) p.execute() print() sample_generator = SampleDataGenerator(client, sites, 1, key_schema) readings_bar = Bar('Generating metrics data', max=sample_generator.size) p = client.pipeline(transaction=False) for _ in sample_generator.generate(p): readings_bar.next() print("\nFinishing up...") p.execute() print("\nData load complete!")
def key_schema(app): yield KeySchema(app.config['REDIS_KEY_PREFIX'])
def test_configurable_prefix(app): key_schema = KeySchema('prefix-test') assert key_schema.site_hash_key(1) == "prefix-test:sites:info:1"