Skip to content

Commit

Permalink
CV2-5050 accommodate changes on add callbacks to not search inside item
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Oct 30, 2024
1 parent acb3b60 commit 917643c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 115 deletions.
16 changes: 8 additions & 8 deletions app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,20 +549,20 @@ def get_items_with_similar_media_v2(args={})

def process_alegre_callback(params)
redis = Redis.new(REDIS_CONFIG)
project_media = ProjectMedia.find(params.dig('data', 'item', 'raw', 'context', 'project_media_id')) rescue nil
project_media = ProjectMedia.find(params.dig('data', 'raw', 'context', 'project_media_id')) rescue nil
should_relate = true
if project_media.nil?
project_media = TemporaryProjectMedia.new
project_media.text = params.dig('data', 'item', 'raw', 'text')
project_media.url = params.dig('data', 'item', 'raw', 'url')
project_media.id = params.dig('data', 'item', 'raw', 'context', 'project_media_id')
project_media.team_id = params.dig('data', 'item', 'raw', 'context', 'team_id')
project_media.field = params.dig('data', 'item', 'raw', 'context', 'field')
project_media.text = params.dig('data', 'raw', 'text')
project_media.url = params.dig('data', 'raw', 'url')
project_media.id = params.dig('data', 'raw', 'context', 'project_media_id')
project_media.team_id = params.dig('data', 'raw', 'context', 'team_id')
project_media.field = params.dig('data', 'raw', 'context', 'field')
project_media.type = params['model_type']
should_relate = false
end
confirmed = params.dig('data', 'item', 'raw', 'confirmed')
field = params.dig('data', 'item', 'raw', 'context', 'field')
confirmed = params.dig('data', 'raw', 'confirmed')
field = params.dig('data', 'raw', 'context', 'field')
access_key = confirmed ? :confirmed_results : :suggested_or_confirmed_results
key = get_required_keys(project_media, field)[access_key]
response = cache_items_via_callback(project_media, field, confirmed, params.dig('data', 'results', 'result').dup) #dup so we can better debug when playing with this in a repl
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/alegre_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def webhook(request)
redis = Redis.new(REDIS_CONFIG)
doc_id = body.dig('data', 'requested', 'id')
# search for doc_id on completed full-circuit callbacks
doc_id = body.dig('data', 'item', 'id') if doc_id.nil?
doc_id = body.dig('data', 'id') if doc_id.nil?
# search for doc_id on completed short-circuit callbacks (i.e. items already known to Alegre but added context TODO make these the same structure)
doc_id = body.dig('data', 'item', 'raw', 'doc_id') if doc_id.nil?
doc_id = body.dig('data', 'raw', 'doc_id') if doc_id.nil?
if doc_id.blank?
CheckSentry.notify(AlegreCallbackError.new('Unexpected params format from Alegre'), params: {alegre_response: request.params, body: body})
end
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/webhooks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def setup

test "should process Alegre callback webhook with is_shortcircuited_search_result_callback" do
id = random_number
payload = { 'action' => 'audio', 'data' => {'is_shortcircuited_search_result_callback' => true, 'item' => { 'callback_url' => '/presto/receive/add_item', 'id' => id.to_s }} }
payload = { 'action' => 'audio', 'data' => {'is_shortcircuited_search_result_callback' => true, 'callback_url' => '/presto/receive/add_item', 'id' => id.to_s} }
Bot::Alegre.stubs(:process_alegre_callback).returns({})
post :index, params: { name: :alegre, token: CheckConfig.get('alegre_token') }, body: payload.to_json
assert_equal '200', response.code
Expand All @@ -258,7 +258,7 @@ def setup

test "should process Alegre callback webhook with is_search_result_callback" do
id = random_number
payload = { 'action' => 'audio', 'data' => {'is_search_result_callback' => true, 'item' => { 'callback_url' => '/presto/receive/add_item', 'id' => id.to_s }} }
payload = { 'action' => 'audio', 'data' => {'is_search_result_callback' => true, 'callback_url' => '/presto/receive/add_item', 'id' => id.to_s} }
Bot::Alegre.stubs(:process_alegre_callback).returns({})
post :index, params: { name: :alegre, token: CheckConfig.get('alegre_token') }, body: payload.to_json
assert_equal '200', response.code
Expand Down
196 changes: 93 additions & 103 deletions test/models/bot/alegre_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -870,29 +870,27 @@ def teardown
params = {
"model_type": "image",
"data": {
"item": {
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010"
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010",
"results": {
"result": [
{
Expand All @@ -918,29 +916,27 @@ def teardown
unconfirmed_params = {
"model_type": "image",
"data": {
"item": {
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.63,
"confirmed": false,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010"
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.63,
"confirmed": false,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010",
"results": {
"result": [
{
Expand Down Expand Up @@ -982,28 +978,26 @@ def teardown
"model_type": "image",
"data": {
"is_shortcircuited_search_result_callback": true,
"item": {
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.85,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010"
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.85,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010",
"results": {
"result": [
{
Expand All @@ -1030,28 +1024,26 @@ def teardown
"model_type": "image",
"data": {
"is_shortcircuited_search_result_callback": true,
"item": {
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": false,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": pm1.id,
"has_custom_id": true,
"temporary_media": false,
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010"
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": false,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010",
"results": {
"result": [
{
Expand Down Expand Up @@ -1092,29 +1084,27 @@ def teardown
params = {
"model_type": "image",
"data": {
"item": {
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": 123456789,
"has_custom_id": true,
"temporary_media": true,
},
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
"id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"callback_url": "http://alegre:3100/presto/receive/add_item/image",
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"text": nil,
"raw": {
"doc_id": "Y2hlY2stcHJvamVjdF9tZWRpYS0yMTQt",
"context": {
"team_id": pm1.team_id,
"project_media_id": 123456789,
"has_custom_id": true,
"temporary_media": true,
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010"
"url": "http://minio:9000/check-api-dev/uploads/uploaded_image/55/09572dedf610aad68090214303c14829.png",
"threshold": 0.73,
"confirmed": true,
"created_at": "2024-03-14T22:05:47.588975",
"limit": 200,
"requires_callback": true,
"final_task": "search"
},
"hash_value": "1110101010001011110100000011110010101000000010110101101010100101101111110101101001011010100001011111110101011010010000101010010110101101010110100000001010100101101010111110101000010101011100001110101010101111100001010101001011101010101011010001010101010010",
"results": {
"result": [
{
Expand Down

0 comments on commit 917643c

Please sign in to comment.