Пример #1
0
def data_ts(timestamp: str) -> flask.Response:
    start = datetime.fromtimestamp(int(timestamp), JST)
    one_day = timedelta(days=1)
    end = start + one_day - timedelta(seconds=1)
    day_before = start - one_day

    rics = config.rics

    # PostgreSQL-specific speedup (raw query)
    if db.session.bind.dialect.name == 'postgresql':
        prices = fetch_all_points_fast(db.session, rics, start, end)
        closes = fetch_all_closes_fast(db.session, rics, day_before, start)
    else:
        prices = {}
        closes = {}
        for ric in rics:
            xs, ys = fetch_points(db.session, ric, start, end)
            prices[ric] = {
                'xs': [epoch(x) for x in xs],
                'ys': [float(y) if y is not None else None for y in ys],
            }
            closes[ric] = fetch_close(db.session, ric, day_before)

    data = {
        'start': start.timestamp(),
        'end': end.timestamp(),
        'prices': prices,
        'closes': closes,
    }
    return app.response_class(response=flask.json.dumps(data),
                              status=http.HTTPStatus.OK,
                              mimetype='application/json')
Пример #2
0
def data(article_id: str) -> flask.Response:

    headline = db \
        .session \
        .query(Headline, in_utc(Headline.t).label('utc')) \
        .filter(Headline.article_id == article_id) \
        .one()

    data = []
    for ric in [Code.N225.value, Code.TOPIX.value]:

        end = headline.utc.replace(tzinfo=UTC)
        start = datetime(end.year, end.month, end.day, 0, 0, tzinfo=UTC)
        xs, ys = fetch_points(db.session, ric, start, end)

        end_prev = datetime \
            .utcfromtimestamp(fetch_max_t_of_prev_trading_day(db.session, ric, end)) \
            .replace(tzinfo=UTC)
        start_prev = datetime(end_prev.year,
                              end_prev.month,
                              end_prev.day,
                              0,
                              0,
                              tzinfo=UTC)
        xs_prev, ys_prev = fetch_points(db.session, ric, start_prev, end_prev)

        data.append({
            'ric': ric,
            'chart': {
                'xs': xs,
                'ys': ys,
                'title': '{} {}'.format(ric, end.strftime('%Y-%m-%d'))
            },
            'chart-prev': {
                'xs': xs_prev,
                'ys': ys_prev,
                'title': '{} {}'.format(ric, end_prev.strftime('%Y-%m-%d'))
            }
        })

    return app.response_class(response=flask.json.dumps(data),
                              status=http.HTTPStatus.OK,
                              mimetype='application/json')