From 72fcbc305b7979fa6d39d3c06c1aa7fde92f1a24 Mon Sep 17 00:00:00 2001 From: Peter Selby Date: Tue, 3 Aug 2021 07:20:06 -0700 Subject: [PATCH] started to create flake --- .gitignore | 6 + src/worther/injest/deps.edn => deps.edn | 3 +- deps.nix | 126 ++++++ flake.lock | 43 ++ flake.nix | 22 + src/worther.clj | 5 + worther.nix | 38 ++ worther_scratch.clj | 523 ++++++++++++++++++++++++ 8 files changed, 765 insertions(+), 1 deletion(-) create mode 100644 .gitignore rename src/worther/injest/deps.edn => deps.edn (92%) create mode 100644 deps.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/worther.clj create mode 100644 worther.nix create mode 100644 worther_scratch.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c091363 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +.idea +*.log +tmp/ +.cpcache/ +.nrepl-port diff --git a/src/worther/injest/deps.edn b/deps.edn similarity index 92% rename from src/worther/injest/deps.edn rename to deps.edn index 8c1a243..9ebaba6 100644 --- a/src/worther/injest/deps.edn +++ b/deps.edn @@ -1,4 +1,5 @@ -{:deps +{:paths ["src"] + :deps {org.clojure/data.csv {:mvn/version "1.0.0"} org.clojure/core.match {:mvn/version "1.0.0"} arachne-framework/valuehash {:git/url "https://git.fudo.org/fudo-public/valuehash.git" diff --git a/deps.nix b/deps.nix new file mode 100644 index 0000000..b0c3c82 --- /dev/null +++ b/deps.nix @@ -0,0 +1,126 @@ +# generated by clj2nix-1.0.7 +{ pkgs ? import {} }: + +let repos = [ + "https://repo1.maven.org/maven2/" + "https://repo.clojars.org/" ]; + + in rec { + fetchmaven = pkgs.callPackage (pkgs.fetchurl { + url = "https://raw.githubusercontent.com/NixOS/nixpkgs/ba5e2222458a52357a3ba5873d88779d5c223269/pkgs/build-support/fetchmavenartifact/default.nix"; + sha512 = "05m7i8hbhyfz7p2f106mfbsasjf04svd9xkgc26pl3shljrk0dfacz39wiwzm6xqw7czgrsx745vciram7al621v7634nfdq3m1x88a"; + }) {}; + makePaths = {extraClasspaths ? null}: + (pkgs.lib.concatMap + (dep: + builtins.map + (path: + if builtins.isString path then + path + else if builtins.hasAttr "jar" path then + path.jar + else if builtins.hasAttr "outPath" path then + path.outPath + else + path + ) + dep.paths) + packages) + ++ (if extraClasspaths != null then [ extraClasspaths ] else []); + makeClasspaths = {extraClasspaths ? null}: builtins.concatStringsSep ":" (makePaths {inherit extraClasspaths;}); + packageSources = builtins.map (dep: dep.src) packages; + packages = [ + rec { + name = "data.csv/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "data.csv"; + groupId = "org.clojure"; + sha512 = "b039775a859ed27eca8f8ae74ccb6afde3ad1fe2b3cbe542240c324d60fe1237e495eb1300ee9eb4ff4ef59f01faf7aec6ef1dd6a025ee4fe556c1d91acfcf1b"; + version = "1.0.0"; + + }; + paths = [ src ]; + } + + rec { + name = "core.match/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "core.match"; + groupId = "org.clojure"; + sha512 = "52ada3bbe73ed1b429be811d3990df0cdb3e9d50f2a6c92b70d490a8ea922d4794da93c3b7487653f801954fc599704599b318b4d7926a9594583df37c55e926"; + version = "1.0.0"; + + }; + paths = [ src ]; + } + + (rec { + name = "arachne-framework/valuehash"; + src = pkgs.fetchgit { + name = "valuehash"; + url = "https://git.fudo.org/fudo-public/valuehash.git"; + rev = "9d2dbafdb5db886a57f44c5b7fe32c824713e6c7"; + sha256 = "1civ393c4yy9p2xbmrrvpbyqczx55k3fkvimkf850fl62ns5zl9r"; + }; + paths = map (path: src + path) [ + "/src" + ]; + }) + + rec { + name = "clojure/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "clojure"; + groupId = "org.clojure"; + sha512 = "d9e2c0676cdc349a3455d92b3ce3c3f01a2410de448c9416edfe72bc7eaf356cfadbb6d746740a821940c3b4cab100ca941e23bab482e98b404ed9ef79c562df"; + version = "1.10.0-alpha4"; + + }; + paths = [ src ]; + } + + rec { + name = "tools.logging/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "tools.logging"; + groupId = "org.clojure"; + sha512 = "f1381bf5bf71048298241738d7b0b0ee5e90e4666ec7067f7544898beb8e3370a93471ffc6766537b5451f820b9aa96432a1e50bf4e21bda6e105e4c92727cbc"; + version = "0.3.1"; + + }; + paths = [ src ]; + } + + rec { + name = "spec.alpha/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "spec.alpha"; + groupId = "org.clojure"; + sha512 = "b8fc40ed9bc52b545e699ed188dd61bfd144ee67f0c70364b8f2715e9f1fea608d3721db7f618f6ef4bc3056e3c2984c626080486ca710f3595dda8ba23730ac"; + version = "0.1.143"; + + }; + paths = [ src ]; + } + + rec { + name = "core.specs.alpha/org.clojure"; + src = fetchmaven { + inherit repos; + artifactId = "core.specs.alpha"; + groupId = "org.clojure"; + sha512 = "b4f5eee01da39914e6024dd529d1f72952d5a9dae65e1e41bf386b1e86a004a0d197b5be95aa70e7e8d6438c92b7fa8fc0c5039f2013e97c0b91c22d86fb7968"; + version = "0.1.24"; + + }; + paths = [ src ]; + } + + ]; + } + \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..28c3b98 --- /dev/null +++ b/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1623875721, + "narHash": "sha256-A8BU7bjS5GirpAUv4QA+QnJ4CceLHkcXdRp4xITDB0s=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "f7e004a55b120c02ecb6219596820fcd32ca8772", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1627892869, + "narHash": "sha256-Fg0KPi9oGTZ7Ju7rb8foq0GOZI3++ZaF6wf10ClMA58=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d4590d21006387dcb190c516724cb1e41c0f8fdf", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-21.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1fc977e --- /dev/null +++ b/flake.nix @@ -0,0 +1,22 @@ +{ + description = "Worther - Calculate cryptocurrency taxes & net worth."; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-21.05"; + + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: with nixpkgs.lib; + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + worther = callPackage ./worther.nix { pkgs = nixpkgs; }; + + in rec { + packages = flake-utils.lib.flattenTree { + worther = nixpkgs.legacyPackages.x86_64-linux.worther; + }; + defaultPackage = packages.worther; + }); +} diff --git a/src/worther.clj b/src/worther.clj new file mode 100644 index 0000000..d02c8aa --- /dev/null +++ b/src/worther.clj @@ -0,0 +1,5 @@ +(ns worther.core) + +(defn -main [& args] + (println "Not implemented yet!") + (println (str "called with: " args))) diff --git a/worther.nix b/worther.nix new file mode 100644 index 0000000..50c6cd5 --- /dev/null +++ b/worther.nix @@ -0,0 +1,38 @@ +{ pkgs ? import {}, ... }: + +let + cljdeps = pkgs.callPackage ./deps.nix {}; + classpath = cljdeps.makeClasspaths {}; + exec-name = "worther"; + main-class = "worther.core"; + manifest = pkgs.writeText "${exec-name}-MANIFEST.MF" '' + Manifest-Version: 1.0 + Main-Class: ${main-class} + + ''; + +in pkgs.stdenv.mkDerivation { + name = exec-name; + + nativeBuildInputs = with pkgs; [ jre makeWrapper ]; + + buildInputs = map (x: x.path) cljdeps.packages; + + src = ./src; + + phases = [ "unpackPhase" "buildPhase" "installPhase" ]; + + buildPhase = '' + mkdir classes + java -cp .:${classpath} clojure.main -e "(compile '${main-class}')" + jar cmf ${manifest} out.jar -C classes worther + ''; + + installPhase = '' + mkdir -p $out/bin + mkdir -p $out/share/java + cp out.jar $out/share/java/${exec-name}.jar + makeWrapper ${pkgs.jdk}/bin/java $out/bin/${exec-name} \ + --add-flags "-cp ${classpath}:$out/share/java/${exec-name}.jar ${main-class}" + ''; +} diff --git a/worther_scratch.clj b/worther_scratch.clj new file mode 100644 index 0000000..57b7b09 --- /dev/null +++ b/worther_scratch.clj @@ -0,0 +1,523 @@ +(ns worther + (:require [crux.api :as crux])) + +(def crux (crux/start-node {})) + +(def manifest {:crux.db/id :manifest + :pilot-name "Johanna" + :id/rocket "SB002-sol" + :id/employee "22910x2" + :badges ["SETUP"] + :cargo ["stereo" "gold fish" "slippers" "secret note"]}) + +(crux/submit-tx crux [[:crux.tx/put manifest]]) + +#_(crux/entity (crux/db crux) :manifest) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :commodity/Pu + :common-name "Plutonium" + :type :element/metal + :density 19.816 + :radioactive true}] + + [:crux.tx/put + {:crux.db/id :commodity/N + :common-name "Nitrogen" + :type :element/gas + :density 1.2506 + :radioactive false}] + + [:crux.tx/put + {:crux.db/id :commodity/CH4 + :common-name "Methane" + :type :molecule/gas + :density 0.717 + :radioactive false}]]) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :stock/Pu + :commod :commodity/Pu + :weight-ton 21 } + #inst "2115-02-13T18"] + + [:crux.tx/put + {:crux.db/id :stock/Pu + :commod :commodity/Pu + :weight-ton 23 } + #inst "2115-02-14T18"] + + [:crux.tx/put + {:crux.db/id :stock/Pu + :commod :commodity/Pu + :weight-ton 22.2 } + #inst "2115-02-15T18"] + + [:crux.tx/put + {:crux.db/id :stock/Pu + :commod :commodity/Pu + :weight-ton 24 } + #inst "2115-02-18T18"] + + [:crux.tx/put + {:crux.db/id :stock/Pu + :commod :commodity/Pu + :weight-ton 24.9 } + #inst "2115-02-19T18"]]) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :stock/N + :commod :commodity/N + :weight-ton 3} + #inst "2115-02-13T18" + #inst "2115-02-19T18"] + [:crux.tx/put + {:crux.db/id :stock/CH4 + :commod :commodity/CH4 + :weight-ton 92} + #inst "2115-02-15T18" + #inst "2115-02-19T18"]]) + +#_(crux/entity (crux/db crux #inst "2115-02-14") :stock/Pu) + +#_(crux/entity (crux/db crux #inst "2115-02-18") :stock/Pu) + +(defn easy-ingest + [node docs] + (crux/submit-tx node + (vec (for [doc docs] + [:crux.tx/put doc])))) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :manifest + :pilot-name "Johanna" + :id/rocket "SB002-sol" + :id/employee "22910x2" + :badges ["SETUP" "PUT"] + :cargo ["stereo" "gold fish" "slippers" "secret note"]}]]) + +;; Mercury + +(def data + [{:crux.db/id :commodity/Pu + :common-name "Plutonium" + :type :element/metal + :density 19.816 + :radioactive true} + + {:crux.db/id :commodity/N + :common-name "Nitrogen" + :type :element/gas + :density 1.2506 + :radioactive false} + + {:crux.db/id :commodity/CH4 + :common-name "Methane" + :type :molecule/gas + :density 0.717 + :radioactive false} + + {:crux.db/id :commodity/Au + :common-name "Gold" + :type :element/metal + :density 19.300 + :radioactive false} + + {:crux.db/id :commodity/C + :common-name "Carbon" + :type :element/non-metal + :density 2.267 + :radioactive false} + + {:crux.db/id :commodity/borax + :common-name "Borax" + :IUPAC-name "Sodium tetraborate decahydrate" + :other-names ["Borax decahydrate" "sodium borate" "sodium tetraborate" "disodium tetraborate"] + :type :mineral/solid + :appearance "white solid" + :density 1.73 + :radioactive false}]) + +(easy-ingest crux data) + +(crux/q (crux/db crux) + '{:find [element] + :where [[element :type :element/metal]]}) + +(= (crux/q (crux/db crux) + '{:find [element] + :where [[element :type :element/metal]]}) + (crux/q (crux/db crux) + {:find '[element] + :where '[[element :type :element/metal]]}) + (crux/q (crux/db crux) + (quote + {:find [element] + :where [[element :type :element/metal]]})) + (crux/q (crux/db crux) + {:find ['element] + :where [['element :type :element/metal]]})) + +(crux/q (crux/db crux) + '{:find [name] + :where [[e :type :element/metal] + [e :common-name name]]}) + +(crux/q (crux/db crux) + '{:find [name rho] + :where [[e :density rho] + [e :common-name name]]}) + +(crux/q (crux/db crux) + {:find '[name] + :where '[[e :type t] + [e :common-name name]] + :args [{'t :element/metal}]}) + +(defn find-by-type [type] + (crux/q (crux/db crux) + {:find '[name] + :where '[[e :type t] + [e :common-name name]] + :args [{'t type}]})) + +(defn filter-type [type] + (crux/q (crux/db crux) + {:find '[name] + :where '[[e :type t] + [e :common-name name]] + :args [{'t type}]})) + +(filter-type :element/metal) + +(filter-type :element/non-metal) + +(defn filter-appearance [description] + (crux/q (crux/db crux) + {:find '[name IUPAC] + :where '[[e :common-name name] + [e :IUPAC-name IUPAC] + [e :appearance ?appearance]] + :args [{'?appearance description}]})) + +(filter-appearance "white solid") + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :manifest + :pilot-name "Johanna" + :id/rocket "SB002-sol" + :id/employee "22910x2" + :badges ["SETUP" "PUT" "DATALOG-QUERIES"] + :cargo ["stereo" "gold fish" "slippers" "secret note"]}]]) + +;; Neptune + +(crux/submit-tx + crux + [[:crux.tx/put + {:crux.db/id :consumer/RJ29sUU + :consumer-id :RJ29sUU + :first-name "Jay" + :last-name "Rose" + :cover? true + :cover-type :Full} + #inst "2114-12-03"]]) + +(crux/submit-tx + crux + [[:crux.tx/put ;; (1) + {:crux.db/id :consumer/RJ29sUU + :consumer-id :RJ29sUU + :first-name "Jay" + :last-name "Rose" + :cover? true + :cover-type :Full} + #inst "2113-12-03" ;; Valid time start + #inst "2114-12-03"] ;; Valid time end + + [:crux.tx/put ;; (2) + {:crux.db/id :consumer/RJ29sUU + :consumer-id :RJ29sUU + :first-name "Jay" + :last-name "Rose" + :cover? true + :cover-type :Full} + #inst "2112-12-03" + #inst "2113-12-03"] + + [:crux.tx/put ;; (3) + {:crux.db/id :consumer/RJ29sUU + :consumer-id :RJ29sUU + :first-name "Jay" + :last-name "Rose" + :cover? false} + #inst "2112-06-03" + #inst "2112-12-02"] + + [:crux.tx/put ;; (4) + {:crux.db/id :consumer/RJ29sUU + :consumer-id :RJ29sUU + :first-name "Jay" + :last-name "Rose" + :cover? true + :cover-type :Promotional} + #inst "2111-06-03" + #inst "2112-06-03"]]) + +(crux/q (crux/db crux #inst "2114-01-01") + {:find '[cover type] + :where '[[e :consumer-id :RJ29sUU] + [e :cover? cover] + [e :cover-type type]]}) + +(crux/q (crux/db crux #inst "2111-07-03") + {:find '[cover type] + :where '[[e :consumer-id :RJ29sUU] + [e :cover? cover] + [e :cover-type type]]}) + +(crux/q (crux/db crux #inst "2112-07-03") + {:find '[cover type] + :where '[[e :consumer-id :RJ29sUU] + [e :cover? cover] + [e :cover-type type]]}) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :manifest + :pilot-name "Johanna" + :id/rocket "SB002-sol" + :id/employee "22910x2" + :badges ["SETUP" "PUT" "DATALOG-QUERIES" "BITEMP"] + :cargo ["stereo" "gold fish" "slippers" "secret note"]}]]) + +;; Saturn + +(let [data [{:crux.db/id :gold-harmony + :company-name "Gold Harmony" + :seller? true + :buyer? false + :units/Au 10211 + :credits 51} + + {:crux.db/id :tombaugh-resources + :company-namnne "Tombaugh Resources Ltd." + :seller? true + :buyer? false + :units/Pu 50 + :units/N 3 + :units/CH4 92 + :credits 51} + + {:crux.db/id :encompass-trade + :company-name "Encompass Trade" + :seller? true + :buyer? true + :units/Au 10 + :units/Pu 5 + :units/CH4 211 + :credits 1002} + + {:crux.db/id :blue-energy + :seller? false + :buyer? true + :company-name "Blue Energy" + :credits 1000}]] + (easy-ingest crux data)) + +(defn stock-check [company-id item] + {:result (crux/q (crux/db crux) + {:find '[name funds stock] + :where [['e :company-name 'name] + ['e :credits 'funds] + ['e item 'stock]] + :args [{'e company-id}]}) + :item item}) + +(defn format-stock-check [{:keys [result item] :as stock-check}] + (for [[name funds commod] result] + (str "Name: " name ", Funds: " funds ", " item " " commod))) + +(crux/submit-tx crux + [[:crux.tx/match + :blue-energy + {:crux.db/id :blue-energy + :seller? false + :buyer? true + :company-name "Blue Energy" + :credits 1000}] + [:crux.tx/put + {:crux.db/id :blue-energy + :seller? false + :buyer? true + :company-name "Blue Energy" + :credits 900 + :units/CH4 10}] + + [:crux.tx/match + :tombaugh-resources + {:crux.db/id :tombaugh-resources + :company-name "Tombaugh Resources Ltd." + :seller? true + :buyer? false + :units/Pu 50 + :units/N 3 + :units/CH4 92 + :credits 51}] + [:crux.tx/put + {:crux.db/id :tombaugh-resources + :company-name "Tombaugh Resources Ltd." + :seller? true + :buyer? false + :units/Pu 50 + :units/N 3 + :units/CH4 82 + :credits 151}]]) + +(format-stock-check (stock-check :tombaugh-resources :units/CH4)) +(format-stock-check (stock-check :blue-energy :units/CH4)) + +(crux/submit-tx crux + [[:crux.tx/match + :gold-harmony + {:crux.db/id :gold-harmony + :company-name "Gold Harmony" + :seller? true + :buyer? false + :units/Au 10211 + :credits 51}] + [:crux.tx/put + {:crux.db/id :gold-harmony + :company-name "Gold Harmony" + :seller? true + :buyer? false + :units/Au 211 + :credits 51}] + + [:crux.tx/match + :encompass-trade + {:crux.db/id :encompass-trade + :company-name "Encompass Trade" + :seller? true + :buyer? true + :units/Au 10 + :units/Pu 5 + :units/CH4 211 + :credits 100002}] + [:crux.tx/put + {:crux.db/id :encompass-trade + :company-name "Encompass Trade" + :seller? true + :buyer? true + :units/Au 10010 + :units/Pu 5 + :units/CH4 211 + :credits 1002}]]) + +(format-stock-check (stock-check :gold-harmony :units/Au)) +(format-stock-check (stock-check :encompass-trade :units/Au)) + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :manifest + :pilot-name "Johanna" + :id/rocket "SB002-sol" + :id/employee "22910x2" + :badges ["SETUP" "PUT" "DATALOG-QUERIES" "BITEMP" "MATCH"] + :cargo ["stereo" "gold fish" "slippers" "secret note"]}]]) + +;; Jupiter + +(crux/submit-tx crux + [[:crux.tx/put {:crux.db/id :kaarlang/clients + :clients [:encompass-trade]} + #inst "2110-01-01T09" + #inst "2111-01-01T09"] + + [:crux.tx/put {:crux.db/id :kaarlang/clients + :clients [:encompass-trade :blue-energy]} + #inst "2111-01-01T09" + #inst "2113-01-01T09"] + + [:crux.tx/put {:crux.db/id :kaarlang/clients + :clients [:blue-energy]} + #inst "2113-01-01T09" + #inst "2114-01-01T09"] + + [:crux.tx/put {:crux.db/id :kaarlang/clients + :clients [:blue-energy :gold-harmony :tombaugh-resources]} + #inst "2114-01-01T09" + #inst "2115-01-01T09"]]) + +(crux/entity-history + (crux/db crux #inst "2115-01-01T08") + :kaarlang/clients + :desc + {:with-docs true}) + + + +(crux/submit-tx crux + [[:crux.tx/delete :kaarlang/clients + #inst "2110-01-01" + #inst "2116-01-01"]]) + +(crux/entity-history + (crux/db crux #inst "2118-01-01T08") + :kaarlang/clients + :desc + {:with-docs? true}) + +(crux/q (crux/db crux #inst "2116-01-01T08") + {:find ['clients] + :where [['e :clients 'clients] + [:crux.db/id :kaarlang/clients]]}) + +;; Secret + +(crux/submit-tx crux + [[:crux.tx/put + {:crux.db/id :person/kaarlang + :full-name "Kaarlang" + :origin-planet "Mars" + :identity-tag :KA01299242093 + :DOB #inst "2040-11-23"}] + + [:crux.tx/put + {:crux.db/id :person/ilex + :full-name "Ilex Jefferson" + :origin-planet "Venus" + :identity-tag :IJ01222212454 + :DOB #inst "2061-02-17"}] + + [:crux.tx/put + {:crux.db/id :person/thadd + :full-name "Thad Christover" + :origin-moon "Titan" + :identity-tag :IJ01222212454 + :DOB #inst "2101-01-01"}] + + [:crux.tx/put + {:crux.db/id :person/johanna + :full-name "Johanna" + :origin-planet "Earth" + :identity-tag :JA012992129120 + :DOB #inst "2090-12-07"}]]) + +(defn full-query [node] + (crux/q + (crux/db node) + '{:find [id] + :where [[e :crux.db/id id]] + :pull? true})) + +(full-query crux) + +(crux/submit-tx crux [[:crux.tx/evict :person/kaarlang]]) + +