Java tutorial
package siddur.solidtrust.scrape; import java.text.MessageFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.mail.internet.MimeMessage; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.apache.commons.lang3.time.DateUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import siddur.solidtrust.SolidtrustConstants; @Service public class ScrapeMonitor { private static final int DAILY_MARKTPLAATS = 4500; //9000; private static final int DAILY_AUTOSCOUT_NL = 125; //250; private static final Logger log4j = Logger.getLogger(ScrapeMonitor.class); @PersistenceContext EntityManager em; @Autowired JavaMailSenderImpl mailSender; /* * [2016/8/5 18:53:45] benveenman: No, I just want to know how many licence plates with mileages are saved every day in the marktplaats and autoscout database. So for example 3000 per day. ( just calculate the average from the past month ) Then every day you check if at least half of that number of cars is collected by the scrapers. When the amount of mileages of plates that is saved per day drops under 1500 then this should send an alert to us so we can check what is wrong with the scraper or our system. [2016/8/5 18:56:35] benveenman: With the owners I mean the number of owners that are saved from license plates that we collect from marktplaats and autoscout. This is actually the same as counting the number of mileages. So it is just one number per day that we need to count. */ public void alert() { Date start = DateUtils.truncate(new Date(), Calendar.DATE); //NewMarktplaats String ql = "from ScrapeLog where endAt > :start"; List<ScrapeLog> logs = em.createQuery(ql, ScrapeLog.class).setParameter("start", start).getResultList(); int marktplaats = 0, autoscout = 0; for (ScrapeLog log : logs) { String table = log.getTablename(); if (table.equals("NewMarktplaats")) { marktplaats = log.getAmount(); } else if (table.equals("AutoscoutNl")) { autoscout = log.getAmount(); } } boolean alert1 = marktplaats < DAILY_MARKTPLAATS; boolean alert2 = autoscout < DAILY_AUTOSCOUT_NL; send(alert1, alert2, marktplaats, autoscout); } private void send(boolean alert1, boolean alert2, int marktplaats, int autoscout) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("gbg1_spsms_gtrak@pactera-pgs-mail.chinacloudapp.cn", "Solidtrust Admin"); if (alert1 || alert2) { log4j.info("Send email to alert that data scraped very little"); helper.setTo(SolidtrustConstants.BEN_EMAIL); helper.setCc(SolidtrustConstants.MY_EMAIL); } else { helper.setTo(SolidtrustConstants.MY_EMAIL); } helper.setSubject("Little data scraped alert"); StringBuilder sb = new StringBuilder(); sb.append( "<html><body><table border='1' cellspacing='0'><tr><td>Source</td><td>Amount Today</td><td>Normal amount daily</td></tr>"); sb.append("<tr><td>Martplaats</td><td>{0}</td><td>9000</td></tr>"); sb.append("<tr><td>AutoscoutNl</td><td>{1}</td><td>250</td></tr>"); sb.append("</table></body></html>"); helper.setText(MessageFormat.format(sb.toString(), alert1 ? "<font color='red'>" + marktplaats + "</font>" : marktplaats, alert2 ? "<font color='red'>" + autoscout + "</font>" : autoscout), true); mailSender.send(message); } catch (Exception e) { log4j.error(e.getMessage(), e); } } }