Skip to content

Commit

Permalink
Adding random junk from my files.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbnpercy committed Jul 30, 2019
0 parents commit 289f880
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added clojure/.DS_Store
Binary file not shown.
94 changes: 94 additions & 0 deletions clojure/json_rpc.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(ns status-im.ethereum.json-rpc
(:require [clojure.string :as string]
[re-frame.core :as re-frame]
[status-im.ethereum.abi-spec :as abi-spec]
[status-im.ethereum.decode :as decode]
[status-im.native-module.core :as status]
[status-im.utils.money :as money]
[status-im.utils.types :as types]
[taoensso.timbre :as log]))

(def json-rpc-api
{"eth_call" {}
"eth_getBalance"
{:on-result money/bignumber}
"eth_estimateGas"
{:on-result #(money/bignumber (int (* % 1.2)))}
"eth_gasPrice"
{:on-result money/bignumber}
"eth_getBlockByHash"
{:on-result #(-> (update % :number decode/uint)
(update :timestamp decode/uint))}
"eth_getTransactionByHash" {}
"eth_getTransactionReceipt" {}
"eth_newBlockFilter" {:subscription? true}
"eth_newFilter" {:subscription? true}
"eth_syncing" {}
"net_version" {}
"shhext_enableInstallation" {}
"shhext_disableInstallation" {}
"shhext_getOurInstallations" {}
"shhext_setInstallationMetadata" {}
"shhext_loadFilters" {}
"shhext_loadFilter" {}
"shhext_removeFilters" {}
"status_joinPublicChat" {}
"status_chats" {}
"status_startOneOnOneChat" {}
"status_removeChat" {}})

(defn call
[{:keys [method params on-success on-error]}]
(when-let [method-options (json-rpc-api method)]
(let [{:keys [id on-result subscription?]
:or {on-result identity
id 1
params []}} method-options
on-error (or on-error
#(log/warn :json-rpc/error method :params params :error %))]
(if (nil? method)
(log/error :json-rpc/method-not-found method)
(status/call-private-rpc
(types/clj->json {:jsonrpc "2.0"
:id id
:method (if subscription?
"eth_subscribeSignal"
method)
:params (if subscription?
[method params]
params)})
(fn [response]
(if (string/blank? response)
(on-error {:message "Blank response"})
(let [{:keys [error result] :as response2} (types/json->clj response)]
(if error
(on-error error)
(if subscription?
(re-frame/dispatch
[:ethereum.callback/subscription-success
result on-success])
(on-success (on-result result))))))))))))

(defn eth-call
[{:keys [contract method params outputs on-success on-error block]
:or {block "latest"
params []}}]
(call {:method "eth_call"
:params [{:to contract
:data (abi-spec/encode method params)}
(if (int? block)
(abi-spec/number-to-hex block)
block)]
:on-success
(if outputs
#(on-success (abi-spec/decode % outputs))
on-success)
:on-error
on-error}))

;; effects
(re-frame/reg-fx
:json-rpc/call
(fn [params]
(doseq [param params]
(call param))))
55 changes: 55 additions & 0 deletions clojure/macros.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(ns status-im.ethereum.macros
(:require [clojure.string :as string]
[clojure.java.io :as io]))

(defn token-icon-path
[network symbol]
(let [s (str "./resources/images/tokens/" (name network) "/" (name symbol) ".png")
image (gensym)]
(if (.exists (io/file s))
`(let [~image (atom nil)]
(fn []
(or @~image
(reset! ~image (js/require ~s)))))
`(let [~image (atom nil)]
(fn []
(or
@~image
(reset! ~image
(js/require "./resources/images/tokens/default-token.png"))))))))

(defn- token->icon [network {:keys [icon symbol]}]
;; Tokens can define their own icons.
;; If not try to make one using a local image as resource, if it does not exist fallback to default.
(or icon (token-icon-path network symbol)))

(defmacro resolve-icons
"In react-native arguments to require must be static strings.
Resolve all icons at compilation time so no variable is used."
[network tokens]
(mapv #(assoc-in % [:icon :source] (token->icon network %)) tokens))

(defn network->icon [network]
(let [s (str "./resources/images/tokens/" (name network) "/0-native.png")
image (gensym)]
(if (.exists (io/file s))
`(let [~image (atom nil)]
(fn []
(or @~image
(reset! ~image (js/require ~s)))))
`(let [~image (atom nil)]
(fn []
(or
@~image
(reset! ~image
(js/require "./resources/images/tokens/default-native.png"))))))))

(defmacro resolve-native-currency-icons
"In react-native arguments to require must be static strings.
Resolve all icons at compilation time so no variable is used."
[all-native-currencies]
(into {}
(map (fn [[network native-currency]]
[network (assoc-in native-currency
[:icon :source]
(network->icon network))]) all-native-currencies)))
Binary file added elixir/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions elixir/dbo
Submodule dbo added at c8571b
1 change: 1 addition & 0 deletions elixir/issues
Submodule issues added at 886f02
34 changes: 34 additions & 0 deletions elixir/simq.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule SimpleQueue do
use GenServer

### GenServer API

def init(state), do: {:ok, state}

def handle_call(:deq, _from, [value | state]) do
{:reply, value, state}
end

def handle_call(:deq, _from, []), do: {:reply, nil, []}

def handle_call(:queue, _from, state), do: {:reply, state, state}


##ASYNC:

def handle_cast({:enq, value}, state) do
{:noreply, state ++ [value]}
end


### Client API

def start_link(state \\ []) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end

def queue, do: GenServer.call(__MODULE__, :queue)
def enq(value), do: GenServer.cast(__MODULE__, {:enq, value})
def deq, do: GenServer.call(__MODULE__, :deq)

end
33 changes: 33 additions & 0 deletions elixir/todos.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule Tasks do
use GenServer

### GenServer API

def init(state), do: {:ok, state}

def handle_call(:done, _from, [value | state]) do
{:reply, value, state}
end

def handle_call(:done, _from, []), do: {:reply, nil, []}

def handle_call(:list, _from, state), do: {:reply, state, state}


## ASYNC

def handle_cast({:push, value}, state) do
{:noreply, state ++ [value]}
end

## CLIENT API

def start_link(state \\ []) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end

def list, do: GenServer.call(__MODULE__, :list)
def push(value), do: GenServer.cast(__MODULE__, {:push, value})
def done, do: GenServer.call(__MODULE__, :done)

end
44 changes: 44 additions & 0 deletions ruby/iris_img_comparer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class IrisController
require 'rmagick'
require "open-uri"

include Magick
include Secured

def check
@user = session[:userinfo]
@iris_url = get_iris_url
@upload_url = get_upload_url
@compare = compare(@upload_url, @iris_url)
end

private

def get_iris_url
@user = session[:userinfo]
url = @user[:extra][:raw_info][:app_metadata][:iris_image_url]
return url
end

def compare(iris_new, iris_stored)
img1 = Magick::Image.read(iris_new).first

impimg = ImageList.new(iris_stored)
img2 = impimg.cur_image

res = img1.compare_channel(img2, Magick::MeanAbsoluteErrorMetric, AllChannels)
diff = res[1]
w, h = img1.columns, img1.rows
pixelcount = w * h
perc = (diff * 100)
percentage = perc/pixelcount

return percentage
end

def get_upload_url
dir = Dir.glob("public/uploads/iris/*").sort_by { |f| File.mtime(f) }.reverse
return dir[1]
end

end
109 changes: 109 additions & 0 deletions ruby/monitor_daemon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env ruby -w

require 'thread'
require 'yaml'

require 'rrd_schema'
require 'memcache_rrd'

class Job

attr_reader :name, :freq, :next

def initialize(freq, name, block)
@name=name
@freq=freq
@block=block

initial_delay=rand [freq, 30].min
@next=Time.now.to_i + initial_delay
end

def run
# Compute the next time in such a way that doesn't drift
now = Time.now.to_i
while @next <= now
@next=now + @freq
end
# And invoke the job
@block.call
end

end

class MonitorDaemon

def initialize
@jobs = []
end

def add_task(freq, name, &block)
@jobs << Job.new(freq, name, block)
end

def run_all
while true
now = Time.now.to_i
# O(n) selection of current jobs to run
@jobs.select {|j| j.next <= now}.each do |j|
begin
j.run
$stdout.flush
rescue => e
$stderr.puts "Problem running #{j.name}: #{e}"
end
end

# And an O(n) computation of the next job to run
delay = @jobs.min {|a,b| a.next <=> b.next}.next - Time.now.to_i
sleep delay if delay > 0
end
end

def add_proc_task(freq, url, user, pass)
lh=LinuxHostInfo.new(url, user, pass)
h=LinuxHostRRD.new(lh)
add_task(freq, "proc #{url}") { h.rrd_inserts }
end

def add_memcached_task(freq, servers)
m=MemCacheRRD.new(servers)
add_task(freq, "memcached [#{servers.join(', ')}]") { m.rrd_inserts }
end

def add_rails_poll(freq, c)
add_task(freq, "rails poll") do
h=c['host']
u=c['user']
p=c['pass'] || ''
d=c['database']
RailsLogRRD.new(RailsLog.new(h, u, p, d)).rrd_inserts
end
end

end

if $0 == __FILE__
raise "Need config file" if $*.empty?
conf=YAML.load_file($*[0])

md=MonitorDaemon.new

if conf['linux']
require 'sysinfo_rrd'
conf['linux'].each do |h|
md.add_proc_task 60, h['url'], h['user'], h['pass']
end
end

conf['memcached'].each do |server|
md.add_memcached_task 60, [server]
end

if conf['db']
require 'rails_log'
md.add_rails_poll 300, conf['db']
end

md.run_all
end

0 comments on commit 289f880

Please sign in to comment.