<commit_before>require "json"
require "kemal"

require "./concourse-summary/*"

serve_static({"gzip" => true, "dir_listing" => false})
gzip true

REFRESH_INTERVAL = (ENV["REFRESH_INTERVAL"]? || 30).to_i
GROUPS = parse_groups(ENV["CS_GROUPS"]? || "{}")

def setup(env)
  refresh_interval = REFRESH_INTERVAL
  username = env.store["credentials_username"]?
  password = env.store["credentials_password"]?

  login_form = env.params.query.has_key?("login_form")
  if login_form && (username.to_s.size == 0 || password.to_s.size == 0)
    raise Unauthorized.new
  end

  collapso_toggle = env.params.query.map {|k,_| k}
  ignore_groups = env.params.query.has_key?("ignore_groups")
  if ignore_groups
    collapso_toggle = collapso_toggle - ["ignore_groups"]
  else
    collapso_toggle = collapso_toggle + ["ignore_groups"]
  end

  {refresh_interval,username,password,ignore_groups,collapso_toggle,login_form}
end

def process(data, ignore_groups)
  if ignore_groups
    MyData.remove_group_info(data)
  end
  statuses = MyData.statuses(data)
end

get "/host/jobs/:host/**" do |env|
  refresh_interval,username,password,ignore_groups,collapso_toggle,login_form = setup(env)
  host = env.params.url["host"]

  data = MyData.get_data(host, username, password, nil, login_form)
  jobs = data.map do |pipeline,job|
    JobInfo.new(pipeline, job)
  end.select do |info|
    info.running || (info.status != "succeeded" && !info.paused)
  end.sort_by{|a| a.start_time || 0 }

  json_or_html(jobs, "jobs")
end

get "/host/:host/**" do |env|
  refresh_interval,username,password,ignore_groups,collapso_toggle,login_form = setup(env)
  host = env.params.url["host"]

  data = MyData.get_data(host, username, password, nil, login_form)
  statuses = process(data, ignore_groups)

  json_or_html(statuses, "host")
end<commit_msg>Allow showing jobs matching name<commit_after>require "json"
require "kemal"

require "./concourse-summary/*"

serve_static({"gzip" => true, "dir_listing" => false})
gzip true

REFRESH_INTERVAL = (ENV["REFRESH_INTERVAL"]? || 30).to_i
GROUPS = parse_groups(ENV["CS_GROUPS"]? || "{}")

def setup(env)
  refresh_interval = REFRESH_INTERVAL
  username = env.store["credentials_username"]?
  password = <PASSWORD>.store["<PASSWORD>"]?

  login_form = env.params.query.has_key?("login_form")
  if login_form && (username.to_s.size == 0 || password.to_s.size == 0)
    raise Unauthorized.new
  end

  collapso_toggle = env.params.query.map {|k,_| k}
  ignore_groups = env.params.query.has_key?("ignore_groups")
  if ignore_groups
    collapso_toggle = collapso_toggle - ["ignore_groups"]
  else
    collapso_toggle = collapso_toggle + ["ignore_groups"]
  end

  {refresh_interval,username,password,ignore_groups,collapso_toggle,login_form}
end

def process(data, ignore_groups)
  if ignore_groups
    MyData.remove_group_info(data)
  end
  statuses = MyData.statuses(data)
end

get "/host/jobs/:host/**" do |env|
  refresh_interval,username,password,ignore_groups,collapso_toggle,login_form = setup(env)
  host = env.params.url["host"]

  data = MyData.get_data(host, username, password, nil, login_form)
  jobs = data.map do |pipeline,job|
    JobInfo.new(pipeline, job)
  end.select do |info|
    info.running || (info.status != "succeeded" && !info.paused)
  end.sort_by{|a| a.start_time || 0 }

  json_or_html(jobs, "jobs")
end

get "/jobs/match/:keyword/:host/**" do |env|
  refresh_interval,username,password,ignore_groups,collapso_toggle,login_form = setup(env)
  keyword = env.params.url["keyword"]
  host = env.params.url["host"]

  data = MyData.get_data(host, username, password, nil, login_form)
  jobs = data.map do |pipeline,job|
    JobInfo.new(pipeline, job)
  end.select do |info|
    info.name.includes? keyword
  end.sort_by{|a| a.start_time || 0 }

  json_or_html(jobs, "jobs")
end

get "/host/:host/**" do |env|
  refresh_interval,username,password,ignore_groups,collapso_toggle,login_form = setup(env)
  host = env.params.url["host"]

  data = MyData.get_data(host, username, password, nil, login_form)
  statuses = process(data, ignore_groups)

  json_or_html(statuses, "host")
end
{'fraction_non_alphanumeric': 0.14240506329113925, 'fraction_numerical': 0.002677702044790652, 'mean_word_length': 5.178947368421053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5362259', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1479, 'n_words': 270}