@@ -355,10 +355,11 @@ function fixTimestampToMilliseconds(ts) {
355
355
356
356
/**
357
357
* Returns a period object used by all time related data calculation functions
358
- * @param {moment } prmPeriod period to be calculated
358
+ * @param {moment } prmPeriod period to be calculated (optional)
359
+ * @param {string } bucket - daily or monthly. If bucket is set, period will be modified to fit full months or days
359
360
* @returns {timeObject } time object
360
361
**/
361
- function getPeriodObject ( prmPeriod ) {
362
+ function getPeriodObject ( prmPeriod , bucket ) {
362
363
var startTimestamp , endTimestamp , periodObject , cycleDuration ;
363
364
periodObject = {
364
365
start : 0 ,
@@ -424,7 +425,7 @@ function getPeriodObject(prmPeriod) {
424
425
fromDate . tz ( _appTimezone ) ;
425
426
toDate . tz ( _appTimezone ) ;
426
427
427
- if ( fromDate . valueOf ( ) === toDate . valueOf ( ) ) {
428
+ if ( fromDate . valueOf ( ) === toDate . valueOf ( ) ) { //single day
428
429
cycleDuration = moment . duration ( 1 , "day" ) ;
429
430
Object . assign ( periodObject , {
430
431
dateString : "D MMM, HH:mm" ,
@@ -528,7 +529,7 @@ function getPeriodObject(prmPeriod) {
528
529
else if ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) w e e k s / . test ( period ) ) {
529
530
const nWeeks = parseInt ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) w e e k s / . exec ( period ) [ 1 ] ) ;
530
531
startTimestamp = _currMoment . clone ( ) . startOf ( "week" ) . subtract ( ( nWeeks - 1 ) , "weeks" ) ;
531
- cycleDuration = moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 ;
532
+ cycleDuration = moment . duration ( moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 , "days" ) ;
532
533
Object . assign ( periodObject , {
533
534
dateString : "D MMM" ,
534
535
isSpecialPeriod : true
@@ -537,7 +538,7 @@ function getPeriodObject(prmPeriod) {
537
538
else if ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) m o n t h s / . test ( period ) ) {
538
539
const nMonths = parseInt ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) m o n t h s / . exec ( period ) [ 1 ] ) ;
539
540
startTimestamp = _currMoment . clone ( ) . startOf ( "month" ) . subtract ( ( nMonths - 1 ) , "months" ) ;
540
- cycleDuration = moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 ;
541
+ cycleDuration = moment . duration ( moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 , "days" ) ;
541
542
Object . assign ( periodObject , {
542
543
dateString : "D MMM" ,
543
544
isSpecialPeriod : true
@@ -546,7 +547,7 @@ function getPeriodObject(prmPeriod) {
546
547
else if ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) y e a r s / . test ( period ) ) {
547
548
const nYears = parseInt ( / ( [ 1 - 9 ] [ 0 - 9 ] * ) y e a r s / . exec ( period ) [ 1 ] ) ;
548
549
startTimestamp = _currMoment . clone ( ) . startOf ( "year" ) . subtract ( ( nYears - 1 ) , "years" ) ;
549
- cycleDuration = moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 ;
550
+ cycleDuration = moment . duration ( moment . duration ( _currMoment . clone ( ) . diff ( startTimestamp ) ) . asDays ( ) + 1 , "days" ) ;
550
551
Object . assign ( periodObject , {
551
552
dateString : "D MMM" ,
552
553
isSpecialPeriod : true
@@ -555,7 +556,6 @@ function getPeriodObject(prmPeriod) {
555
556
//incorrect period, defaulting to 30 days
556
557
else {
557
558
let nDays = 30 ;
558
-
559
559
startTimestamp = _currMoment . clone ( ) . startOf ( "day" ) . subtract ( nDays - 1 , "days" ) ;
560
560
cycleDuration = moment . duration ( nDays , "days" ) ;
561
561
Object . assign ( periodObject , {
@@ -564,6 +564,27 @@ function getPeriodObject(prmPeriod) {
564
564
} ) ;
565
565
}
566
566
567
+ if ( bucket ) {
568
+ if ( bucket === "monthly" ) {
569
+ //we modify choosen period to extend to full months
570
+ startTimestamp = startTimestamp . clone ( ) . startOf ( "month" ) ;
571
+ endTimestamp = endTimestamp . clone ( ) . endOf ( "month" ) ;
572
+ Object . assign ( periodObject , {
573
+ dateString : "MMM" ,
574
+ isSpecialPeriod : true
575
+ } ) ;
576
+
577
+ cycleDuration = moment . duration ( moment . duration ( Math . round ( moment . duration ( endTimestamp - startTimestamp ) . asMonths ( ) ) , "months" ) , "months" ) ;
578
+ }
579
+ else if ( bucket === "daily" ) {
580
+ Object . assign ( periodObject , {
581
+ dateString : "D MMM" ,
582
+ isSpecialPeriod : true
583
+ } ) ;
584
+ cycleDuration = moment . duration ( moment . duration ( Math . round ( moment . duration ( endTimestamp - startTimestamp ) . asDays ( ) ) , "days" ) , "days" ) ;
585
+ }
586
+ }
587
+
567
588
Object . assign ( periodObject , {
568
589
start : startTimestamp . valueOf ( ) ,
569
590
end : endTimestamp . valueOf ( ) ,
@@ -594,6 +615,12 @@ function getPeriodObject(prmPeriod) {
594
615
var peY = date1 [ 0 ] ;
595
616
var peM = date1 [ 1 ] ;
596
617
618
+ var dateFormat = "YYYY.M.D" ;
619
+ if ( bucket ) {
620
+ if ( bucket === "monthly" ) {
621
+ dateFormat = "YYYY.M" ;
622
+ }
623
+ }
597
624
598
625
for ( var dayIt = startTimestamp . clone ( ) ; dayIt < endTimestamp ; dayIt . add ( 1 , "day" ) ) {
599
626
@@ -602,16 +629,25 @@ function getPeriodObject(prmPeriod) {
602
629
dateVal = dateVal . split ( "." ) ;
603
630
604
631
uniqueMap [ dateVal [ 0 ] ] = uniqueMap [ dateVal [ 0 ] ] || { } ; //each year
605
- if ( dateVal [ 0 ] === sY || dateVal [ 0 ] === eY ) {
632
+ if ( dateVal . length >= 2 && dateVal [ 0 ] === sY || dateVal [ 0 ] === eY ) {
606
633
uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] = uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] || { } ; //each month
607
- if ( ( dateVal [ 0 ] === sY && dateVal [ 1 ] === sM ) || ( dateVal [ 0 ] === eY && dateVal [ 1 ] === eM ) ) {
634
+ if ( ( dateVal [ 0 ] === sY && dateVal [ 1 ] === sM ) || ( dateVal [ 0 ] === eY && dateVal [ 1 ] === eM ) ) { //bucket is daily
608
635
uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] = uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] || { } ; //each week
609
- uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] [ dateVal [ 2 ] ] = uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] [ dateVal [ 2 ] ] || { } ; //each day
636
+ if ( dateVal . length >= 2 ) {
637
+ uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] [ dateVal [ 2 ] ] = uniqueMap [ dateVal [ 0 ] ] [ dateVal [ 1 ] ] [ "w" + week ] [ dateVal [ 2 ] ] || { } ; //each day
638
+ }
610
639
}
611
640
}
641
+ var label = dayIt . format ( dateFormat ) ;
642
+ if ( periodObject . currentPeriodArr . length === 0 || periodObject . currentPeriodArr [ periodObject . currentPeriodArr . length - 1 ] !== label ) {
643
+ periodObject . currentPeriodArr . push ( label ) ;
644
+ }
612
645
613
- periodObject . currentPeriodArr . push ( dayIt . format ( "YYYY.M.D" ) ) ;
614
- periodObject . previousPeriodArr . push ( dayIt . clone ( ) . subtract ( cycleDuration ) . format ( "YYYY.M.D" ) ) ;
646
+ label = dayIt . clone ( ) . subtract ( cycleDuration ) . format ( dateFormat ) ;
647
+
648
+ if ( periodObject . previousPeriodArr . length === 0 || periodObject . previousPeriodArr [ periodObject . previousPeriodArr . length - 1 ] !== label ) {
649
+ periodObject . previousPeriodArr . push ( label ) ;
650
+ }
615
651
616
652
dateVal = dayIt . clone ( ) . subtract ( cycleDuration ) . format ( "YYYY.M.D" ) ;
617
653
week = Math . ceil ( dayIt . clone ( ) . subtract ( cycleDuration ) . format ( "DDD" ) / 7 ) ;
@@ -687,15 +723,15 @@ function getPeriodObject(prmPeriod) {
687
723
688
724
let zeroIDs = new Set ( ) ,
689
725
monthIDs = new Set ( ) ;
690
-
691
726
for ( let index = 0 ; index < periodObject . currentPeriodArr . length ; index ++ ) {
692
- let [ year , month ] = periodObject . currentPeriodArr [ index ] . split ( "." ) ,
693
- [ pYear , pMonth ] = periodObject . previousPeriodArr [ index ] . split ( "." ) ;
694
-
727
+ let [ year , month ] = periodObject . currentPeriodArr [ index ] . split ( "." ) ;
695
728
zeroIDs . add ( year + ":0" ) ;
696
729
monthIDs . add ( year + ":" + month ) ;
697
- zeroIDs . add ( pYear + ":0" ) ;
698
- monthIDs . add ( pYear + ":" + pMonth ) ;
730
+ if ( periodObject . previousPeriodArr [ index ] ) {
731
+ let [ pYear , pMonth ] = periodObject . previousPeriodArr [ index ] . split ( "." ) ;
732
+ zeroIDs . add ( pYear + ":0" ) ;
733
+ monthIDs . add ( pYear + ":" + pMonth ) ;
734
+ }
699
735
}
700
736
701
737
periodObject . reqZeroDbDateIds = Array . from ( zeroIDs ) ;
@@ -1871,8 +1907,14 @@ countlyCommon.extractData = function(db, clearFunction, dataProperties, periodOb
1871
1907
dataObj = countlyCommon . getDescendantProp ( db , activeDate + "." + i ) ;
1872
1908
}
1873
1909
else {
1874
- dateString = "YYYY-M-D" ;
1875
- formattedDate = moment ( ( activeDateArr [ i ] ) . replace ( / \. / g, "/" ) , "YYYY/MM/DD" ) ;
1910
+ if ( activeDateArr [ i ] . split ( '.' ) . length === 2 ) {
1911
+ dateString = "YYYY-M" ;
1912
+ formattedDate = moment ( ( activeDateArr [ i ] ) . replace ( / \. / g, "/" ) , "YYYY/MM" ) ;
1913
+ }
1914
+ else {
1915
+ dateString = "YYYY-M-D" ;
1916
+ formattedDate = moment ( ( activeDateArr [ i ] ) . replace ( / \. / g, "/" ) , "YYYY/MM/DD" ) ;
1917
+ }
1876
1918
dataObj = countlyCommon . getDescendantProp ( db , activeDateArr [ i ] ) ;
1877
1919
}
1878
1920
@@ -2634,10 +2676,11 @@ countlyCommon.fixPercentageDelta = function(items, totalPercent) {
2634
2676
/**
2635
2677
* Calculate period function
2636
2678
* @param {object } period - given period
2679
+ * @param {string } bucket - bucket for period - monthly or daily. If bucket passed range will be expanded to full months/days
2637
2680
* @returns {object } returns {@link countlyCommon.periodObj}
2638
2681
*/
2639
- countlyCommon . calculatePeriodObject = function ( period ) {
2640
- return getPeriodObject ( period ) ;
2682
+ countlyCommon . calculatePeriodObject = function ( period , bucket ) {
2683
+ return getPeriodObject ( period , bucket ) ;
2641
2684
} ;
2642
2685
2643
2686
/**
0 commit comments