def GenerateIndexFromHistory(query_history, all_indexes=None, manual_indexes=None): """Generate most of the text for index.yaml from the query history. Args: query_history: Query history, a dict mapping query all_indexes: Optional datastore_index.IndexDefinitions instance representing all the indexes found in the input file. May be None. manual_indexes: Optional datastore_index.IndexDefinitions instance containing indexes for which we should not generate output. May be None. Returns: A string representation that can safely be appended to an existing index.yaml file. """ all_keys = datastore_index.IndexDefinitionsToKeys(all_indexes) manual_keys = datastore_index.IndexDefinitionsToKeys(manual_indexes) indexes = dict((key, 0) for key in all_keys - manual_keys) for query, count in query_history.iteritems(): key = datastore_index.CompositeIndexForQuery(query) if key is not None: key = key[:3] if key not in manual_keys: if key in indexes: indexes[key] += count else: indexes[key] = count res = [] for (kind, ancestor, props), count in sorted(indexes.iteritems()): res.append('') if count == 0: message = '# Unused in query history -- copied from input.' elif count == 1: message = '# Used once in query history.' else: message = '# Used %d times in query history.' % count res.append(message) res.append('- kind: %s' % kind) if ancestor: res.append(' ancestor: yes') if props: res.append(' properties:') for name, direction in props: res.append(' - name: %s' % name) if direction == datastore_index.DESCENDING: res.append(' direction: desc') res.append('') return '\n'.join(res)
def GenerateIndexFromHistory(query_history, all_indexes=None, manual_indexes=None): """Generate most of the text for index.yaml from the query history. Args: query_history: Query history, a dict mapping query all_indexes: Optional datastore_index.IndexDefinitions instance representing all the indexes found in the input file. May be None. manual_indexes: Optional datastore_index.IndexDefinitions instance containing indexes for which we should not generate output. May be None. Returns: A string representation that can safely be appended to an existing index.yaml file. Returns the empty string if it would generate no output. """ all_keys = datastore_index.IndexDefinitionsToKeys(all_indexes) manual_keys = datastore_index.IndexDefinitionsToKeys(manual_indexes) indexes = dict((key, 0) for key in all_keys - manual_keys) for query, count in query_history.iteritems(): required, kind, ancestor, props, num_eq_filters = datastore_index.CompositeIndexForQuery(query) if required: key = (kind, ancestor, props) if key not in manual_keys: if key in indexes: indexes[key] += count else: indexes[key] = count if not indexes: return '' res = [] for (kind, ancestor, props), count in sorted(indexes.iteritems()): res.append('') res.append(datastore_index.IndexYamlForQuery(kind, ancestor, props)) res.append('') return '\n'.join(res)
def GenerateIndexDictFromHistory(query_history, all_indexes=None, manual_indexes=None): """Generate a dict of automatic index entries from the query history. Args: query_history: Query history, a dict mapping datastore_pb.Query to a count of the number of times that query has been issued. all_indexes: Optional datastore_index.IndexDefinitions instance representing all the indexes found in the input file. May be None. manual_indexes: Optional datastore_index.IndexDefinitions instance containing indexes for which we should not generate output. May be None. Returns: A dict where each key is a tuple (kind, ancestor, properties) and the corresponding value is a count of the number of times that query has been issued. The dict contains no entries for keys that appear in manual_keys. In the tuple, "properties" is itself a tuple of tuples, where each contained tuple is (name, direction), with "name" being a string and "direction" being datastore_index.ASCENDING or .DESCENDING. """ all_keys = datastore_index.IndexDefinitionsToKeys(all_indexes) manual_keys = datastore_index.IndexDefinitionsToKeys(manual_indexes) indexes = dict((key, 0) for key in all_keys - manual_keys) for query, count in six.iteritems(query_history): required, kind, ancestor, props = ( datastore_index.CompositeIndexForQuery(query)) if required: props = datastore_index.GetRecommendedIndexProperties(props) key = (kind, ancestor, props) if key not in manual_keys: if key in indexes: indexes[key] += count else: indexes[key] = count return indexes