11import _ from "lodash" ;
2+ import moment from "moment" ;
23import { rawIngestionSpecDefaults } from "../configs/IngestionConfig" ;
34import { datasetService } from "./DatasetService" ;
45
@@ -125,7 +126,7 @@ class TableGenerator extends BaseTableGenerator {
125126 "dataSchema" : {
126127 "dataSource" : datasourceRef ,
127128 "dimensionsSpec" : { "dimensions" : this . getDruidDimensions ( allFields , this . getTimestampKey ( dataset , "druid" ) , dataset_config . keys_config . partition_key ) } ,
128- "timestampSpec" : { "column" : this . getTimestampKey ( dataset , "druid" ) , "format" : "auto" } ,
129+ "timestampSpec" : { "column" : this . getTimestampKey ( dataset , "druid" ) , "format" : this . getTimestampFormat ( dataset , dataset . sample_data ) } ,
129130 "metricsSpec" : [ ] ,
130131 "granularitySpec" : ingestionSpecDefaults . granularitySpec
131132 } ,
@@ -332,6 +333,49 @@ class TableGenerator extends BaseTableGenerator {
332333 }
333334 return _ . replace ( timestamp , / \. / g, "_" ) ;
334335 }
336+
337+ private getTimestampFormat = ( dataset : Record < string , any > , sampleData : Record < string , any > ) : string => {
338+ const timestampKey = this . getTimestampKey ( dataset , "druid" ) ;
339+ const timestampValue = sampleData ?. mergedEvent ?. [ timestampKey ] ;
340+ if ( ! timestampValue || timestampValue === "obsrv_meta.syncts" ) {
341+ return "auto" ;
342+ }
343+ return detectTimestampFormat ( timestampValue ) ;
344+ }
345+
346+ }
347+
348+ const POSSIBLE_FORMATS = [
349+
350+ "YYYY-MM-DD" ,
351+ "DD-MM-YYYY" ,
352+ "MM-dd-yyyy" ,
353+ ] ;
354+
355+ export function detectTimestampFormat ( timestampValue : string | number ) : string {
356+
357+ const value = String ( timestampValue ) ;
358+
359+ if ( / ^ \d + $ / . test ( value ) ) {
360+ const length = value . length ;
361+
362+ if ( length === 10 ) {
363+ return "posix" ;
364+ } else if ( length === 16 ) {
365+ return "micro" ;
366+ } else if ( length === 19 ) {
367+ return "nano" ;
368+ }
369+ }
370+
371+ for ( const format of POSSIBLE_FORMATS ) {
372+ if ( moment ( value , format , true ) . isValid ( ) ) {
373+ return format ;
374+ }
375+ }
376+ return "auto" ;
335377}
336378
379+
380+
337381export const tableGenerator = new TableGenerator ( ) ;
0 commit comments