rails query Virtual columns is not exits - ruby-on-rails

I have write down this query in rails 4
result = User.select("id, (3959 * acos( cos( radians("+latitude+") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians("+longitude+") ) + sin( radians(" +latitude +") ) * sin( radians( latitude ) ) ) ) as distants")
when I inspect the data, it does show me distants virtual fields? How can i display it?

Its actually present in the ActiveRecord object. You can get it by accessing it as a key of that object:
result = User.select("id, (3959 * acos( cos( radians("+latitude+") ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians("+longitude+") ) + sin( radians(" +latitude +") ) * sin( radians( latitude ) ) ) ) as distants")
result.each do |user|
puts user['distants']
end

Related

DarkaOnLine/l5-swagger Laravel Array Input Incorrect

I am trying to send an array using swagger. But when I check the array in inspect mode, I notice that the array being send is just a string and is not an array
/**
* #OA\Post(
* path="/api/update-landing-page",
* summary="Update Landing Page",
* description="Update Landing Page",
* tags={"Landing Page Backend"},
* security={{"bearer": {} }},
* #OA\RequestBody(
* required=true,
* #OA\MediaType(
* mediaType="multipart/form-data",
* #OA\Schema(
* required={"businesses","image"},
* #OA\Property(
* property="businesses",
* description="Business ID",
* type="array",
* #OA\Items(type="string", format="id"),
* ),
* #OA\Property(property="image", type="string", format="binary")
* )
* )
* ),
* #OA\Response(
* response=200,
* description="",
* #OA\MediaType(
* mediaType="application/json",
* )
* )
* )
*/
I want to have an array of business IDs. Which look something like this
businesses: [1,2,3].
But When I click in inspect mode, I notice it turns into a string
How do I make it turn into an array?
Thanks for taking you time and appreciate it.
I managed to solve this problem with:
#OA\Property(
property="businesses[]",
description="Business ID",
type="array",
collectionFormat="multi",
#OA\Items(type="string", format="id"),
),
Not sure whether this will help others, but this is what works for me.

Swagger PHP - Describe array of objects

I use Laravel + Swagger PHP. In one of my services, I handle parameters like this :
$params = [
'addressChild' => [
'address' => ' ... ',
'city' => ' ... ',
],
'addressParent' => [
'address' => ' ... ',
'city' => ' ... ',
],
];
I am trying to describe this schema in Swagger Annotations, using OpenApi 3 :
/**
* #OA\Post(
* path="/entity/{entity}/addresses",
* #OA\Response(
* response=200,
* #OA\JsonContent()
* ),
* #OA\RequestBody(
* description="Addresses to store",
* required=true,
* #OA\JsonContent(
* type="object",
* #OA\Property()
* ),
* ),
* )
*
*/
public function update(Request $request)
{
// ...
}
I tried many things like :
#OA\JsonContent(
type="object",
#OA\Property(name="addressKid", ref="#/components/schemas/ParkingAddressRequest") // error
),
My address data is described in ref="#/components/schemas/ParkingAddressRequest" schema, how can I set it as an array in RequestBody ?
If I understood your problem, you could do it like this
/**
* #OA\Post(
* path="/entity/{entity}/addresses",
* #OA\Response(
* response=200,
* #OA\JsonContent()
* ),
* #OA\RequestBody(
* description="Addresses to store",
* required=true,
* #OA\JsonContent(
* type="array",
* #OA\Items(ref="#/components/schemas/ParkingAddressRequest")
* ),
* ),
* )
*
*/

It is necessary to generate the swagger json correctly for generate SDK

The problem is that I need the documentation to show that it is possible to transfer the field colums in the form of an array and in it the following fields (name, description, created_at, updated_at, author). json is successfully generated but in the swagger-editor documentation it is not visible these fields
/**
* #SWG\Get(path="/articles",
* tags={"Article"},
* summary="Get all Articles",
* description="Show list of Articles",
* operationId="all",
* produces={"application/json"},
* #SWG\Parameter(
* name="columns",
* in="query",
* description="get specific columns",
* required=false,
* type="array",
* collectionFormat="multi",
* #SWG\Items(
* type="string",
* #SWG\Property(property="name", type="string"),
* #SWG\Property(property="description", type="string"),
* #SWG\Property(property="author", type="string"),
* #SWG\Property(property="created_at", type="string"),
* #SWG\Property(property="updated_at)", type="string")
* )
* ),
* #SWG\Response(response="200"),
* #SWG\Response(response=500)
* ),
* )
*/

Spline with multiple durations

I'm trying to make a spline that has multiple durations. Basically, I want to create a spline generated through user supplied key frames. There may be multiple key frames with different durations. So I ask, how would I make a that travels at different speeds through different key frames. Let's say I want there to be 1 second between key frame A and B, but 5 seconds between B and C. Similar to the way animation editors like Source Filmmaker, or Autodesk Maya do it. Every time I've seen someone make a spline, it always has one, constant speed. Always X seconds between key frames, but this is not how animation editors work, as they have different speeds, that's what I want.
Just to note, I have tried. I made the duration change once it gets to a different key frame, but that just instantly slows it down, like some slow motion movie effect, which is not what I'm looking for. Do I just gradually transition the speed to fit that of the next key frame? Is there just some equation?
function smooth( points, steps ) --points is an array, steps is how many frames inbetween spline points
if #points < 3 then
return points
end
local steps = steps or 5
local spline = {}
local count = #points - 1
local p0, p1, p2, p3, x, y, z
for i = 1, count do
if i == 1 then
p0, p1, p2, p3 = points[i], points[i], points[i + 1], points[i + 2]
elseif i == count then
p0, p1, p2, p3 = points[#points - 2], points[#points - 1], points[#points], points[#points]
else
p0, p1, p2, p3 = points[i - 1], points[i], points[i + 1], points[i + 2]
end
for t = 0, 1, 1 / steps do
-- Main spline equation
x = 1 * ( ( 2 * p1.x ) + ( p2.x - p0.x ) * t + ( 2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x ) * t * t + ( 3 * p1.x - p0.x - 3 * p2.x + p3.x ) * t * t * t )
y = 1 * ( ( 2 * p1.y ) + ( p2.y - p0.y ) * t + ( 2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y ) * t * t + ( 3 * p1.y - p0.y - 3 * p2.y + p3.y ) * t * t * t )
z = 1 * ( ( 2 * p1.z ) + ( p2.z - p0.z ) * t + ( 2 * p0.z - 5 * p1.z + 4 * p2.z - p3.z ) * t * t + ( 3 * p1.z - p0.z - 3 * p2.z + p3.z ) * t * t * t )
if not(#spline > 0 and spline[#spline].x == x and spline[#spline].y == y and spline[#spline].z == z) then
table.insert( spline , { x = x , y = y, z = z } )
end
end
end
return spline
end
Straightforward approach was used:
local zero_vector = {0, 0, 0}
local function get_slope(is_edge, left, right)
if is_edge then
return zero_vector
else
local t = right.time - left.time
assert(t > 0, "Non-increasing time sequence")
return {(right[1] - left[1])/t,
(right[2] - left[2])/t,
(right[3] - left[3])/t}
end
end
function smooth(checkpoints, frames_per_second)
frames_per_second = frames_per_second or 5
if #checkpoints < 2 then
return checkpoints
end
-- Prepare formulas for each segment of spline
local formulas = {}
for segment = 1, #checkpoints - 1 do
local left = checkpoints[segment]
local right = checkpoints[segment + 1]
local t = right.time - left.time
assert(t > 0, "Non-increasing time sequence")
local left_deriv = get_slope(segment == 1,
checkpoints[segment - 1], right)
local right_deriv = get_slope(segment == #checkpoints - 1,
left, checkpoints[segment + 2])
formulas[segment] = {}
for j = 1, 3 do
local d = left[j]
local c = left_deriv[j]
local a = (right[j] - d - c*t) / (t*t)
local b = 3*a + (c - right_deriv[j])/t
formulas[segment][j] = {(a - b)/t, b, c, d}
end
end
-- Calculate spline points
local total_seconds = checkpoints[#checkpoints].time - checkpoints[1].time
local segment = 1
local spline = {}
for frame_no = 0, total_seconds * frames_per_second do
local t = checkpoints[1].time + frame_no / frames_per_second
local point = {time = t}
while segment < #formulas and t > checkpoints[segment + 1].time do
segment = segment + 1
end
t = t - checkpoints[segment].time
for j = 1, 3 do
local c = formulas[segment][j]
point[j] = ((c[1]*t + c[2])*t + c[3])*t + c[4]
end
table.insert(spline, point)
end
return spline
end
Usage example:
-- x y z "timestamp in seconds"
local checkpoint_A = {11, 12, 13, time = 0}
local checkpoint_B = {21, 15, 18, time = 1} -- 1 second between A and B
local checkpoint_C = {13, 24, 20, time = 6} -- 5 seconds between B and C
local checkpoints = {checkpoint_A, checkpoint_B, checkpoint_C}
-- total duration is 6 seconds, 10 frames per second, 61 points returned
local array_of_61_points = smooth(checkpoints, 10)
for _, point in ipairs(array_of_61_points) do
print(string.format("time = %.1f, x = %.3f, y = %.3f, z = %.3f",
point.time, point[1], point[2], point[3]))
end

Why is my EA not moving my position to breakeven?

I'm attempting to modify my market orders to breakeven the position when the position get 100 pips to the good. This also accounts for the StopLevels which are around 20-30 pips for my broker. It checks the param's via a "for(){...} loop" function
The MagicNumber is the timeframe number for the chart it is on (i.e. 240=4H, 60=1H) I don't set a TakeProfit price & initially no StopLoss price.
The EA is not adding a SL to be equal to the opening price when the trade reaches 100 pip in profit (plus stoplevels). Profit points reaches well over 130 points.
My code is below for a OP_SELL order - any help would be appreciated. Regards, Todd
/*Global Declarations*/
double pnlPoints;
double price, sl, tp;
double point;
int stopLevel;
int breakeven;
double newSL;
/*Local Declaratons*/
pnlPoints = 0;
point = MarketInfo( Symbol(), MODE_POINT );
stopLevel = int( MarketInfo( Symbol(), MODE_STOPLEVEL )
+ MarketInfo( Symbol(), MODE_SPREAD )
);
sl = NormalizeDouble( OrderStopLoss(), Digits );
tp = OrderTakeProfit();
breakeven = 100;
for( int s = OrdersTotal() - 1; s >= 0; s-- )
{ if ( ( OrderSelect( s, SELECT_BY_POS, MODE_TRADES ) ) == true )
price = MarketInfo( Symbol(), MODE_ASK );
newSL = NormalizeDouble( OrderOpenPrice(), Digits );
pnlPoints = ( OrderOpenPrice() - price ) / point;
if ( OP_SELL == OrderType() )
if ( Period() == OrderMagicNumber() )
if ( stopLevel < ( newSL - price ) / point )
if ( breakeven < pnlPoints )
if ( newSL != sl )
ModSell = OrderModify( OrderTicket(),
OrderOpenPrice(),
newSL,
tp,
buycolor
);
else if ( ModBuy == false )
{ Print( "OrderModify failed with error #",
GetLastError()
);
}
}
For the moment being,refine the codeandadd self-debuging / tracing code
After OrderModify() use a self-debugging / journaling Print( StringFormat( ... ) ) to document all instructed values used in the actual OrderModify() call and also the remote-execution ( { server-side | StrategyTester } ) reported issues.
The current code does not enter into such self-diagnostics and ModSell is not inspected at all, ModBuy is inspected only at uncertain conditions / by-coincidence at some future visit of the for(){...} code-execution path to a part after newSL == sl ( and all above stated conditions are just by chance met too )
Next, check an assigned value of tp
As stated above,
/*Local Declarations*/
...
tp = OrderTakeProfit();
which introduces a reasonable doubt, that re-using of this ( inherently uncertain value, as no one knows, which OrderSelect() was the last one that set a db.Pool pointer to decide, from which record from the db.Pool this veryOrderTakeProfit() would accidentally read ( if any record is present in db.Pool already ) inside the whole for(){...} traversing the db.Pool records will not meet conditions for setting properly a TakeProfit price in the next series of OrderModify() calls.
This seems to be the root cause, or a source of unhandled exceptions to the valid, Broker-compliant, OrderModify() values.
Try this:
if (newSL != sl ) {
ModSell = OrderModify( OrderTicket(),
OrderOpenPrice(),
OrderOpenPrice(),
0,
OrderExpiration(),
clrRed
);
if(ModBuy == false )
Print( "OrderModify failed with error #", GetLastError());
}
Then check the Expert-tab for error-message if it fails to set the stop.
Also, you need to take note that StopLoss will ONLY occur if you are on the right chart-timeframe; Otherwise, it won't even get into the if-statements.

Resources