Are my Rails methods/helper functions executing every time anyone does a GET request to the server, in parallel? Is this an issue? - html

My website is mostly to display results, so the admin enters the results and they are rendered nicely for the users/clients. Currently, in my main_controller.rb and other controllers I have functions for the main index page...
def index
#next_tournament = helpers.next_tournament()
#last_tournament = helpers.last_tournament()
#last_tournament_date_display = helpers.last_tournament_date_display(#last_tournament)
#next_tournament_date_display = helpers.next_tournament_date_display(#next_tournament)
#current_season = #last_tournament.season_year
#aoys = helpers.display_aoy(#last_tournament.id)
#aoy_diff = helpers.aoy_point_diff(#aoys)
#individual_winners = helpers.individual_winners(#last_tournament.id)
#team_winners = helpers.team_winners(#last_tournament.id)
#big_bass_winners = helpers.big_bass_winners(#last_tournament.id)
#total_club_stats = helpers.total_club_stats()
#annual_club_stats = helpers.annual_club_stats(#current_season )
...
end
Does this mean, that for every person that navigates to my main index page the server will re-run these functions and spit out the result?
I'm trying to understand what happens on the server side. Would this be a problem? Most of these functions would only change when a new result is entered. Is it best practice to do something in the code, so that the function is only re-ran when any of the variables in the equation change, and otherwise static?

Related

Can't wrap my head around MySQL statement

I have two tables:
cache and main
In cache there are a lot of fields; in main a little less. A UNION is not going to work because of the unequal number of columns.
cache
client - file - target - many other columns
main
client - file - target - few other columns
From cache I would like all columns for which main.target LIKE '%string%', cache.client = main.client, cache.file = main.file
For these particular records, target, client and file are always the same in main and cache.
I just can't get my head around this, but then again MySQL never was my strongest point.
Thank you very much in advance!
In the end combining the two SELECT statements with a UNION made things very complicated, for the simple reason there were countless other queries, some without UNION, that in the end all had to be processed by the same end routine presenting the results. As this was only a one-time query and time wasn't really an issue, in the end I just used SELECT on the two different tables and then combined the results by checking if a certain field was present. If not, the remaining results had to be fetched from the cache table; if so, the remaining results had to be fetched from the main table.
I actually wonder whether this solution is faster, slower or just as fast.
if (!isset($row['current']))
{
$field = $row['field'];
$sqlcache = "SELECT * FROM " . $dbtable . " WHERE (client = '$sqlclient' AND file = '$sqlfile' AND field = '$field')";
$resultcache = $conn->query($sqlcache);
if (!$resultcache)
{
die($conn->error);
}
$rowcache = $resultcache->fetch_assoc();
$currenttarget = $rowcache['current'];
$context = $rowcache['context'];
$dirtysource = $rowcache['dirtysource'];
$stringid = $rowcache['stringid'];
$limit = $rowcache['maxlength'];
$locked = $rowcache['locked'];
$filei = $rowcache['filei'];
}
else
{
$currenttarget = $row['current'];
$context = $row['context'];
$dirtysource = $row['dirtysource'];
$stringid = $row['stringid'];
$limit = $row['maxlength'];
$locked = $row['locked'];
$filei = $row['filei'];
}

Trying to populate dynamically infoblox domain-name with ruby & json

I'm using this code repo for registering newly provisioned vm's with infoblox providing the next available ip address.
What i want to achieve now, is to get also dynamically the domain-name of the used infoblox network.
I tried to copy list_infoblox_networks.rb into a modified "list_infoblox_domain.rb" with the following code below:
if networks_array.blank?
# get an array of networks from infoblox
network_search_filter = "network?network_view=#{network_view}"
# specify fields to return in payload
return_fields = "&_return_fields=network_view,network,netmask,ipv4addr,extattrs,comment,options"
networks_array = call_infoblox(:get, "#{network_search_filter}" + "#{return_fields}")
$evm.log(:info, "Inspecting networks_array: #{networks_array.inspect}")
networks_array.each do |net|
comment = "#{net['comment']}#" if net['comment']
display_string = "#{comment}#{net['options'][0]},#{net['network_view']}"
dialog_hash[net['options'][0]] = display_string
end
end
The expected result should return:
testlab.local from network 192.168.254.0/24, actual result displays the whole hash with all options. dropdown of available infoblox domains
A call to infoblox gives me the following hash back:
<AEMethod list_infoblox_domain> Inspecting networks_array: [{"_ref"=>"network/ZG5zLm5ldHdvcmskMTAuMC4yMC4wLzI0LzA:10.0.20.0/24/default", "comment"=>"test-netz", "extattrs"=>{}, "ipv4addr"=>"10.0.20.0", "netmask"=>24, "network"=>"10.0.20.0/24", "network_view"=>"default", "options"=>[{"name"=>"dhcp-lease-time", "num"=>51, "use_option"=>false, "value"=>"43200", "vendor_class"=>"DHCP"}]}, {"_ref"=>"network/ZG5zLm5ldHdvcmskMTAuMC4zMC4wLzI0LzA:10.0.30.0/24/default", "comment"=>"zweites test-netz", "extattrs"=>{}, "ipv4addr"=>"10.0.30.0", "netmask"=>24, "network"=>"10.0.30.0/24", "network_view"=>"default", "options"=>[{"name"=>"dhcp-lease-time", "num"=>51, "use_option"=>false, "value"=>"43200", "vendor_class"=>"DHCP"}]}, {"_ref"=>"network/ZG5zLm5ldHdvcmskMTkyLjE2OC4yNTQuMC8yNC8w:192.168.254.0/24/default", "comment"=>"prod netz", "extattrs"=>{}, "ipv4addr"=>"192.168.254.0", "netmask"=>24, "network"=>"192.168.254.0/24", "network_view"=>"default", "options"=>[{"name"=>"broadcast-address", "num"=>28, "use_option"=>true, "value"=>"192.168.254.255", "vendor_class"=>"DHCP"}, {"name"=>"dhcp-lease-time", "num"=>51, "use_option"=>false, "value"=>"43200", "vendor_class"=>"DHCP"}, {"name"=>"domain-name-servers", "num"=>6, "use_option"=>true, "value"=>"192.168.254.10", "vendor_class"=>"DHCP"}, {"name"=>"domain-name", "num"=>15, "use_option"=>true, "value"=>"testlab.local", "vendor_class"=>"DHCP"}, {"name"=>"routers", "num"=>3, "use_option"=>true, "value"=>"192.168.254.1", "vendor_class"=>"DHCP"}]}]
it would be great, if somebody could help me to get this flying!

Update planned order - two committed modifications, only one saved

I need to update two information on one object: the quantity (PLAF-gsmng) and refresh the planned order via the module function 'MD_SET_ACTION_PLAF'.
I successfully find a way to update each data separately. But when I execute the both solutions the second modification is not saved on the database.
Do you know how I can change the quantity & set the action on PLAF (Planned order) table ?
Do you know other module function to update only the quantity ?
Maybe a parameter missing ?
It's like if the second object is locked (sm12 empty, no sy-subrc = locked) ... and the modification is not committed.
I tried to:
change the order of the algorithm (refresh and after, change PLAF)
add, remove, move the COMMIT WORK & COMMIT WORK AND WAIT
add DEQUEUE_ALL or DEQUEUE_EMPLAFE
This is the current code:
1) Read the data
lv_plannedorder = '00000000001'
"Read PLAF data
SELECT SINGLE * FROM PLAF INTO ls_plaf WHERE plnum = lv_plannedorder.
2) Update Quantity data
" Standard configuration for FM MD_PLANNED_ORDER_CHANGE
CLEAR ls_610.
ls_610-nodia = 'X'. " No dialog display
ls_610-bapco = space. " BAPI type. Do not use mode 2 -> Action PLAF-MDACC will be autmatically set up to APCH by the FM
ls_610-bapix = 'X'. " Run BAPI
ls_610-unlox = 'X'. " Update PLAF
" Customize values
MOVE p_gsmng TO ls_plaf-gsmng. " Change quantity value
MOVE sy-datlo TO ls_plaf-mdacd. " Change by/datetime, because ls_610-bapco <> 2.
MOVE sy-uzeit TO ls_plaf-mdact.
CALL FUNCTION 'MD_PLANNED_ORDER_CHANGE'
EXPORTING
ecm61o = ls_610
eplaf = ls_plaf
EXCEPTIONS
locked = 1
locking_error = 2
OTHERS = 3.
" Already committed on the module function
" sy-subrc = 0
If I go on the PLAF table, I can see that the quantity is edited. It's working :)
3) Refresh BOM & change Action (MDACC) and others fields
CLEAR ls_imdcd.
ls_imdcd-pafxl = 'X'.
CALL FUNCTION 'MD_SET_ACTION_PLAF'
EXPORTING
iplnum = lv_plannedorder
iaccto = 'BOME'
iaenkz = 'X'
imdcd = ls_imdcd
EXCEPTIONS
illegal_interface = 1
system_failure = 2
error_message = 3
OTHERS = 4.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.
If I go on the table, no modification (only the modif. of the part 2. can be found on it).
Any idea ?
Maybe because the ls_610-bapco = space ?
It should be possible to update planned order quantity with MD_SET_ACTION_PLAF too, at least SAP Help tells us so. Why don't you use it like that?
Its call for changing the quantity should possibly look like this:
DATA: lt_acct LIKE TABLE OF MDACCTO,
ls_acct LIKE LINE OF lt_acct.
ls_acct-accto = 'BOME'.
APPEND lt_acct.
ls_acct-accto = 'CPOD'.
APPEND lt_acct.
is_mdcd-GSMNG = 'value' "updated quantity value
CALL FUNCTION 'MD_SET_ACTION_PLAF'
EXPORTING
iplnum = iv_plnum
iaenkz = 'X'
IVBKZ = 'X'
imdcd = is_mdcd "filled with your BOME-related data + new quantity
TABLES
TMDACCTO = lt_accto
EXCEPTIONS
illegal_interface = 1
system_failure = 2
error_message = 3.
So there is no more need for separate call of MD_PLANNED_ORDER_CHANGE anymore and no more problems with update.
I used word possibly because I didn't find any example of this FM call in the Web (and SAP docu is quite ambiguous), so I propose this solution just as is, without verification.
P.S. Possible actions are listed in T46AS table, and possible impact of imdcd fields on order can be checked in MDAC transaction. It is somewhat GUI equivalent of this FM for single order.

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points

page number variable: html,django

i want to do paging. but i only want to know the current page number, so i will call the webservice function and send this parameter and recieve the curresponding data. so i only want to know how can i be aware of current page number? i'm writing my project in django and i create the page with xsl. if o know the page number i think i can write this in urls.py:
url(r'^ask/(\d+)/$',
'ask',
name='ask'),
and call the function in views.py like:
ask(request, pageNo)
but i don't know where to put pageNo var in html page. (so fore example with pageN0=2, i can do pageNo+1 or pageNo-1 to make the url like 127.0.0.01/ask/3/ or 127.0.0.01/ask/2/). to make my question more cleare i want to know how can i do this while we don't have any variables in html?
sorry for my crazy question, i'm new in creating website and also in django. :">
i'm creating my html page with xslt. so i send the total html page. (to show.html which contains only {{str}} )
def ask(request:
service = GetConfigLocator().getGetConfigHttpSoap11Endpoint()
myRequest = GetConfigMethodRequest()
myXml = service.GetConfigMethod(myRequest)
myXmlstr = myXml._return
styledoc = libxml2.parseFile("ask.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseDoc(myXmlstr)
result = style.applyStylesheet(doc, None)
out = style.saveResultToString( result )
ok = mark_safe(out)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()
return render_to_response("show.html", {
'str': ok,
}, context_instance=RequestContext(request))
i'm not working with db and i just receive xml file to parse it. so i don't have contact_list = Contacts.objects.all(). can i still use this way? should i put the first parameter inpaginator = Paginator(contact_list, 25) blank?
if you user standart django paginator, thay send you to url http://example.com/?page=N, where N - number you page
So,
# urls.py
url('^ask/$', 'ask', name='viewName'),
You can get page number in views:
# views.py
def ask(request):
page = request.GET.get('page', 1)