<commit_before>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%% @author <NAME> <<EMAIL>>
%% @copyright 2014 Chef Software Inc

%% @doc license status endpoint

-module(oc_chef_wm_license).

-include_lib("chef_wm/include/chef_wm.hrl").
-include("oc_chef_wm.hrl").

-mixin([{chef_wm_base, [content_types_provided/2,
                        finish_request/2,
                        malformed_request/2,
                        ping/2 ]}]).

-mixin([{oc_chef_wm_base, [forbidden/2,
                          is_authorized/2,
                          service_available/2]}]).

-behavior(chef_wm).
-export([auth_info/2,
         init/1,
         init_resource_state/1,
         malformed_request_message/3,
         request_type/0,
         validate_request/3]).

-export([allowed_methods/2,
         to_json/2]).

init(Config) ->
    chef_wm_base:init(?MODULE, Config).

init_resource_state(_Config) ->
    {ok, #base_state{}}.

request_type() ->
    "license".

allowed_methods(Req, State) ->
    {['GET'], Req, State}.

validate_request('GET', Req, State) ->
    {Req, State}.

auth_info(Req, State) ->
    {authorized, Req, State}.

to_json(Req, #base_state{resource_state = NodeState}= State) ->
    {chef_json:encode(license_body(Req, State)), Req, State}.

%% internal functions

license_body(Req, State) ->
    % TODO: retrieve actual number -- where does this come from?
    License = 25,
    Count = node_count(Req, State),
    Exceeded = Count > License,
    % TODO: retrieve actual URL -- where does this come from?
    UpgradeURL = <<"http://example.com">>,
    {[ {<<"limit_exceeded">>, Exceeded},
       {<<"node_license">>, License},
       {<<"node_count">>, Count},
       {<<"upgrade_url">>, UpgradeURL} ]}.

node_count(Req, #base_state{chef_db_context = DbContext}=State) ->
    chef_db:count_nodes(DbContext).

%% error message functions<commit_msg>Removed unused cruft; added support for license node/url config<commit_after>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%% @author <NAME> <<EMAIL>>
%% @copyright 2014 Chef Software Inc

%% @doc license status endpoint

-module(oc_chef_wm_license).

-include_lib("chef_wm/include/chef_wm.hrl").
-include("oc_chef_wm.hrl").

-mixin([{chef_wm_base, [content_types_provided/2,
                        finish_request/2,
                        malformed_request/2,
                        ping/2 ]}]).

-mixin([{oc_chef_wm_base, [forbidden/2,
                          is_authorized/2,
                          service_available/2]}]).

-behavior(chef_wm).
-export([auth_info/2,
         init/1,
         init_resource_state/1,
         malformed_request_message/3,
         request_type/0,
         validate_request/3]).

-export([allowed_methods/2,
         to_json/2]).

init(Config) ->
    chef_wm_base:init(?MODULE, Config).

init_resource_state(_Config) ->
    {ok, #base_state{}}.

request_type() ->
    "license".

allowed_methods(Req, State) ->
    {['GET'], Req, State}.

validate_request('GET', Req, State) ->
    {Req, State}.

auth_info(Req, State) ->
    {authorized, Req, State}.

to_json(Req, State) ->
    {chef_json:encode(license_body(State)), Req, State}.

%% internal functions

license_body(State) ->
    License = envy:get(oc_chef_wm, node_license, pos_integer),
    Count = node_count(State),
    Exceeded = Count > License,
    UpgradeURL = envy:get(oc_chef_wm, upgrade_url, binary),
    {[ {<<"limit_exceeded">>, Exceeded},
       {<<"node_license">>, License},
       {<<"node_count">>, Count},
       {<<"upgrade_url">>, UpgradeURL} ]}.

node_count(#base_state{chef_db_context = DbContext}) ->
    chef_db:count_nodes(DbContext).

%% error message functions
{'fraction_non_alphanumeric': 0.16025641025641027, 'fraction_numerical': 0.013354700854700854, 'mean_word_length': 2.8848547717842323, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 47, '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': '5599974', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1281, 'n_words': 229}