Skip to content

Instantly share code, notes, and snippets.

@bkutil
Last active July 6, 2016 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkutil/4c783120ea70fbeb49125ea096d6f19b to your computer and use it in GitHub Desktop.
Save bkutil/4c783120ea70fbeb49125ea096d6f19b to your computer and use it in GitHub Desktop.
Import ledger transactions to local elastic search instance
#!/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