func parseMinTime() time.Time { tm, err := timelib.ParseTime(*min) if err != nil { tm = time.Time{} } return tm }
func cleanupRangeParam(in, def string) (string, error) { if in == "" { return def, nil } t, err := timelib.ParseTime(in) if err != nil { return in, err } return t.UTC().Format(time.RFC3339Nano), nil }
func setTimeParam(uv url.Values, name, val string) error { if val == "" { return nil } t, err := timelib.ParseTime(val) if err != nil { return err } uv.Set(name, strconv.FormatInt(t.UnixNano(), 10)) return nil }
func main() { flag.Parse() if flag.NArg() < 1 { log.Fatalf("Usage: gzip -dc backup.gz | %v http://seriesly:3133/dbname", os.Args[0]) } httputil.InitHTTPTracker(false) u := flag.Arg(0) setupDb(u) minTime := parseMinTime() t := time.Tick(5 * time.Second) i := 0 var latestKey string d := json.NewDecoder(os.Stdin) for { kv := map[string]*json.RawMessage{} err := d.Decode(&kv) if err == io.EOF { log.Printf("Done! Processed %v items. Last was %v", i, latestKey) break } maybeFatal(err) for k, v := range kv { if !minTime.IsZero() { thist, err := timelib.ParseTime(k) if err == nil && minTime.After(thist) { continue } } body := []byte(*v) sendOne(u, k, body) latestKey = k } i++ select { case <-t: log.Printf("Processed %v items, latest was %v", i, latestKey) default: } } }
func newDocument(args []string, w http.ResponseWriter, req *http.Request) { var k, fk string form, err := url.ParseQuery(req.URL.RawQuery) if err == nil { fk = form.Get("ts") } if fk == "" { k = time.Now().UTC().Format(time.RFC3339Nano) } else { t, err := timelib.ParseTime(fk) if err != nil { emitError(400, w, "Bad time format", err.Error()) return } k = t.UTC().Format(time.RFC3339Nano) } putDocument([]string{args[0], k}, w, req) }