input {
  #file {
  #  path => "/var/log/copr/backend.log"
  #  type => "copr.backend.main"
  #}
  file {
    path => "/var/log/lighttpd/access.log"
    type => "lighttpd-access"
  }
}

filter {
  mutate {
    add_tag => [ "backend" ]
  }
  if [type] == 'lighttpd-access' {
    grok {
      patterns_dir => "/usr/share/logstash/patterns"
      pattern => "%{LIGHTTPD}"
    }

    # ignore requests generated by search engine crawlers
    useragent {
      source => "agent"
      target => "ua_parsed"
    }
    if [ua_parsed][device] == "Spider" {
      drop {}
    }

    date {
       match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
    }
    if "repodata/repomd.xml" in [request] and "devel/repodata/repomd.xml" not in [request]  {
      mutate { add_tag => "repomdxml" }
      mutate { add_tag => "publish_stat" }
      grok {
        match => ["request", "/results/%{USERNAME:copr_user}/%{USERNAME:copr_project_name}/%{USERNAME:copr_chroot}/repodata/repomd.xml"]
      }
    }
    if [request] =~ "rpm$" {
      mutate { add_tag => "rpm" }
      mutate { add_tag => "publish_stat" }
      grok {
        match => ["request", "/results/%{USERNAME:copr_user}/%{USERNAME:copr_project_name}/%{USERNAME:copr_chroot}/%{USERNAME:copr_build_dir}/%{USERNAME:copr_rpm}"]
      }
    }
  }
}

output {
  if "publish_stat" in [tags] and "200" == [response] {
      http {
        url => "{{ frontend_base_url }}/stats_rcv/from_logstash"
        format => "json"
        http_method => "post"
      }

      file {
        path => "/var/log/logstash/published.log"
        codec => "rubydebug"
      }
  }

}
