def suggestSearchTypeahead(output, search, usersquery, count, max_time, earliest_time, latest_time, servers, namespace, user):

    commandAndArgs = utils.getLastCommand(search, None)
    if commandAndArgs != None:
        command, args = commandAndArgs
        if command == "search":
            output['typeahead'] = getTypeaheadTerms(args, usersquery, count, max_time, earliest_time, latest_time, servers, namespace, user)
def commandHelp(output, user, search, aliasMap, bnf):
    commandAndArgs = utils.getLastCommand(search, aliasMap)
    if commandAndArgs != None:
        command, args = commandAndArgs
        description = describer.describeCommand(bnf, command, True)
        if description != None:
            output['command'] = description
def commandHelp(output, user, search, aliasMap, bnf):
    commandAndArgs = utils.getLastCommand(search, aliasMap)
    if commandAndArgs != None:
        command, args = commandAndArgs
        description = describer.describeCommand(bnf, command, True)
        if description != None:
            output['command'] = description
Exemplo n.º 4
0
def suggestSearchTypeahead(output, search, count, max_time, earliest_time, latest_time, servers, namespace, user):

    commandAndArgs = utils.getLastCommand(search, None)
    if commandAndArgs != None:
        command, args = commandAndArgs
        if command == "search":
            output['typeahead'] = getTypeaheadTerms(args, count, max_time, earliest_time, latest_time, servers, namespace, user)
def argTypeahead(output, sessionKey, namespace, user, bnf, search):
    try:
        commandAndArgs = utils.getLastCommand(search, None)
        if commandAndArgs != None:
            cmd, args = commandAndArgs
            typeahead = []
            stanza = cmd + '-command'
            s = describer.cleanSyntax(
                describer.recurseSyntax(
                    stanza, bnf, bnf[stanza], {}, True, 0,
                    1500))  # recurse syntax up to 1500 chars
            e = parser.getExp(s)

            tokens = {}
            hasFields = False
            parser.getTokens(e, tokens)
            getvalue = False
            for a, v in tokens.items():
                if a == cmd: continue

                if a.startswith('<') and a.endswith(
                        '>') and 'field' in a.lower() or v == '<field>':
                    hasFields = True

                if args.endswith('='):
                    args = args[:-1]
                    getvalue = True
                b1, replacement, b2 = getReplacement(args, a)
                # only show keywords when we match because we have so low confidence about their correctness in any given spot of a search command
                if replacement != '':
                    prev = len(replacement) + 1
                    if prev > len(args) or not args[-prev].isalpha():
                        #print "%s\t: %s ('%s')" % (a,v, replacement)
                        if getvalue:
                            if v == '<bool>':
                                v = ['true', 'false']
                            a = v
                            v = 'datatype'
                        if isinstance(a, list):
                            for val in a:
                                typeahead.append((val, 'choice', replacement))
                        else:
                            if isinstance(v, list):
                                v = '<list>'
                            typeahead.append((a, v, replacement))

            output['has_field_args'] = hasFields
            output['arg_typeahead'] = typeahead

    except Exception, e:
        msg = str(e) + traceback.format_exc()
        output['notices'].insert(0, msg)
def argTypeahead(output, sessionKey, namespace, user, bnf, search):
    try:
        commandAndArgs = utils.getLastCommand(search, None)
        if commandAndArgs != None:
            cmd, args = commandAndArgs
            typeahead = []
            stanza = cmd + '-command'
            s = describer.cleanSyntax(describer.recurseSyntax(stanza, bnf, bnf[stanza], {}, True, 0, 1500)) # recurse syntax up to 1500 chars
            e = parser.getExp(s)

            tokens = {}
            hasFields = False
            parser.getTokens(e, tokens)
            getvalue = False
            for a,v in tokens.items():
                if a == cmd: continue

                if a.startswith('<') and a.endswith('>') and 'field' in a.lower() or v == '<field>':
                    hasFields = True

                if args.endswith('='):
                    args = args[:-1]
                    getvalue = True
                b1, replacement, b2 = getReplacement(args, a)
                # only show keywords when we match because we have so low confidence about their correctness in any given spot of a search command
                if replacement != '':
                    prev = len(replacement)+1
                    if prev > len(args) or not args[-prev].isalpha():
                        #print "%s\t: %s ('%s')" % (a,v, replacement)
                        if getvalue:
                            if v == '<bool>':
                                v = ['true','false']
                            a = v
                            v = 'datatype'
                        if isinstance(a, list):
                            for val in a:
                                typeahead.append((val,'choice', replacement))
                        else:
                            if isinstance(v, list):
                                v = '<list>'
                            typeahead.append((a,v, replacement))

            output['has_field_args'] = hasFields
            output['arg_typeahead'] = typeahead

    except Exception, e:
        msg = str(e) + traceback.format_exc()
        output['notices'].insert(0,msg)
def nextCommand(output, sessionKey, namespace, user, search, usersquery, queryprefix, aliasMap, bnf, showargs):

    ## overallstart = start = time.time()
    ## timing_last_command = 0
    ## timing_all_commands = 0
    ## timing_get_next_data = 0
    ## timing_get_args = 0
    ## timing_add_commands = 0
    ## timing_past_matches = 0
    ## timing_sort_past_matches = 0
    
    atPipe = False

    # if search ends in "|", don't give args for last command, but
    # give next information from previous commandd
    if search[-1] == "|":
        search = search[:-1]
        showargs = False
        atPipe = True
        
    nextcommands = []
    typeaheadcommands = []
    
    commandAndArgs = utils.getLastCommand(search, aliasMap)
    
    ###################
    ## now = time.time()
    ## timing_last_command = now - start
    ## start = now
    ###################

    
    if commandAndArgs == None:
        # list all generating commands.
        # make a list() copy so we don't trash it by adding search
        typeaheadcommands = list(utils.getAllCommands(bnf, user, namespace, True))

        ###################
        ## now = time.time()
        ## timing_all_commands = now - start
        ## start = now
        ###################

        
    else:        
        command, args = commandAndArgs
        data, pastsearches = next.getNextData(user, bnf, sessionKey, namespace)

        ###################
        ## now = time.time()
        ## timing_get_next_data = now - start
        ## start = now
        ###################

        
        for datum in data:
            if datum['command'] == command:
                typeaheadcommands = [x for x,y in datum['nextcommands'] if x != "<RUN>"]
                if showargs:

                    matchingargs = []
                    fs = fuzzSearch(usersquery)
                    for arg, perc in datum['args']:
                        replacement = "%s | %s %s" % (queryprefix, command, arg)
                        fr = fuzzSearch(replacement)
                        if fr.startswith(fs) and fr != fs:
                            matchingargs.append((arg,perc))
                    output['args'] =  matchingargs
                    #output['args'] =  datum['args']
                break

        ###################
        ## now = time.time()
        ## timing_get_args = now - start
        ## start = now
        ###################
            
        # now add in all commands that were not already added
        if command in aliasMap:
            # adding all the other commands not already added
            for thiscommand in utils.getAllCommands(bnf, user, namespace):
                if thiscommand not in nextcommands and thiscommand not in typeaheadcommands:
                    nextcommands.append(thiscommand)

        ###################
        ## now = time.time()
        ## timing_add_commands = now - start
        ## start = now
        ###################


        # look for pastsearches that the current search is a subset of  (like firefox url autocomplete looking for any term)
        usersearch = normalizeSearch(search)

        # if user didn't enter anything don't match on "search" or "search *", just get most recent
        if usersearch == "" or usersearch == "*":
            pastMatches = [userifySearch(p) for p in pastsearches]
        else:
            pastMatches = [userifySearch(pastsearch) for pastsearch in pastsearches if normalizedSearchMatch(True, usersquery, pastsearch)]
            pastMatches.extend([userifySearch(pastsearch) for pastsearch in pastsearches if normalizedSearchMatch(False, usersquery, pastsearch)])

        ###################
        ## now = time.time()
        ## timing_past_matches = now - start
        ## start = now
        ###################
            
        # dedup
        pastMatches = sorted(list(set(pastMatches)), key=pastMatches.index)         
        output['autocomplete'] = pastMatches[:10] # just the 10 most recent
        output['autocomplete_match'] = usersearch

        ###################
        ## now = time.time()
        ## timing_sort_past_matches = now - start
        ## start = now
        ###################


    # new.  only show next command if we aren't showing the args for the current command.
    # use will see next commands when they type "|"
    if atPipe:
        # keep only those that alias to themselves -- i.e., don't show aliases
        nextcommands = [x for x in nextcommands if aliasMap.get(x, '') == x]
        typeaheadcommands = [x for x in typeaheadcommands if aliasMap.get(x, '') == x]
        s = usersquery.strip()
        if '|' in s:
            s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf,x,"shortdesc",""), s + " | " + x) for x in typeaheadcommands]
        output['nexts'] = [(x, utils.getAttr(bnf,x,"shortdesc",""), s + " | " + x) for x in nextcommands]
def nextCommand(output, sessionKey, namespace, user, search, usersquery,
                queryprefix, aliasMap, bnf, showargs):

    ## overallstart = start = time.time()
    ## timing_last_command = 0
    ## timing_all_commands = 0
    ## timing_get_next_data = 0
    ## timing_get_args = 0
    ## timing_add_commands = 0
    ## timing_past_matches = 0
    ## timing_sort_past_matches = 0

    atPipe = False

    # if search ends in "|", don't give args for last command, but
    # give next information from previous commandd
    if search[-1] == "|":
        search = search[:-1]
        showargs = False
        atPipe = True

    nextcommands = []
    typeaheadcommands = []

    commandAndArgs = utils.getLastCommand(search, aliasMap)

    ###################
    ## now = time.time()
    ## timing_last_command = now - start
    ## start = now
    ###################

    if commandAndArgs == None:
        # list all generating commands.
        # make a list() copy so we don't trash it by adding search
        typeaheadcommands = list(
            utils.getAllCommands(bnf, user, namespace, True))

        ###################
        ## now = time.time()
        ## timing_all_commands = now - start
        ## start = now
        ###################

    else:
        command, args = commandAndArgs
        data, pastsearches = next.getNextData(user, bnf, sessionKey, namespace)

        ###################
        ## now = time.time()
        ## timing_get_next_data = now - start
        ## start = now
        ###################

        for datum in data:
            if datum['command'] == command:
                typeaheadcommands = [
                    x for x, y in datum['nextcommands'] if x != "<RUN>"
                ]
                if showargs:

                    matchingargs = []
                    fs = fuzzSearch(usersquery)
                    for arg, perc in datum['args']:
                        replacement = "%s | %s %s" % (queryprefix, command,
                                                      arg)
                        fr = fuzzSearch(replacement)
                        if fr.startswith(fs) and fr != fs:
                            matchingargs.append((arg, perc))
                    output['args'] = matchingargs
                    #output['args'] =  datum['args']
                break

        ###################
        ## now = time.time()
        ## timing_get_args = now - start
        ## start = now
        ###################

        # now add in all commands that were not already added
        if command in aliasMap:
            # adding all the other commands not already added
            for thiscommand in utils.getAllCommands(bnf, user, namespace):
                if thiscommand not in nextcommands and thiscommand not in typeaheadcommands:
                    nextcommands.append(thiscommand)

        ###################
        ## now = time.time()
        ## timing_add_commands = now - start
        ## start = now
        ###################

        # look for pastsearches that the current search is a subset of  (like firefox url autocomplete looking for any term)
        usersearch = normalizeSearch(search)

        # if user didn't enter anything don't match on "search" or "search *", just get most recent
        if usersearch == "" or usersearch == "*":
            pastMatches = [userifySearch(p) for p in pastsearches]
        else:
            pastMatches = [
                userifySearch(pastsearch) for pastsearch in pastsearches
                if normalizedSearchMatch(True, usersquery, pastsearch)
            ]
            pastMatches.extend([
                userifySearch(pastsearch) for pastsearch in pastsearches
                if normalizedSearchMatch(False, usersquery, pastsearch)
            ])

        ###################
        ## now = time.time()
        ## timing_past_matches = now - start
        ## start = now
        ###################

        # dedup
        pastMatches = sorted(list(set(pastMatches)), key=pastMatches.index)
        output['autocomplete'] = pastMatches[:10]  # just the 10 most recent
        output['autocomplete_match'] = usersearch

        ###################
        ## now = time.time()
        ## timing_sort_past_matches = now - start
        ## start = now
        ###################

    # new.  only show next command if we aren't showing the args for the current command.
    # use will see next commands when they type "|"
    if atPipe:
        # keep only those that alias to themselves -- i.e., don't show aliases
        nextcommands = [x for x in nextcommands if aliasMap.get(x, '') == x]
        typeaheadcommands = [
            x for x in typeaheadcommands if aliasMap.get(x, '') == x
        ]
        s = usersquery.strip()
        if '|' in s:
            s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                                 ""), s + " | " + x)
                               for x in typeaheadcommands]
        output['nexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                             ""), s + " | " + x)
                           for x in nextcommands]