Jump to content

Getting tasks assigned to user in new python_api


mattmoyes

Recommended Posts

OK. I've got the answer. This is how I did it: 

First I query the user, then I use their ID to get all of their tasks.

user = session.query('User where username is {}'.format(name)).one()
tasks = session.query('Task where assignments any (resource_id={})'.format(user['id']))
Link to comment
Share on other sites

Hello,

 

One question :

 

session = ftrack_api.Session()user = session.query('select assignments.context.name, assignments.context.project.name from User where username is damien').one()for assignment in user['assignments']:    print assignment['context']['name'], assignment['context']['project']['name']

Why i have this error :

 

 
File "C:\Python27\lib\site-packages\ftrack_api\query.py", line 56, in one    results = self.all()  File "C:\Python27\lib\site-packages\ftrack_api\query.py", line 39, in all    return list(self)  File "C:\Python27\lib\site-packages\ftrack_api\query.py", line 29, in __len__    self._fetch_results()  File "C:\Python27\lib\site-packages\ftrack_api\query.py", line 35, in _fetch_results    self._results = self._session._query(self._expression)  File "C:\Python27\lib\site-packages\ftrack_api\session.py", line 651, in _query    results = self._call(batch)  File "C:\Python27\lib\site-packages\ftrack_api\session.py", line 1341, in _call    raise ftrack_api.exception.ServerError(error_message)ftrack_api.exception.ServerError: Server reported error: KeyError(u'project')

with this code.

 

My Task has a "project" key, so i don't know why it doesn't work (?).

I got the same error with other "object" keys (like status...)

 

Thank you,

Damien

Link to comment
Share on other sites

Hi,

 

Unfortunately that query is not supported at the moment. Under the hood assignments.context can be either a project or a task. They have a common base called Context which only has name, id and a few other relations but not "project".

 

However you should still be able to do this (without preloading project):

assignment['context']['project']['name'

Link to comment
Share on other sites

  • 3 months later...

It turns out that getting all tasks for a user is very slow, even if I filter out projects that are no longer active. It's taking about 45 seconds to load for someone with 181 active tasks. Is there a way to make it faster or is this just an inherently slow query?

I've tried two different ways. One is the way I did it in a previous post in this thread, and the other way is the way that Mattius did it. They both are similar in time.

Link to comment
Share on other sites

I realized my problem while I was trying to create an example for you. I was looping through each task and accessing a key that wasn't projected making it a lot slower. It was for parent.type.name. Yet, I can't select this because I get a ServerError. It must be similar to the problem deex had. 

Here's the example code if you still want to see it:

session = ftrack_api.Session()
query = 'User where username is "fred"'
user = session.query(query).one()
query = 'Task where project.status is active and assignments any (resource_id={})'.format(user['id'])
tasks = session.query(query).all()

 

Link to comment
Share on other sites

7 hours ago, eight said:

I realized my problem while I was trying to create an example for you. I was looping through each task and accessing a key that wasn't projected making it a lot slower.

Ah, yes - those are sneaky! I usually start looking there myself if I come over code that runs slow.

Quote

Yet, I can't select this because I get a ServerError. It must be similar to the problem deex had.

Sounds likely that, that is the issue. The "parent" can either be a TypedContext or a Project so in the backend we treat it as their common base class Context (which has no type). 

Link to comment
Share on other sites

I have another update. The missing select statement was only a part of the problem. The rest was because of FileCache.

I tried the example below with and without the session cache (183 tasks). With the cache, it took about 13 seconds, and without, it took about 4 seconds. The big difference showed when adding a lot of select statements. With select statements + cache, it took about 35 seconds while select statements without cache took about 6 seconds.

import os
import time
import tempfile
import ftrack_api

def testCacheMaker(session):
    testCachePath = os.path.join(tempfile.gettempdir(), 'ftrack')
    try:
        os.makedirs(testCachePath)
    except:
        pass
    testCachePath = os.path.join(testCachePath, 'test_cache.dbm')
    print 'testCachePath: {}'.format(testCachePath)
    
    return ftrack_api.cache.SerialisedCache(
        ftrack_api.cache.FileCache(testCachePath),
        encode=session.encode,
        decode=session.decode
    )

def test():
    start = time.time()
    session = ftrack_api.Session(cache=testCacheMaker)
    query = 'User where username is "fred"'
    user = session.query(query).one()
    query = 'Task where project.status is active and assignments any (resource_id={})'.format(user['id'])
    tasks = session.query(query).all()
    print len(tasks)
    print 'time: {}'.format(time.time() - start)

 

Link to comment
Share on other sites

On 2/6/2016 at 3:48 AM, eight said:

I have another update. The missing select statement was only a part of the problem. The rest was because of FileCache.

It would be interesting to know how much is due to the FileCache and rule out any extra overhead from layered caches. Could you try and use an additional MemoryCache cache instead of the FileCache? Just to see what happens.

Link to comment
Share on other sites

So you mean the cache maker would look like this?

def testCacheMaker(session):
    return ftrack_api.cache.MemoryCache()

I get very similar performance as not using a FileCache. Also, I forgot to mention that I was deleting the file that the FileCache made before each run.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...