<commit_before>require 'rubygems'
require 'net/https'
require 'json'
require 'time'
require 'uri'

module TempoDB
    API_HOST = "api.tempo-db.com"
    API_PORT = 443
    API_VERSION = "v1"

    TRUSTED_CERT_FILE = File.join(File.dirname(__FILE__), "trusted-certs.crt")
end

class Database
    attr_accessor :key, :secret

    def initialize(key, secret)
        @key = key
        @secret = secret
    end

    def to_json(*a)
        { "id" => key, "password" => secret }.to_json(*a)
    end
    def self.from_json(m)
        new(m["id"], m["password"])
    end
end

class Series
    attr_accessor :id, :key, :attributes, :tags

    def initialize(id, key, attributes={}, tags=[])
        @id = id
        @key = key
        @attributes = attributes
        @tags = tags
    end

    def to_json(*a)
        { "id" => id, "key" => key, "attributes" => attributes, "tags" => tags }.to_json(*a)
    end

    def self.from_json(m)
        new(m["id"], m["key"], m["attributes"], m["tags"])
    end
end

class DataPoint
    attr_accessor :ts, :value

    def initialize(ts, value)
        @ts = ts
        @value = value
    end

    def to_json(*a)
        "{\"t\":\"#{ts.iso8601(3)}\",\"v\":#{value}}"
    end

    def self.from_json(m)
        new(Time.parse(m["t"]), m["v"])
    end
end

class TempoDBClient
    def initialize(key, secret, host=TempoDB::API_HOST, port=TempoDB::API_PORT, secure=true)
        @key = key
        @secret = secret
        @host = host
        @port = port
        @secure = secure
    end

    def get_series()
        json = do_get("/series/")
        json.map {|series| Series.from_json(series)}
    end

    def read_id(series_id, start, stop, interval="", function="")
        series_type = "id"
        series_val = series_id
        read(series_type, series_val, start, stop, interval, function)
    end

    def read_key(series_key, start, stop, interval="", function="")
        series_type = "key"
        series_val = series_key
        read(series_type, series_val, start, stop, interval, function)
    end

    def write_id(series_id, data)
        series_type = 'id'
        series_val = series_id
        write(series_type, series_val, data)
    end

    def write_key(series_key, data)
        series_type = 'key'
        series_val = series_key
        write(series_type, series_val, data)
    end

    private
<commit_msg>Add bulk writing<commit_after>require 'rubygems'
require 'net/https'
require 'json'
require 'time'
require 'uri'

module TempoDB
    API_HOST = "api.tempo-db.com"
    API_PORT = 443
    API_VERSION = "v1"

    TRUSTED_CERT_FILE = File.join(File.dirname(__FILE__), "trusted-certs.crt")
end

class Database
    attr_accessor :key, :secret

    def initialize(key, secret)
        @key = key
        @secret = secret
    end

    def to_json(*a)
        { "id" => key, "password" => secret }.to_json(*a)
    end
    def self.from_json(m)
        new(m["id"], m["password"])
    end
end

class Series
    attr_accessor :id, :key, :attributes, :tags

    def initialize(id, key, attributes={}, tags=[])
        @id = id
        @key = key
        @attributes = attributes
        @tags = tags
    end

    def to_json(*a)
        { "id" => id, "key" => key, "attributes" => attributes, "tags" => tags }.to_json(*a)
    end

    def self.from_json(m)
        new(m["id"], m["key"], m["attributes"], m["tags"])
    end
end

class DataPoint
    attr_accessor :ts, :value

    def initialize(ts, value)
        @ts = ts
        @value = value
    end

    def to_json(*a)
        "{\"t\":\"#{ts.iso8601(3)}\",\"v\":#{value}}"
    end

    def self.from_json(m)
        new(Time.parse(m["t"]), m["v"])
    end
end

class TempoDBClient
    def initialize(key, secret, host=TempoDB::API_HOST, port=TempoDB::API_PORT, secure=true)
        @key = key
        @secret = secret
        @host = host
        @port = port
        @secure = secure
    end

    def get_series()
        json = do_get("/series/")
        json.map {|series| Series.from_json(series)}
    end

    def read_id(series_id, start, stop, interval="", function="")
        series_type = "id"
        series_val = series_id
        read(series_type, series_val, start, stop, interval, function)
    end

    def read_key(series_key, start, stop, interval="", function="")
        series_type = "key"
        series_val = series_key
        read(series_type, series_val, start, stop, interval, function)
    end

    def write_id(series_id, data)
        series_type = 'id'
        series_val = series_id
        write(series_type, series_val, data)
    end

    def write_key(series_key, data)
        series_type = 'key'
        series_val = series_key
        write(series_type, series_val, data)
    end

    def write_bulk(ts, data)
        items = data.map do |item|
            id = item["id"] ? "\"id\":\"#{item["id"]}\"," : ""
            key = item["key"] ? "\"key\":\"#{item["key"]}\"," : ""
            value = item["v"] ? "\"v\":#{item["v"]}" : ""
            "{" + id + key + value + "}"
        end
        d = "[" + items.join(",") + "]"
        json = "{\"t\":\"#{ts.iso8601(3)}\",\"data\":#{d}}"

        url = "/data/"
        do_post(url, nil, json)
    end

    private

{'fraction_non_alphanumeric': 0.1520030668966839, 'fraction_numerical': 0.004408663983132068, 'mean_word_length': 2.3235668789808916, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1623679', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1782, 'n_words': 426}