Search in sources :

Example 1 with Read

use of primal.MoreVerbs.Read in project suite by stupidsing.

the class Yahoo method getStockHistory.

private StockHistory getStockHistory(String symbol) {
    var path = HomeDir.dir("yahoo").resolve(symbol + ".txt");
    StockHistory stockHistory0;
    if (Files.exists(path))
        try {
            var lines = ex(() -> Files.readAllLines(path));
            stockHistory0 = StockHistory.of(Read.from(lines).puller());
        } catch (Exception ex) {
            stockHistory0 = StockHistory.new_();
        }
    else
        stockHistory0 = StockHistory.new_();
    var time = HkexUtil.getCloseTimeBefore(Time.now());
    StockHistory stockHistory1;
    if (stockHistory0.isActive && Time.compare(stockHistory0.time, time) < 0) {
        var json = queryL1(symbol, TimeRange.of(stockHistory0.time.addDays(-14), Time.now()));
        var jsons = // 
        Read.each(json).flatMap(json_ -> json_.path("chart").path("result"));
        var exchange = // 
        jsons.map(// 
        json_ -> json_.path("meta").path("exchangeName").textValue()).uniqueResult();
        var ts = // 
        jsons.flatMap(// 
        json_ -> json_.path("timestamp")).collect(// 
        LiftLng.of(t -> getOpenTimeBefore(exchange, t.longValue()))).toArray();
        var length = ts.length;
        var dataJsons0 = // 
        Read.<String>empty().map2(tag -> // 
        jsons.flatMap(json_ -> {
            var json0 = json_.path("indicators");
            JsonNode json1;
            if (// 
            false || // 
            !(json1 = json0.path("unadjclose")).isMissingNode() || !(json1 = json0.path("unadjquote")).isMissingNode())
                return json1;
            else
                return List.of();
        }).flatMap(json_ -> json_.path("unadj" + tag)));
        var dataJsons1 = // 
        Read.each("open", "close", "high", "low", // 
        "volume").map2(tag -> // 
        jsons.flatMap(// 
        json_ -> json_.path("indicators").path("quote")).flatMap(json_ -> json_.path(tag)));
        var data = // 
        Streamlet2.concat(dataJsons0, // 
        dataJsons1).mapValue(// 
        json_ -> json_.collect(LiftFlt.of(JsonNode::floatValue)).toArray()).filterValue(// 
        fs -> length <= fs.length).mapValue(// 
        fs -> New.array(length, LngFltPair.class, i -> LngFltPair.of(ts[i], fs[i]))).toMap();
        var dividends = // 
        jsons.flatMap(// 
        json_ -> json_.path("events").path("dividends")).map(// 
        json_ -> LngFltPair.of(json_.path("date").longValue(), json_.path("amount").floatValue())).sort(// 
        LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
        var splits = // 
        jsons.flatMap(// 
        json_ -> json_.path("events").path("splits")).map(json_ -> LngFltPair.of(json_.path("date").longValue(), // 
        json_.path("numerator").floatValue() / json_.path("denominator").floatValue())).sort(// 
        LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
        if (data.containsKey("close"))
            stockHistory1 = // 
            StockHistory.of(exchange, time, true, data, dividends, // 
            splits).merge(// 
            stockHistory0).alignToDate();
        else
            stockHistory1 = fail();
        WriteFile.to(path).writeAndClose(stockHistory1.write());
    } else
        stockHistory1 = stockHistory0;
    Predicate<LngFltPair> splitFilter;
    LngFltPair[] splits2;
    if (Equals.string(symbol, "0700.HK"))
        splitFilter = pair -> pair.t0 != Time.of(2014, 5, 15, 9, 30).epochSec();
    else if (Equals.string(symbol, "2318.HK"))
        splitFilter = pair -> pair.t0 != Time.of(2015, 7, 27, 9, 30).epochSec();
    else
        splitFilter = null;
    splits2 = // 
    splitFilter != null ? // 
    Read.from(stockHistory1.splits).filter(splitFilter).toArray(LngFltPair.class) : stockHistory1.splits;
    var stockHistory2 = stockHistory1.create(stockHistory1.data, stockHistory1.dividends, splits2);
    var stockHistory3 = LogUtil.prefix("for " + symbol + ": ", () -> stockHistory2.cleanse());
    return stockHistory3;
}
Also used : Fail.fail(primal.statics.Fail.fail) LogUtil(suite.os.LogUtil) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) New(primal.Verbs.New) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Utf8(primal.Nouns.Utf8) HttpClient(suite.http.HttpClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) LiftFlt(primal.primitive.FltMoreVerbs.LiftFlt) Files(java.nio.file.Files) LiftLng(primal.primitive.LngMoreVerbs.LiftLng) Predicate(java.util.function.Predicate) Compare(primal.Verbs.Compare) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) WriteFile(primal.Verbs.WriteFile) Read(primal.MoreVerbs.Read) HomeDir(suite.cfg.HomeDir) URLEncoder(java.net.URLEncoder) List(java.util.List) Time(suite.trade.Time) Rethrow.ex(primal.statics.Rethrow.ex) As(suite.streamlet.As) Streamlet2(primal.streamlet.Streamlet2) Equals(primal.Verbs.Equals) TimeRange(suite.trade.TimeRange) LngFltPair(primal.primitive.adt.pair.LngFltPair) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with Read

use of primal.MoreVerbs.Read in project suite by stupidsing.

the class HttpNio method listen.

private void listen(Reg reg) {
    var rw = new Object() {

        // 0 - read, 1 - write, 2 - close after all written
        private int stage = 0;

        private Bytes br = Bytes.empty;

        private Bytes bw = Bytes.empty;

        private Puller<Bytes> write;

        private Source<Boolean> eater = () -> parseLine(line -> handleRequest1stLine(line.trim(), response -> {
            var data = // 
            "HTTP/1.1 " + response.status + "\r\n" + response.headers.streamlet().map((k, v) -> k + ": " + v + "\r\n").toJoinedString() + "\r\n";
            stage = 1;
            if (response.body != null)
                write = Puller.concat(Pull.from(data), response.body);
            else {
                bw = Bytes.of(data.getBytes(Utf8.charset));
                response.write.f(bytes -> {
                    if (bytes != null)
                        bw = bw.append(bytes);
                    else
                        stage = 2;
                    listen();
                });
            }
        }));

        private void listen() {
            if (!bw.isEmpty())
                reg.listenWrite(() -> bw, this::written);
            else if (stage == 0)
                reg.listenRead(in -> {
                    read(in);
                    listen();
                });
            else if (stage == 1 && write != null)
                reg.listenWrite(() -> bw = write.pull(), this::written);
            else if (stage == 2)
                reg.listenWrite(() -> null, null);
            else if (stage == 3)
                reg.listen(0, null, null, null);
        }

        private void read(Bytes in) {
            if (in != null) {
                br = br.append(in);
                while (eater.g()) ;
            } else
                // closes connection
                write = Puller.empty();
        }

        private void written(int n) {
            if (0 <= n)
                bw = bw.range(n);
            else
                stage = 3;
            listen();
        }

        private void handleRequest1stLine(String line, Sink<Response> cb) {
            var hrhl = handleRequestHeaderLine(lines -> handleRequestBody(line, lines, cb));
            eater = () -> parseLine(hrhl);
        }

        private Sink<String> handleRequestHeaderLine(Sink<List<String>> cb) {
            var lines = new ArrayList<String>();
            return line0 -> {
                var line1 = line0.trim();
                if (!line1.isEmpty())
                    lines.add(line1);
                else
                    cb.f(lines);
            };
        }

        private void handleRequestBody(String line0, List<String> headerLines, Sink<Response> cb) {
            eater = () -> // 
            FixieArray.of(// 
            line0.split(" ")).map((method, url, proto) -> handleRequestBody(proto, method, url, headerLines, cb));
        }

        private // 
        boolean handleRequestBody(// 
        String proto, // 
        String method, // 
        String url, // 
        List<String> lines, Sink<Response> cb) {
            var headers = // 
            Read.from(// 
            lines).fold(new Header(), (headers_, line_) -> // 
            Split.strl(line_, // 
            ":").map((k, v) -> headers_.put(k, v)));
            var queue = new ArrayBlockingQueue<Bytes>(Buffer.size);
            Sink<Bytes> offer = queue::add;
            Source<Bytes> take = queue::poll;
            Fun2<String, String, Request> requestFun = (host, pqs) -> Split.strl(pqs, "?").map((path0, query) -> {
                var path1 = path0.startsWith("/") ? path0 : "/" + path0;
                var path2 = ex(() -> URLDecoder.decode(path1, Utf8.charset));
                return // 
                Equals.string(proto, "HTTP/1.1") ? // 
                new Request(method, host, path2, query, headers, Puller.of(take)) : fail("only HTTP/1.1 is supported");
            });
            var pp = Split.string(url, "://");
            var request = pp != null ? Split.strl(pp.v, "/").map(requestFun) : requestFun.apply("", url);
            var cl = request.headers.getOpt("Content-Length").map(Long::parseLong);
            var te = Equals.ab(request.headers.getOpt("Transfer-Encoding"), Opt.of("chunked"));
            Log_.info(request.getLogString());
            if (te)
                eater = handleChunkedRequestBody(request, offer, cb);
            else if (cl.hasValue())
                eater = handleRequestBody(request, offer, cl.g(), cb);
            else
                eater = handleRequestBody(request, offer, 0, cb);
            return true;
        }

        private // 
        Source<Boolean> handleRequestBody(// 
        Request request, // 
        Sink<Bytes> body, // 
        long contentLength, Sink<Response> cb) {
            return new Source<>() {

                private int n;

                public Boolean g() {
                    body.f(br);
                    var isOpen = br != null;
                    if (isOpen) {
                        n += br.size();
                        br = Bytes.empty;
                    }
                    if (!isOpen || contentLength <= n)
                        cb.f(handler.handle(request));
                    return false;
                }
            };
        }

        private Source<Boolean> handleChunkedRequestBody(Request request, Sink<Bytes> body, Sink<Response> cb) {
            return () -> {
                for (var i0 = 0; i0 < br.size(); i0++) if (br.get(i0) == 10) {
                    var line = new String(br.range(0, i0).toArray(), Utf8.charset);
                    var size = Integer.parseInt(line.trim(), 16);
                    for (var i1 = i0 + 1 + size; i1 < br.size(); i1++) if (br.get(i1) == 10) {
                        var chunk = br.range(i0 + 1, i1);
                        br = br.range(i1);
                        body.f(chunk);
                        return true;
                    }
                    if (size == 0)
                        cb.f(handler.handle(request));
                }
                return false;
            };
        }

        private boolean parseLine(Sink<String> handleLine) {
            for (var i = 0; i < br.size(); i++) if (br.get(i) == 10) {
                var line = new String(br.range(0, i).toArray(), Utf8.charset);
                br = br.range(i + 1);
                handleLine.f(line);
                return true;
            }
            return false;
        }
    };
    rw.listen();
}
Also used : FixieArray(primal.adt.FixieArray) Fail.fail(primal.statics.Fail.fail) URLDecoder(java.net.URLDecoder) Reg(suite.os.ListenNio.Reg) Sink(primal.fp.Funs.Sink) ArrayList(java.util.ArrayList) Response(suite.http.Http.Response) Utf8(primal.Nouns.Utf8) Request(suite.http.Http.Request) Split(primal.MoreVerbs.Split) Opt(primal.adt.Opt) Pull(primal.MoreVerbs.Pull) Handler(suite.http.Http.Handler) ListenNio(suite.os.ListenNio) Buffer(primal.Nouns.Buffer) Source(primal.fp.Funs.Source) Header(suite.http.Http.Header) Log_(primal.os.Log_) Read(primal.MoreVerbs.Read) Bytes(primal.primitive.adt.Bytes) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) Rethrow.ex(primal.statics.Rethrow.ex) Fun2(primal.fp.Funs2.Fun2) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) ArrayList(java.util.ArrayList) Request(suite.http.Http.Request) Source(primal.fp.Funs.Source) Bytes(primal.primitive.adt.Bytes) Sink(primal.fp.Funs.Sink) Header(suite.http.Http.Header) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Puller(primal.puller.Puller) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

List (java.util.List)2 Read (primal.MoreVerbs.Read)2 Utf8 (primal.Nouns.Utf8)2 Equals (primal.Verbs.Equals)2 Fail.fail (primal.statics.Fail.fail)2 Rethrow.ex (primal.statics.Rethrow.ex)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 URLDecoder (java.net.URLDecoder)1 URLEncoder (java.net.URLEncoder)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 Predicate (java.util.function.Predicate)1 Pull (primal.MoreVerbs.Pull)1 Split (primal.MoreVerbs.Split)1 Buffer (primal.Nouns.Buffer)1