nested queries in Rally Agile Central - rally

can someone show me how to nest query in a query in Agile central? I am trying to filter all the stories belonging to all the features under a given initiative.
So, the hierarchy will be:
Epic/Initiative **XYZ** >> Feature A /Story 1
>> Feature B /Story 2/Story 3/Story 4
>> Feature B /Story 5/Story 6/Story 7
So, I need one query where I need to show all the stories and their corresponding features under Epic/Initiative XYZ

Something like that:
from pyral import Rally
INITIATIVE_ID = "{{ YOUR_INITIATIVE_ID }}"
RESULT_MAP = {}
rally = Rally(server="rally1.rallydev.com",
apikey="{{ YOUR_API_KEY }}",
workspace="{{ YOUR_WORKSPACE }}",
project="{{ YOUR_PROJECT }}")
initative_req = rally.get("PortfolioItem/Initiative", fetch=True, query=("FormattedID = %s") % INITIATIVE_ID,
pagesize=2000,
projectScopeDown=True)
initiative = initative_req.next()
for feature in initiative.Children:
RESULT_MAP[feature.FormattedID] = [user_story.FormattedID for user_story in feature.UserStories] if (
feature.DirectChildrenCount > 0) else []
print RESULT_MAP

Related

retrieve a variable in the case of an outline scenario

I want to recover an object id (processId) that I pass in a simple Post request in a feature A
Given url <url>
And path 'processes'
And header Authorization = 'Bearer ' + <token>
And request process
When method post
Then status 201
* def processId = response.id
I test this request with 3 different environments in a Scenario outline (varriable <url>). So I have to recover the 3 id to use them in a feature B
My question is: how can I retrieve these IDs for use in feature B
Thanks
There is no way you can re-use data from one Scenario in another, and a Feature is certainly out of the question. Please take some time to read this: https://stackoverflow.com/a/46080568/143475
That said, if all you need to do is call a Scenario in a loop, just do that. Here is a simple example you can try:
Feature:
Scenario:
* table data
| value |
| 'one' |
| 'two' |
* def result = call read('called.feature') data
* def traceIds = $result[*].traceId
* print traceIds
And called.feature is simply:
#ignore
Feature:
Scenario:
* url 'https://httpbin.org/post'
* request { key: '#(value)' }
* method post
* def traceId = response.headers['X-Amzn-Trace-Id']
Please read the docs to understand how you can "collect" data from the "called" feature, traceId in this case: https://github.com/karatelabs/karate#data-driven-features

Process fields with nested arrays into strings with strcat_array for output in Kusto

I would like to process Azure AD audit Logs into HTML tables/csv files. The data contains nested sets of arrays that I would like to summarise into a comma separated string.
eg data that looks like this
{
"TargetResources": [{"displayName": "Policy",
"modifiedProperties": [{"displayname": "PolicySetting1"},
{"displayname": "PolicySetting2"}]
}]
}
Would be processed into
TargetResource | Policy
modifedProps | PolicySetting1, PolicySetting2
mv-expand doesn't seem to work because some rows do not have modifiedProperties so those rows get eliminated
The only solution I have been able to find that gets close to what I am trying to do looks like this:
AuditLogs
| extend TargetResource = tostring(TargetResources[0].displayName)
| extend ModifiedProperty0 = tostring(parse_json(tostring(TargetResources[0].modifiedProperties))[0].displayName)
| extend ModifiedProperty1 = tostring(parse_json(tostring(TargetResources[0].modifiedProperties))[1].displayName)
| extend ModifiedProperty2 = tostring(parse_json(tostring(TargetResources[0].modifiedProperties))[2].displayName)
| extend ModifiedProperties = strcat(ModifiedProperty0,", ",ModifiedProperty1,", ",ModifiedProperty2)
This solution is limited in that it cannot work for arbitrary numbers of modifiedProperty values (it only works properly for exactly 3) which is a requirement for my purposes, I would like the solution to work if modifiedProperties does not exist and if there are 0-15 values.
Thank you for any help you can provide
if I understood your description correctly, you could use mv-apply (twice) to achieve that:
datatable(d: dynamic)
[
dynamic({"TargetResources":[{"displayName": "Policy0","someOtherProperty":"hello world"}]}),
dynamic({"TargetResources":[{"displayName": "Policy1","modifiedProperties":[{"displayname":"PolicySetting1"},{"displayname":"PolicySetting2"}]}]}),
dynamic({"TargetResources":[{"displayName": "Policy2","modifiedProperties":[{"displayname":"PolicySetting3"},{"displayname":"PolicySetting4"}]}, {"displayName":"Policy3","modifiedProperties":[{"displayname":"PolicySetting5"},{"displayname":"PolicySetting6"}]}]}),
]
| mv-apply tr = d.TargetResources on (
extend TargetResource = tr.displayName
| mv-apply mp = tr.modifiedProperties on (
extend propertyName = mp.displayname
| summarize modifiedProps = strcat_array(make_set(propertyName), ", ")
)
)
| project TargetResource, modifiedProps
TargetResource
modifiedProps
Policy0
Policy1
PolicySetting1, PolicySetting2
Policy2
PolicySetting3, PolicySetting4
Policy3
PolicySetting5, PolicySetting6

ecto select fields with map function as alias or different name

iex(18)> fields = [:id]
[:id]
iex(19)> fields_map = %{id: :job_id}
%{id: :job_id}
iex(20)> query = from p in Qber.V1.JobModel, where: p.id == 1, select: map(p, ^fields)
#Ecto.Query<from j in Qber.V1.JobModel, where: j.id == 1, select: map(j, [:id])>
I want to select my fields dynamically with AS names on the basis of params I got(which may have some joins as this app is going to have heavy admin dashboard queries). So I need dynamic solution. is there a way I can pass a map instead of list dynamically to map function and ecto will send me the selected result with custom names I passed in map. Instead of
[%{id: 1}]
I want the result to be
[%{job_id: 1}]
Note: I have tried and searched different keywords on the web and I have gone through almost all the docs many times and unable to find solution.

Find a records containg a specific tag

I have a following DB schema:
User
- id
- name (String)
UserTag
- user_id
- tag_id
Tag
- id
- key (String)
And i also have a pretty complex users-search chain with a lot of where statements. I'm trying to figure out how to include one another where condition to my chain - filtering for users that has a specific tag assigned (ID of this tag is unknown, just it's key is known).
So, here is more or less how my code looks:
col = User.all
col = col.where('cats_count <= 0') if args[:no_cat]
col = col.where('dogs_count <= 0') if args[:no_dog]
col = col.where('other_pets_count <= 0') if args[:no_other_pet]
# ... tag logic filtering here ...
col = col.where('age > 100') if args[:old]
And i want to filter for a users, who has a Tag with key=non_smoking assigned. Ideally, i would love it to be database-engine independen, if that's important - i'm on sqlite/postgres.
Honestly i have completely no any ideas on how to deal with that, i probably lack some knowledge in SQL matter and then, in Rails/ActiveRecord.
You can specify joins and the put conditions on the joined tables. So maybe something like (untested, off the top of my head)
col.joins(user_tags: :tag).where(user_tags: { tag: { key: 'non_smoking' } })

Filtering model with HABTM relationship

I have 2 models - Restaurant and Feature. They are connected via has_and_belongs_to_many relationship. The gist of it is that you have restaurants with many features like delivery, pizza, sandwiches, salad bar, vegetarian option,… So now when the user wants to filter the restaurants and lets say he checks pizza and delivery, I want to display all the restaurants that have both features; pizza, delivery and maybe some more, but it HAS TO HAVE pizza AND delivery.
If I do a simple .where('features IN (?)', params[:features]) I (of course) get the restaurants that have either - so or pizza or delivery or both - which is not at all what I want.
My SQL/Rails knowledge is kinda limited since I'm new to this but I asked a friend and now I have this huuuge SQL that gets the job done:
Restaurant.find_by_sql(['SELECT restaurant_id FROM (
SELECT features_restaurants.*, ROW_NUMBER() OVER(PARTITION BY restaurants.id ORDER BY features.id) AS rn FROM restaurants
JOIN features_restaurants ON restaurants.id = features_restaurants.restaurant_id
JOIN features ON features_restaurants.feature_id = features.id
WHERE features.id in (?)
) t
WHERE rn = ?', params[:features], params[:features].count])
So my question is: is there a better - more Rails even - way of doing this? How would you do it?
Oh BTW I'm using Rails 4 on Heroku so it's a Postgres DB.
This is an example of a set-iwthin-sets query. I advocate solving these with group by and having, because this provides a general framework.
Here is how this works in your case:
select fr.restaurant_id
from features_restaurants fr join
features f
on fr.feature_id = f.feature_id
group by fr.restaurant_id
having sum(case when f.feature_name = 'pizza' then 1 else 0 end) > 0 and
sum(case when f.feature_name = 'delivery' then 1 else 0 end) > 0
Each condition in the having clause is counting for the presence of one of the features -- "pizza" and "delivery". If both features are present, then you get the restaurant_id.
How much data is in your features table? Is it just a table of ids and names?
If so, and you're willing to do a little denormalization, you can do this much more easily by encoding the features as a text array on restaurant.
With this scheme your queries boil down to
select * from restaurants where restaurants.features #> ARRAY['pizza', 'delivery']
If you want to maintain your features table because it contains useful data, you can store the array of feature ids on the restaurant and do a query like this:
select * from restaurants where restaurants.feature_ids #> ARRAY[5, 17]
If you don't know the ids up front, and want it all in one query, you should be able to do something along these lines:
select * from restaurants where restaurants.feature_ids #> (
select id from features where name in ('pizza', 'delivery')
) as matched_features
That last query might need some more consideration...
Anyways, I've actually got a pretty detailed article written up about Tagging in Postgres and ActiveRecord if you want some more details.
This is not "copy and paste" solution but if you consider following steps you will have fast working query.
index feature_name column (I'm assuming that column feature_id is indexed on both tables)
place each feature_name param in exists():
select fr.restaurant_id
from
features_restaurants fr
where
exists(select true from features f where fr.feature_id = f.feature_id and f.feature_name = 'pizza')
and
exists(select true from features f where fr.feature_id = f.feature_id and f.feature_name = 'delivery')
group by
fr.restaurant_id
Maybe you're looking at it backwards?
Maybe try merging the restaurants returned by each feature.
Simplified:
pizza_restaurants = Feature.find_by_name('pizza').restaurants
delivery_restaurants = Feature.find_by_name('delivery').restaurants
pizza_delivery_restaurants = pizza_restaurants & delivery_restaurants
Obviously, this is a single instance solution. But it illustrates the idea.
UPDATE
Here's a dynamic method to pull in all filters without writing SQL (i.e. the "Railsy" way)
def get_restaurants_by_feature_names(features)
# accepts an array of feature names
restaurants = Restaurant.all
features.each do |f|
feature_restaurants = Feature.find_by_name(f).restaurants
restaurants = feature_restaurants & restaurants
end
return restaurants
end
Since its an AND condition (the OR conditions get dicey with AREL). I reread your stated problem and ignoring the SQL. I think this is what you want.
# in Restaurant
has_many :features
# in Feature
has_many :restaurants
# this is a contrived example. you may be doing something like
# where(name: 'pizza'). I'm just making this condition up. You
# could also make this more DRY by just passing in the name if
# that's what you're doing.
def self.pizza
where(pizza: true)
end
def self.delivery
where(delivery: true)
end
# query
Restaurant.features.pizza.delivery
Basically you call the association with ".features" and then you use the self methods defined on features. Hopefully I didn't misunderstand the original problem.
Cheers!
Restaurant
.joins(:features)
.where(features: {name: ['pizza','delivery']})
.group(:id)
.having('count(features.name) = ?', 2)
This seems to work for me. I tried it with SQLite though.