vogievetsky commented on a change in pull request #7572: Data loader (GUI 
component)
URL: https://github.com/apache/incubator-druid/pull/7572#discussion_r280985295
 
 

 ##########
 File path: web-console/src/utils/sampler.ts
 ##########
 @@ -0,0 +1,342 @@
+/*
+ * 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.
+ */
+
+import axios from 'axios';
+
+import { getDruidErrorMessage } from './druid-query';
+import { filterMap, sortWithPrefixSuffix } from './general';
+import {
+  DimensionsSpec,
+  getEmptyTimestampSpec,
+  IngestionSpec,
+  IoConfig, MetricSpec,
+  Parser,
+  ParseSpec,
+  Transform, TransformSpec
+} from './ingestion-spec';
+import { deepGet, deepSet, shallowCopy, whitelistKeys } from './object-change';
+import { QueryState } from './query-state';
+
+const SAMPLER_URL = `/druid/indexer/v1/sampler`;
+const BASE_SAMPLER_CONFIG: SamplerConfig = {
+  // skipCache: true,
+  numRows: 500
+};
+
+export interface SampleSpec {
+  type: 'index';
+  spec: IngestionSpec;
+  samplerConfig: SamplerConfig;
+}
+
+export interface SamplerConfig {
+  numRows?: number;
+  cacheKey?: string;
+  skipCache?: boolean;
+}
+
+export interface SampleResponse {
+  cacheKey?: string;
+  data: SampleEntry[];
+}
+
+export interface SampleEntry {
+  raw: string;
+  parsed?: Record<string, any>;
+  unparseable?: boolean;
+  error?: string;
+}
+
+export interface HeaderAndRows {
+  header: string[];
+  rows: SampleEntry[];
+}
+
+function dedupe(xs: string[]): string[] {
+  const seen: Record<string, boolean> = {};
+  return xs.filter((x) => {
+    if (seen[x]) {
+      return false;
+    } else {
+      seen[x] = true;
+      return true;
+    }
+  });
+}
+
+export function headerFromSampleResponse(sampleResponse: SampleResponse, 
ignoreColumn?: string): string[] {
+  let columns = sortWithPrefixSuffix(dedupe(
+    [].concat(...(filterMap(sampleResponse.data, s => s.parsed ? 
Object.keys(s.parsed) : null) as any))
+  ).sort(), ['__time'], []);
+
+  if (ignoreColumn) {
+    columns = columns.filter(c => c !== ignoreColumn);
+  }
+
+  return columns;
+}
+
+export function headerAndRowsFromSampleResponse(sampleResponse: 
SampleResponse, ignoreColumn?: string, parsedOnly = false): HeaderAndRows {
+  return {
+    header: headerFromSampleResponse(sampleResponse, ignoreColumn),
+    rows: parsedOnly ? sampleResponse.data.filter((d: any) => d.parsed) : 
sampleResponse.data
+  };
+}
+
+async function postToSampler(sampleSpec: SampleSpec, forStr: string): 
Promise<SampleResponse> {
+  let sampleResp: any;
+  try {
+    sampleResp = await axios.post(`${SAMPLER_URL}?for=${forStr}`, sampleSpec);
+  } catch (e) {
+    throw new Error(getDruidErrorMessage(e));
+  }
+
+  return sampleResp.data;
+}
+
+export async function sampleForConnect(spec: IngestionSpec): 
Promise<SampleResponse> {
+  const ioConfig: IoConfig = deepGet(spec, 'ioConfig') || {};
+
+  const sampleSpec: SampleSpec = {
+    type: 'index',
+    spec: {
+      ioConfig: deepSet(ioConfig, 'type', 'index')
+      // dataSchema: {
 
 Review comment:
   That will be commented in to support 
https://github.com/apache/incubator-druid/pull/7566

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to