How to chef search for role with OR - ruby-on-rails

I want to search nodes with role "mapreduce-datanode" & "mapreduce-namenode".
So i tried following :
hadoop_nodes = search(:node, "role:mapreduce-datanode OR role:mapreduce-namenode AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")
Is this correct way to do ?
Thanks.

If you want "mapreduce-datanode" & "mapreduce-namenode", as you state, then why are you using an OR?
If you really want an AND, then you want:
hadoop_nodes = search(:node, "role:mapreduce-datanode AND role:mapreduce-namenode AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")
If you want the union of the two sets (ie you really did mean OR) then try this:
hadoop_nodes = search(:node, "(role:mapreduce-datanode OR role:mapreduce-namenode) AND chef_environment:#{node.chef_environment} AND domain:#{node['domain']}")

Related

Denodo Json Source and Base View: how to make VQL more traditional SQL syntax

I have a web service api
In Denodo, I have virtualized the following api:
https://delphi.cmu.edu/epidata/fluview/?regions=nat&epiweeks=201501,201601,201701
as
https://delphi.cmu.edu/epidata/fluview/?regions=#{regions}&epiweeks=#{epiweeks}
This allows us, in Denodo, to write the following:
Select * from bv_fluview where epiweeks = '201501,201601,201701' and regions = 'nat'
Is there any way where the query could be written in a more traditional way such as:
Select * from bv_fluview where epiweeks in ('201501','201601','201701') and regions = 'nat'
I am relatively new to Denodo.
Thanks,
Dan
In Denodo, you can use the IN clause to check if a value is present in a list
SELECT *
FROM bv_fluview
WHERE epiweeks IN ('201501','201601','201701')
AND regions = 'nat'
This would return the same result as your original query but with a more conventional syntax for the IN operator.

Yii2: How to do a inner join of three tables?

I have a many-to-many relationship so I have three tables (administrators, offices and the intermediate table):
I need to do a very simple innerjoin of the three tables to show lastnames and their offices with Active Record. I tried but I couldn't.
This is what I tried:
$admins = Admins::find()
->joinWith(Oficinas::tableName())
->all();
echo '<pre>';
print_r($admins);
echo '</pre>';
die();
Also I would like to know how to show the SQL query so it can help me to find a solution.
You need to specify the relation name for joinWith() rather than table names. Since there isn't any info on the relation names I will use simple innerJoin using table names as per your requirements to display the last name and the office name for the admins.
Admins::find()
->alias('a')
->select('a.lastnameadm, o.nombreofi')
->innerJoin('admin_oficinas ao','ao.idadm = a.idadm')
->innerJoin('oficinas o','o.idofi = ao.idofi')
->all();
Try this:
TABLE_NAME_1::find()
->join('inner join',
'table_name_2',
'table_name_2.column = table_name_1.column'
);
->join('inner join',
'table_name_3',
'table_name_3.column = table_name_2.column'
)->all();
but if you can also use Via like the following example:
public function getClassschedule()
{
return $this->hasOne(TABLE_NAME_2::className(), ['table_name_1.column' => 'table_name_2.column'])
->via('tableName3');
}
when excuded, the results should be obtained like the previous example. so there's no need to repeated relations. full documentation can be seen here :
https://www.yiiframework.com/doc/guide/2.0/en/db-active-record

How to make multiple where->like() with Zend Framework 2?

I tried making a nest on a :
$where = new Where();
I also tried making multiple :
$where->like
Could someone please provide me an example of how I can make multiple like ?
I would like to search two different fields with the same value %$value%
Thank you and best regards
Within the Where object you can NEST (wrap in parenthesis) your options and specify an operator (in this case, OR):
$where = new Where();
$where->NEST
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->UNNEST;
This will generate:
... WHERE (`field1` LIKE '%value%' OR `field2` LIKE '%value%')
Found solution by using OR on the final select. The following example shows it :
$where = array();
$where[] = $rel->field1 . " LIKE '%". $value ."%' ";
$select->where($where, 'OR');
I'm not including the foreach but you get the idea, add your queries in where and use OR as predicates will generated for multiple entries.
Extending above solution with more functional way :
$where = new Where();
$where->nest()
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->unnest();

Conditional Dynamic finder in grails

so i require something like the dynamic finder should change as per the condition, let me explain by code what i mean
the below code find all employee by status and lastdateattended
def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)
now i have two fields in Employee LastDateAttended and LastDateSigned , and now i want that if a record does not have LastDateAttended, then it should find by LastDateSigned , otherwise LastDateAttended, so another condition is like
def employee = Employee.findAllByStatusAndLastDateAttended("INA",dateObject)
and i somehow wants to join and use both the query based on a condition , can it be achieved ?, if possible please help
I think criteria query make sense here, something like following
Employee.createCriteria().list{
and{
eq('status','INA')
or {
eq('lastDateAttended',dateObject)
eq('lastDateSigned',dateObject)
}
}
}
Employee.list().findAll { emp->
emp.status == "INA" && ( emp.lastDateAttended!=null?
emp.lastDateAttended.equals(dateObject):emp.lastDateSigned.equals(dateObject)
)
}
Employee.findAllByStatusOrLastDateAttendedOrStatusAndLastDateAttended("INA",dateObject)

Default django-admin list filter

My question is just an extension of this thread [Question]http://stackoverflow.com/questions/851636/default-filter-in-django-admin .
from myproject.myapp.mymodels import fieldC
class Poll(models.Model):
fieldA = models.CharField(max_length=80, choices=CHOICES.MyCHOICES)
fieldB = models.ForeignKey(fieldC)
admin.py
list_display = ('fieldB__fieldc1')
Now my list filter shows four criteria All, A ,B ,C .
What I want is if the superuser is logged in ,the filter should show all four criteria All,A,B,C and if the user is other than superuser filter should only show All, A, B.
How can i acheive this ?
Here is my actual piece of admin.py
def changelist_view(self, request, extra_context=None):
referer = request.META.get('HTTP_REFERER', '')
test = referer.split(request.META['PATH_INFO'])
if test[-1] and not test[-1].startswith('?'):
if not request.GET.has_key('patient__patient_type__exact'):
q = request.GET.copy()
q['patient__patient_type__exact'] = 'Real'
request.GET = q
request.META['QUERY_STRING'] = request.GET.urlencode()
if not request.user.is_superuser:
q['patient__patient_type__exact'] = 'Real'
return super(VisitAdmin, self).changelist_view(request, extra_context)
Thanks in advance
I think the new FilterSpec API in Django 1.4 gives you exactly what you need here. Check out the docs on list_filter. In 1.4 you can now make custom list filters that subclass django.contrib.admin.SimpleListFilter and give you the power to write custom lookup and queryset code, and since the request is passed in you can do a simple conditional with is_superuser.
if request.user.is_superuser:
# pass one set of lookups
else:
# pass a different set
read the example code in the docs carefully and I think it will all be clear.

Resources