コード例 #1
0
ファイル: tests.py プロジェクト: dingens/zwl
    def test_get_train_ids_within_timeframe(self):
        ids = trains.get_train_ids_within_timeframe(time(15, 40), time(16, 00),
                                                    get_lineconfig('sample'))
        assert self.t1.id in ids
        assert self.t3.id not in ids

        ids = trains.get_train_ids_within_timeframe(time(15, 00),
                                                    time(15, 36),
                                                    get_lineconfig('sample'),
                                                    startpos=0,
                                                    endpos=.2)
        self.assertEqual(ids, [])
コード例 #2
0
ファイル: tests.py プロジェクト: dingens/zwl
    def test_locations_extended_between(self):
        line = get_lineconfig('sample')
        locs = list(line.locations_extended_between())
        assert locs == line.locations

        locs = line.locations_extended_between(.4, .4)
        assert [l.id for l in locs] == ['XCE#1', 'XLG#1']

        locs = line.locations_extended_between(.31, .55)
        assert [l.id for l in locs] == ['XCE#1', 'XLG#1', 'XBG#2']

        # simulate floating point errors in javascript
        locs = line.locations_extended_between(.2999999999999, .6000000000001)
        assert [l.id for l in locs] == ['XCE#1', 'XLG#1', 'XBG#2']
コード例 #3
0
def get_train_information(trains, line):
    """
    Get information and timetable about all given trains.
    If line is given, limit timetable information to locations on that line.
    Note that trains that only "touch" one location on the line are not
    included (for example, trains that start on the last stop of the line)

    @param trains: List of train ids or `Train` objects.
    @param line: `Line` object or line id.
    """
    line = get_lineconfig(line)

    if not trains:
        return

    # fetch all trains and create a lookup dict of the form {id: Train}
    #TODO: try to joinedload transition_{from,to}
    trains = dict(
        db.session.query(Train.id, Train).filter(
            Train.id.in_(
                (t if isinstance(t, (int, long)) else t.id) for t in trains)))

    # fetch all timetable entries we need in one query, sort them apart locally
    timetable_entries = TimetableEntry.query \
        .filter(TimetableEntry.train_id.in_(trains.keys())) \
        .order_by(TimetableEntry.sorttime).all()
    timetables = defaultdict(list)
    for row in timetable_entries:
        timetables[row.train_id].append(row)

    for tid, train in trains.items():
        segments = make_timetable(train, timetables[tid], line)

        if not segments:
            continue

        yield {
            'id': train.id,
            'type': train.type,
            'category': train.category,
            'nr': train.nr,
            'segments': segments,
            'transition_to': train.transition_to_nr,
            'transition_from': train.transition_from_nr,
            'comment': u'',
            'start': timetables[tid][0].loc,
            'end': timetables[tid][-1].loc,
        }
コード例 #4
0
ファイル: tests.py プロジェクト: dingens/zwl
    def test_get_train_information(self):
        res = list(
            trains.get_train_information([self.t1], get_lineconfig('sample')))

        assert len(res) == 1
        inf = res[0]
        assert inf['nr'] == 700
        assert inf['type'] == 'ICE'
        assert len(inf['segments']) == 2

        assert sorted([['XDE#1', 'XCE#1'], ['XLG#1', 'XBG#2', 'XDE#2']]) == \
            sorted([[e['loc'] for e in seg['timetable']] for seg in inf['segments']])

        allelemsd = {
            e['loc']: e
            for e in itertools.chain.from_iterable(
                seg['timetable'] for seg in inf['segments'])
        }
コード例 #5
0
def get_train_ids_within_timeframe(starttime,
                                   endtime,
                                   line,
                                   startpos=0,
                                   endpos=1):
    """
    Get IDs of about all trains that run on the given
    line within the given timeframe.
    """
    #TODO allow to filter for stations between xstart and xend

    line = get_lineconfig(line)
    locations = {
        l.code
        for l in line.locations_extended_between(startpos, endpos)
    }

    #TODO filter for stations on `line`
    q = db.session.query(TimetableEntry.train_id).distinct() \
        .filter(TimetableEntry.sorttime.between(starttime, endtime)) \
        .filter(TimetableEntry.loc.in_(locations))
    train_ids = [row[0] for row in db.session.execute(q).fetchall()]

    return train_ids
コード例 #6
0
ファイル: views.py プロジェクト: dingens/zwl
def get_graph_data(line):
    sleep(app.config['RESPONSE_DELAY'])

    if line == 'sample':
        return jsonify(trains=[
            {
                'type':
                'ICE',
                'nr':
                407,
                'category':
                'fv',
                'segments': [
                    {
                        'timetable': [
                            {
                                'loc': 'XDE#1',
                                'arr_plan': None,
                                'dep_plan': 13099020,
                                'track_plan': 4
                            },
                            {
                                'line': 'XDE#1_XCE#1'
                            },
                            {
                                'loc': 'XCE#1',
                                'arr_plan': 13099200,
                                'dep_plan': 13099260,
                                'track_plan': 3
                            },
                            {
                                'line': 'XCE#1_XLG#1'
                            },
                            {
                                'loc': 'XLG#1',
                                'arr_plan': 13099500,
                                'dep_plan': 13099500
                            },
                            {
                                'line': 'XLG#1_XDE#2'
                            },
                            {
                                'loc': 'XDE#2',
                                'arr_plan': 13099800,
                                'dep_plan': None,
                                'track_plan': 3
                            },
                        ],
                        'direction':
                        'right',
                    },
                ],
                'comment':
                u'',
            },
            {
                'type':
                'IRE',
                'nr':
                2342,
                'category':
                'nv',
                'segments': [
                    {
                        'timetable': [
                            {
                                'loc': 'XDE#2',
                                'arr_plan': None,
                                'dep_plan': 13099440,
                                'track_plan': 1
                            },
                            {
                                'line': 'XLG#1_XDE#2'
                            },
                            {
                                'loc': 'XLG#1',
                                'arr_plan': 13099740,
                                'dep_plan': 13099740
                            },
                        ],
                        'direction':
                        'left',
                    },
                ],
            },
            {
                'type':
                'RB',
                'nr':
                12345,
                'category':
                'nv',
                'segments': [
                    {
                        'timetable': [
                            {
                                'loc': 'XDE#1',
                                'arr_plan': None,
                                'dep_plan': 13099000,
                                'track_plan': 2
                            },
                            {
                                'line': 'XDE#1_XCE#1',
                                'opposite': True
                            },
                            {
                                'loc': 'XCE#1',
                                'arr_plan': 13099140,
                                'dep_plan': 13099140,
                                'track_plan': 2
                            },
                            {
                                'line': 'XCE#1_XLG#1'
                            },
                            {
                                'loc': 'XDE#2',
                                'arr_plan': 13099460,
                                'dep_plan': None,
                                'track_plan': 3
                            },
                        ],
                        'direction':
                        'right',
                    },
                ],
            },
        ])

    try:
        line = get_lineconfig(line)
    except KeyError:
        abort(404)

    starttime = js2time(request.args['starttime'])
    endtime = js2time(request.args['endtime'])

    startpos = request.args.get('startpos', 0, float)
    endpos = request.args.get('endpos', 1, float)

    train_ids = list(
        get_train_ids_within_timeframe(starttime,
                                       endtime,
                                       line,
                                       startpos=startpos,
                                       endpos=endpos))
    trains = Train.query.filter(
        Train.id.in_(train_ids)).all() if train_ids else []

    return jsonify(
        trains=list(get_train_information(trains, line)),
        line=line.id,
        starttime=time2js(starttime),
        endtime=time2js(endtime),
    )
コード例 #7
0
ファイル: views.py プロジェクト: dingens/zwl
def line_info():
    # sort '-foo' immediately after 'foo' (the former being reversion of the latter)
    ids = sorted(lineconfigs, key=lambda k: (k.lstrip('-'), k.startswith('-')))
    return Response(u'\n\n\n'.join(get_lineconfig(l).info() for l in ids),
                    mimetype='text/plain')