Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6770F200B81 for ; Tue, 13 Sep 2016 12:41:25 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 661D1160AE6; Tue, 13 Sep 2016 10:41:25 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E60EA160AE8 for ; Tue, 13 Sep 2016 12:41:23 +0200 (CEST) Received: (qmail 88117 invoked by uid 500); 13 Sep 2016 10:41:23 -0000 Mailing-List: contact commits-help@ambari.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ambari-dev@ambari.apache.org Delivered-To: mailing list commits@ambari.apache.org Received: (qmail 85285 invoked by uid 99); 13 Sep 2016 10:41:19 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Sep 2016 10:41:19 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C1870E3839; Tue, 13 Sep 2016 10:41:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: oleewere@apache.org To: commits@ambari.apache.org Date: Tue, 13 Sep 2016 10:41:54 -0000 Message-Id: <6cbff85a84f54c0688d0672354a73dd7@git.apache.org> In-Reply-To: <10d77c3352364029bcbcc7a7ac6c1261@git.apache.org> References: <10d77c3352364029bcbcc7a7ac6c1261@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [37/51] [abbrv] ambari git commit: AMBARI-18310. Refactor logsearch portal side code (oleewere) archived-at: Tue, 13 Sep 2016 10:41:25 -0000 http://git-wip-us.apache.org/repos/asf/ambari/blob/bda3a1eb/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/ManagerBase.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/ManagerBase.java b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/ManagerBase.java new file mode 100644 index 0000000..13df470 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/ManagerBase.java @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ambari.logsearch.manager; + +import java.io.File; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Scanner; + +import org.apache.ambari.logsearch.common.LogSearchConstants; +import org.apache.ambari.logsearch.common.MessageEnums; +import org.apache.ambari.logsearch.model.response.LogData; +import org.apache.ambari.logsearch.model.response.LogSearchResponse; +import org.apache.ambari.logsearch.query.model.SearchCriteria; +import org.apache.ambari.logsearch.dao.SolrDaoBase; +import org.apache.ambari.logsearch.query.QueryGeneration; +import org.apache.ambari.logsearch.util.DateUtil; +import org.apache.ambari.logsearch.util.JSONUtil; +import org.apache.ambari.logsearch.util.RESTErrorUtil; +import org.apache.ambari.logsearch.util.SolrUtil; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.response.QueryResponse; +import org.apache.solr.common.SolrDocumentList; +import org.apache.solr.common.SolrException; + +import javax.inject.Inject; + +public abstract class ManagerBase extends JsonManagerBase { + private static final Logger logger = Logger.getLogger(ManagerBase.class); + + @Inject + protected QueryGeneration queryGenerator; + + public enum LogType { + SERVICE("Service"), + AUDIT("Audit"); + + private String label; + + private LogType(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + } + + public ManagerBase() { + super(); + } + + public String getHadoopServiceConfigJSON() { + StringBuilder result = new StringBuilder(""); + + // Get file from resources folder + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("HadoopServiceConfig.json").getFile()); + + try (Scanner scanner = new Scanner(file)) { + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + result.append(line).append("\n"); + } + + scanner.close(); + + } catch (IOException e) { + logger.error("Unable to read HadoopServiceConfig.json", e); + throw RESTErrorUtil.createRESTException(e.getMessage(), MessageEnums.ERROR_SYSTEM); + } + + String hadoopServiceConfig = result.toString(); + if (JSONUtil.isJSONValid(hadoopServiceConfig)) { + return hadoopServiceConfig; + } + throw RESTErrorUtil.createRESTException("Improper JSON", MessageEnums.ERROR_SYSTEM); + + } + + protected SEARCH_RESPONSE getLastPage(SearchCriteria searchCriteria, String logTimeField, SolrDaoBase solrDoaBase, + SolrQuery lastPageQuery) { + + Integer maxRows = searchCriteria.getMaxRows(); + String givenSortType = searchCriteria.getSortType(); + searchCriteria = new SearchCriteria(); + searchCriteria.setSortBy(logTimeField); + if (givenSortType == null || givenSortType.equals(LogSearchConstants.DESCENDING_ORDER)) { + lastPageQuery.removeSort(LogSearchConstants.LOGTIME); + searchCriteria.setSortType(LogSearchConstants.ASCENDING_ORDER); + } else { + searchCriteria.setSortType(LogSearchConstants.DESCENDING_ORDER); + } + queryGenerator.setSingleSortOrder(lastPageQuery, searchCriteria); + + + Long totalLogs = 0l; + int startIndex = 0; + int numberOfLogsOnLastPage = 0; + SEARCH_RESPONSE logResponse = null; + try { + SolrUtil.setStart(lastPageQuery, 0); + SolrUtil.setRowCount(lastPageQuery, maxRows); + logResponse = getLogAsPaginationProvided(lastPageQuery, solrDoaBase); + totalLogs = countQuery(lastPageQuery,solrDoaBase); + startIndex = Integer.parseInt("" + ((totalLogs / maxRows) * maxRows)); + numberOfLogsOnLastPage = Integer.parseInt("" + (totalLogs - startIndex)); + logResponse.setStartIndex(startIndex); + logResponse.setTotalCount(totalLogs); + logResponse.setPageSize(maxRows); + List docList = logResponse.getLogList(); + List lastPageDocList = new ArrayList<>(); + logResponse.setLogList(lastPageDocList); + int cnt = 0; + for(LOG_DATA_TYPE doc:docList){ + if(cnt serviceLogDataList = convertToSolrBeans(response); + if (docList != null && !docList.isEmpty()) { + logResponse.setLogList(serviceLogDataList); + logResponse.setStartIndex((int) docList.getStart()); + logResponse.setTotalCount(docList.getNumFound()); + Integer rowNumber = solrQuery.getRows(); + if (rowNumber == null) { + logger.error("No RowNumber was set in solrQuery"); + return createLogSearchResponse(); + } + logResponse.setPageSize(rowNumber); + } + return logResponse; + } catch (SolrException | SolrServerException | IOException e) { + logger.error("Error during solrQuery=" + solrQuery, e); + throw RESTErrorUtil.createRESTException(MessageEnums.SOLR_ERROR.getMessage().getMessage(), MessageEnums.ERROR_SYSTEM); + } + } + + protected Long countQuery(SolrQuery query,SolrDaoBase solrDaoBase) throws SolrException, SolrServerException, IOException { + query.setRows(0); + QueryResponse response = solrDaoBase.process(query); + if (response == null) { + return 0l; + } + SolrDocumentList docList = response.getResults(); + if (docList == null) { + return 0l; + } + return docList.getNumFound(); + } + + protected String getUnit(String unit) { + if (StringUtils.isBlank(unit)) { + unit = "+1HOUR"; + } + return unit; + } + + protected String getFrom(String from) { + if (StringUtils.isBlank(from)) { + Date date = DateUtil.getTodayFromDate(); + try { + from = DateUtil.convertGivenDateFormatToSolrDateFormat(date); + } catch (ParseException e) { + from = "NOW"; + } + } + return from; + } + + protected String getTo(String to) { + if (StringUtils.isBlank(to)) { + to = "NOW"; + } + return to; + } + + protected abstract List convertToSolrBeans(QueryResponse response); + + protected abstract SEARCH_RESPONSE createLogSearchResponse(); +} http://git-wip-us.apache.org/repos/asf/ambari/blob/bda3a1eb/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/MgrBase.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/MgrBase.java b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/MgrBase.java deleted file mode 100644 index c0be79d..0000000 --- a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/MgrBase.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.ambari.logsearch.manager; - -import java.io.File; -import java.io.IOException; -import java.text.ParseException; -import java.util.Collections; -import java.util.Date; -import java.util.Scanner; - -import org.apache.ambari.logsearch.common.LogSearchConstants; -import org.apache.ambari.logsearch.common.MessageEnums; -import org.apache.ambari.logsearch.common.SearchCriteria; -import org.apache.ambari.logsearch.dao.SolrDaoBase; -import org.apache.ambari.logsearch.query.QueryGeneration; -import org.apache.ambari.logsearch.util.DateUtil; -import org.apache.ambari.logsearch.util.JSONUtil; -import org.apache.ambari.logsearch.util.RESTErrorUtil; -import org.apache.ambari.logsearch.util.SolrUtil; -import org.apache.ambari.logsearch.view.VSolrLogList; -import org.apache.commons.lang.StringUtils; -import org.apache.log4j.Logger; -import org.apache.solr.client.solrj.SolrQuery; -import org.apache.solr.client.solrj.SolrServerException; -import org.apache.solr.client.solrj.response.QueryResponse; -import org.apache.solr.common.SolrDocument; -import org.apache.solr.common.SolrDocumentList; -import org.apache.solr.common.SolrException; -import org.springframework.beans.factory.annotation.Autowired; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -public class MgrBase { - private static final Logger logger = Logger.getLogger(MgrBase.class); - - @Autowired - protected QueryGeneration queryGenerator; - - private JsonSerializer jsonDateSerialiazer = null; - private JsonDeserializer jsonDateDeserialiazer = null; - - public enum LogType { - SERVICE("Service"), - AUDIT("Audit"); - - private String label; - - private LogType(String label) { - this.label = label; - } - - public String getLabel() { - return label; - } - } - - public MgrBase() { - jsonDateSerialiazer = new JsonSerializer() { - - @Override - public JsonElement serialize(Date paramT, java.lang.reflect.Type paramType, JsonSerializationContext paramJsonSerializationContext) { - return paramT == null ? null : new JsonPrimitive(paramT.getTime()); - } - }; - - jsonDateDeserialiazer = new JsonDeserializer() { - - @Override - public Date deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return json == null ? null : new Date(json.getAsLong()); - } - - }; - } - - protected String convertObjToString(Object obj) { - if (obj == null) { - return ""; - } - - Gson gson = new GsonBuilder() - .registerTypeAdapter(Date.class, jsonDateSerialiazer) - .registerTypeAdapter(Date.class, jsonDateDeserialiazer).create(); - - return gson.toJson(obj); - } - - public String getHadoopServiceConfigJSON() { - StringBuilder result = new StringBuilder(""); - - // Get file from resources folder - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("HadoopServiceConfig.json").getFile()); - - try (Scanner scanner = new Scanner(file)) { - - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - result.append(line).append("\n"); - } - - scanner.close(); - - } catch (IOException e) { - logger.error("Unable to read HadoopServiceConfig.json", e); - throw RESTErrorUtil.createRESTException(e.getMessage(), MessageEnums.ERROR_SYSTEM); - } - - String hadoopServiceConfig = result.toString(); - if (JSONUtil.isJSONValid(hadoopServiceConfig)) { - return hadoopServiceConfig; - } - throw RESTErrorUtil.createRESTException("Improper JSON", MessageEnums.ERROR_SYSTEM); - - } - - protected VSolrLogList getLastPage(SearchCriteria searchCriteria, String logTimeField, SolrDaoBase solrDoaBase, - SolrQuery lastPageQuery) { - - Integer maxRows = searchCriteria.getMaxRows(); - String givenSortType = searchCriteria.getSortType(); - searchCriteria = new SearchCriteria(); - searchCriteria.setSortBy(logTimeField); - if (givenSortType == null || givenSortType.equals(LogSearchConstants.DESCENDING_ORDER)) { - lastPageQuery.removeSort(LogSearchConstants.LOGTIME); - searchCriteria.setSortType(LogSearchConstants.ASCENDING_ORDER); - } else { - searchCriteria.setSortType(LogSearchConstants.DESCENDING_ORDER); - } - queryGenerator.setSingleSortOrder(lastPageQuery, searchCriteria); - - - Long totalLogs = 0l; - int startIndex = 0; - int numberOfLogsOnLastPage = 0; - VSolrLogList collection = null; - try { - SolrUtil.setStart(lastPageQuery, 0); - SolrUtil.setRowCount(lastPageQuery, maxRows); - collection = getLogAsPaginationProvided(lastPageQuery, solrDoaBase); - totalLogs = countQuery(lastPageQuery,solrDoaBase); - if(maxRows != null){ - startIndex = Integer.parseInt("" + ((totalLogs/maxRows) * maxRows)); - numberOfLogsOnLastPage = Integer.parseInt("" + (totalLogs-startIndex)); - } - collection.setStartIndex(startIndex); - collection.setTotalCount(totalLogs); - collection.setPageSize(maxRows); - SolrDocumentList docList = collection.getList(); - SolrDocumentList lastPageDocList = new SolrDocumentList(); - collection.setSolrDocuments(lastPageDocList); - int cnt = 0; - for(SolrDocument doc:docList){ - if(cnt nameValues = new ArrayList<>(); + NameValueData nameValue = new NameValueData(); + nameValue.setName("simpleAuth"); + nameValue.setValue("" + simpleAuthenticationProvider.isEnable()); + nameValues.add(nameValue); + nameValueList.setvNameValues(nameValues); + return convertObjToString(nameValueList); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/bda3a1eb/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/PublicMgr.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/PublicMgr.java b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/PublicMgr.java deleted file mode 100644 index 398d270..0000000 --- a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/PublicMgr.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.ambari.logsearch.manager; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.ambari.logsearch.view.VNameValue; -import org.apache.ambari.logsearch.view.VNameValueList; -import org.apache.ambari.logsearch.web.security.LogsearchSimpleAuthenticationProvider; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class PublicMgr extends MgrBase { - @Autowired - private LogsearchSimpleAuthenticationProvider simpleAuthenticationProvider; - - public String getGeneralConfig() { - VNameValueList nameValueList = new VNameValueList(); - List nameValues = new ArrayList(); - VNameValue nameValue = new VNameValue(); - nameValue.setName("simpleAuth"); - nameValue.setValue("" + simpleAuthenticationProvider.isEnable()); - nameValues.add(nameValue); - nameValueList.setVNameValues(nameValues); - return convertObjToString(nameValueList); - } -}