Search in sources :

Example 1 with Portfolio

use of com.datastax.demo.portfolio.Portfolio in project brisk by riptano.

the class PortfolioMgrHandler method get_portfolios.

public List<Portfolio> get_portfolios(String start_key, int limit) throws TException {
    KeyRange kr = new KeyRange(limit).setStart_key(start_key.getBytes()).setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER);
    try {
        List<KeySlice> kslices = getClient().get_range_slices(pcp, sp, kr, ConsistencyLevel.ONE);
        CqlResult result = getClient().execute_cql_query(ByteBufferUtil.bytes(buildPortfoliosQuery(start_key, limit)), Compression.NONE);
        //List<Portfolio> portfolios = buildPorfoliosFromRangeSlices(kslices);
        List<Portfolio> portfolios = buildPorfoliosFromCqlResult(result);
        addLossInformation(portfolios);
        addHistInformation(portfolios);
        return portfolios;
    } catch (Exception e) {
        throw new TException(e);
    }
}
Also used : TException(org.apache.thrift.TException) Portfolio(com.datastax.demo.portfolio.Portfolio) CharacterCodingException(java.nio.charset.CharacterCodingException) TException(org.apache.thrift.TException)

Example 2 with Portfolio

use of com.datastax.demo.portfolio.Portfolio in project brisk by riptano.

the class PortfolioMgrHandler method addLossInformation.

private void addLossInformation(List<Portfolio> portfolios) {
    Map<ByteBuffer, Portfolio> portfolioLookup = new HashMap<ByteBuffer, Portfolio>();
    List<ByteBuffer> portfolioNames = new ArrayList<ByteBuffer>();
    for (Portfolio p : portfolios) {
        ByteBuffer name = ByteBufferUtil.bytes(p.name);
        portfolioLookup.put(name, p);
        portfolioNames.add(name);
    }
    try {
        Map<ByteBuffer, List<ColumnOrSuperColumn>> result = getClient().multiget_slice(portfolioNames, lcp, lcols, ConsistencyLevel.ONE);
        for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : result.entrySet()) {
            Portfolio portfolio = portfolioLookup.get(entry.getKey());
            if (portfolio == null)
                continue;
            for (ColumnOrSuperColumn col : entry.getValue()) {
                if (col.getColumn().name.equals(lossCol))
                    portfolio.setLargest_10day_loss(Double.valueOf(ByteBufferUtil.string(col.getColumn().value)));
                if (col.getColumn().name.equals(lossDateCol))
                    portfolio.setLargest_10day_loss_date(ByteBufferUtil.string(col.getColumn().value));
            }
        }
    } catch (InvalidRequestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimedOutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CharacterCodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) Portfolio(com.datastax.demo.portfolio.Portfolio) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Portfolio

use of com.datastax.demo.portfolio.Portfolio in project brisk by riptano.

the class PortfolioMgrHandler method addHistInformation.

private void addHistInformation(List<Portfolio> portfolios) {
    for (Portfolio p : portfolios) {
        ByteBuffer name = ByteBufferUtil.bytes(p.name);
        List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
        for (Position position : p.constituents) {
            tickers.add(ByteBufferUtil.bytes(position.ticker));
        }
        try {
            Map<ByteBuffer, List<ColumnOrSuperColumn>> result = getClient().multiget_slice(tickers, hcp, hsp, ConsistencyLevel.ONE);
            Map<String, Double> histPrices = new LinkedHashMap<String, Double>();
            for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : result.entrySet()) {
                for (ColumnOrSuperColumn col : entry.getValue()) {
                    Double price = histPrices.get(ByteBufferUtil.string(col.column.name));
                    if (price == null)
                        price = 0.0;
                    price = +Double.valueOf(ByteBufferUtil.string(col.column.value));
                    histPrices.put(ByteBufferUtil.string(col.column.name), price);
                }
            }
            p.setHist_prices(Arrays.asList(histPrices.values().toArray(new Double[] {})));
        } catch (InvalidRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimedOutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CharacterCodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : TException(org.apache.thrift.TException) Position(com.datastax.demo.portfolio.Position) Portfolio(com.datastax.demo.portfolio.Portfolio) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with Portfolio

use of com.datastax.demo.portfolio.Portfolio in project brisk by riptano.

the class PortfolioMgrHandler method buildPorfoliosFromRangeSlices.

private List<Portfolio> buildPorfoliosFromRangeSlices(List<KeySlice> kslices) throws Exception {
    List<Portfolio> portfolios = new ArrayList<Portfolio>();
    for (KeySlice ks : kslices) {
        Portfolio p = new Portfolio();
        p.setName(new String(ks.getKey()));
        Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
        List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
        for (ColumnOrSuperColumn cosc : ks.getColumns()) {
            tickers.add(cosc.getColumn().name);
            tickerLookup.put(cosc.getColumn().name, ByteBufferUtil.toLong(cosc.getColumn().value));
        }
        Map<ByteBuffer, List<ColumnOrSuperColumn>> prices = getClient().multiget_slice(tickers, scp, sp, ConsistencyLevel.ONE);
        double total = 0;
        double basis = 0;
        Random r = new Random(Long.valueOf(new String(ks.getKey())));
        for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : prices.entrySet()) {
            if (!entry.getValue().isEmpty()) {
                Double price = Double.valueOf(ByteBufferUtil.string(entry.getValue().get(0).column.value));
                Position s = new Position(ByteBufferUtil.string(entry.getKey()), price, tickerLookup.get(entry.getKey()));
                p.addToConstituents(s);
                total += price * tickerLookup.get(entry.getKey());
                basis += r.nextDouble() * 100 * tickerLookup.get(entry.getKey());
            }
        }
        p.setPrice(total);
        p.setBasis(basis);
        portfolios.add(p);
    }
    return portfolios;
}
Also used : Position(com.datastax.demo.portfolio.Position) Portfolio(com.datastax.demo.portfolio.Portfolio) ByteBuffer(java.nio.ByteBuffer)

Example 5 with Portfolio

use of com.datastax.demo.portfolio.Portfolio in project brisk by riptano.

the class PortfolioMgrHandler method buildPorfoliosFromCqlResult.

private List<Portfolio> buildPorfoliosFromCqlResult(CqlResult result) throws Exception {
    List<Portfolio> portfolios = new ArrayList<Portfolio>();
    for (CqlRow row : result.rows) {
        Portfolio p = new Portfolio();
        p.setName(new String(row.getKey()));
        Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
        List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();
        for (Column cosc : row.getColumns()) {
            tickers.add(cosc.name);
            tickerLookup.put(cosc.name, ByteBufferUtil.toLong(cosc.value));
        }
        double total = 0;
        double basis = 0;
        Random r = new Random(Long.valueOf(new String(row.getKey())));
        for (ByteBuffer ticker : tickers) {
            CqlResult tResult = getClient().execute_cql_query(ByteBufferUtil.bytes(buildStocksQuery(ticker)), Compression.NONE);
            CqlRow tRow = tResult.getRowsIterator().hasNext() ? tResult.getRowsIterator().next() : null;
            if (tRow != null) {
                Double price = Double.valueOf(ByteBufferUtil.string(tRow.columns.get(0).value));
                Position s = new Position(ByteBufferUtil.string(tRow.key), price, tickerLookup.get(tRow.key));
                p.addToConstituents(s);
                total += price * tickerLookup.get(tRow.key);
                basis += r.nextDouble() * 100 * tickerLookup.get(tRow.key);
            }
        }
        p.setPrice(total);
        p.setBasis(basis);
        portfolios.add(p);
    }
    return portfolios;
}
Also used : Position(com.datastax.demo.portfolio.Position) Portfolio(com.datastax.demo.portfolio.Portfolio) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Portfolio (com.datastax.demo.portfolio.Portfolio)5 ByteBuffer (java.nio.ByteBuffer)4 Position (com.datastax.demo.portfolio.Position)3 CharacterCodingException (java.nio.charset.CharacterCodingException)3 TException (org.apache.thrift.TException)3