-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable disable all agents within a scenario #1506
enable disable all agents within a scenario #1506
Conversation
@agents = @scenario.agents | ||
|
||
respond_to do |format| | ||
if @agents.update_all disabled: params[:scenario] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be params[:scenario][:disabled]
or params[:disabled]
?
@scenario = current_user.scenarios.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @scenario.agents.update_all disabled: params[:scenario][:disabled] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dsander for some reason update_all
isn't working........but I think it should. Would you know anything that scenario model is hooked up with that potentially make it fail? When I try it manually, it shows 2 records in the log but nothing was "committed".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems right. If you do p @scenario.agents
, do you see the expected agents?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd, if the SQL fails it should raise an exception. Can you paste the log output for a call of the method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is from better errors after I deliberately make it fail to access the console
>> params[:scenario][:disabled]
=> "true"
>> @scenario.agents #here disabled is false
=> #<ActiveRecord::Associations::CollectionProxy [#<Agents::TwitterSearchAgent id: 38, user_id: 1, options: {"search"=>"freebandnames", "expected_update_period_in_days"=>"2"}, type: "Agents::TwitterSearchAgent", name: "Twitter Search Agent", schedule: "every_12h", events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:33", updated_at: "2016-05-16 18:54:39", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "c48ff0f1e8fe5359e8e84601fe618ae5", deactivated: false>, #<Agents::TwitterPublishAgent id: 37, user_id: 1, options: {"expected_update_period_in_days"=>"10", "message"=>"{{text}}"}, type: "Agents::TwitterPublishAgent", name: "Twitter Publish Agent", schedule: nil, events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:16", updated_at: "2016-05-16 18:59:40", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "b0cc289b0c160778033718bdd3f8dd6a", deactivated: false>, #<Agents::TwitterFavorites id: 36, user_id: 1, options: {"username"=>"tectonic", "number"=>"10", "history"=>"100", "expected_update_period_in_days"=>"2"}, type: "Agents::TwitterFavorites", name: "Twitter Favorites", schedule: "every_12h", events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:02", updated_at: "2016-05-16 19:03:26", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "03c480dfb76dd1a254309bee864ec7ae", deactivated: false>]>
>> @scenario.agents.update_all disabled: params[:scenario][:disabled]
=> 3 #does not show committed?
>> @scenario.agents #here disabled is still false
=> #<ActiveRecord::Associations::CollectionProxy [#<Agents::TwitterSearchAgent id: 38, user_id: 1, options: {"search"=>"freebandnames", "expected_update_period_in_days"=>"2"}, type: "Agents::TwitterSearchAgent", name: "Twitter Search Agent", schedule: "every_12h", events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:33", updated_at: "2016-05-16 18:54:39", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "c48ff0f1e8fe5359e8e84601fe618ae5", deactivated: false>, #<Agents::TwitterPublishAgent id: 37, user_id: 1, options: {"expected_update_period_in_days"=>"10", "message"=>"{{text}}"}, type: "Agents::TwitterPublishAgent", name: "Twitter Publish Agent", schedule: nil, events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:16", updated_at: "2016-05-16 18:59:40", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "b0cc289b0c160778033718bdd3f8dd6a", deactivated: false>, #<Agents::TwitterFavorites id: 36, user_id: 1, options: {"username"=>"tectonic", "number"=>"10", "history"=>"100", "expected_update_period_in_days"=>"2"}, type: "Agents::TwitterFavorites", name: "Twitter Favorites", schedule: "every_12h", events_count: 0, last_check_at: nil, last_receive_at: nil, last_checked_event_id: nil, created_at: "2016-05-11 21:25:02", updated_at: "2016-05-16 19:03:26", memory: {}, last_web_request_at: nil, keep_events_for: 0, last_event_at: nil, last_error_log_at: nil, propagate_immediately: false, disabled: false, service_id: 1, guid: "03c480dfb76dd1a254309bee864ec7ae", deactivated: false>]>
I don't see any errors in my rails log
I see a before_save
call attach to the model which I commented out but it didn't go through. Also I recalled I can update a single record with update_attributes
and had it successfully committed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks totally fine to me, in the rails console you need to call @scenario.agents(true)
or @scenario.reload; @scenario.agents
to fetch the updated data from the database.
The log output also shows that sql UPDATE
call was successful, are the agents not shown as disabled in the UI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried reloading the scenario and the agents disabled status is still false.
The params is the same (with true status)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3t0nIpcahAGCZq6zueMZMnrc/fUATkEqRleoT6zB96lNZF9cIjzS3hT3fZ7tZPjmlW+DVv4JhSQChlme80DAsA==", "scenario"=>{"disabled"=>"true"}, "commit"=>"Yes", "id"=>"18"}
and no the agents did not showed disabled in the UI.
@scenario.agents(true)
and @scenario.agents
bring up records but is not updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind I fixed it, all I did was change true
to 1
and false
to 0
........
$('#disabledfield').val 1 | ||
$('#enable_all').click -> | ||
$('.modal-body').text 'Would you like to enable all agents?' | ||
$('#disabledfield').val 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it alright to have two functions within one instance property? I was looking at agent-show-page.js.coffee
and it seems to be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems ok
flash[:notice] = 'The agents has been successfully updated.' | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I whittled this down to a bit because of rubocop assignment branch error (method does too much). I think this is more akin to the destroy
method below it than the update
method above it. The likelihood that this will fail is on the low side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry about the code complexity offenses of rubocop too much, in my suggestion #1505 I disabled them completely because the default values are really low and almost impossible to achieve in a rails project (at least in my experience) 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if we are going to use rubocop from now on to check the code style. I actually grab the yml file from your pr. I will put what I had back in later.
@cantino Will you code review this as well when you have time? I am having trouble with a bug that ci pointed out with |
@@ -95,6 +95,16 @@ def update | |||
end | |||
end | |||
|
|||
def enable_or_disable_all_agents | |||
@scenario = current_user.scenarios.find(params[:id]) | |||
@scenario.agents.update_all disabled: params[:scenario][:disabled] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specs are failing on postgres because it handles boolean field differenctly then mysql. I think there are two options, either cast the param value to a ruby boolean here (!!params[:scenario][:disabled].to_i
or params[:scenario][:disabled] == 'true' ? true : false
) or change the hidden field to a hidden checkbox whose value rails automatically casts to a boolean.
expect(response).to redirect_to(scenario_path(scenarios(:bob_weather))) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure why this test isn't passing. The params
are in hash
and it should be getting put
correctly. I also tried post
. Locally it is fine but the test just isn't passing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind I fixed it. I just need to modify params a bit and is straight from my log.
@cantino This is good for a code review as well. |
@@ -0,0 +1,15 @@ | |||
class @ScenarioShowPage | |||
constructor:() -> | |||
@changeModaltext() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: changeModalText
Sorry for the delay. I commented on a couple of minor things. Looking good! |
@scenario = current_user.scenarios.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @scenario.agents.update_all disabled: (params[:scenario][:disabled]) == 'true' ? true : false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's an extra parentheses here.
You could just do:
@scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I don't believe that update_all can return false, so the else
case will never get executed?
@cantino Sorry I was away for something lately. I will find time for that other ticket that I have. I just put in two more fixes for your last comments, tested it locally and its good for another review. If all things go well I will squash it before we merge. |
@scenario = current_user.scenarios.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest just:
@scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true')
respond_to do |format|
format.html { redirect_to @scenario, notice: 'The agents in this scenario have been successfully updated.' }
format.json { head :no_content }
end
No if
, since the return value of update_all
isn't meaningful.
Hi @cantino I just removed the unnecessary if end statement. I think we should be good now. |
Merged, thanks for the nice new feature @Jngai! |
Enabling or Disabling all agents within a scenario by updating all agents
:disabled
status. Showing a button and popping up a modal (partially inspired and code reused from the modal in action menu).