<commit_before><% if can?(:manage, Alert) && @source.state > 1 && (controller.action_name == "edit" or (controller.action_name == "update" && !@source.errors.empty?)) %>

  <%= simple_form_for([:admin, @source.becomes(Source)], :url => admin_source_path(@source.name), :html => { :remote => true }) do |f| %>
    <% f.object = @source.becomes(@source.class) %>
    <fieldset>
      <legend>Edit Source</legend>

      <%= f.input :display_name %>
      <%= f.input :state_event, :as => :boolean, label: "Active", :checked_value => 'activate', :unchecked_value => 'inactivate', input_html: { checked: @source.active? }, hint: "Source is talking to external APIs" %>
      <%= f.input :private, :as => :boolean, hint: "Source not made available via API" %>
      <%= f.input :description, :input_html => { :rows => 3 } %>

      <% if not @source.config_fields.empty? %>
        <% @source.config_fields.each do |field| %>
          <% if field == "password" %>
            <%= f.input field, :as => :string %>
          <% elsif field == "expires_at" %>
            <%= f.input field, :as => :hidden %>
          <% else %>
            <%= f.input field %>
          <% end %>
        <% end %>
      <% end %>
      <%= f.input :workers, :as => :numeric, label: "Workers",
                                                    hint: "Max. number of workers" %>
      <%= f.input :job_batch_size, :as => :numeric, label: "Job queue",
                                                    hint: "Number of articles queued per job" %>
      <%= f.input :rate_limiting, :as => :numeric, label: false,
                                                    hint: "Max. requests per hour" %>

      <%= f.input :wait_time, label: false,
                              hint: "Time interval before more jobs can be added to the job queue (seconds)" %>
      <% if @source.queueable %>
        <%= f.input :staleness_week,  collection: INTERVAL_OPTIONS,
                                      label: "Update interval",
                                      include_blank: false,
                                      hint: "First 7 days after publication" %>
        <%= f.input :staleness_month, collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "First 31 days after publication" %>
        <%= f.input :staleness_year,  collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "First year after publication" %>
        <%= f.input :staleness_all,   collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "More than one year after publication" %>
      <% else %>
        <%= f.input :cron_line, label: "Update interval",
                                hint: "Time in crontab format before more jobs can be added to the job queue" %>
      <% end %>

      <%= f.input :timeout, hint: "Time to wait for response from the source (seconds)"  %>

      <%= f.input :max_failed_queries, label: "Failed queries",
                                       hint: "Maximum number of failed queries allowed before being disabled" %>
      <%= f.input :max_failed_query_time_interval, collection: INTERVAL_OPTIONS,
                                                   label: false,
                                                   include_blank: false,
                                                   hint: "Time interval for maximum number of failed queries" %>
      <%= f.input :disable_delay, label: false,
                                  hint: "Time interval before source is disabled after maximum number of failed queries is reached (seconds)" %>

      <div class="form-group">
        <%= f.submit "Save ", class: "btn btn-default" %>
        <%= link_to 'Cancel', admin_source_path(@source.name), { :remote => true, :class => 'btn' } %>
      </div>
    </fieldset>
  <% end %>

<% else %>
  <% if can?(:manage, Alert) && @source.state > 1 %>
    <%= link_to '<span class="glyphicon glyphicon-pencil"></span>'.html_safe, edit_admin_source_path(@source.name), { :remote => true, :class => 'btn btn-default btn-sm pull-right configuration-edit', :title => "Edit" } %>
  <% end %>
  <table class="table table-striped">
    <thead>
    </thead>
    <tbody>
      <% unless @source.config_fields.empty? %>
        <% @source.config_fields.each do |field| %>
          <tr>
            <th><%= field.to_s.gsub(/_/, " ").capitalize %></th>
            <% if [:username, :password, :client_id, :secret, :access_token, :api_key].include?(field) %>
              <td><em>hidden</em></td>
            <% else %>
              <td><%= @source.send(field) %></td>
            <% end %>
          </tr>
        <% end %>
      <% end %>
      <% if @source.private %>
        <tr>
          <th>Private source</th>
          <td>Source not made available via API</td>
        </tr>
      <% end %>
      <tr>
        <th>Job queue</th>
        <td><commit_msg>admin users should see passwords in display mode<commit_after><% if can?(:manage, Alert) && @source.state > 1 && (controller.action_name == "edit" or (controller.action_name == "update" && !@source.errors.empty?)) %>

  <%= simple_form_for([:admin, @source.becomes(Source)], :url => admin_source_path(@source.name), :html => { :remote => true }) do |f| %>
    <% f.object = @source.becomes(@source.class) %>
    <fieldset>
      <legend>Edit Source</legend>

      <%= f.input :display_name %>
      <%= f.input :state_event, :as => :boolean, label: "Active", :checked_value => 'activate', :unchecked_value => 'inactivate', input_html: { checked: @source.active? }, hint: "Source is talking to external APIs" %>
      <%= f.input :private, :as => :boolean, hint: "Source not made available via API" %>
      <%= f.input :description, :input_html => { :rows => 3 } %>

      <% if not @source.config_fields.empty? %>
        <% @source.config_fields.each do |field| %>
          <% if field == "password" %>
            <%= f.input field, :as => :string %>
          <% elsif field == "expires_at" %>
            <%= f.input field, :as => :hidden %>
          <% else %>
            <%= f.input field %>
          <% end %>
        <% end %>
      <% end %>
      <%= f.input :workers, :as => :numeric, label: "Workers",
                                                    hint: "Max. number of workers" %>
      <%= f.input :job_batch_size, :as => :numeric, label: "Job queue",
                                                    hint: "Number of articles queued per job" %>
      <%= f.input :rate_limiting, :as => :numeric, label: false,
                                                    hint: "Max. requests per hour" %>

      <%= f.input :wait_time, label: false,
                              hint: "Time interval before more jobs can be added to the job queue (seconds)" %>
      <% if @source.queueable %>
        <%= f.input :staleness_week,  collection: INTERVAL_OPTIONS,
                                      label: "Update interval",
                                      include_blank: false,
                                      hint: "First 7 days after publication" %>
        <%= f.input :staleness_month, collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "First 31 days after publication" %>
        <%= f.input :staleness_year,  collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "First year after publication" %>
        <%= f.input :staleness_all,   collection: INTERVAL_OPTIONS,
                                      label: false,
                                      include_blank: false,
                                      hint: "More than one year after publication" %>
      <% else %>
        <%= f.input :cron_line, label: "Update interval",
                                hint: "Time in crontab format before more jobs can be added to the job queue" %>
      <% end %>

      <%= f.input :timeout, hint: "Time to wait for response from the source (seconds)"  %>

      <%= f.input :max_failed_queries, label: "Failed queries",
                                       hint: "Maximum number of failed queries allowed before being disabled" %>
      <%= f.input :max_failed_query_time_interval, collection: INTERVAL_OPTIONS,
                                                   label: false,
                                                   include_blank: false,
                                                   hint: "Time interval for maximum number of failed queries" %>
      <%= f.input :disable_delay, label: false,
                                  hint: "Time interval before source is disabled after maximum number of failed queries is reached (seconds)" %>

      <div class="form-group">
        <%= f.submit "Save ", class: "btn btn-default" %>
        <%= link_to 'Cancel', admin_source_path(@source.name), { :remote => true, :class => 'btn' } %>
      </div>
    </fieldset>
  <% end %>

<% else %>
  <% if can?(:manage, Alert) && @source.state > 1 %>
    <%= link_to '<span class="glyphicon glyphicon-pencil"></span>'.html_safe, edit_admin_source_path(@source.name), { :remote => true, :class => 'btn btn-default btn-sm pull-right configuration-edit', :title => "Edit" } %>
  <% end %>
  <table class="table table-striped">
    <thead>
    </thead>
    <tbody>
      <% unless @source.config_fields.empty? %>
        <% @source.config_fields.each do |field| %>
          <tr>
            <th><%= field.to_s.gsub(/_/, " ").capitalize %></th>
            <% if [:username, :password, :client_id, :secret, :access_token, :api_key].include?(field) && cannot?(:manage, Alert) %>
              <td><em>hidden</em></td>
            <% else %>
              <td><%= @source.send(field) %></td>
            <% end %>
          </tr>
        <% end %>
      <% end %>
      <% if @source.private %>
        <tr>
          <th>Private source</th>
          <td>Source not made available via API</td>
        </tr>
      <% end %>
      <tr>
        <th>Job queue</th>
        <td>
{'fraction_non_alphanumeric': 0.14365849020726373, 'fraction_numerical': 0.0011409013120365088, 'mean_word_length': 1.613416149068323, 'pattern_counts': {'":': 0, '<': 171, '<?xml version=': 0, '>': 213, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3740870', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2766, 'n_words': 762}