I have a defined AbstractQuery as a Child of GraphObject :
class AbstractQuery(GraphObject):
__primarykey__ = "hash"
hash = Property()
projname = Property()
def __init__(self, hash):
self.hash = hash
self.projname = "" # TODO:initialize this
and iterate over all SQLQuery objects in my (already existing) graph and want to create a new AbstractQuery consisting of a hash. Any SQLQuery hashing to the hash determining the AbstractQuery should be connected:
def addAbstractionLayerSqlQueries(graph, logger=None):
abstractQueryTable = getAbstractQueries(graph, logger)
sqlqueries = graph.data("MATCH (n:SQLQuery) return n")
if logger is not None:
logger.info("abstracting {} queries".format(len(sqlqueries)))
counter = 1
for iterator in sqlqueries:
query = iterator['n']
if logger is not None:
logger.info("abstracting query {}/{}".format(counter,
len(sqlqueries)))
hash = sqlNormalizer.generate_normalized_query_hash(query['ts'])
if hash not in abstractQueryTable:
abstractQueryNode = al.AbstractQuery(hash)
abstractQueryTable[hash] = abstractQueryNode
graph.push(abstractQueryNode)
rel = Relationship(query, "ABSTRACTSTO", abstractQueryTable[hash])
graph.create(rel)
counter = counter + 1
Before I start this process I extract a table (using the hash as key) of all already existing AbstractQueries to prevent creating the same AbstractQuery twice.
However, when I run the method I end up with the exception:
TypeError: Cannot cast AbstractQuery to Node
Why is this and how can I fix this?
I previously entered multiple SQLQueries into my graph by using this representation:
class SQLQuery(GraphObject):
__primarykey__ = "uuid"
uuid = Property()
projname = Property()
session = Property()
user = Property()
seq = Property()
ts = Property()
sql = Property()
Statement = RelatedTo("SQLStatement")
AbstractsTo = RelatedTo("AbstractQuery")
def __init__(self, projname=None, session=None, user=None,
seq=None, ts=None, sql=None):
self.projname = projname
self.session = session
self.user = user
self.seq = seq
self.ts = ts
self.sql = sql
self.uuid = "{} [{} {}] {}.{}.{}".format(type(self).__name__,
seq, ts, projname,
session, user)
As I was able to use this representation to represent and enter Nodes I am quite flabbergasted on why py2neo rejects my AbstractQuery class as a node in my addAbstractionLayerSqlQueries function.
You're mixing two layers of API. The OGM layer sits above the regular py2neo API and a GraphObject does not directly correspond to a Node (it contains other stuff too). Therefore, you cannot build a Relationship to or from a GraphObject directly.
To access the core node behind your GraphObject, you can use my_object.__ogm__.node.
I fixed the problem by only using the Object Model and replacing the graph.data(...) query part:
def addAbstractionLayerSqlQueries(graph, logger=None):
abstractQueryTable = getAbstractQueries(graph, logger)
sqlqueries = list(adlsql.SQLQuery.select(graph))
print type(sqlqueries)
if logger is not None:
logger.info("abstracting {} queries".format(len(sqlqueries)))
counter = 1
for query in sqlqueries:
print type(query)
if logger is not None:
logger.info("abstracting query {}/{}".format(counter,
len(sqlqueries)))
hash = sqlNormalizer.generate_normalized_query_hash(query.ts)
if hash not in abstractQueryTable:
abstractQueryNode = al.AbstractQuery(hash)
abstractQueryTable[hash] = abstractQueryNode
graph.push(abstractQueryNode)
query.AbstractsTo.add(abstractQueryTable[hash])
graph.push(query)
counter = counter + 1
However, the actual reason for the error is still unknown and I'll accept any answer that explains this. This answer is only fixing the problem.
Related
I am using the Terraform provider mrparkers/keycloak to attempt to assign Keycloak groups a list of users.
The snippet below creates realms, groups, and users correctly, but I am stumped on the final line for calling a list of users which should belong to the group being looped through.
Anything to point me in the right direction would be hugely appreciated. :)
vars
variable "realms" {
description = "realms"
type = set(string)
default = ["mrpc"]
}
variable "mrpc-groups" {
type = map(object({
name = string
realm = string
members = set(string)
}))
default = {
"0" = {
realm = "mrpc"
name = "mrpc-admins"
members = ["hellfire", "hellfire2"]
},
"1" = {
realm = "mrpc"
name = "mrpc-mods"
members = ["hellfire2"]
}
}
}
variable "mrpc-users" {
type = map(object({
username = string
email = string
first_name = string
last_name = string
realm = string
}))
default = {
"0" = {
realm = "mrpc"
username = "hellfire"
email = "bla#bla.bla"
first_name = "hell"
last_name = "fire"
}
"1" = {
realm = "mrpc"
username = "hellfire2"
email = "bla2#bla.bla"
first_name = "hell2"
last_name = "fire2"
}
}
}
resources
resource "keycloak_realm" "realm" {
for_each = var.realms
realm = each.value
}
resource "keycloak_group" "group" {
for_each = var.mrpc-groups
realm_id = each.value["realm"]
name = each.value["name"]
depends_on = [keycloak_realm.realm]
}
resource "keycloak_user" "user" {
for_each = var.mrpc-users
realm_id = each.value["realm"]
username = each.value["username"]
email = each.value["email"]
first_name = each.value["first_name"]
last_name = each.value["last_name"]
}
resource "keycloak_group_memberships" "group_members" {
for_each = keycloak_group.group
realm_id = each.value["realm_id"]
group_id = each.value["name"]
members = [ "hellfire2" ]
# i want this to be var.mrpc-groups.*.members (* used incorrectly here i think)
# if
# var.mrpc-groups.*.name == each.value["name"]
#
# so that the correct member list in the vars is used when the matching group is being looped over
# any method to get the final outcome is good :)
}
We can use the distinct and flatten functions in conjunction with a for expression within a list constructor to solve this:
distinct(flatten([for key, attrs in var.mrpc_groups : attrs.members]))
As tested locally, this will return the following for your values exactly as requested in the question indicated by var.mrpc-groups.*.members:
members = [
"hellfire",
"hellfire2",
]
The for expression iterates through the variable mrpc_groups map and returns the list(string) value assigned to the members key within each group's key value pairs. The lambda/closure scope variables are simply key and attrs because the context is unclear to me, so I was unsure what a more descriptive name would be.
The returned structure would be a list where each element would be the list assigned to the members key (i.e. [["hellfire", "hellfire2"], ["hellfire2"]]). We use flatten to flatten the list of lists into a single list comprised of the elements of each nested list.
There would still be duplicates in this flattened list, and therefore we use the distinct function to return a list comprised of only unique elements.
For the additional question about assigning the members associated with the group at the current iteration, we can simply implement the following:
members = flatten([for key, attrs in var.mrpc_groups : attrs.members if attrs.name == each.value["name"]])
This will similarly iterate through the map variable of var.mrpc_groups, and construct a list of the members list filtered to only the group matching the name of the current group iterated through keycloak_group.group. We then flatten again because it is also a nested list similar to the first question and answer.
Note that for this additional question it would be easier for you in general and for this answer if you restructured the variable keys to be the name of the group instead of as a nested key value pair.
I am wondering if I can pass variable to be evaluated as String inside gstring evaluation.
simplest example will be some thing like
def var ='person.lName'
def value = "${var}"
println(value)
I am looking to get output the value of lastName in the person instance. As a last resort I can use reflection, but wondering there should be some thing simpler in groovy, that I am not aware of.
Can you try:
def var = Eval.me( 'new Date()' )
In place of the first line in your example.
The Eval class is documented here
edit
I am guessing (from your updated question) that you have a person variable, and then people are passing in a String like person.lName , and you want to return the lName property of that class?
Can you try something like this using GroovyShell?
// Assuming we have a Person class
class Person {
String fName
String lName
}
// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )
// And given a command string to execute
def commandString = 'person.lName'
GroovyShell shell = new GroovyShell( binding )
def result = shell.evaluate( commandString )
Or this, using direct string parsing and property access
// Assuming we have a Person class
class Person {
String fName
String lName
}
// And a variable 'person' stored in the binding of the script
person = new Person( fName:'tim', lName:'yates' )
// And given a command string to execute
def commandString = 'person.lName'
// Split the command string into a list based on '.', and inject starting with null
def result = commandString.split( /\./ ).inject( null ) { curr, prop ->
// if curr is null, then return the property from the binding
// Otherwise try to get the given property from the curr object
curr?."$prop" ?: binding[ prop ]
}
How can I display the count of related objects after each filter in list_filter in django admin?
class Application(TimeStampModel):
name = models.CharField(verbose_name='CI Name', max_length=100, unique=True)
description = models.TextField(blank=True, help_text="Business application")
class Server(TimeStampModel):
name = models.CharField(max_length=100, verbose_name='Server Name', unique=True)
company = models.CharField(max_length=3, choices=constants.COMPANIES.items())
online = models.BooleanField(default=True, blank=True, verbose_name='OnLine')
application_members = models.ManyToManyField('Application',through='Rolemembership',
through_fields = ('server', 'application'),
)
class Rolemembership(TimeStampModel):
server = models.ForeignKey(Server, on_delete = models.CASCADE)
application = models.ForeignKey(Application, on_delete = models.CASCADE)
name = models.CharField(verbose_name='Server Role', max_length=50, choices=constants.SERVER_ROLE.items())
roleversion = models.CharField(max_length=100, verbose_name='Version', blank=True)
Admin.py
#admin.register(Server)
class ServerAdmin(admin.ModelAdmin):
save_on_top = True
list_per_page = 30
list_max_show_all = 500
inlines = [ServerInLine]
list_filter = (
'region',
'rolemembership__name',
'online',
'company',
'location',
'updated_on',
)
i.e After each filter in list filter, I want to show the count of related objects.
Now it only shows the list of filter
i.e location filter list
Toronto
NY
Chicago
I want the filter to show the count like below:
Toronto(5)
NY(3)
Chicago(2)
And if the filter has 0 related objects, don't display the filter.
This is possible with a custom list filter by combining two ideas.
One: the lookups method lets you control the value used in the query string and the text displayed as filter text.
Two: you can inspect the data set when you build the list of filters. The docs at https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter shows examples for a start decade list filter (always shows «80s» and «90s») and a dynamic filter (shows «80s» if there are matching records, same for «90s»).
Also as a convenience, the ModelAdmin object is passed to the lookups
method, for example if you want to base the lookups on the available
data
This is a filter I wrote to filter data by language:
class BaseLanguageFilter(admin.SimpleListFilter):
title = _('language')
parameter_name = 'lang'
def lookups(self, request, model_admin):
# Inspect the existing data to return e.g. ('fr', 'français (11)')
# Note: the values and count are computed from the full data set,
# ignoring currently applied filters.
qs = model_admin.get_queryset(request)
for lang, name in settings.LANGUAGES:
count = qs.filter(language=lang).count()
if count:
yield (lang, f'{name} ({count})')
def queryset(self, request, queryset):
# Apply the filter selected, if any
lang = self.value()
if lang:
return queryset.filter(language=lang)
You can start from that and adapt it for your cities by replacing the part with settings.LANGUAGES with a queryset aggregation + values_list that will return the distinct values and counts for cities.
Éric's code got me 80% of what I needed. To address the comment he left in his code (about ignoring currently applied filters), I ended up using the following for my use case:
from django.db.models import Count
class CountAnnotatedFeedFilter(admin.SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
qs = model_admin.get_queryset(request).filter(**request.GET.dict())
for pk, name, count in qs.values_list('feed__feed_id', 'feed__feed_name').annotate(total=Count('feed')).order_by('-total'):
if count:
yield pk, f'{name} ({count})'
def queryset(self, request, queryset):
feed_id = self.value()
if feed_id:
return queryset.filter(feed_id=feed_id)
And then, in the admin model:
class FeedEntryAdmin(admin.ModelAdmin):
list_filter = (CountAnnotatedFeedFilter,)
Note: As Éric also mentioned, this can impact the speed of the admin panel quite heavily, as it may have to perform expensive queries.
I am writing a query and want to set parameter which is a list of strings.
My query is
def strQuery = """SELECT * from user u where u.status in (?)"""
def session = sessionFactory.getCurrentSession()
def resultList = session.createSQLQuery(strQuery)
.setParameter(0, UserStatus.List)
.list();
Where
public enum UserStatus {
ACTIVE("Active"),
ARCHIVE("Archived"),
public static final List<UserStatus> List = [ACTIVE,ARCHIVED]
}
but I am getting an exception
ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: operator does not exist: character varying = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
To bind a list, you will have to use setParameterList with named parameter.
setParameter is only for single value.
def strQuery = """SELECT * from user u where u.status in (:statusList)"""
def session = sessionFactory.getCurrentSession()
def resultList = session.createSQLQuery(strQuery)
.setParameterList('statusList', UserStatus.List)
.list();
I got params.user return the list.
Ex:
params.user = 10 20
then I used
params.user.each { id ->
}
Then id has value:
id = 10, id = 20
When params.user only returns 10, iterating over it I get 1 and 0.
So my idea is count the length() of params.user and put the condition
if (params.user == 1){
//to do something
}
But when I used params.user.length(), it returns 2 and I need it return 1. Please help
When you want to use the reserved params map, you actually use a Map. Then of course, when you do params.myParams.length(), you will get the length of your String value.
But Grails provides tools to get data from the param map. For a list use:
List myList = params.list('user')
Grails provides other ways to grab other kind of data:
def intValue = params.int('paramInt')
def shortValue = params.short('paramShort')
def byteValue = params.byte('paramByte')
def longValue = params.long('paramLong')
def doubleValue = params.double('paramDouble')
def floatValue = params.float('paramFloat')
def booleanValue = params.boolean('paramBoolean')
Last but not least, if you have complex data to bind, don't forget to use Command Objects ;)