func bksvSubmitUserHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) cdb := complaintdb.ComplaintDB{C: c, Memcache: true} start, end := date.WindowForYesterday() bksv_ok, bksv_not_ok := 0, 0 email := r.FormValue("user") if cp, err := cdb.GetProfileByEmailAddress(email); err != nil { c.Errorf(" /bksv/submit-user(%s): getprofile: %v", email, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } else if complaints, err := cdb.GetComplaintsInSpanByEmailAddress(email, start, end); err != nil { c.Errorf(" /bksv/submit-user(%s): getcomplaints: %v", email, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } else { for i, complaint := range complaints { time.Sleep(time.Millisecond * 200) if debug, err := bksv.PostComplaint(urlfetch.Client(c), *cp, complaint); err != nil { //cdb.C.Infof("pro: %v", cp) //cdb.C.Infof("comp: %#v", complaint) cdb.C.Errorf("BKSV posting error: %v", err) cdb.C.Infof("BKSV Debug\n------\n%s\n------\n", debug) bksv_not_ok++ } else { if i == 0 { cdb.C.Infof("BKSV [OK] Debug\n------\n%s\n------\n", debug) } bksv_ok++ } } } c.Infof("bksv for %s, %d/%d", email, bksv_ok, bksv_not_ok) if bksv_not_ok > 0 { c.Errorf("bksv for %s, %d/%d", email, bksv_ok, bksv_not_ok) } w.Write([]byte("OK")) }
// Examine all users. If they had any complaints, throw them in the queue. func bksvScanYesterdayHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) cdb := complaintdb.ComplaintDB{C: c, Memcache: true} var cps = []types.ComplainerProfile{} cps, err := cdb.GetAllProfiles() if err != nil { c.Errorf(" /bksv/scan-yesterday: getallprofiles: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } start, end := date.WindowForYesterday() bksv_ok := 0 for _, cp := range cps { if cp.CcSfo == false { continue } var complaints = []types.Complaint{} complaints, err = cdb.GetComplaintsInSpanByEmailAddress(cp.EmailAddress, start, end) if err != nil { c.Errorf(" /bksv/scan-yesterday: getbyemail(%s): %v", cp.EmailAddress, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if len(complaints) > 0 { t := taskqueue.NewPOSTTask("/bksv/submit-user", map[string][]string{ "user": {cp.EmailAddress}, }) if _, err := taskqueue.Add(c, t, "submitreports"); err != nil { c.Errorf(" /bksv/scan-yesterday: enqueue: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } bksv_ok++ } } c.Infof("enqueued %d bksv", bksv_ok) w.Write([]byte(fmt.Sprintf("OK, enqueued %d", bksv_ok))) }
func emailHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) session := sessions.Get(r) cdb := complaintdb.ComplaintDB{C: c} cp, err := cdb.GetProfileByEmailAddress(session.Values["email"].(string)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } start, end := date.WindowForYesterday() // end = time.Now() complaints, err2 := cdb.GetComplaintsInSpanByEmailAddress(cp.EmailAddress, start, end) if err2 != nil { http.Error(w, err2.Error(), http.StatusInternalServerError) return } var cap = types.ComplaintsAndProfile{ Profile: *cp, Complaints: complaints, } if err := templates.ExecuteTemplate(w, "email-update", map[string]interface{}{}); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } else { return } msg, err3 := GenerateEmail(c, cap) if err3 != nil { http.Error(w, err3.Error(), http.StatusInternalServerError) return } cap.Profile.CcSfo = true if len(cap.Complaints) == 0 { http.Error(w, "No complaints found ?!", http.StatusInternalServerError) return } msg2, err4 := GenerateSingleComplaintEmail(c, cap.Profile, cap.Complaints[len(cap.Complaints)-1]) if err4 != nil { http.Error(w, err4.Error(), http.StatusInternalServerError) return } var params = map[string]interface{}{ "Cap": cap, "EmailBundle": msg, "EmailSingle": msg2, "EmailBundleBody": template.HTML(msg.HTMLBody), "EmailSingleBody": template.HTML(msg2.HTMLBody), } /* if err4 := sendViaHTTPGateway(c, msg); err4 != nil { http.Error(w, err4.Error(), http.StatusInternalServerError) return } */ if err := templates.ExecuteTemplate(w, "email-debug", params); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
func SendComplaintsWithSpan(c appengine.Context, start, end time.Time) (err error) { c.Infof("--- Emails, %s -> %s", start, end) blacklist := map[string]bool{} for _, e := range blacklistAddrs { blacklist[e] = true } cdb := complaintdb.ComplaintDB{C: c, Memcache: true} var cps = []types.ComplainerProfile{} cps, err = cdb.GetAllProfiles() if err != nil { return } complaints_private, complaints_submitted, no_data, sent_ok, sent_fail := 0, 0, 0, 0, 0 sent_single_ok, sent_single_fail := 0, 0 for _, cp := range cps { var complaints = []types.Complaint{} complaints, err = cdb.GetComplaintsInSpanByEmailAddress(cp.EmailAddress, start, end) if err != nil { c.Errorf("Could not get complaints [%v->%v] for <%s>: %v", start, end, cp.EmailAddress, err) no_data++ continue } if len(complaints) == 0 { no_data++ continue } // Emailing disabled; last run was Fri Oct 9, 4am, with data for Oct 8. BKSV is now live. if false { if cp.CcSfo == true { for _, complaint := range complaints { if msg, err := GenerateSingleComplaintEmail(c, cp, complaint); err != nil { c.Errorf("Could not generate single email to <%s>: %v", cp.EmailAddress, err) sent_single_fail++ continue } else { if blacklist[cp.EmailAddress] { sent_single_fail++ } else { if err := mail.Send(c, msg); err != nil { c.Errorf("Could not send email to <%s>: %v", cp.EmailAddress, err) sent_single_fail++ continue } else { sent_single_ok++ } } } } } } var cap = types.ComplaintsAndProfile{ Profile: cp, Complaints: complaints, } var msg *mail.Message if msg, err = GenerateEmail(c, cap); err != nil { c.Errorf("Could not generate email to <%s>: %v", cp.EmailAddress, err) sent_fail++ continue } useGateway := false if useGateway { if err = sendViaHTTPGateway(c, msg); err != nil { c.Errorf("Could not gateway email to <%s>: %v", cp.EmailAddress, err) sent_fail++ continue } } else { if blacklist[cp.EmailAddress] { sent_fail++ } else { if err = mail.Send(c, msg); err != nil { c.Errorf("Could not send email to <%s>: %v", cp.EmailAddress, err) sent_fail++ continue } } } if cap.Profile.CcSfo == true { complaints_submitted += len(cap.Complaints) } else { complaints_private += len(cap.Complaints) } sent_ok++ } subject := fmt.Sprintf("Daily report stats: users:%d/%d reports:%d/%d emails:%d:%d", sent_ok, (sent_ok + no_data), complaints_submitted, (complaints_submitted + complaints_private), sent_single_ok, sent_single_fail) SendEmailToAdmin(c, subject, "") dc := complaintdb.DailyCount{ Datestring: date.Time2Datestring(start.Add(time.Hour)), NumComplaints: complaints_submitted + complaints_private, NumComplainers: sent_ok, } cdb.AddDailyCount(dc) c.Infof("--- email wrapup: %d ok, %d fail (%d no data) : %d reports submitted (%d kept back) single[%d/%d]", sent_ok, sent_fail, no_data, complaints_submitted, complaints_private, sent_single_ok, sent_single_fail) return }