aboutsummaryrefslogtreecommitdiffstats
path: root/container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java
blob: ef79fab890bbfc1c60ea594787499fc0fabd2d4d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.gui;

import com.google.inject.Inject;

import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.LoggingRequestHandler;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.querytransform.RecallSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.query.Model;
import com.yahoo.search.query.Presentation;
import com.yahoo.search.query.Ranking;
import com.yahoo.search.query.ranking.Diversity;
import com.yahoo.search.query.ranking.MatchPhase;
import com.yahoo.search.query.restapi.ErrorResponse;
import com.yahoo.search.yql.MinimalQueryInserter;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.search.config.IndexInfoConfig;
import com.yahoo.yolean.Exceptions;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;


/**
 * Takes requests on /querybuilder
 *
 * @author  Henrik Høiness
 */

public class GUIHandler extends LoggingRequestHandler {

    private final IndexModel indexModel;
    private final RankProfilesConfig rankProfilesConfig;

    @Inject
    public GUIHandler(Context parentContext, IndexInfoConfig indexInfo, QrSearchersConfig clusters, RankProfilesConfig rankProfilesConfig) {
        super(parentContext);
        this.indexModel = new IndexModel(indexInfo, clusters);
        this.rankProfilesConfig = rankProfilesConfig;

    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        try {
            switch (request.getMethod()) {
                case GET: return handleGET(request);
                default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
            }
        } catch (IllegalArgumentException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        } catch (RuntimeException e) {
            log.log(Level.WARNING, "Unexpected error handling '" + request.getUri() + "'", e);
            return ErrorResponse.internalServerError(Exceptions.toMessageString(e));
        }
    }

    private HttpResponse handleGET(HttpRequest request) {
        com.yahoo.restapi.Path path = new com.yahoo.restapi.Path(request.getUri().getPath());
        if (path.matches("/querybuilder/")) {
            return new FileResponse("_includes/index.html", null, null);
        }
        if (!path.matches("/querybuilder/{*}") ) {
            return ErrorResponse.notFoundError("Nothing at path:" + path);
        }
        String filepath = path.getRest();
        if (!isValidPath(filepath) && !filepath.equals("config.json")){
            return ErrorResponse.notFoundError("Nothing at path:" + filepath);
        }
        return new FileResponse(filepath, indexModel, rankProfilesConfig);
    }

    private static boolean isValidPath(String path) {
        InputStream in = GUIHandler.class.getClassLoader().getResourceAsStream("gui/"+path);
        boolean isValid = (in != null);
        if(isValid){
            try { in.close(); } catch (IOException e) {/* Problem with closing inputstream */}
        }

        return isValid;
    }

    private static class FileResponse extends HttpResponse {

        private final String path;
        private final IndexModel indexModel;
        private final RankProfilesConfig rankProfilesConfig;

        public FileResponse(String relativePath, IndexModel indexModel, RankProfilesConfig rankProfilesConfig) {
            super(200);
            this.path = relativePath;
            this.indexModel = indexModel;
            this.rankProfilesConfig = rankProfilesConfig;

        }

        @Override
        public void render(OutputStream out) throws IOException {
            InputStream is;
            if (this.path.equals("config.json")){
                String json = "{}";
                try { json = getGUIConfig(); } catch (JSONException e) { /*Something happened while parsing JSON */ }
                is = new ByteArrayInputStream(json.getBytes());
            } else{
                is = GUIHandler.class.getClassLoader().getResourceAsStream("gui/"+this.path);
            }
            byte[] buf = new byte[1024];
            int numRead;
            while ( (numRead = is.read(buf) ) >= 0) {
                out.write(buf, 0, numRead);
            }
        }

        @Override
        public String getContentType() {
            if (path.endsWith(".css")) {
                return "text/css";
            } else if (path.endsWith(".js")) {
                return "application/javascript";
            } else if (path.endsWith(".html")) {
                return "text/html";
            } else if (path.endsWith(".php")) {
                return "text/php";
            } else if (path.endsWith(".svg")) {
                return "image/svg+xml";
            } else if (path.endsWith(".eot")) {
                return "application/vnd.ms-fontobject";
            } else if (path.endsWith(".ttf")) {
                return "font/ttf";
            } else if (path.endsWith(".woff")) {
                return "font/woff";
            } else if (path.endsWith(".woff2")) {
                return "font/woff2";
            } else if (path.endsWith(".otf")) {
                return "font/otf";
            } else if (path.endsWith(".png")) {
                return "image/png";
            } else if (path.endsWith(".xml")) {
                return "application/xml";
            } else if (path.endsWith(".ico")) {
                return "image/x-icon";
            } else if (path.endsWith(".json")) {
                return "application/json";
            } else if (path.endsWith(".ttf")) {
                return "font/ttf";
            }
            return "text/html";
        }

        private String getGUIConfig() throws JSONException {
            JSONObject json = new JSONObject();
            json.put("ranking_properties", Arrays.asList("propertyname"));
            json.put("ranking_features", Arrays.asList("featurename"));

            List<String> sources = new ArrayList<>();

            try {
                sources = new ArrayList<>(indexModel.getMasterClusters().keySet());
            } catch (NullPointerException ex){ /* clusters are not set */ }
            json.put("model_sources", sources);

            List<String> rankProfiles = new ArrayList<>();
            try {
                rankProfilesConfig.rankprofile().forEach(rankProfile -> rankProfiles.add(rankProfile.name()));
            } catch (NullPointerException ex){ /* rankprofiles are not set*/ }
            json.put("ranking_profile", rankProfiles);


            // Creating map from parent to children for GUI: parameter --> child-parameters
            HashMap<String, List<String>> childMap = new HashMap<>();
            childMap.put(Model.MODEL, Arrays.asList(Model.DEFAULT_INDEX, Model.ENCODING, Model.LANGUAGE, Model.QUERY_STRING, Model.RESTRICT, Model.SEARCH_PATH, Model.SOURCES, Model.TYPE));
            childMap.put(Ranking.RANKING, Arrays.asList(Ranking.LOCATION, Ranking.FEATURES, Ranking.LIST_FEATURES, Ranking.PROFILE, Ranking.PROPERTIES, Ranking.SORTING, Ranking.FRESHNESS, Ranking.QUERYCACHE, Ranking.MATCH_PHASE));
            childMap.put(Ranking.RANKING +"."+ Ranking.MATCH_PHASE, Arrays.asList(MatchPhase.MAX_HITS, MatchPhase.ATTRIBUTE, MatchPhase.ASCENDING, Ranking.DIVERSITY));
            childMap.put(Ranking.RANKING +"."+ Ranking.MATCH_PHASE +"."+Ranking.DIVERSITY, Arrays.asList(Diversity.ATTRIBUTE, Diversity.MINGROUPS));
            childMap.put(Presentation.PRESENTATION, Arrays.asList(Presentation.BOLDING, Presentation.FORMAT, Presentation.SUMMARY, "template", Presentation.TIMING ));
            childMap.put("trace", Arrays.asList("timestamps"));
            childMap.put("tracelevel", Arrays.asList("rules"));
            childMap.put("metrics", Arrays.asList("ignore"));
            childMap.put("collapse", Arrays.asList("summary"));
            childMap.put("pos", Arrays.asList("ll", "radius", "bb", "attribute"));
            childMap.put("streaming", Arrays.asList("userid", "groupname", "selection", "priority", "maxbucketspervisitor"));
            childMap.put("rules", Arrays.asList("off", "rulebase"));
            json.put("childMap", childMap);

            List<String> levelZeroParameters = Arrays.asList(MinimalQueryInserter.YQL.toString(), Query.HITS.toString(), Query.OFFSET.toString(),
                                "queryProfile", Query.NO_CACHE.toString(), Query.GROUPING_SESSION_CACHE.toString(),
                                Query.SEARCH_CHAIN.toString(), Query.TIMEOUT.toString(), "trace", "tracelevel",
                                Query.TRACE_LEVEL.toString(), Model.MODEL, Ranking.RANKING, "collapse", "collapsesize","collapsefield",
                                Presentation.PRESENTATION, "pos", "streaming", "rules", RecallSearcher.recallName.toString(), "user",
                                "metrics", "");
            json.put("levelZeroParameters", levelZeroParameters);

            return json.toString();
        }
    }
}