Permalink
Browse files

use regex

  • Loading branch information...
1 parent 233b355 commit 2c091488291f21e8c7d0521e2a30267999e73e40 Ryan R. Smith (ace hacker) committed Feb 13, 2012
Showing with 21 additions and 21 deletions.
  1. +1 −1 .gitignore
  2. +20 −20 wcld.go
View
@@ -1,2 +1,2 @@
.env
-herokuapp/**.*
+herokuapp/
View
40 wcld.go
@@ -7,6 +7,7 @@ import (
"log"
"net"
"os"
+ "regexp"
"strings"
)
@@ -59,17 +60,25 @@ func clientConns(listener net.Listener) (ch chan net.Conn) {
func handleInput(logLine string) {
log.Printf("handleInput logLine=%v", logLine)
- hLine := toHstore(trimKeys(logLine))
- if len(hLine) > 0 {
- log.Printf("insert into log_data data=%v", hLine)
- _, err := pg.Exec("INSERT INTO log_data (data) VALUES ($1::hstore)", hLine)
+ logData := toHstore(trimKeys(logLine))
+ logTime := parseTime(logLine)
+ if len(logData) > 0 {
+ log.Printf("insert into log_data data=%v", logData)
+ _, err := pg.Exec("INSERT INTO log_data (data, time) VALUES ($1::hstore, $2)", logData, logTime)
if err != nil {
log.Printf("insert error message=%v", err)
}
}
return
}
+func parseTime(logLine string) (time string) {
+ t, _ := regexp.Compile(`(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))`)
+ time = t.FindString(logLine)
+ log.Printf("parsed time t=%v", time)
+ return
+}
+
/*
Takes a string like "name=ryan age=25" and converts in into "name=>ryan age=>25"
*/
@@ -85,24 +94,15 @@ func toHstore(kvs string) (string) {
i.e. string="hi #foo name=ryan age=25" would return "name=ryan age=25"
*/
func trimKeys(logLine string) (kvs string) {
- kvs = ""
+ kv, _ := regexp.Compile("([a-z0-9]+)=([a-z0-9_.-]+)")
+ pairs := kv.FindAllString(logLine, -1)
+ max := len(pairs) - 1
- if !(strings.Contains(logLine, "#wcld")) {
- return
- }
-
- fields := strings.Fields(logLine)
- max := len(fields) - 1
-
- for i, elt := range fields {
+ for i, elt := range pairs {
log.Printf("processing elt=%v at position=%v max=%v", elt, i, max)
- if strings.Contains(elt, "=") {
- if !(strings.HasPrefix(elt, "=") && strings.HasSuffix(elt, "=")) {
- kvs += elt
- if i != max {
- kvs += ", "
- }
- }
+ kvs += elt
+ if i != max {
+ kvs += ", "
}
}
return

0 comments on commit 2c09148

Please sign in to comment.