Skip to content

Commit

Permalink
schedule graph rebuilds. #9.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Conway committed Sep 7, 2012
1 parent c20fa39 commit ef496a7
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 9 deletions.
22 changes: 19 additions & 3 deletions app/deployment/DeploymentPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ the License, or (props, at your option) any later version.

import play.Play;
import play.libs.WS;
import play.modules.spring.Spring;

import models.BikeRentalSystem;
import models.BikeRentalSystemType;
Expand All @@ -53,6 +54,7 @@ public class DeploymentPlan {
private SimpleDateFormat isoDate;
private Calendar calendar;
private FeedDescriptor[] feeds;
private int window;

/**
* Create a plan for the given metro at the current time and for the default window.
Expand Down Expand Up @@ -80,6 +82,7 @@ public DeploymentPlan(MetroArea area, Date date) {
*/
public DeploymentPlan(MetroArea area, Date date, int window) {
this.area = area;
this.window = window;
this.calendar = Calendar.getInstance(gmt);
this.isoDate = new SimpleDateFormat("yyyy-MM-dd");
this.startDate = date;
Expand All @@ -89,6 +92,10 @@ public DeploymentPlan(MetroArea area, Date date, int window) {

Set<FeedDescriptor> toInclude = new HashSet<FeedDescriptor>();

// Clear all the scheduled rebuilds of this area; if still applicable, they will be
// recreated automatically.
DeploymentPlanScheduler.clearRebuilds(this.area);

for (NtdAgency agency : area.agencies) {
// all the unsuperseded feeds for this agency

Expand All @@ -103,10 +110,10 @@ public DeploymentPlan(MetroArea area, Date date, int window) {

addFeeds(agency.name + "_" + agency.id, feed, toInclude);
}

this.feeds = new FeedDescriptor[toInclude.size()];
this.feeds = toInclude.toArray(this.feeds);
}

this.feeds = new FeedDescriptor[toInclude.size()];
this.feeds = toInclude.toArray(this.feeds);
}

/**
Expand Down Expand Up @@ -160,6 +167,15 @@ private void addFeeds(String agency, GtfsFeed feed, Set<FeedDescriptor> toInclud
if (!toInclude.add(fd))
return;
}
else {
// the feed starts after the end of the window, so it shouldn't be included, but
// the graph needs to be rebuilt on the day it comes into the window.
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("gmt"));
cal.setTime(feed.startDate);
// - 1 so it will be sure to rebuild
cal.add(Calendar.DAY_OF_YEAR, -this.window - 1);
DeploymentPlanScheduler.scheduleRebuild(feed, cal.getTime());
}

olderFeed = GtfsFeed.find(
"supersededBy = ? ORDER BY startDate DESC WHERE status <> 'FAILED'",
Expand Down
81 changes: 80 additions & 1 deletion test/DeploymentPlanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ the License, or (props, at your option) any later version.

import deployment.DeploymentPlan;
import deployment.DeploymentPlan.FeedDescriptor;
import deployment.DeploymentPlanScheduler;

import java.text.SimpleDateFormat;
import java.util.*;

import play.db.jpa.JPA;
import play.test.*;
import models.*;

Expand All @@ -28,6 +32,8 @@ public class DeploymentPlanTest extends UnitTest {
public void setUp () {
// TODO: this seems to only delete the ones loaded from fixtures.
Fixtures.deleteAllModels();
JPA.em().getTransaction().rollback();
JPA.em().getTransaction().begin();
Fixtures.loadModels("planner.yml");
}

Expand Down Expand Up @@ -106,7 +112,7 @@ public void testMultipleConcurrentFeeds () {
* common feed being split to uncommon feeds at different times; keep in mind that "proper"
* is a relative term here.
*/
//@Test
@Test
public void testCommonFeedSplit () {
// note that this implicitly tests Unicode in the DB.
MetroArea sb = MetroArea.find("byName", "Santa Bárbara, CA").first();
Expand Down Expand Up @@ -178,4 +184,77 @@ else if ("lacombined2".equals(fd.getFeedId())) {
assertTrue(comb2Found);
assertEquals(2, feeds.length);
}

/**
* Test the case of a GTFS feed beyond the window; make sure it is both (a) not included in the
* current build and (b) there is a scheduled rebuild of the graph
*/
@Test
public void testDeploymentScheduler () {
MetroArea chi = MetroArea.find("byName", "Chicago, IL").first();
assertNotNull(chi);

DeploymentPlan dp = new DeploymentPlan(chi, getDate(2012, 6, 15), 10);

FeedDescriptor[] feeds = dp.getFeeds();

boolean ctaFound = false, cta2Found = false, metraFound = false;

for (FeedDescriptor feed : feeds) {
if ("cta1".equals(feed.getFeedId())) {
assertTrue(!ctaFound);
ctaFound = true;
}
else if ("cta2".equals(feed.getFeedId())) {
assertTrue(!cta2Found);
cta2Found = true;
}
else if ("metra".equals(feed.getFeedId())) {
assertTrue(!metraFound);
metraFound = true;
}
else {
assertTrue(false);
}
}

assertTrue(ctaFound);
assertTrue(!metraFound);
assertTrue(!cta2Found);
assertEquals(1, feeds.length);

// and make sure an entry was generated for it in the rebuild table
List<Date> rebuilds = DeploymentPlanScheduler.getRebuildsForMetro(chi);
SimpleDateFormat dates = new SimpleDateFormat("yyyy-MM-dd");

// one for CTA, on for Metra
assertEquals(2, rebuilds.size());

Collections.sort(rebuilds);

// CTA: 2012-10-15, less 10 days for the window, less 1 day to get the previous day
assertEquals("2012-10-04", dates.format(rebuilds.get(0)));

// Metra
assertEquals("2012-11-04", dates.format(rebuilds.get(1)));

// and clear them all to make sure they go away
DeploymentPlanScheduler.clearRebuilds(chi);

rebuilds = DeploymentPlanScheduler.getRebuildsForMetro(chi);
assertEquals(0, rebuilds.size());
}

/**
* Get a date
* @param year The year, as you would expect, e.g. 2012
* @param month The month, 1 based, i.e. 1 == January, 6 == June
* @param day The day of the month, 1 - 31
* @return A date representation of the given day
*/
public static Date getDate (int year, int month, int day) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("gmt"));
cal.set(year, month, day);
return cal.getTime();
}
}
38 changes: 33 additions & 5 deletions test/planner.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
MetroArea(sf):
name: San Francisco, CA

GtfsFeed(bart3):
startDate: 2012-12-10
expirationDate: 2013-4-10
Expand All @@ -25,14 +22,16 @@ GtfsFeed(muni):

NtdAgency(bart):
url: http://www.bart.gov
metroArea: sf
feeds: [bart1, bart2, bart3]

NtdAgency(muni):
url: http://example.com
metroArea: sf
feeds: [muni]

MetroArea(sf):
name: San Francisco, CA
agencies: [muni, bart]

GtfsFeed(sbmtd):
startDate: 2012-11-01
expirationDate: 2014-10-10
Expand Down Expand Up @@ -84,6 +83,35 @@ MetroArea(la):
name: Los Angeles, CA
agencies: [metro, metrolink]

# Chicago: used to test feeds beyond the window
GtfsFeed(cta2):
startDate: 2012-10-15Z
expirationDate: 2014-01-01Z
storedId: cta2

GtfsFeed(cta1):
startDate: 2012-01-01Z
expirationDate: 2013-03-01Z
storedId: cta1
supersededBy: cta2

GtfsFeed(metra):
startDate: 2012-11-15Z
expirationDate: 2014-01-01Z
storedId: metra

NtdAgency(cta):
url: http://www.transitchicago.com
feeds: [cta1, cta2]

NtdAgency(metra):
url: http://www.metrarail.com
feeds: [metra]

MetroArea(chi):
name: Chicago, IL
agencies: [cta, metra]

BikeRentalSystem:
type: CITYBIKES

0 comments on commit ef496a7

Please sign in to comment.