Last active
July 6, 2016 13:34
-
-
Save bkutil/4c783120ea70fbeb49125ea096d6f19b to your computer and use it in GitHub Desktop.
Import ledger transactions to local elastic search instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'csv' | |
require 'elasticsearch' | |
require 'securerandom' | |
file = ARGV.shift | |
unless file | |
warn "Usage: $0 <ledger file>" | |
exit 1 | |
end | |
src = %x{ledger csv -f "#{file}"} | |
client = Elasticsearch::Client.new log: true | |
#client.indices.delete index: :ledger | |
keys = [ | |
:date, | |
:_, | |
:txn, | |
:account, | |
:currency, | |
:amount, | |
:_, | |
:_ | |
] | |
CSV.parse(src) do |row| | |
h = keys.zip(row).to_h | |
h[:date] = Date.parse(h[:date]) | |
h[:account_tags] = h[:account].split(":") | |
h[:amount] = h[:amount].to_f | |
h.delete(:_) | |
client.index index: 'ledger', type: 'txn', id: SecureRandom.uuid, body: h | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment