<commit_before>-module(disco_web).
-export([op/3]).

-include("disco.hrl").
-include("config.hrl").

op('GET', "/disco/version", Req) ->
    {ok, Vsn} = application:get_key(vsn),
    reply({ok, list_to_binary(Vsn)}, Req);

op('POST', "/disco/job/" ++ _, Req) ->
    BodySize = list_to_integer(Req:get_header_value("content-length")),
    if BodySize > ?MAX_JOB_PACKET ->
        Req:respond({413, [], ["Job packet too large"]});
    true ->
        Body = Req:recv_body(?MAX_JOB_PACKET),
        case catch job_coordinator:new(Body) of
            {ok, JobName} ->
                reply({ok, [<<"ok">>, list_to_binary(JobName)]}, Req);
            Error ->
                ErrorString = disco:format("Job failed to start: ~p", [Error]),
                error_logger:warning_report(ErrorString),
                reply({ok, [<<"error">>, list_to_binary(ErrorString)]}, Req)
        end
    end;

op('POST', "/disco/ctrl/" ++ Op, Req) ->
    Json = mochijson2:decode(Req:recv_body(?MAX_JSON_POST)),
    reply(postop(Op, Json), Req);

op('GET', "/disco/ctrl/" ++ Op, Req) ->
    Query = Req:parse_qs(),
    Name =
        case lists:keysearch("name", 1, Query) of
            {value, {_, N}} -> N;
            _ -> false
        end,
    reply(getop(Op, {Query, Name}), Req);

op('GET', Path, Req) ->
    ddfs_get:serve_disco_file(Path, Req);

op(_, _, Req) ->
    Req:not_found().

reply({ok, Data}, Req) ->
    Req:ok({"application/json", [], mochijson2:encode(Data)});
reply({raw, Data}, Req) ->
    Req:ok({"text/plain", [], Data});
reply({file, File, Docroot}, Req) ->
    Req:serve_file(File, Docroot);
reply(not_found, Req) ->
    Req:not_found();
reply({error, E}, Req) ->
    Req:respond({400, [], mochijson2:encode(E)});
reply(E, Req) ->
    error_logger:error_report({"500 Error", E}),
    Req:respond({500, [], ["Internal server error"]}).

getop("load_config_table", _Query) ->
    disco_config:get_config_table();
<commit_msg>Fix another dialyzer warning.<commit_after>-module(disco_web).
-export([op/3]).

-include("disco.hrl").
-include("config.hrl").

op('GET', "/disco/version", Req) ->
    {ok, Vsn} = application:get_key(vsn),
    reply({ok, list_to_binary(Vsn)}, Req);

op('POST', "/disco/job/" ++ _, Req) ->
    BodySize = list_to_integer(Req:get_header_value("content-length")),
    if BodySize > ?MAX_JOB_PACKET ->
        Req:respond({413, [], ["Job packet too large"]});
    true ->
        Body = Req:recv_body(?MAX_JOB_PACKET),
        case catch job_coordinator:new(Body) of
            {ok, JobName} ->
                reply({ok, [<<"ok">>, list_to_binary(JobName)]}, Req);
            Error ->
                ErrorString = disco:format("Job failed to start: ~p", [Error]),
                error_logger:warning_report(ErrorString),
                reply({ok, [<<"error">>, list_to_binary(ErrorString)]}, Req)
        end
    end;

op('POST', "/disco/ctrl/" ++ Op, Req) ->
    Json = mochijson2:decode(Req:recv_body(?MAX_JSON_POST)),
    reply(postop(Op, Json), Req);

op('GET', "/disco/ctrl/" ++ Op, Req) ->
    Query = Req:parse_qs(),
    Name =
        case lists:keysearch("name", 1, Query) of
            {value, {_, N}} -> N;
            _ -> false
        end,
    reply(getop(Op, {Query, Name}), Req);

op('GET', Path, Req) ->
    ddfs_get:serve_disco_file(Path, Req);

op(_, _, Req) ->
    Req:not_found().

reply({ok, Data}, Req) ->
    Req:ok({"application/json", [], mochijson2:encode(Data)});
reply({raw, Data}, Req) ->
    Req:ok({"text/plain", [], Data});
reply({file, File, Docroot}, Req) ->
    Req:serve_file(File, Docroot);
reply(not_found, Req) ->
    Req:not_found();
reply({error, E}, Req) ->
    Req:respond({400, [], mochijson2:encode(E)}).

getop("load_config_table", _Query) ->
    disco_config:get_config_table();

{'fraction_non_alphanumeric': 0.2062649323068755, 'fraction_numerical': 0.007432970533581099, 'mean_word_length': 3.109051254089422, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2939248', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1381, 'n_words': 250}