1+ import { validate } from "@babel/types" ;
2+ import { sheets } from "googleapis/build/src/apis/sheets" ;
13import _ from "lodash" ;
4+ import { countBy , size } from "lodash-es" ;
25import moment from "moment" ;
36import BigQueryQuery from "./bigquery_query" ;
47import ResponseParser , { IResultFormat } from "./response_parser" ;
5- import { countBy , size } from "lodash-es" ;
6- import { sheets } from "googleapis/build/src/apis/sheets" ;
7- import { validate } from "@babel/types" ;
88
99const Shifted = "_shifted" ;
1010function sleep ( ms ) {
@@ -134,6 +134,21 @@ export class BigQueryDatasource {
134134 q = q . replace ( to , newTo ) + "\n " ;
135135 return q ;
136136 }
137+
138+ private static _updateTableSuffix ( q , options ) {
139+ const ind = q . indexOf ( "AND _TABLE_SUFFIX BETWEEN " ) ;
140+ if ( ind < 1 ) {
141+ return q ;
142+ }
143+ const from = q . substr ( ind + 28 , 8 ) ;
144+
145+ const newFrom = BigQueryQuery . formatDateToString ( options . range . from . _d ) ;
146+ q = q . replace ( from , newFrom ) ;
147+ const to = q . substr ( ind + 43 , 8 ) ;
148+ const newTo = BigQueryQuery . formatDateToString ( options . range . to . _d ) ;
149+ q = q . replace ( to , newTo ) + "\n " ;
150+ return q ;
151+ }
137152 public authenticationType : string ;
138153 public projectName : string ;
139154 private readonly id : any ;
@@ -233,6 +248,7 @@ export class BigQueryDatasource {
233248 ) ;
234249 let q = this . queryModel . expend_macros ( modOptions ) ;
235250 q = BigQueryDatasource . _updatePartition ( q , modOptions ) ;
251+ q = BigQueryDatasource . _updateTableSuffix ( q , modOptions ) ;
236252 if ( query . refId . search ( Shifted ) > - 1 ) {
237253 q = this . _updateAlias ( q , modOptions , query . refId ) ;
238254 }
@@ -251,29 +267,28 @@ export class BigQueryDatasource {
251267 for ( const response of responses ) {
252268 if ( response . type && response . type === "table" ) {
253269 data . push ( response ) ;
254- } else {
255- for ( const dp of response ) {
256- data . push ( dp ) ;
257- }
270+ } else {
271+ for ( const dp of response ) {
272+ data . push ( dp ) ;
273+ }
258274 }
259275 }
260- for ( const d of data ) {
276+ for ( const d of data ) {
261277 if ( typeof d . target !== "undefined" && d . target . search ( Shifted ) > - 1 ) {
262- const res = BigQueryDatasource . _getShiftPeriod (
263- d . target . substring ( d . target . lastIndexOf ( "_" ) + 1 , d . target . length )
264- ) ;
265- const shiftPeriod = res [ 0 ] ;
266- const shiftVal = res [ 1 ] ;
267- for ( let i = 0 ; i < d . datapoints . length ; i ++ ) {
268- d . datapoints [ i ] [ 1 ] = moment ( d . datapoints [ i ] [ 1 ] )
278+ const res = BigQueryDatasource . _getShiftPeriod (
279+ d . target . substring ( d . target . lastIndexOf ( "_" ) + 1 , d . target . length )
280+ ) ;
281+ const shiftPeriod = res [ 0 ] ;
282+ const shiftVal = res [ 1 ] ;
283+ for ( let i = 0 ; i < d . datapoints . length ; i ++ ) {
284+ d . datapoints [ i ] [ 1 ] = moment ( d . datapoints [ i ] [ 1 ] )
269285 . subtract ( shiftVal , shiftPeriod )
270- . valueOf ( ) ;
271- }
286+ . valueOf ( ) ;
272287 }
273288 }
274- return { data } ;
275289 }
276- ) ;
290+ return { data } ;
291+ } ) ;
277292 }
278293
279294 public metricFindQuery ( query , optionalOptions ) {
0 commit comments