// 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 SendEmailToAllUsers(c appengine.Context, subject string) int { cdb := complaintdb.ComplaintDB{C: c} if cps, err := cdb.GetAllProfiles(); err != nil { c.Errorf("SendEmailToAllUsers/GetAllProfiles: %v", err) return 0 } else { buf := new(bytes.Buffer) params := map[string]interface{}{} if err := templates.ExecuteTemplate(buf, "email-update", params); err != nil { return 0 } n := 0 for _, cp := range cps { // This message update goes only to the opt-outers ... if cp.CcSfo == true && cp.CallerCode != "WOR005" { continue } msg := &mail.Message{ Sender: kSenderEmail, ReplyTo: kSenderEmail, To: []string{cp.EmailAddress}, Subject: subject, HTMLBody: buf.String(), } if err := mail.Send(c, msg); err != nil { c.Errorf("Could not send useremail to <%s>: %v", cp.EmailAddress, err) } n++ } return n } }
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 }