List of usage examples for org.joda.time Duration dividedBy
public Duration dividedBy(long divisor)
From source file:ch.oakmountain.tpa.solver.TrainPathApplicationStatistics.java
License:Apache License
List<String> compileAndGetTrainPathApplicationListRow() throws IOException { HashMap<TrainPathSlot, Vertex> slotVertexHashMap = new HashMap<>(); HashMap<SystemNode, Set<TrainPathSlot>> systemNodeTrainPathSlotHashMap = new HashMap<>(); HashMap<SystemNode, Set<Pair<TrainPathSlot, TrainPathSlot>>> connectionsThroughSystemNode = new HashMap<>(); for (Vertex vertex : dag.getVerticies()) { if (vertex.isLeaf() || vertex.isRoot()) { continue; }/* w w w .j a va2s. co m*/ TrainPathSlot trainPathSlot = dag.getSlotFromVertex(vertex.getLabel()); slotVertexHashMap.put(trainPathSlot, vertex); SystemNode from = trainPathSlot.getFrom(); SystemNode to = trainPathSlot.getTo(); initSystemNodeInMaps(systemNodeTrainPathSlotHashMap, connectionsThroughSystemNode, to); initSystemNodeInMaps(systemNodeTrainPathSlotHashMap, connectionsThroughSystemNode, from); systemNodeTrainPathSlotHashMap.get(from).add(trainPathSlot); for (Vertex child : vertex.getChildren()) { if (vertex.isLeaf() || vertex.isRoot()) { continue; } TrainPathSlot childSlot = dag.getSlotFromVertex(child.getLabel()); Pair<TrainPathSlot, TrainPathSlot> connection = new Pair<TrainPathSlot, TrainPathSlot>( trainPathSlot, childSlot); connectionsThroughSystemNode.get(to).add(connection); } } int minSlotsPerSystemNode = Integer.MAX_VALUE; int maxSlotsPerSystemNode = Integer.MIN_VALUE; for (SystemNode systemNode : systemNodeTrainPathSlotHashMap.keySet()) { Set<TrainPathSlot> succSlots = systemNodeTrainPathSlotHashMap.get(systemNode); int nbSuccSlots = succSlots.size(); maxSlotsPerSystemNode = Math.max(nbSuccSlots, maxSlotsPerSystemNode); minSlotsPerSystemNode = Math.min(nbSuccSlots, minSlotsPerSystemNode); Duration minDwellTime = new Duration(Long.MAX_VALUE); Duration maxDwellTime = Duration.ZERO; Duration totalDwellTime = Duration.ZERO; Set<Pair<TrainPathSlot, TrainPathSlot>> connections = connectionsThroughSystemNode.get(systemNode); String dwellStats = "--"; if (!systemNode.equals(simpleTrainPathApplication.getTo()) && !systemNode.equals(simpleTrainPathApplication.getFrom())) { for (Pair<TrainPathSlot, TrainPathSlot> trainPathSlotTrainPathSlotPair : connections) { Duration dwell = trainPathSlotTrainPathSlotPair.second.getStartTime() .distanceAfter(trainPathSlotTrainPathSlotPair.first.getEndTime()); if (dwell.isShorterThan(Duration.ZERO)) { throw new IllegalStateException(""); } if (dwell.isLongerThan(maxDwellTime)) { maxDwellTime = dwell; } if (dwell.isShorterThan(minDwellTime)) { minDwellTime = dwell; } totalDwellTime = totalDwellTime.plus(dwell); } dwellStats = PeriodicalTimeFrame.formatDuration(minDwellTime) + "/" + PeriodicalTimeFrame.formatDuration(maxDwellTime) + "/" + PeriodicalTimeFrame.formatDuration( totalDwellTime.dividedBy(connectionsThroughSystemNode.get(systemNode).size())); } String timeWindow; if (systemNode.equals(simpleTrainPathApplication.getFrom())) { timeWindow = "[" + simpleTrainPathApplication.getParams().getDepartureLowerBound().toString() + "," + simpleTrainPathApplication.getParams().getDepartureUpperBound().toString() + "]"; } else if (systemNode.equals(simpleTrainPathApplication.getTo())) { timeWindow = "[" + simpleTrainPathApplication.getParams().getArrivalLowerBound().toString() + "," + simpleTrainPathApplication.getParams().getArrivalUpperBound().toString() + "]"; } else { timeWindow = "[arr+ " + PeriodicalTimeFrame.formatDuration(simpleTrainPathApplication.getParams() .getMINIMUM_DWELL_TIME()) + ", arr+" + PeriodicalTimeFrame.formatDuration(simpleTrainPathApplication.getParams() .getHARD_MINIMUM_DWELL_TIME().plus(simpleTrainPathApplication.getParams() .getMAXIMUM_ADDITIONAL_DWELL_TIME(systemNode))) + "]"; } table.writeRow(Arrays.asList( systemNode.getName(), String.valueOf(nbSuccSlots), timeWindow, "[" + PeriodicalTimeFrame .formatDuration(simpleTrainPathApplication.getParams().getMINIMUM_DWELL_TIME()) + "," + PeriodicalTimeFrame.formatDuration(simpleTrainPathApplication.getParams() .getMAXIMUM_ADDITIONAL_DWELL_TIME(systemNode)) + "]", "Min/max/average slots", dwellStats)); } List<String> data = Arrays.asList(simpleTrainPathApplication.getName(), simpleTrainPathApplication.getFrom().getName(), simpleTrainPathApplication.getTo().getName(), simpleTrainPathApplication.getStartTime().toString(), simpleTrainPathApplication.getEndTime().toString(), PeriodicalTimeFrame .formatDuration(simpleTrainPathApplication.getParams().getHARD_MAXIMUM_EARLIER_DEPARTURE()), PeriodicalTimeFrame .formatDuration(simpleTrainPathApplication.getParams().getHARD_MAXIMUM_LATER_ARRIVAL()), PeriodicalTimeFrame .formatDuration(simpleTrainPathApplication.getParams().getHARD_MINIMUM_DWELL_TIME()), String.valueOf(dag.nbPaths()), String.valueOf(dag.getCyclomaticComplexity())); table.finishTable(); return data; }
From source file:com.google.cloud.training.dataanalyst.sandiego.AccidentAlert.java
License:Apache License
@SuppressWarnings("serial") public static void main(String[] args) { MyOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(MyOptions.class); options.setStreaming(true);/*from w w w . j av a2 s . com*/ Pipeline p = Pipeline.create(options); String topic = "projects/" + options.getProject() + "/topics/sandiego"; String avgSpeedTable = options.getProject() + ":demos.accidents"; double RATIO = options.getRatioThreshold(); // if we need to average over 60 minutes and speedup is 30x // then we need to average over 2 minutes of sped-up stream Duration averagingInterval = Duration .millis(Math.round(1000 * 60 * (options.getAveragingInterval() / options.getSpeedupFactor()))); Duration averagingFrequency = averagingInterval.dividedBy(2); // 2 times // in // window System.out.println("Averaging interval = " + averagingInterval); System.out.println("Averaging freq = " + averagingFrequency); // Build the table schema for the output table. List<TableFieldSchema> fields = new ArrayList<>(); fields.add(new TableFieldSchema().setName("timestamp").setType("TIMESTAMP")); fields.add(new TableFieldSchema().setName("latitude").setType("FLOAT")); fields.add(new TableFieldSchema().setName("longitude").setType("FLOAT")); fields.add(new TableFieldSchema().setName("highway").setType("STRING")); fields.add(new TableFieldSchema().setName("direction").setType("STRING")); fields.add(new TableFieldSchema().setName("lane").setType("INTEGER")); fields.add(new TableFieldSchema().setName("speed").setType("FLOAT")); fields.add(new TableFieldSchema().setName("sensorId").setType("STRING")); TableSchema schema = new TableSchema().setFields(fields); PCollection<LaneInfo> laneInfo = p // .apply("GetMessages", PubsubIO.<String>read().topic(topic).withCoder(StringUtf8Coder.of())) // .apply("TimeWindow", Window.into(SlidingWindows// .of(averagingInterval)// .every(averagingFrequency))) // .apply("ExtractData", ParDo.of(new DoFn<String, LaneInfo>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { String line = c.element(); c.output(LaneInfo.newLaneInfo(line)); } })); PCollectionView<Map<String, Double>> avgSpeedLocation = laneInfo // .apply("ByLocation", ParDo.of(new DoFn<LaneInfo, KV<String, Double>>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { LaneInfo info = c.element(); String key = info.getLocationKey(); Double speed = info.getSpeed(); c.output(KV.of(key, speed)); } })) // .apply("AvgByLocation", Mean.perKey()) // .apply("ToView", View.asMap()); PCollection<LaneInfo> accidents = laneInfo.apply("FindSlowdowns", ParDo.withSideInputs(avgSpeedLocation).of(new DoFn<LaneInfo, LaneInfo>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { LaneInfo info = c.element(); String location = info.getLocationKey(); double speed = info.getSpeed(); double avgSpeed = c.sideInput(avgSpeedLocation).get(location); if (speed < RATIO * avgSpeed) { // 50% of average speed? c.output(info); } } })); // accidents.apply("ToBQRow", ParDo.of(new DoFn<LaneInfo, TableRow>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { TableRow row = new TableRow(); LaneInfo info = c.element(); row.set("timestamp", info.getTimestamp()); row.set("latitude", info.getLatitude()); row.set("longitude", info.getLongitude()); row.set("highway", info.getHighway()); row.set("direction", info.getDirection()); row.set("lane", info.getLane()); row.set("speed", info.getSpeed()); row.set("sensorId", info.getSensorKey()); c.output(row); } })) // .apply(BigQueryIO.Write.to(avgSpeedTable)// .withSchema(schema)// .withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND) .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)); p.run(); }
From source file:com.google.cloud.training.dataanalyst.sandiego.AverageSpeeds.java
License:Apache License
@SuppressWarnings("serial") public static void main(String[] args) { MyOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(MyOptions.class); options.setStreaming(true);//from ww w . j a v a2 s. c o m Pipeline p = Pipeline.create(options); String topic = "projects/" + options.getProject() + "/topics/sandiego"; String avgSpeedTable = options.getProject() + ":demos.average_speeds"; // if we need to average over 60 minutes and speedup is 30x // then we need to average over 2 minutes of sped-up stream Duration averagingInterval = Duration .millis(Math.round(1000 * 60 * (options.getAveragingInterval() / options.getSpeedupFactor()))); Duration averagingFrequency = averagingInterval.dividedBy(2); // 2 times // in // window System.out.println("Averaging interval = " + averagingInterval); System.out.println("Averaging freq = " + averagingFrequency); // Build the table schema for the output table. List<TableFieldSchema> fields = new ArrayList<>(); fields.add(new TableFieldSchema().setName("timestamp").setType("TIMESTAMP")); fields.add(new TableFieldSchema().setName("latitude").setType("FLOAT")); fields.add(new TableFieldSchema().setName("longitude").setType("FLOAT")); fields.add(new TableFieldSchema().setName("highway").setType("STRING")); fields.add(new TableFieldSchema().setName("direction").setType("STRING")); fields.add(new TableFieldSchema().setName("lane").setType("INTEGER")); fields.add(new TableFieldSchema().setName("speed").setType("FLOAT")); fields.add(new TableFieldSchema().setName("sensorId").setType("STRING")); TableSchema schema = new TableSchema().setFields(fields); PCollection<LaneInfo> laneInfo = p // .apply("GetMessages", PubsubIO.<String>read().topic(topic).withCoder(StringUtf8Coder.of())) // .apply("TimeWindow", Window.into(SlidingWindows// .of(averagingInterval)// .every(averagingFrequency))) // .apply("ExtractData", ParDo.of(new DoFn<String, LaneInfo>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { String line = c.element(); c.output(LaneInfo.newLaneInfo(line)); } })); PCollection<KV<String, Double>> avgSpeed = laneInfo // .apply("BySensor", ParDo.of(new DoFn<LaneInfo, KV<String, Double>>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { LaneInfo info = c.element(); String key = info.getSensorKey(); Double speed = info.getSpeed(); c.output(KV.of(key, speed)); } })) // .apply("AvgBySensor", Mean.perKey()); avgSpeed.apply("ToBQRow", ParDo.of(new DoFn<KV<String, Double>, TableRow>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { TableRow row = new TableRow(); String stationKey = c.element().getKey(); Double speed = c.element().getValue(); String line = Instant.now().toString() + "," + stationKey + "," + speed; // CSV LaneInfo info = LaneInfo.newLaneInfo(line); row.set("timestamp", info.getTimestamp()); row.set("latitude", info.getLatitude()); row.set("longitude", info.getLongitude()); row.set("highway", info.getHighway()); row.set("direction", info.getDirection()); row.set("lane", info.getLane()); row.set("speed", info.getSpeed()); row.set("sensorId", info.getSensorKey()); c.output(row); } })) // .apply(BigQueryIO.Write.to(avgSpeedTable)// .withSchema(schema)// .withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND) .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)); p.run(); }
From source file:com.google.cloud.training.flights.AverageDelayPipeline.java
License:Apache License
private static WindowStats movingAverageOf(MyOptions options, Pipeline p, String event) { // if we need to average over 60 minutes and speedup is 30x // then we need to average over 2 minutes of sped-up stream Duration averagingInterval = Duration .millis(Math.round(1000 * 60 * (options.getAveragingInterval() / options.getSpeedupFactor()))); Duration averagingFrequency = averagingInterval.dividedBy(2); // 2 times in window System.out.println("Averaging interval = " + averagingInterval); System.out.println("Averaging freq = " + averagingFrequency); String topic = "projects/" + options.getProject() + "/topics/" + event; final FieldNumberLookup eventType = FieldNumberLookup.create(event); PCollection<Flight> flights = p // .apply(event + ":read", // PubsubIO.<String>read().topic(topic).withCoder(StringUtf8Coder.of())) // .apply(event + ":window", Window.into(SlidingWindows// .of(averagingInterval)// .every(averagingFrequency))) // .apply(event + ":parse", ParDo.of(new DoFn<String, Flight>() { @ProcessElement/*from w ww. j a v a 2 s . c o m*/ public void processElement(ProcessContext c) throws Exception { try { String line = c.element(); Flight f = new Flight(line.split(","), eventType); c.output(f); } catch (NumberFormatException e) { // ignore errors about empty delay fields ... } } })); WindowStats stats = new WindowStats(); stats.delay = flights // .apply(event + ":airportdelay", ParDo.of(new DoFn<Flight, KV<Airport, Double>>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { Flight stats = c.element(); c.output(KV.of(stats.airport, stats.delay)); // delay at airport } }))// .apply(event + ":avgdelay", Mean.perKey()); stats.timestamp = flights // .apply(event + ":timestamps", ParDo.of(new DoFn<Flight, KV<Airport, String>>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { Flight stats = c.element(); c.output(KV.of(stats.airport, stats.timestamp)); } }))// .apply(event + ":lastTimeStamp", Max.perKey()); stats.num_flights = flights // .apply(event + ":numflights", ParDo.of(new DoFn<Flight, KV<Airport, Integer>>() { @ProcessElement public void processElement(ProcessContext c) throws Exception { Flight stats = c.element(); c.output(KV.of(stats.airport, 1)); } }))// .apply(event + ":total", Sum.integersPerKey()); return stats; }
From source file:me.tongfei.progressbar.ProgressThread.java
License:Apache License
String eta(Duration elapsed) { if (progress.max <= 0 || progress.indefinite) return "?"; else if (progress.current == 0) return "?"; else// ww w .j a v a2s. co m return Util.formatDuration( elapsed.dividedBy(progress.current).multipliedBy(progress.max - progress.current)); }
From source file:ru.codemine.ccms.dao.TaskDAOImpl.java
License:Open Source License
@Override public Period getMidTimeByPerformer(Employee performer, LocalDate startDate, LocalDate endDate) { List<Task> tasks = getByPerformerAndCloseTimeInPeriod(performer, startDate, endDate); if (tasks.isEmpty()) return new Period(0); Duration totalDuration = new Duration(0); for (Task task : tasks) { Duration d = new Duration(task.getCreationTime(), task.getCloseTime()); totalDuration = totalDuration.plus(d); }// w w w . j av a2s . c o m Duration resultDuration = totalDuration.dividedBy(tasks.size()); return resultDuration.toPeriod(); }