aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/DumpTool.java
blob: a07b8a254a72b43204313d77ded6f3cc51611187 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile;

import java.io.File;
import java.util.Map;

import com.yahoo.yolean.Exceptions;
import com.yahoo.search.Query;
import com.yahoo.search.query.profile.config.QueryProfileXMLReader;

/**
 * A standalone tool for dumping query profile properties
 *
 * @author bratseth
 */
public class DumpTool {

    /** Creates and returns a dump from some parameters */
    public String resolveAndDump(String... args) {
        if (args.length == 0 || args[0].startsWith("-")) {
            StringBuilder result = new StringBuilder();
            result.append("Dumps all resolved query profile properties for a set of dimension values\n");
            result.append("USAGE: dump [query-profile] [dir]? [parameters]?\n");
            result.append("  and [query-profile] is the name of the query profile to dump the values of\n");
            result.append("  and [dir] is a path to an application package or query profile directory. Default: current dir\n");
            result.append("  and   [parameters] is the http request encoded dimension keys used during resolving. Default: none\n");
            result.append("Examples:\n");
            result.append("  dump default\n");
            result.append("  - dumps the 'default' profile non-variant values in the current dir\n");
            result.append("  dump default x=x1&y=y1\n");
            result.append("  - dumps the 'default' profile resolved with dimensions values x=x1 and y=y1 in the current dir\n");
            result.append("  dump default myapppackage\n");
            result.append("  - dumps the 'default' profile non-variant values in myapppackage/search/query-profiles\n");
            result.append("  dump default dev/myprofiles x=x1&y=y1\n");
            result.append("  - dumps the 'default' profile resolved with dimensions values x=x1 and y=y1 in dev/myprofiles\n");
            return result.toString();
        }

        // Find what the arguments means
        if (args.length >= 3) {
            return dump(args[0], args[1], args[2]);
        }
        else if (args.length == 2) {
            if (args[1].contains("="))
                return dump(args[0], "", args[1]);
            else
                return dump(args[0], args[1],"");
        }
        else { // args.length=1
            return dump(args[0], "", "");
        }
    }

    private String dump(String profileName,String dir,String parameters) {
        // Import profiles
        if (dir.isEmpty())
            dir = ".";
        File dirInAppPackage = new File(dir, "search/query-profiles");
        if (dirInAppPackage.exists())
            dir = dirInAppPackage.getPath();
        QueryProfileXMLReader reader = new QueryProfileXMLReader();
        QueryProfileRegistry registry = reader.read(dir);
        registry.freeze();

        // Dump (through query to get wiring & parameter parsing done easily)
        Query query = new Query("?" + parameters, registry.compile().findQueryProfile(profileName));
        Map<String,Object> properties = query.properties().listProperties();

        // Create result
        StringBuilder b = new StringBuilder();
        for (Map.Entry<String,Object> property : properties.entrySet()) {
            b.append(property.getKey());
            b.append("=");
            b.append(property.getValue().toString());
            b.append("\n");
        }
        return b.toString();
    }

    public static void main(String... args) {
        try {
            System.out.print(new DumpTool().resolveAndDump(args));
        }
        catch (Exception e) {
            System.err.println(Exceptions.toMessageString(e));
        }
    }

}