<commit_before>%% @author Couchbase <<EMAIL>>
%% @copyright 2014 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
-module(menelaus_web_cluster_logs).

-export([handle_start_collect_logs/1,
         handle_cancel_collect_logs/1,
         handle_settings_log_redaction/1,
         handle_settings_log_redaction_post/1]).

-import(menelaus_util,
        [reply_json/2,
         assert_is_enterprise/0,
         assert_is_vulcan/0,
         validate_has_params/1,
         validate_one_of/3,
         validate_unsupported_params/1,
         execute_if_validated/3]).

handle_settings_log_redaction(Req) ->
    assert_is_enterprise(),
    assert_is_vulcan(),

    {value, Config} = ns_config:search(ns_config:get(), log_redaction_default_cfg),
    Level = proplists:get_value(redact_level, Config),
    Settings = [{redact_level, Level}],
    reply_json(Req, {struct, Settings}).

handle_settings_log_redaction_post(Req) ->
    assert_is_enterprise(),
    assert_is_vulcan(),

    execute_if_validated(
      fun (Values) ->
              do_handle_settings_log_redaction_post_body(Req, Values)
      end, Req, validate_settings_log_redaction_post(Req:parse_post())).

validate_settings_log_redaction_post(Args) ->
    R0 = validate_has_params({Args, [], []}),
    R1 = validate_one_of(redact_level, ["none", "partial"], R0),
    validate_unsupported_params(R1).

do_handle_settings_log_redaction_post_body(Req, Values) ->
    Settings = [{redact_level, proplists:get_value(redact_level, Values)}],
    ns_config:set(log_redaction_default_cfg, Settings),
    handle_settings_log_redaction(Req).

handle_start_collect_logs(Req) ->
    Params = Req:parse_post(),

    case parse_validate_collect_params(Params, ns_config:get()) of
        {ok, Nodes, BaseURL, RedactLevel} ->
            case cluster_logs_collection_task:preflight_base_url(BaseURL) of
                ok ->
                    case cluster_logs_sup:start_collect_logs(Nodes, BaseURL, RedactLevel) of
                        ok ->
                            menelaus_util:reply_json(Req, [], 200);
                        already_started ->
                            menelaus_util:reply_json(Req, {struct, [{'_', <<"Logs collection task is already started">>}]}, 400)
                    end;
                {error, Message} ->
                    menelaus_util:reply_json(Req, {struct, [{'_', Message}]}, 400)
            end;
        {errors, RawErrors} ->
            Errors = [begin
                          {Field, Msg} = stringify_one_node_upload_error(E),
                          {Field, iolist_to_binary(Msg)}
                      end || E <- RawErrors],
            menelaus_util:reply_json(Req, {struct, lists:flatten(Errors)}, 400)
    end.

%% we're merely best-effort-sync and we don't care about results
handle_cancel_collect_logs(Req) ->
    cluster_logs_sup:cancel_logs_collection(),
    menelaus_util:reply_json(Req, []).<commit_msg>MB-26600 rename redact_level parameter of /settings/logRedaction<commit_after>%% @author Couchbase <<EMAIL>>
%% @copyright 2014 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
-module(menelaus_web_cluster_logs).

-export([handle_start_collect_logs/1,
         handle_cancel_collect_logs/1,
         handle_settings_log_redaction/1,
         handle_settings_log_redaction_post/1]).

-import(menelaus_util,
        [reply_json/2,
         assert_is_enterprise/0,
         assert_is_vulcan/0,
         validate_has_params/1,
         validate_one_of/3,
         validate_unsupported_params/1,
         execute_if_validated/3]).

handle_settings_log_redaction(Req) ->
    assert_is_enterprise(),
    assert_is_vulcan(),

    {value, Config} =
        ns_config:search(ns_config:get(), log_redaction_default_cfg),
    Level = proplists:get_value(redact_level, Config),
    Settings = [{logRedactionLevel, Level}],
    reply_json(Req, {struct, Settings}).

handle_settings_log_redaction_post(Req) ->
    assert_is_enterprise(),
    assert_is_vulcan(),

    execute_if_validated(
      fun (Values) ->
              do_handle_settings_log_redaction_post_body(Req, Values)
      end, Req, validate_settings_log_redaction_post(Req:parse_post())).

validate_settings_log_redaction_post(Args) ->
    R0 = validate_has_params({Args, [], []}),
    R1 = validate_one_of(logRedactionLevel, ["none", "partial"], R0),
    validate_unsupported_params(R1).

do_handle_settings_log_redaction_post_body(Req, Values) ->
    Settings = [{redact_level, proplists:get_value(logRedactionLevel, Values)}],
    ns_config:set(log_redaction_default_cfg, Settings),
    handle_settings_log_redaction(Req).

handle_start_collect_logs(Req) ->
    Params = Req:parse_post(),

    case parse_validate_collect_params(Params, ns_config:get()) of
        {ok, Nodes, BaseURL, RedactLevel} ->
            case cluster_logs_collection_task:preflight_base_url(BaseURL) of
                ok ->
                    case cluster_logs_sup:start_collect_logs(Nodes, BaseURL, RedactLevel) of
                        ok ->
                            menelaus_util:reply_json(Req, [], 200);
                        already_started ->
                            menelaus_util:reply_json(Req, {struct, [{'_', <<"Logs collection task is already started">>}]}, 400)
                    end;
                {error, Message} ->
                    menelaus_util:reply_json(Req, {struct, [{'_', Message}]}, 400)
            end;
        {errors, RawErrors} ->
            Errors = [begin
                          {Field, Msg} = stringify_one_node_upload_error(E),
                          {Field, iolist_to_binary(Msg)}
                      end || E <- RawErrors],
            menelaus_util:reply_json(Req, {struct, lists:flatten(Errors)}, 400)
    end.

%% we're merely best-effort-sync and we don't care about results
handle_cancel_collect_logs(Req) ->
    cluster_logs_sup:cancel_logs_collection(),
    menelaus_util:reply_json(Req, []).
{'fraction_non_alphanumeric': 0.10720436280137773, 'fraction_numerical': 0.010763490241102182, 'mean_word_length': 3.0353213665315577, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2211378', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2096, 'n_words': 433}