Google app engine countrycode in key_name - performance

I have a table with countrycodes, whats the best way to store these 2 digit codes, just in a regular property or in the key_name property?
I thought using key_name will produce faster performance?
Ton.

Whether or not you want to use key_name depends on how you want to use it. I'm not entirely certain how you want to use these country codes based on the information in your question. Let's say, however, that you want to be able to look up the full name of the country based on the two-letter code, for instance, to get "United States" from "us". In that case, you could have an entity like this:
class CountryName(db.Model):
name = db.StringProperty()
#classmethod
def lookup_by_code(cls, code):
country_name = ""
country = cls.get_by_key_name(code)
if country is not None:
country_name = country.name
return country_name
Now, this code is not complete; you'd need to populate the model with data, and this approach does not allow for reverse lookups -- getting the code when you have the country name, but this should give you an idea of how you might be able to use key_names. Doing it this way would be substantially faster then, say, having a model that contained both code and name as fields.

Related

Is there a way to compare each item to a aggreated value?

I'm new to graphQL and Hasura. I'm trying(in Hasura) to let me users provide custom aggregation (ideally in the form of a normal graphQL query) and have then each item the results compared against the aggreation.
Here's a example. Assume I have this schema:
USERTABLE:
userID
Name
Age
City
Country
Gender
HairColor
INCOMETABLE:
userID
Income
I created a relationship in hasura and I can query the data but my users want to do custom scoring of users' income level. For example, one user may want to query the data broken down by country and gender.
For the first example the result maybe:
{Country : Canada
{ gender : female
{ userID: 1,
Name: Nancy Smith,..
#data below is on aggregated results
rank: 1
%fromAverage: 35%
}...
Where I'm struggling is the data showing the users info relative to the aggregated data.
for Rank, I get the order by sorting but I'm not sure how to display the relative ranking and for the %fromAverage, I'm not sure how to do it at all.
Is there a way to do this in Hasura? I suspected that actions might be able to do this but I'm not sure.
You can use track a Postgres view. Your view would have as many fields as you'd like calculated in SQL and tracked as a separate "table" on your graphql api.
I am giving examples below based on a simplification where you have just table called contacts with just a single field called: id which is an auto-integer. I am just adding the id of the current contact to the avg(id) (a useless endeavor to be sure; just to illustrate...). Obviously you can customize the logic to your liking.
A simple implementation of a view would look like this (make sure to hit 'track this' in hasura:
CREATE OR REPLACE VIEW contact_with_custom AS
SELECT id, (SELECT AVG(ID) FROM contacts) + id as custom FROM contacts;
See Extend with views
Another option is to use a computed field. This is just a postgres function that takes a row as an argument and returns some data and it just adds a new field to your existing 'table' in the Graphql API that is the return value of said function. (you don't 'track this' function; once created in the SQL section of Hasura, you add it as a 'computed field' under 'Modify' for the relevant table) Important to note that this option does not allow you to filter by this computed function, whereas in a view, all fields are filterable.
In the same schema mentioned above, a function for a computed field would look like this:
CREATE OR REPLACE FUNCTION custom(contact contacts)
RETURNS Numeric AS $$
SELECT (SELECT AVG(ID) from contacts ) + contact.id
$$ LANGUAGE sql STABLE;
Then you select this function for your computed field, naming it whatever you'd like...
See Computed fields

JMeter populate user defined variables for forEach Controller

I have a service API to be tested which returns some default values of various parameters for different countries. country code being a template parameter of the service. i.e.
http://${hostname}:${port}/country/${countryCode}
One of the country code I can use is "ALL" (the others being "IN", "US" , "UK" , "MX" ...) which will return all the countries supported by the API and the response will contain the country code as well.
Thus I am able to populate all the country codes I need to test. The groovy scipt
import groovy.json.JsonSlurper
def slurper = new JsonSlurper()
def result = slurper.parseText(prev.getResponseDataAsString())
assert prev.isResponseCodeOK()
def countries = result.country
assert countries instanceof List // Should get as [IN, US, UK, MX] for e.g.
def numOfCountries = countries.size()
I am trying to use the variable "countries" which is a list of all the countries I need to test the service in a ForEachController. For this I will be needing the UDV Names in the format
country_1 , country_2 , country_3 ...
How do I save the elements of list to the UDVs with that name format. The problem is I do not know or rather cannot assume how many countries are supported, so the UDVs cannot be named in advance.
Can that be done ? Am I going the correct way for the problem ? Any different approach is welcome.
Thanks
I'm not familiar with groovy, but here goes:
You have the list of countries- you get that in your script. If you create a for loop (in your groovy script), you can dynamically create your variable name ("country_" + [loop iterator]) and plug that into your vars.put().
So:
for(int x=0; x < v.size(); x++)
{
String country_name = "country_" + x;
vars.put(country_name, v[x]);
}

Mezzanine Forms Dropdown

I'm trying out Django/Mezzanine and if I have a custom user profile as such:
class UserProfile(models.Model):
user = models.OneToOneField("auth.User")
street_address1 = models.CharField(max_length=100)
street_address2 = models.CharField(max_length=100)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=32)
country = models.CharField(max_length=2)
phone = models.CharField(max_length=15)
Mezzanine creates a sign up form at account/signup/ and I would like to modify the Country field to have a drop down list of countries from a table or xml file. The foreign key is a two character field.
How should go about doing this? Do I create a model form or try to extend the right template (tried looking at accounts\templates\account_form.html but don't think it is there?
I believe if you defined a "choices" arg for the field, it'll do just that:
https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
A quick Google search will probably also reveal some pre-built packages for country lists.

Extbase Mapping with non-TYPO3-table

I have too classes and two non-TYPO3-tables. I defined a non-TYPO3-table as a table without uid, pid, etc. columns.
My two classes:
class Tx_Abc_Domain_Model_Location extends Tx_Extbase_DomainObject_AbstractEntity
class Tx_Abc_Domain_Model_Facility extends Tx_Extbase_DomainObject_AbstractEntity
My two tables (with columns):
locations
zipcode
city
facility_id
facilities
facility_id
name
I've mapped the attributes like this:
config.tx_extbase.persistence.classes {
Tx_Abc_Domain_Model_Location.mapping {
tableName = locations
columns {
zipcode.mapOnProperty = zipcode
city.mapOnProperty = city
facility_id.mapOnProperty = facility
}
}
Tx_Abc_Domain_Model_Facility.mapping {
tableName = facilities
columns {
facility_id.mapOnProperty = uid
name.mapOnProperty = name
}
}
}
My problem:
The facility attribute of my location model got the type Tx_Abc_Domain_Model_Facility and when I'm looking for a location via the LocationRepository it builds me a location model which contains a facility model.
The problem appears, when I the search I am doing returns several results. i.e. the location with the zipcode 12345 has two different facilities (and the table locations got two rows with different facility_ids), then I would expect to get two location models and each of it got the right facility model.
But instead I get the two location models, which have all same facility model inside. They've got all the facility of the first found location.
Even if I change the type of the facility attribute to integer, there are the wrong ids. But if I enable raw query result in repository I get the correct ids.
I get also the correct ids or models, when I add to both tables an uid-column.
Is there no possibility to map tables without uid column with Extbase models?
Thanks.
Okay, the answer to my last question is: Yes, there is no possibility to map tables without uid column with Extbase models.
There is an existing ticket on forge: http://forge.typo3.org/issues/25984
The reason seems to be the hardcoded $row['uid'] in mapSingleRow() method in Tx_Extbase_Persistence_Mapper_DataMapper class.
If it's not alot of tables you have to map, a work-around could be to create views for those tables to just map the uid.
I.e.:
CREATE VIEW tx_abc_domain_model_facility AS
SELECT facility_id AS uid, facilities.* FROM facilities;

What is the best way pre filter user access for sqlalchemy queries?

I have been looking at the sqlalchemy recipes on their wiki, but don't know which one is best to implement what I am trying to do.
Every row on in my tables have an user_id associated with it. Right now, for every query, I queried by the id of the user that's currently logged in, then query by the criteria I am interested in. My concern is that the developers might forget to add this filter to the query (a huge security risk). Therefore, I would like to set a global filter based on the current user's admin rights to filter what the logged in user could see.
Appreciate your help. Thanks.
Below is simplified redefined query constructor to filter all model queries (including relations). You can pass it to as query_cls parameter to sessionmaker. User ID parameter don't need to be global as far as session is constructed when it's already available.
class HackedQuery(Query):
def get(self, ident):
# Use default implementation when there is no condition
if not self._criterion:
return Query.get(self, ident)
# Copied from Query implementation with some changes.
if hasattr(ident, '__composite_values__'):
ident = ident.__composite_values__()
mapper = self._only_mapper_zero(
"get() can only be used against a single mapped class.")
key = mapper.identity_key_from_primary_key(ident)
if ident is None:
if key is not None:
ident = key[1]
else:
from sqlalchemy import util
ident = util.to_list(ident)
if ident is not None:
columns = list(mapper.primary_key)
if len(columns)!=len(ident):
raise TypeError("Number of values doen't match number "
'of columns in primary key')
params = {}
for column, value in zip(columns, ident):
params[column.key] = value
return self.filter_by(**params).first()
def QueryPublic(entities, session=None):
# It's not directly related to the problem, but is useful too.
query = HackedQuery(entities, session).with_polymorphic('*')
# Version for several entities needs thorough testing, so we
# don't use it yet.
assert len(entities)==1, entities
cls = _class_to_mapper(entities[0]).class_
public_condition = getattr(cls, 'public_condition', None)
if public_condition is not None:
query = query.filter(public_condition)
return query
It works for single model queries only, and there is a lot of work to make it suitable for other cases. I'd like to see an elaborated version since it's MUST HAVE functionality for most web applications. It uses fixed condition stored in each model class, so you have to modify it to your needs.
Here is a very naive implementation that assumes there is the attribute/property self.current_user logged in user has stored.
class YourBaseRequestHandler(object):
#property
def current_user(self):
"""The current user logged in."""
pass
def query(self, session, entities):
"""Use this method instead of :method:`Session.query()
<sqlalchemy.orm.session.Session.query>`.
"""
return session.query(entities).filter_by(user_id=self.current_user.id)
I wrote an SQLAlchemy extension that I think does what you are describing: https://github.com/mwhite/multialchemy
It does this by proxying changes to the Query._from_obj and QueryContext._froms properties, which is where the tables to select from ultimately get set.

Resources