"""Feature flags""" import re from typing import Literal from flask import g from charts.api import app try: # it's a very dirty hack for tests, but it works. from pythonfeatures import pythonfeatures except ValueError: pass def get_all_feature_flags() -> dict[str, Literal['control'] | Literal['enabled']]: """Get all feature flags and their restricted status.""" return pythonfeatures.get_all_features(g.request_context).message def get_controlled_chart_feature_flags() -> list[str]: """Get list of the controlled insights_chart_* feature flags.""" features = get_all_feature_flags() chart_features = [k for k, v in features.items() if re.search('^insights_chart_.*$', k) is not None and v == 'control'] return chart_features def ff_chart_sql_filter(feature_flagged_charts: list[str]) -> str: # Hide NMF charts by default (IN-15241) chart_sql_filters = ['dc.type != \'spotify_nmf\''] if len(feature_flagged_charts) > 0: charts_to_filter = ["'" + ff.replace('insights_chart_', '') + "'" for ff in feature_flagged_charts] chart_sql_filter = "concat(dc.platform,'_',dc.frequency) not in " chart_sql_filter = chart_sql_filter + "(" + ",".join(charts_to_filter) + ")" chart_sql_filters.append(chart_sql_filter) return '(' + ' AND '.join(chart_sql_filters) + ')' def get_ff_charts_sql_filter() -> str: """Generate the SQL filter for feature flag controlled charts""" feature_flagged_charts = get_controlled_chart_feature_flags() return ff_chart_sql_filter(feature_flagged_charts) def get_ff_charts_cache() -> bool: """Get whether to use Redis for caching chart entry data.""" return pythonfeatures.get_single_feature('insights_charts_redis_cache', g.request_context).message == 'enabled' def get_show_sme_data_flag() -> bool: """Checks if the user has `show_sme_data` flag enabled""" # Bypass show_sme_data FF check based on user's profile brand: sme user_brand = getattr(g.request_context, 'brand', None) if user_brand and user_brand == 'sme': return True return pythonfeatures.get_single_feature('show_sme_data', g.request_context).message == 'enabled'