本文整理汇总了Golang中github.com/skypies/complaints/complaintdb.ComplaintDB.QueryInSpan方法的典型用法代码示例。如果您正苦于以下问题:Golang ComplaintDB.QueryInSpan方法的具体用法?Golang ComplaintDB.QueryInSpan怎么用?Golang ComplaintDB.QueryInSpan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/skypies/complaints/complaintdb.ComplaintDB
的用法示例。
在下文中一共展示了ComplaintDB.QueryInSpan方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: generateMonthlyCSV
func generateMonthlyCSV(cdb complaintdb.ComplaintDB, month, year int) (string, int, error) {
ctx := cdb.Ctx()
bucketname := "serfr0-reports"
now := date.NowInPdt()
s := time.Date(int(year), time.Month(month), 1, 0, 0, 0, 0, now.Location())
e := s.AddDate(0, 1, 0).Add(-1 * time.Second)
log.Infof(ctx, "Starting /be/month: %s", s)
// One time, at 00:00, for each day of the given month
days := date.IntermediateMidnights(s.Add(-1*time.Second), e)
filename := s.Format("complaints-20060102") + e.Format("-20060102.csv")
gcsName := "gs://" + bucketname + "/" + filename
if exists, err := gcs.Exists(ctx, bucketname, filename); err != nil {
return gcsName, 0, fmt.Errorf("gcs.Exists=%v for gs://%s/%s (err=%v)", exists, bucketname, filename, err)
} else if exists {
return gcsName, 0, nil
}
gcsHandle, err := gcs.OpenRW(ctx, bucketname, filename, "text/plain")
if err != nil {
return gcsName, 0, err
}
csvWriter := csv.NewWriter(gcsHandle.IOWriter())
cols := []string{
"CallerCode", "Name", "Address", "Zip", "Email", "HomeLat", "HomeLong",
"UnixEpoch", "Date", "Time(PDT)", "Notes", "ActivityDisturbed", "Flightnumber",
"Notes",
// Column names above are incorrect, but BKSV are used to them.
//
//"CallerCode", "Name", "Address", "Zip", "Email", "HomeLat", "HomeLong",
//"UnixEpoch", "Date", "Time(PDT)", "Notes", "Flightnumber",
//"ActivityDisturbed", "CcSFO",
}
csvWriter.Write(cols)
tStart := time.Now()
n := 0
for _, dayStart := range days {
dayEnd := dayStart.AddDate(0, 0, 1).Add(-1 * time.Second)
log.Infof(ctx, " /be/month: %s - %s", dayStart, dayEnd)
tIter := time.Now()
iter := cdb.NewLongBatchingIter(cdb.QueryInSpan(dayStart, dayEnd))
for {
c, err := iter.NextWithErr()
if err != nil {
return gcsName, 0, fmt.Errorf("iterator failed after %s (%s): %v", err, time.Since(tIter),
time.Since(tStart))
}
if c == nil {
break
}
r := []string{
c.Profile.CallerCode,
c.Profile.FullName,
c.Profile.Address,
c.Profile.StructuredAddress.Zip,
c.Profile.EmailAddress,
fmt.Sprintf("%.4f", c.Profile.Lat),
fmt.Sprintf("%.4f", c.Profile.Long),
fmt.Sprintf("%d", c.Timestamp.UTC().Unix()),
c.Timestamp.Format("2006/01/02"),
c.Timestamp.Format("15:04:05"),
c.Description,
c.AircraftOverhead.FlightNumber,
c.Activity,
fmt.Sprintf("%v", c.Profile.CcSfo),
}
if err := csvWriter.Write(r); err != nil {
return gcsName, 0, err
}
n++
}
}
csvWriter.Flush()
if err := gcsHandle.Close(); err != nil {
return gcsName, 0, err
}
log.Infof(ctx, "monthly CSV successfully written to %s, %d rows", gcsName, n)
return gcsName, n, nil
}
示例2: monthTaskHandler
func monthTaskHandler(w http.ResponseWriter, r *http.Request) {
//ctx,_ := context.WithTimeout(appengine.NewContext(r), 599*time.Second)
ctx := appengine.NewContext(r)
cdb := complaintdb.ComplaintDB{
//C: oldappengine.NewContext(r),
C: oldappengine.Timeout(oldappengine.NewContext(r), 599*time.Second),
}
year, err := strconv.ParseInt(r.FormValue("year"), 10, 64)
if err != nil {
http.Error(w, "need arg 'year' (2015)", http.StatusInternalServerError)
return
}
month, err := strconv.ParseInt(r.FormValue("month"), 10, 64)
if err != nil {
http.Error(w, "need arg 'month' (1-12)", http.StatusInternalServerError)
return
}
now := date.NowInPdt()
s := time.Date(int(year), time.Month(month), 1, 0, 0, 0, 0, now.Location())
e := s.AddDate(0, 1, 0).Add(-1 * time.Second)
log.Infof(ctx, "Starting /be/month: %s", s)
// One time, at 00:00, for each day of the given month
days := date.IntermediateMidnights(s.Add(-1*time.Second), e)
filename := s.Format("complaints-20060102") + e.Format("-20060102.csv")
gcsHandle, err := gcs.OpenRW(ctx, "serfr0-reports", filename, "text/plain")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
csvWriter := csv.NewWriter(gcsHandle.IOWriter())
cols := []string{
"CallerCode", "Name", "Address", "Zip", "Email", "HomeLat", "HomeLong",
"UnixEpoch", "Date", "Time(PDT)", "Notes", "ActivityDisturbed", "Flightnumber",
"Notes",
// Column names above are incorrect, but BKSV are used to them.
//
//"CallerCode", "Name", "Address", "Zip", "Email", "HomeLat", "HomeLong",
//"UnixEpoch", "Date", "Time(PDT)", "Notes", "Flightnumber",
//"ActivityDisturbed", "CcSFO",
}
csvWriter.Write(cols)
for _, dayStart := range days {
dayEnd := dayStart.AddDate(0, 0, 1).Add(-1 * time.Second)
log.Infof(ctx, " /be/month: %s - %s", dayStart, dayEnd)
iter := cdb.NewIter(cdb.QueryInSpan(dayStart, dayEnd))
for {
c, err := iter.NextWithErr()
if err != nil {
http.Error(w, fmt.Sprintf("iterator failed: %v", err),
http.StatusInternalServerError)
return
}
if c == nil {
break
}
r := []string{
c.Profile.CallerCode,
c.Profile.FullName,
c.Profile.Address,
c.Profile.StructuredAddress.Zip,
c.Profile.EmailAddress,
fmt.Sprintf("%.4f", c.Profile.Lat),
fmt.Sprintf("%.4f", c.Profile.Long),
fmt.Sprintf("%d", c.Timestamp.UTC().Unix()),
c.Timestamp.Format("2006/01/02"),
c.Timestamp.Format("15:04:05"),
c.Description,
c.AircraftOverhead.FlightNumber,
c.Activity,
fmt.Sprintf("%v", c.Profile.CcSfo),
}
if err := csvWriter.Write(r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
csvWriter.Flush()
if err := gcsHandle.Close(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Infof(ctx, "GCS report '%s' successfully written", filename)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(fmt.Sprintf("OK!\nGCS file '%s' written to bucket", filename)))
//.........这里部分代码省略.........