Skip to content

Commit

Permalink
Merge pull request #79 from keboola/kacurez-ad-id-pk
Browse files Browse the repository at this point in the history
ex fb ads: support ad_id in primary key
  • Loading branch information
kacurez committed Oct 9, 2019
2 parents 80842e7 + 300cc77 commit 9f60c3d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/keboola/facebook/api/request.clj
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
(defn page-and-collect
"collect data from response and make another paging requests if needed.
Returns lazy sequence of flattened data resulting from processing the whole query"
[{:keys [ex-account-id parent-id fb-graph-node table-name body-data response] :as init-params}]
[{:keys [ex-account-id parent-id fb-graph-node table-name path body-data response] :as init-params}]
((fn step [params this-object-data rest-objects top-node]
(if (and (empty? rest-objects) (empty? this-object-data))
nil
Expand All @@ -158,6 +158,7 @@
:parent-id (:parent-id next-object)
:fb-graph-node (:fb-graph-node next-object)
:table-name (:name next-object)
:path path
:response (:data next-object)
:body-data (:data (:data next-object)))]
(lazy-seq (cons new-rows (step new-params (:body-data new-params) (rest all-objects) top-node)))))) init-params body-data [] fb-graph-node))
Expand All @@ -184,6 +185,7 @@
:parent-id (name (first %))
:fb-graph-node "page"
:table-name "page"
:path path
:body-data [(if (not-empty path) {sanitized-path (second %)} (second %))]
:response response-body})

Expand All @@ -195,6 +197,7 @@
:parent-id ""
:fb-graph-node "page"
:table-name "page"
:path path
:body-data [(if (not-empty path) {sanitized-path response-body} response-body)]
:response (if (not-empty path) {sanitized-path response-body} response-body)}))))

Expand Down
20 changes: 13 additions & 7 deletions src/keboola/facebook/extractor/output.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@
{"insights" ["age" "country" "dma" "gender" "frequency_value" "hourly_stats_aggregated_by_advertiser_time_zone" "hourly_stats_aggregated_by_audience_time_zone" "impression_device" "place_page_id" "placement" "publisher_platform" "platform_position" "device_platform" "product_id" "region"]
"ratings" ["reviewer_id"]})

(defn get-primary-key [table-columns table-name]
(let [basic-pk ["parent_id"]
(def ENDPOINT-SPECIFIC-PK-MAP
{"" ["ad_id"]})

(defn get-primary-key [table-columns table-name context]
(let [endpoint (:path context)
basic-pk ["parent_id"]
all-tables-pk ["id" "key1" "key2" "end_time" "account_id" "campaign_id" "date_start" "date_stop" "ads_action_name" "action_type" "action_reaction"]
table-only-pk (TABLES-SPECIFIC-PK-MAP table-name #{})
extended-pk (concat all-tables-pk table-only-pk)]
endpoint-only-pk (ENDPOINT-SPECIFIC-PK-MAP endpoint #{})
extended-pk (concat all-tables-pk table-only-pk endpoint-only-pk)]
(concat basic-pk
(filter
(fn [column] (some #(= % (keyword column)) table-columns))
Expand All @@ -53,9 +58,9 @@
(def columns-map (atom {}))
(defn reset-columns-map [] (reset! columns-map {}))

(defn write-manifest [manifest-path columns is-write? table-name]
(defn write-manifest [manifest-path columns is-write? table-name context]
(if is-write?
(let [manifest {:incremental true :primary_key (get-primary-key columns table-name) :columns columns}]
(let [manifest {:incremental true :primary_key (get-primary-key columns table-name context) :columns columns}]
(if (not (contains? @columns-map manifest-path))
(do
(runtime/save-manifest manifest-path manifest)
Expand Down Expand Up @@ -83,10 +88,11 @@
(let [first-write? (:first-write? memo)
header (if first-write?
(prepare-header (:buffer memo) manifest-path)
(:header memo))]
(:header memo))
context (-> memo :buffer first :keboola)]
(if header
(do
(write-manifest manifest-path header first-write? table-name)
(write-manifest manifest-path header first-write? table-name context)
(csv/write-to-file csv-file header (:buffer memo) false)
(if (= (mod (:cnt memo) (* chan-buffer-size 20)) 0)
(runtime/log-strings "Written" (:cnt memo) "rows to" table-name))
Expand Down
23 changes: 13 additions & 10 deletions test/keboola/facebook/extractor/output_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
(is (= (set value) (set complete-expected)))))

(deftest test-primary-key
(test-pk (sut/get-primary-key ["id"] "") [])
(test-pk (sut/get-primary-key [:id] "") ["id"])
(test-pk (sut/get-primary-key ["foo"] "") [])
(test-pk (sut/get-primary-key [:foo] "") [])
(test-pk (sut/get-primary-key [:id :key] "") ["id"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2] "insights") ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :placement :device_platform :foo :key2 :age] "insights") ["id" "placement" "key2" "age" "device_platform"])
(test-pk (sut/get-primary-key [:id :placement :foo :key2 :age :region :country :gender] "asdasfoo") ["id" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2] "") ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2 :reviewer_id] "ratings") ["id" "key1" "key2" "reviewer_id"]))
(test-pk (sut/get-primary-key ["id"] "" {}) [])
(test-pk (sut/get-primary-key [:id] "" {}) ["id"])
(test-pk (sut/get-primary-key ["foo"] "" {}) [])
(test-pk (sut/get-primary-key [:foo] "" {}) [])
(test-pk (sut/get-primary-key [:id :key] "" {}) ["id"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2] "insights" {}) ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :placement :device_platform :foo :key2 :age] "insights" {}) ["id" "placement" "key2" "age" "device_platform"])
(test-pk (sut/get-primary-key [:id :placement :foo :key2 :age :region :country :gender] "asdasfoo" {}) ["id" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2] "" {}) ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2] "" {:path ""}) ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2 :ad_id] "" {:path ""}) ["id" "key1" "key2" "ad_id"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2 :ad_id] "" {:path "ads"}) ["id" "key1" "key2"])
(test-pk (sut/get-primary-key [:id :key1 :foo :key2 :reviewer_id] "ratings" {}) ["id" "key1" "key2" "reviewer_id"]))

0 comments on commit 9f60c3d

Please sign in to comment.