|
| 1 | +%% @author Couchbase <[email protected]> |
| 2 | +%% @copyright 2021-Present Couchbase, Inc. |
| 3 | +%% |
| 4 | +%% Use of this software is governed by the Business Source License included |
| 5 | +%% in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 6 | +%% in that file, in accordance with the Business Source License, use of this |
| 7 | +%% software will be governed by the Apache License, Version 2.0, included in |
| 8 | +%% the file licenses/APL2.txt. |
| 9 | +%% |
| 10 | +-module(analytics_settings_manager). |
| 11 | + |
| 12 | +-include("ns_common.hrl"). |
| 13 | + |
| 14 | +-behavior(json_settings_manager). |
| 15 | + |
| 16 | +-export([start_link/0, |
| 17 | + get/1, |
| 18 | + get_from_config/3, |
| 19 | + update/2, |
| 20 | + config_default/0]). |
| 21 | + |
| 22 | +-export([cfg_key/0, |
| 23 | + is_enabled/0, |
| 24 | + known_settings/0, |
| 25 | + on_update/2]). |
| 26 | + |
| 27 | +-import(json_settings_manager, |
| 28 | + [id_lens/1]). |
| 29 | + |
| 30 | +-define(ANALYTICS_CONFIG_KEY, {metakv, <<"/analytics/settings/config">>}). |
| 31 | + |
| 32 | +start_link() -> |
| 33 | + json_settings_manager:start_link(?MODULE). |
| 34 | + |
| 35 | +get(Key) -> |
| 36 | + json_settings_manager:get(?MODULE, Key, []). |
| 37 | + |
| 38 | +get_from_config(Config, Key, Default) -> |
| 39 | + json_settings_manager:get_from_config(?MODULE, Config, Key, Default). |
| 40 | + |
| 41 | +cfg_key() -> |
| 42 | + ?ANALYTICS_CONFIG_KEY. |
| 43 | + |
| 44 | +is_enabled() -> |
| 45 | + cluster_compat_mode:is_cluster_NEO(). |
| 46 | + |
| 47 | +on_update(_Key, _Value) -> |
| 48 | + ok. |
| 49 | + |
| 50 | +update(Key, Value) -> |
| 51 | + json_settings_manager:update(?MODULE, [{Key, Value}]). |
| 52 | + |
| 53 | +config_default() -> |
| 54 | + {?ANALYTICS_CONFIG_KEY, json_settings_manager:build_settings_json( |
| 55 | + default_settings(), dict:new(), known_settings())}. |
| 56 | + |
| 57 | +known_settings() -> |
| 58 | + [{generalSettings, general_settings_lens()}]. |
| 59 | + |
| 60 | +default_settings() -> |
| 61 | + [{generalSettings, general_settings_defaults()}]. |
| 62 | + |
| 63 | +general_settings() -> |
| 64 | + [{numReplicas, "analytics.settings.num_replicas", 0}]. |
| 65 | + |
| 66 | +general_settings_defaults() -> |
| 67 | + [{K, D} || {K, _, D} <- general_settings()]. |
| 68 | + |
| 69 | +general_settings_lens() -> |
| 70 | + json_settings_manager:props_lens( |
| 71 | + [{K, id_lens(list_to_binary(L))} || {K, L, _} <- general_settings()]). |
0 commit comments