def test_get(self):
     db_path = self.cfg['cache-directory'] + '/object_cache.db'
     test_table = [{
         'constructor': {
             'path': db_path,
             'workspace_url': self.cfg['workspace-url'],
             'token': self.token,
             'username': self.username
         },
         'get': [[34742, 34599]]
     }]
     for test in test_table:
         try:
             cache = PermissionsCache(**test['constructor'])
             cache.initialize()
             items = cache.get(*test['get'])
             self.assertEqual(len(items), 2)
             items = cache.get(*test['get'])
             self.assertEqual(len(items), 2)
             self.assertTrue(True)
         except ValueError as err:
             self.assertTrue(False)
コード例 #2
0
    def list_all_narratives(self):
        # current_username = ctx['user_id']
        stats = []
        then = time.time()

        # WORKSPACES
        print('getting workspaces...')
        narrative_workspaces = sorted(self.fetch_narrative_workspaces(),
                                      key=lambda x: x.get('id'))
        now = time.time()
        print('got %s in %s' % (len(narrative_workspaces), now - then))
        stats.append(['list_workspace', now - then])
        then = now

        # NB - all workpace/narrative related data below must be
        # eventually formed into a list with the same order as the
        # narrative workspaces.

        # PERMISSION
        print('getting permissions...')
        # TODO: we need to ensure that at the very least the items which are
        # returned are returned in the same order requested. This will allow us
        # to weave the results back together in the end.
        workspace_cache = PermissionsCache(
            workspace_url=self.config['services']['Workspace'],
            path=self.config['caches']['workspace']['path'],
            username=self.username,  # just use the token for now...
            token=self.token)
        workspaces_to_get = [ws['id'] for ws in narrative_workspaces]
        narrative_workspaces_perms = workspace_cache.get(workspaces_to_get)
        now = time.time()
        print('...got %s, %s in %s' %
              (len(workspaces_to_get), len(narrative_workspaces_perms),
               now - then))
        stats.append(['get_permissions', now - then])
        then = now

        # PROFILE PER USER
        print('getting profiles...')
        # Get all profiles for all users in the permissions list. It is returned with the
        # response so that the caller can map usernames to profiles.
        users = set()
        for perms in narrative_workspaces_perms:
            for username, _perm in perms:
                users.add(username)

        # We end up with a map of username -> profile
        # Transform profiles into map of user to record.
        # TODO: parse down profile record, also define in the spec
        # The profile record itself is simplified down to just what
        # we need
        profiles_cache = UserProfileCache(
            user_profile_url=self.config['services']['UserProfile'],
            path=self.config['caches']['userprofile']['path'])
        profiles = profiles_cache.get(list(users))
        profiles_map = dict()
        # for (username, profile) in itertools.izip(users, profiles):
        #     profiles_map[username] = profile
        for [username, profile] in profiles:
            profiles_map[username] = profile

        now = time.time()
        print('...got %s in %s' % (len(profiles), now - then))
        stats.append(['user_profiles', now - then])
        then = now

        # NARRATIVE OBJECT
        # based on the WS lookup table, lookup the narratives
        # narrative_objects, _missing, _missed = self.objectInfo.get_object_info_for_workspaces(
        #     narrative_workspaces, clients['Workspace'])
        object_cache = ObjectCache(
            workspace_url=self.config['services']['Workspace'],
            path=self.config['caches']['object']['path'],
            token=self.token)
        # object_cache.start()

        # object_cache = object_cache.newInstance(token=ctx['token'])
        # convert to the cache format, which includes the mod date as timestamp
        # NB: includes object id, because the key should have enough info to fetch
        # the object from storage as well as form a unique key
        to_get = [(ws['id'], int(ws['metadata']['narrative']), ws['modDateMs'])
                  for ws in narrative_workspaces]

        # to_get = [(ws['id'],
        #           int(ws['metadata']['narrative']))
        #           for ws in narrative_workspaces]

        # TODO: split up into batches of 1000
        # if len(to_get) >= 1000:
        #     to_get = to_get[1:1000]

        print('getting objects...')
        narrative_objects = object_cache.get(to_get)
        print('done')

        now = time.time()
        print('...got %s in %s' % (len(narrative_objects), now - then))
        stats.append(['narrative_objects', now - then])
        then = now

        # APPS
        # Gather all apps in this narrative,
        # Get the apps
        # Profile a map of apps for a given tag

        print('parsing apps...')
        (narrative_apps, narrative_cell_types, apps,
         elapsed) = self.parse_apps(narrative_objects)
        print('...done')

        stats.append(['app_gets', elapsed])

        now = time.time()
        stats.append(['parse_apps', now - then])
        then = now

        # TODO: permissions, user profiles for permissions, apps

        print('assembling...')
        # Now weave together the sources into a single narrative
        narratives = []
        for (ws, obj, perms, cell_stats,
             napps) in zip(narrative_workspaces, narrative_objects,
                           narrative_workspaces_perms, narrative_cell_types,
                           narrative_apps):
            if obj is None:
                continue
            narratives.append({
                'workspaceId': ws['id'],
                'objectId': obj['id'],
                'objectVersion': obj['version'],
                'owner': ws['owner'],
                'permission': ws['user_permission'],
                'isPublic': ws['isPublic'],
                'isNarratorial': ws['isNarratorial'],
                'title': ws['metadata']['narrative_nice_name'],
                'modifiedTime': ws['modDateMs'],
                'savedTime': obj['saveDateMs'],
                'savedBy': obj['saved_by'],
                'permissions': perms,
                'cellTypes': cell_stats,
                'apps': napps
            })

        now = time.time()
        stats.append(['finalize', now - then])
        then = now
        result = {
            'narratives': narratives,
            'profiles': profiles_map,
            'apps': apps
        }
        return result, stats