File size: 7,171 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { eye } from '@automattic/components/src/icons';
import { formatNumber } from '@automattic/number-formatters';
import {
	Icon,
	people,
	starEmpty,
	commentContent,
	chevronRight,
	postContent,
} from '@wordpress/icons';
import clsx from 'clsx';
import { translate } from 'i18n-calypso';
import { capitalize } from 'lodash';
import moment from 'moment';
import memoizeLast from 'calypso/lib/memoize-last';
import { rangeOfPeriod } from 'calypso/state/stats/lists/utils';
import { parseLocalDate } from '../utils';

export function formatDate( date, period, chartStart = null, chartEnd = null ) {
	// NOTE: Consider localizing the dates, especially for the 'week' case.
	const momentizedDate = moment( date );
	const endDate = momentizedDate.clone().add( 6, 'days' );

	switch ( period ) {
		case 'hour':
			// TODO: align the time format with email stats.
			return momentizedDate.format( 'MMM D HH:00' );
		case 'day':
			return momentizedDate.format( 'LL' );
		case 'week':
			// Make partial period display with correct start and end dates.
			if ( chartStart && momentizedDate.isBefore( chartStart ) ) {
				return (
					moment( chartStart ).format( 'LL' ) +
					' - ' +
					momentizedDate.add( 6, 'days' ).format( 'LL' )
				);
			}
			if ( chartEnd && endDate.isAfter( chartEnd ) ) {
				return momentizedDate.format( 'LL' ) + ' - ' + moment( chartEnd ).format( 'LL' );
			}

			return momentizedDate.format( 'LL' ) + ' - ' + momentizedDate.add( 6, 'days' ).format( 'LL' );
		case 'month':
			return momentizedDate.format( 'MMMM YYYY' );
		case 'year':
			return momentizedDate.format( 'YYYY' );
		default:
			return null;
	}
}

export function getQueryDate( queryDate, timezoneOffset, period, quantity ) {
	const momentSiteZone = moment().utcOffset( timezoneOffset );
	const endOfPeriodDate = rangeOfPeriod( period, momentSiteZone.locale( 'en' ) ).endOf;
	const periodDifference = moment( endOfPeriodDate ).diff( moment( queryDate ), period );
	if ( periodDifference >= quantity ) {
		return moment( endOfPeriodDate )
			.subtract( Math.floor( periodDifference / quantity ) * quantity, period )
			.format( 'YYYY-MM-DD' );
	}
	return endOfPeriodDate;
}

const EMPTY_RESULT = [];
export const buildChartData = memoizeLast(
	( activeLegend, chartTab, data, period, queryDate, customRange ) => {
		if ( ! data ) {
			return EMPTY_RESULT;
		}
		return data.map( ( record ) => {
			const nestedValue = activeLegend.length ? record[ activeLegend[ 0 ] ] : null;

			const recordClassName =
				record.classNames && record.classNames.length ? record.classNames.join( ' ' ) : null;
			const className = clsx( recordClassName, {
				'is-selected': record.period === queryDate,
			} );

			const item = addTooltipData(
				chartTab,
				{
					label: record[ `label${ capitalize( period ) }` ],
					value: record[ chartTab ],
					data: record,
					nestedValue,
					className,
				},
				period,
				customRange
			);

			return item;
		} );
	}
);

// data validation for line chart
export const transformChartDataToLineFormat = memoizeLast(
	( chartData, activeLegend = [], activeTab, primaryColor, secondaryColor ) => {
		if ( ! Array.isArray( chartData ) || chartData.length === 0 ) {
			return [];
		}

		const series = [];

		const mainSeries = chartData
			.map( ( record ) => {
				const date = parseLocalDate( record.data.period );
				const value = record.data[ activeTab.attr ];
				if ( isNaN( date.getTime() ) || typeof value !== 'number' ) {
					return null;
				}
				return { date, value, label: record.tooltipData?.[ 0 ].label };
			} )
			.filter( Boolean );

		if ( mainSeries.length > 0 ) {
			series.push( {
				label: activeTab.label,
				options: { stroke: primaryColor },
				icon: activeTab.icon,
				data: mainSeries,
			} );
		}

		// Only add visitors series if visitors is active in legend
		// It has to be visitors as that is the only case where we show two series, i.e. activeLegend[ 0 ] is 'visitors' only.
		// We should probably figure out a more general way to handle this.
		if ( activeLegend.length > 0 ) {
			const secondarySeries = chartData
				.map( ( record ) => {
					const date = parseLocalDate( record.data.period );
					const value = record.data.visitors;
					if ( isNaN( date.getTime() ) || typeof value !== 'number' ) {
						return null;
					}
					return { date, value, label: record.tooltipData?.[ 0 ].label };
				} )
				.filter( Boolean );

			if ( secondarySeries.length > 0 ) {
				series.push( {
					label: translate( 'Visitors' ),
					options: { stroke: secondaryColor },
					icon: <Icon className="gridicon" icon={ people } />,
					data: secondarySeries,
				} );
			}
		}

		return series;
	}
);

function addTooltipData( chartTab, item, period, customRange = {} ) {
	const tooltipData = [];
	tooltipData.push( {
		label: formatDate( item.data.period, period, customRange.chartStart, customRange.chartEnd ),
		className: 'is-date-label',
		value: null,
	} );

	const viewsPerVisitor = item.data.views / item.data.visitors;

	switch ( chartTab ) {
		case 'comments':
			tooltipData.push( {
				label: translate( 'Comments' ),
				value: formatNumber( item.value ),
				className: 'is-comments',
				icon: <Icon className="gridicon" icon={ commentContent } />,
			} );
			break;

		case 'likes':
			tooltipData.push( {
				label: translate( 'Likes' ),
				value: formatNumber( item.value ),
				className: 'is-likes',
				icon: <Icon className="gridicon" icon={ starEmpty } />,
			} );
			break;

		default:
			tooltipData.push( {
				label: translate( 'Views' ),
				value: formatNumber( item.data.views ),
				className: 'is-views',
				icon: <Icon className="gridicon" icon={ eye } />,
			} );

			if ( Number.isFinite( item.data.visitors ) ) {
				tooltipData.push( {
					label: translate( 'Visitors' ),
					value: formatNumber( item.data.visitors ),
					className: 'is-visitors',
					icon: <Icon className="gridicon" icon={ people } />,
				} );
			}

			if ( Number.isFinite( viewsPerVisitor ) ) {
				tooltipData.push( {
					label: translate( 'Views Per Visitor' ),
					value: formatNumber( viewsPerVisitor, { decimals: 2 } ),
					className: 'is-views-per-visitor',
					icon: <Icon className="gridicon" icon={ chevronRight } />,
				} );
			}

			if ( item.data.post_titles && item.data.post_titles.length ) {
				// only show two post titles
				if ( item.data.post_titles.length > 2 ) {
					tooltipData.push( {
						label: translate( 'Posts Published' ),
						value: formatNumber( item.data.post_titles.length ),
						className: 'is-published-nolist',
						icon: <Icon className="gridicon" icon={ postContent } />,
					} );
				} else {
					tooltipData.push( {
						label:
							translate( 'Post Published', 'Posts Published', {
								textOnly: true,
								count: item.data.post_titles.length,
							} ) + ':',
						className: 'is-published',
						icon: <Icon className="gridicon" icon={ postContent } />,
						value: '',
					} );
					item.data.post_titles.forEach( ( post_title ) => {
						tooltipData.push( {
							className: 'is-published-item',
							label: post_title,
						} );
					} );
				}
			}
			break;
	}

	return { ...item, tooltipData };
}