Last active
September 25, 2015 09:32
-
-
Save fujiwara/3337b8abfa27137d4c4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"os" | |
"strconv" | |
"strings" | |
"github.com/ymotongpoo/goltsv" | |
) | |
func main() { | |
reader := goltsv.NewReader(os.Stdin) | |
writer := json.NewEncoder(os.Stdout) | |
for { | |
in, err := reader.Read() | |
if err != nil { | |
break | |
} | |
out := make(map[string]interface{}, len(in)) | |
for key, value := range in { | |
switch key { | |
case "request_time", "upstream_response_time", "reqtime", "apptime": | |
if v, err := strconv.ParseFloat(value, 64); err == nil { | |
out[key] = v | |
} else { | |
out[key] = float64(0) | |
} | |
case "status", "size": | |
if v, err := strconv.ParseInt(value, 10, 64); err == nil { | |
out[key] = v | |
} else { | |
out[key] = 0 | |
} | |
case "uri": | |
v := strings.SplitN(value, "?", 2) | |
out[key] = v[0] | |
default: | |
out[key] = value | |
} | |
} | |
writer.Encode(out) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.16.0; | |
use Text::LTSV; | |
use JSON::XS qw/ encode_json /; | |
my $p = Text::LTSV->new; | |
while (my $line = <>) { | |
my $in = $p->parse_line($line); | |
for my $key (keys %$in) { | |
if ($key eq "request_time" || $key eq "upstream_response_time" || $key eq "reqtime" || $key eq "apptime") { | |
if ($in->{$key} eq "-") { | |
$in->{$key} = 0; | |
} else { | |
$in->{$key} += 0; | |
} | |
} elsif ($key eq "status" || $key eq "size") { | |
$in->{$key} += 0; | |
} | |
} | |
say encode_json($in); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'ltsv' | |
require 'json' | |
STDIN.each do |line| | |
record = LTSV.parse(line) | |
result = {} | |
record[0].each { |key, value| | |
case key | |
when :request_time, :upstream_response_time, :apptime, :reqtime | |
result[key] = Float(value) rescue 0.0 | |
when :status, :size, :api_status, :user_id | |
result[key] = Integer(value) rescue 0 | |
when :uri | |
result[key] = value.gsub(/\?.*/, "") | |
else | |
result[key] = value | |
end | |
} | |
puts JSON.generate(result) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment