From f5cc838acc48a90afae6015cb484144e0506952b Mon Sep 17 00:00:00 2001 From: David Whittington Date: Wed, 23 Oct 2024 15:28:43 -0500 Subject: [PATCH] refactor: make updating OI knowledge a babashka task Switching to babashka tasks to help organize the code a bit better. --- bb.edn | 4 ++++ bb/metasys/tasks.clj | 43 +++++++++++++++++++++++++++++++++ deps.edn | 1 + scripts/update-oi-files | 20 ---------------- scripts/update-oi-knowledge | 47 ------------------------------------- 5 files changed, 48 insertions(+), 67 deletions(-) create mode 100644 bb.edn create mode 100644 bb/metasys/tasks.clj create mode 100644 deps.edn delete mode 100755 scripts/update-oi-files delete mode 100755 scripts/update-oi-knowledge diff --git a/bb.edn b/bb.edn new file mode 100644 index 00000000..c70257bb --- /dev/null +++ b/bb.edn @@ -0,0 +1,4 @@ +{:paths ["bb"] + :tasks + {:requires ([metasys.tasks :as tasks]) + update-oi-knowledge (tasks/update-oi-knowledge)}} diff --git a/bb/metasys/tasks.clj b/bb/metasys/tasks.clj new file mode 100644 index 00000000..39063db9 --- /dev/null +++ b/bb/metasys/tasks.clj @@ -0,0 +1,43 @@ +(ns metasys.tasks + (:require [babashka.http-client :as http] + [cheshire.core :as json] + [clojure.edn :as edn] + [clojure.string :as str] + [taoensso.timbre :as log])) + +(defn strip-quotes [s] + (clojure.string/replace s #"^[\"](.*)[\"]$" "$1")) + +(defn read-env [] + (->> (slurp ".env") + (str/split-lines) + (remove #(str/starts-with? % "#")) ; Remove comments + (remove #(str/blank? %)) ; Remove blank lines + (map #(str/split % #"\s*=\s*")) ; Split key-value pairs + (map (fn [[k v]] [k (strip-quotes v)])) ; Strip value leading and trailing quotes + (into {}))) + +;; Read and parse .env into a map +(def env (read-env)) + +(def oi-api-key (env "OI_API_KEY")) +(def oi-url (env "OI_URL")) + +(def oi-knowledge (-> "oi-knowledge.edn" slurp edn/read-string)) + +(defn update-oi-knowledge [] + (doseq [[k-id k] oi-knowledge] + (doseq [[f-id path] (:files k) + :let [f-url (str oi-url "/api/v1/files/" f-id "/data/content/update") + k-url (str oi-url "/api/v1/knowledge/" k-id "/file/update")]] + (log/infof "Updating file %s in knowledge %s with contents of %s..." f-id k-id path) + (http/post f-url + {:headers {:content-type "application/json"} + :oauth-token oi-api-key + :body (json/generate-string {"content" (slurp path)})}) + (log/infof "Updating file %s in knowledge %s..." f-id k-id path) + (http/post k-url + {:headers {:content-type "application/json"} + :oauth-token oi-api-key + :body (json/generate-string {"file_id" f-id})}) + (log/info "Updates complete.")))) diff --git a/deps.edn b/deps.edn new file mode 100644 index 00000000..07d7eab6 --- /dev/null +++ b/deps.edn @@ -0,0 +1 @@ +{:paths ["bb"]} diff --git a/scripts/update-oi-files b/scripts/update-oi-files deleted file mode 100755 index 75df6cea..00000000 --- a/scripts/update-oi-files +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -# Load the .env -set -o allexport -source .env -set +o allexport - -content="$(jq -sRr @json < docs/madr/001-clickhouse-gql.md)" - -curl -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $OI_API_KEY" \ - -d "{ \"content\": $content }" \ - "$OI_URL/api/v1/files/9fe5e13f-b294-49d4-94fa-5a55c8a0dcea/data/content/update" - -curl -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $OI_API_KEY" \ - -d "{ \"file_id\": \"9fe5e13f-b294-49d4-94fa-5a55c8a0dcea\" }" \ - "$OI_URL/api/v1/knowledge/33c0fd24-c547-4d66-b9d3-aaad2151f651/file/update" diff --git a/scripts/update-oi-knowledge b/scripts/update-oi-knowledge deleted file mode 100755 index 5fc02885..00000000 --- a/scripts/update-oi-knowledge +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env bb - -;; TODO: turn this into a babashka task - -(require '[clojure.string :as str]) -(require '[clojure.edn :as edn]) -(require '[babashka.http-client :as http]) -(require '[taoensso.timbre :as log]) -(require '[cheshire.core :as json]) - -(defn strip-quotes [s] - (clojure.string/replace s #"^[\"](.*)[\"]$" "$1")) - -;; Read and parse .env into a map -(def env (->> (slurp ".env") - (str/split-lines) - (remove #(str/starts-with? % "#")) ; Remove comments - (remove #(str/blank? %)) ; Remove blank lines - (map #(str/split % #"\s*=\s*")) ; Split key-value pairs - (map (fn [[k v]] [k (strip-quotes v)])) ; Strip value leading and trailing quotes - (into {}))) - -(def oi-api-key (env "OI_API_KEY")) -(def oi-url (env "OI_URL")) - -(def oi-knowledge (-> "oi-knowledge.edn" slurp edn/read-string)) - -(doseq [[k-id k] oi-knowledge] - (doseq [[f-id path] (:files k) - :let [f-url (str oi-url "/api/v1/files/" f-id "/data/content/update") - k-url (str oi-url "/api/v1/knowledge/" k-id "/file/update")]] - (prn f-url) - (prn k-url) - (prn [k-id f-id path]) - (log/infof "Updating file %s in knowledge %s with contents of %s..." f-id k-id path) - (http/post f-url - {:headers {:content-type "application/json"} - :oauth-token oi-api-key - :body (json/generate-string {"content" (slurp path)})}) - (log/infof "Updating file %s in knowledge %s..." f-id k-id path) - (http/post k-url - {:headers {:content-type "application/json"} - :oauth-token oi-api-key - :body (json/generate-string {"file_id" f-id})}) - (log/info "Updates complete."))) - -;; vim: ft=clojure