public void schedule(User user, int dashId, Report report, long delayInSeconds) { schedule( new PeriodicReportTask(user, dashId, report, this), delayInSeconds, TimeUnit.SECONDS ); }
protected BaseReportTask(User user, int dashId, Report report, MailWrapper mailWrapper, ReportingDiskDao reportingDiskDao, String downloadUrl) { this.key = new ReportTaskKey(user, dashId, report.id); this.report = report; this.mailWrapper = mailWrapper; this.reportingDiskDao = reportingDiskDao; this.downloadUrl = downloadUrl; }
@Override public int getPrice() { return Report.getPrice() * reports.length; }
ReportingWidget reportingWidget = (ReportingWidget) widget; for (Report report : reportingWidget.reports) { if (report.isValid() && report.isPeriodic() && report.isActive) { try { long now = System.currentTimeMillis(); initialDelaySeconds = 0; } else { initialDelaySeconds = report.calculateDelayInSeconds(); log.trace("Adding periodic report for user {} with delay {} to scheduler.", user.email, initialDelaySeconds); report.nextReportAt = now + initialDelaySeconds * 1000; schedule(user, dashBoard.id, report, initialDelaySeconds); counter++; } catch (IllegalCommandBodyException e) {
int existingReportIndex = reportingWidget.getReportIndexById(reportId); if (existingReportIndex == -1) { throw new IllegalCommandException("Cannot find report with provided id."); user.addEnergy(Report.getPrice()); reportingWidget.reports = ArrayUtil.remove(reportingWidget.reports, existingReportIndex, Report.class); dash.updatedAt = System.currentTimeMillis(); if (reportToDel.isPeriodic()) { ReportScheduler reportScheduler = holder.reportScheduler; boolean isRemoved = reportScheduler.cancelStoredFuture(user, dashId, reportId); log.debug("Deleting reportId {} in scheduler for {}. Is removed: {}?.", reportToDel.id, user.email, isRemoved);
Report report = reportingWidget.getReportById(reportId); if (report == null) { throw new IllegalCommandException("Cannot find report with passed id."); if (!report.isValid()) { log.debug("Report is not valid {} for {}.", report, user.email); throw new IllegalCommandException("Report is not valid."); reportScheduler.schedule(new BaseReportTask(user, dashId, report, reportScheduler.mailWrapper, reportScheduler.reportingDao, reportScheduler.downloadUrl) {
private void reschedule(long reportFinishedAt) { long initialDelaySeconds = report.calculateDelayInSeconds(); report.nextReportAt = reportFinishedAt + initialDelaySeconds * 1000; //rescheduling report log.info("Rescheduling report for {} with delay {}.", key.user.email, initialDelaySeconds); reportScheduler.schedule(this, initialDelaySeconds, TimeUnit.SECONDS); } }
private ReportResult generateReport(Path userCsvFolder, Profile profile, DashBoard dash, long now) throws Exception { int fetchCount = (int) report.reportType.getFetchCount(report.granularityType); long startFrom = now - TimeUnit.DAYS.toMillis(report.reportType.getDuration()); //truncate second, minute, hour, depending of granularity in order to do not filter first point. //https://github.com/blynkkk/blynk-server/issues/1149 startFrom = (startFrom / report.granularityType.period) * report.granularityType.period; Path output = Paths.get(userCsvFolder.toString() + ".zip"); boolean hasData = generateReport(output, profile, dash, fetchCount, startFrom); if (hasData) { sendEmail(output); return ReportResult.OK; } log.info("No data for report for user {} and reportId {}.", key.user.email, report.id); return ReportResult.NO_DATA; }
private boolean generateReport(Path output, Profile profile, DashBoard dash, int fetchCount, long startFrom) throws Exception { switch (report.reportOutput) { case MERGED_CSV: return merged(output, profile, dash, fetchCount, startFrom); case CSV_FILE_PER_DEVICE: return filePerDevice(output, profile, dash, fetchCount, startFrom); case CSV_FILE_PER_DEVICE_PER_PIN: case EXCEL_TAB_PER_DEVICE: default: return filePerDevicePerPin(output, profile, dash, fetchCount, startFrom); } }
public ReportScheduler(int corePoolSize, String downloadUrl, MailWrapper mailWrapper, ReportingDiskDao reportingDao, Map<UserKey, User> users) { super(corePoolSize, BlynkTPFactory.build("report")); setRemoveOnCancelPolicy(true); setExecuteExistingDelayedTasksAfterShutdownPolicy(false); this.map = new ConcurrentHashMap<>(); this.downloadUrl = downloadUrl; this.mailWrapper = mailWrapper; this.reportingDao = reportingDao; init(users); }
private void sendEmail(Path output) throws Exception { String durationLabel = report.reportType.getDurationLabel().toLowerCase(); String subj = "Your " + durationLabel + " " + report.getReportName() + " is ready"; String gzipDownloadUrl = downloadUrl + output.getFileName(); String dynamicSection = report.buildDynamicSection(); mailWrapper.sendReportEmail(report.recipients, subj, gzipDownloadUrl, dynamicSection); }
@Override public void run() { try { long finishedAt = generateReport(); report.lastReportAt = finishedAt; reschedule(finishedAt); log.debug("After rescheduling: {}", report); } catch (IllegalCommandBodyException ice) { log.info("Seems like report is expired for {}.", key.user.email); report.lastRunResult = ReportResult.EXPIRED; } catch (Exception e) { log.debug("Error generating report {} for {}.", report, key.user.email, e); } }
String buildDynamicSection() { StringBuilder sb = new StringBuilder(); sb.append("Report name: ").append(getReportName()).append("<br>"); reportType.buildDynamicSection(sb, tzName); return sb.toString(); }
public void validateId(int id) { Report report = getReportById(id); if (report != null) { throw new IllegalCommandException("Report with passed id already exists."); } }
BlockingIOStat(BlockingIOProcessor blockingIOProcessor, ReportScheduler reportScheduler) { this(blockingIOProcessor.messagingExecutor.getQueue().size(), blockingIOProcessor.messagingExecutor.getCompletedTaskCount(), blockingIOProcessor.historyExecutor.getQueue().size(), blockingIOProcessor.historyExecutor.getCompletedTaskCount(), blockingIOProcessor.dbExecutor.getQueue().size(), blockingIOProcessor.dbExecutor.getCompletedTaskCount(), blockingIOProcessor.dbGetServerExecutor.getQueue().size(), blockingIOProcessor.dbGetServerExecutor.getCompletedTaskCount(), reportScheduler.getQueue().size(), reportScheduler.getCompletedTaskCount(), reportScheduler.map.size() ); }
@Override public void run() { try { report.lastReportAt = generateReport(); log.debug(report); } catch (Exception e) { log.debug("Error generating report {} for {}.", report, key.user.email, e); } }
public Widget getWidgetWithLoggedPin(DashBoard dash, int deviceId, short pin, PinType pinType) { for (Widget widget : dash.widgets) { if (widget instanceof Superchart) { Superchart graph = (Superchart) widget; if (isWithinGraph(dash, graph, pin, pinType, deviceId)) { return graph; } } if (widget instanceof DeviceTiles) { DeviceTiles deviceTiles = (DeviceTiles) widget; for (TileTemplate tileTemplate : deviceTiles.templates) { for (Widget tilesWidget : tileTemplate.widgets) { if (tilesWidget instanceof Superchart) { Superchart graph = (Superchart) tilesWidget; if (isWithinGraph(dash, graph, pin, pinType, deviceId, tileTemplate.deviceIds)) { return graph; } } } } } if (widget instanceof ReportingWidget) { ReportingWidget reportingWidget = (ReportingWidget) widget; if (reportingWidget.hasPin(pin, pinType)) { return reportingWidget; } } } return null; }
@Override public void run() { try { report.lastReportAt = generateReport(); if (ctx.channel().isWritable()) { ctx.writeAndFlush( makeUTF8StringMessage(EXPORT_REPORT, message.id, report.toString()), ctx.voidPromise() ); } } catch (Exception e) { log.debug("Error generating export report {} for {}.", report, key.user.email, e); ctx.writeAndFlush(illegalCommand(message.id), ctx.voidPromise()); } } }, 0, TimeUnit.SECONDS);
public boolean cancelStoredFuture(User user, int dashId, int reportId) { ReportTaskKey key = new ReportTaskKey(user, dashId, reportId); ScheduledFuture<?> scheduledFuture = map.remove(key); if (scheduledFuture == null) { return false; } return scheduledFuture.cancel(true); }
protected long generateReport() { long now = System.currentTimeMillis(); String date = LocalDate.now(report.tzName).toString(); Path userCsvFolder = FileUtils.getUserReportDir( key.user.email, key.user.appName, key.reportId, date); try { Profile profile = key.user.profile; DashBoard dash = profile.getDashByIdOrThrow(key.dashId); report.lastRunResult = generateReport(userCsvFolder, profile, dash, now); } catch (IllegalCommandException illegalState) { report.lastRunResult = ReportResult.ERROR; log.debug("Dashboard is not exists anymore for the report {} for user {}. ", report.id, key.user.email); } catch (Exception e) { report.lastRunResult = ReportResult.ERROR; log.error("Error generating report {} for user {}. ", report.id, key.user.email); log.error("Error: ", e); } long newNow = System.currentTimeMillis(); log.info("Processed report for {}, time {} ms.", key.user.email, newNow - now); return newNow; }