-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.js
1001 lines (901 loc) · 25 KB
/
utils.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* External dependencies
*/
import {
sortBy,
toPairs,
camelCase,
mapKeys,
isNumber,
get,
filter,
map,
concat,
flatten,
} from 'lodash';
import { translate, getLocaleSlug } from 'i18n-calypso';
import moment from 'moment';
/**
* Internal dependencies
*/
import { PUBLICIZE_SERVICES_LABEL_ICON } from './constants';
/**
* Returns a string of the moment format for the period. Supports store stats
* isoWeek and shortened formats.
*
* @param {string} period Stats query
* @param {string} date Stats date
* @returns {object} Period range
*/
export function getPeriodFormat( period, date ) {
const strDate = date.toString();
switch ( period ) {
case 'week':
return strDate.length === 8 && strDate.substr( 4, 2 ) === '-W' ? 'YYYY-[W]WW' : 'YYYY-MM-DD';
case 'month':
return strDate.length === 7 && strDate.substr( 4, 1 ) === '-' ? 'YYYY-MM' : 'YYYY-MM-DD';
case 'year':
return strDate.length === 4 ? 'YYYY' : 'YYYY-MM-DD';
case 'day':
default:
return 'YYYY-MM-DD';
}
}
/**
* Returns an object with the startOf and endOf dates
* for the given stats period and date
*
* @param {string} period Stats query
* @param {string} date Stats date
* @returns {object} Period range
*/
export function rangeOfPeriod( period, date ) {
const format = getPeriodFormat( period, date );
const momentDate = moment( date, format ).locale( 'en' );
const startOf = momentDate.clone().startOf( period );
const endOf = momentDate.clone().endOf( period );
if ( 'week' === period ) {
if ( '0' === momentDate.format( 'd' ) ) {
startOf.subtract( 6, 'd' );
endOf.subtract( 6, 'd' );
} else {
startOf.add( 1, 'd' );
endOf.add( 1, 'd' );
}
}
return {
startOf: startOf.format( 'YYYY-MM-DD' ),
endOf: endOf.format( 'YYYY-MM-DD' ),
};
}
/**
* Returns true if is auto refreshing astats is allowed
* for the give stats query
* It's allowed for queries without dates and for periods including today
*
* @param {string} query Stats query
* @returns {boolean} AutoRefresh allowed or not
*/
export function isAutoRefreshAllowedForQuery( query ) {
if ( ! query || ! query.date || ( ! query.unit && ! query.period ) ) {
return true;
}
const range = rangeOfPeriod( query.period || query.unit, query.date );
const today = moment();
return today >= moment( range.startOf ) && today < moment( range.endOf ).add( 1, 'day' );
}
/**
* Parse the avatar URL
*
* @param {string} avatarUrl Raw avatar URL
* @returns {string} Parsed URL
*/
function parseAvatar( avatarUrl ) {
if ( ! avatarUrl ) {
return null;
}
const [ avatarBaseUrl ] = avatarUrl.split( '?' );
return avatarBaseUrl + '?d=mm';
}
/**
* Builds data into escaped array for CSV export
*
* @param {object} data Normalized stats data object
* @param {string} parent Label of parent
* @returns {Array} CSV Row
*/
export function buildExportArray( data, parent = null ) {
if ( ! data || ! data.label || ! data.value ) {
return [];
}
const label = parent ? parent + ' > ' + data.label : data.label;
const escapedLabel = label.replace( /\"/, '""' ); // eslint-disable-line no-useless-escape
let exportData = [ [ '"' + escapedLabel + '"', data.value ] ];
if ( data.children ) {
const childData = map( data.children, child => {
return buildExportArray( child, label );
} );
exportData = concat( exportData, flatten( childData ) );
}
return exportData;
}
/**
* Returns a serialized stats query, used as the key in the
* `state.stats.lists.items` and `state.stats.lists.requesting` state objects.
*
* @param {object} query Stats query
* @returns {string} Serialized stats query
*/
export function getSerializedStatsQuery( query = {} ) {
return JSON.stringify( sortBy( toPairs( query ), pair => pair[ 0 ] ) );
}
/**
* Return delta data in a format used by 'extensions/woocommerce/app/store-stats`. The fields array is matched to
* the data in a single object.
*
* @param {object} payload - response
* @returns {Array} - Array of data objects
*/
export function parseOrderDeltas( payload ) {
if (
! payload ||
! payload.deltas ||
! payload.delta_fields ||
Object.keys( payload.deltas ).length === 0
) {
return [];
}
return payload.deltas.map( row => {
// will be renamed to deltas
const notPeriodKeys = Object.keys( row ).filter( key => key !== 'period' );
const newRow = { period: parseUnitPeriods( payload.unit, row.period ).format( 'YYYY-MM-DD' ) };
notPeriodKeys.forEach( key => {
newRow[ key ] = row[ key ].reduce( ( acc, curr, i ) => {
acc[ payload.delta_fields[ i ] ] = curr;
return acc;
}, {} );
} );
return newRow;
} );
}
/**
* Create the correct property and value for a label to be used in a chart
*
* @param {string} unit - day, week, month, year
* @param {object} date - moment object
* @param {object} localizedDate - moment object
* @returns {object} chart labels
*/
export function getChartLabels( unit, date, localizedDate ) {
const validDate = moment.isMoment( date ) && date.isValid();
const validLocalizedDate = moment.isMoment( localizedDate ) && localizedDate.isValid();
if ( validDate && validLocalizedDate && unit ) {
const dayOfWeek = date.toDate().getDay();
const isWeekend = 'day' === unit && ( 6 === dayOfWeek || 0 === dayOfWeek );
const labelName = `label${ unit.charAt( 0 ).toUpperCase() + unit.slice( 1 ) }`;
const formats = {
day: translate( 'MMM D', {
context: 'momentjs format string (day)',
comment: 'This specifies a day for the stats x-axis label.',
} ),
week: translate( 'MMM D', {
context: 'momentjs format string (week)',
comment: 'This specifies a week for the stats x-axis label.',
} ),
month: 'MMM',
year: 'YYYY',
};
return {
[ labelName ]: localizedDate.format( formats[ unit ] ),
classNames: isWeekend ? [ 'is-weekend' ] : [],
};
}
return {};
}
/**
* Return data in a format used by 'components/chart`. The fields array is matched to
* the data in a single object.
*
* @param {object} payload - response
* @returns {Array} - Array of data objects
*/
export function parseOrdersChartData( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data.map( function( record ) {
// Initialize data
const dataRecord = {};
// Fill Field Values
record.forEach( function( value, i ) {
dataRecord[ payload.fields[ i ] ] = value;
} );
if ( dataRecord.period ) {
const date = parseUnitPeriods( payload.unit, dataRecord.period ).locale( 'en' );
const localizedDate = parseUnitPeriods( payload.unit, dataRecord.period );
Object.assign( dataRecord, getChartLabels( payload.unit, date, localizedDate ) );
}
dataRecord.period = parseUnitPeriods( payload.unit, dataRecord.period ).format( 'YYYY-MM-DD' );
return dataRecord;
} );
}
/**
* Return data in a format used by 'components/chart`. The fields array is matched to
* the data in a single object.
*
* @param {object} payload - response
* @param {Array} nullAttributes - properties on data objects to be initialized with
* a null value
* @returns {Array} - Array of data objects
*/
export function parseChartData( payload, nullAttributes = [] ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data.map( function( record ) {
// Initialize data
const dataRecord = nullAttributes.reduce( ( memo, attribute ) => {
memo[ attribute ] = null;
return memo;
}, {} );
// Fill Field Values
record.forEach( function( value, i ) {
// Remove W from weeks
if ( 'period' === payload.fields[ i ] ) {
value = value.replace( /W/g, '-' );
}
dataRecord[ payload.fields[ i ] ] = value;
} );
if ( dataRecord.period ) {
const date = moment( dataRecord.period, 'YYYY-MM-DD' ).locale( 'en' );
const localeSlug = getLocaleSlug();
const localizedDate = moment( dataRecord.period, 'YYYY-MM-DD' ).locale( localeSlug );
Object.assign( dataRecord, getChartLabels( payload.unit, date, localizedDate ) );
}
return dataRecord;
} );
}
/**
* Return moment date object for the day or last day of the period.
*
* @param {string} unit - day, week, month or year
* @param {string} period - period in shortened store sting format, eg '2017-W26'
* @returns {object} - moment date object
*/
export function parseUnitPeriods( unit, period ) {
let splitYearWeek;
const localeSlug = getLocaleSlug();
switch ( unit ) {
case 'week':
splitYearWeek = period.split( '-W' );
return moment()
.locale( localeSlug )
.isoWeekYear( splitYearWeek[ 0 ] )
.isoWeek( splitYearWeek[ 1 ] )
.endOf( 'isoWeek' );
case 'month':
return moment( period, 'YYYY-MM' )
.locale( localeSlug )
.endOf( 'month' );
case 'year':
return moment( period, 'YYYY' )
.locale( localeSlug )
.endOf( 'year' );
case 'day':
default:
return moment( period, 'YYYY-MM-DD' ).locale( localeSlug );
}
}
export function parseStoreStatsReferrers( payload ) {
if ( ! payload || ! payload.data || ! payload.fields || ! Array.isArray( payload.data ) ) {
return [];
}
const { fields } = payload;
return payload.data.map( record => {
const parsedDate = parseUnitPeriods( payload.unit, record.date ).locale( 'en' );
const parsedLocalizedDate = parseUnitPeriods( payload.unit, record.date );
const period = parsedLocalizedDate.format( 'YYYY-MM-DD' );
return {
date: period,
data: record.data.map( referrer => {
const obj = {};
referrer.forEach( ( value, i ) => {
const key = fields[ i ];
obj[ key ] = value;
} );
return obj;
} ),
...getChartLabels( payload.unit, parsedDate, parsedLocalizedDate ),
};
} );
}
export const normalizers = {
/**
* Returns a normalized payload from `/sites/{ site }/stats`
*
* @param {object} data Stats data
* @returns {object?} Normalized stats data
*/
stats( data ) {
if ( ! data || ! data.stats ) {
return null;
}
return mapKeys( data.stats, ( value, key ) => camelCase( key ) );
},
/**
* Returns a normalized payload from `/sites/{ site }/stats/insights`
*
* @param {object} data Stats query
* @returns {object?} Normalized stats data
*/
statsInsights: data => {
if ( ! data || ! isNumber( data.highest_day_of_week ) ) {
return {};
}
const {
highest_hour,
highest_day_percent,
highest_day_of_week,
highest_hour_percent,
hourly_views,
years,
} = data;
// Adjust Day of Week from 0 = Monday to 0 = Sunday (for Moment)
let dayOfWeek = highest_day_of_week + 1;
if ( dayOfWeek > 6 ) {
dayOfWeek = 0;
}
const localeSlug = getLocaleSlug();
return {
day: moment()
.locale( localeSlug )
.day( dayOfWeek )
.format( 'dddd' ),
percent: Math.round( highest_day_percent ),
hour: moment()
.locale( localeSlug )
.hour( highest_hour )
.startOf( 'hour' )
.format( 'LT' ),
hourPercent: Math.round( highest_hour_percent ),
hourlyViews: hourly_views,
years,
};
},
/**
* Returns a normalized payload from `/sites/{ site }/stats/top-posts`
*
* @param {object} data Stats data
* @param {object} query Stats query
* @param {number} siteId Site ID
* @param {object} site Site object
* @returns {object?} Normalized stats data
*/
statsTopPosts: ( data, query, siteId, site ) => {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf, endOf } = rangeOfPeriod( query.period, query.date );
const dataPath = query.summarize
? [ 'summary', 'postviews' ]
: [ 'days', startOf, 'postviews' ];
const viewData = get( data, dataPath, [] );
return map( viewData, item => {
const detailPage = site ? `/stats/post/${ item.id }/${ site.slug }` : null;
let inPeriod = false;
// Archive and home pages do not have dates
if ( item.date ) {
const postDate = moment( item.date );
// TODO: might be nice to update moment and use isSameOrAfter and isSameOrBefore
if (
( postDate.isAfter( startOf, 'day' ) || postDate.isSame( startOf, 'day' ) ) &&
( postDate.isBefore( endOf, 'day' ) || postDate.isSame( endOf, 'day' ) )
) {
inPeriod = true;
}
}
return {
label: item.title,
value: item.views,
page: detailPage,
actions: [
{
type: 'link',
data: item.href,
},
],
labelIcon: null,
children: null,
className: inPeriod ? 'published' : null,
};
} );
},
/**
* Returns a normalized payload from `/sites/{ site }/stats/country-views`
*
* @param {object} data Stats data
* @param {object} query Stats query
* @returns {object?} Normalized stats data
*/
statsCountryViews: ( data, query = {} ) => {
// parsing a country-views response requires a period and date
if ( ! data || ! query.period || ! query.date ) {
return null;
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const countryInfo = get( data, [ 'country-info' ], {} );
// the API response object shape depends on if this is a summary request or not
const dataPath = query.summarize ? [ 'summary', 'views' ] : [ 'days', startOf, 'views' ];
// filter out country views that have no legitimate country data associated with them
const countryData = filter( get( data, dataPath, [] ), viewData => {
return countryInfo[ viewData.country_code ];
} );
return map( countryData, viewData => {
const country = countryInfo[ viewData.country_code ];
// ’ in country names causes google's geo viz to break
return {
label: country.country_full.replace( /’/, "'" ),
countryCode: viewData.country_code,
value: viewData.views,
region: country.map_region,
};
} );
},
/**
* Returns a normalized statsPublicize array, ready for use in stats-module
*
* @param {object} data Stats data
* @returns {Array} Parsed publicize data array
*/
statsPublicize( data = {} ) {
if ( ! data || ! data.services ) {
return [];
}
return data.services.map( service => {
const { label, icon } = PUBLICIZE_SERVICES_LABEL_ICON[ service.service ];
return { label, icon, value: service.followers };
} );
},
/**
* Returns a normalized statsVideoPlays array, ready for use in stats-module
*
* @param {object} data Stats data
* @param {object} query Stats query
* @param {number} siteId Site ID
* @param {object} site Site object
* @returns {Array} Normalized stats data
*/
statsVideoPlays( data, query = {}, siteId, site ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const videoPlaysData = get( data, [ 'days', startOf, 'plays' ], [] );
return videoPlaysData.map( item => {
const detailPage = site
? `/stats/${ query.period }/videodetails/${ site.slug }?post=${ item.post_id }`
: null;
return {
label: item.title,
page: detailPage,
value: item.plays,
actions: [
{
type: 'link',
data: item.url,
},
],
};
} );
},
/**
* Returns a normalized statsFollowers object
*
* @param {object} data Stats data
* @returns {?object} Normalized stats data
*/
statsFollowers( data ) {
if ( ! data ) {
return null;
}
const { total_wpcom, total_email } = data;
const subscriberData = get( data, [ 'subscribers' ], [] );
const subscribers = subscriberData.map( item => {
return {
label: item.label,
iconClassName: 'avatar-user',
icon: parseAvatar( item.avatar ),
link: item.url,
value: {
type: 'relative-date',
value: item.date_subscribed,
},
actions: [
{
type: 'follow',
data: item.follow_data ? item.follow_data.params : false,
},
],
};
} );
return { total_wpcom, total_email, subscribers };
},
statsCommentFollowers( data ) {
if ( ! data ) {
return null;
}
const page = data.page || 0;
const pages = data.pages || 0;
const total = data.total || 0;
let posts = [];
if ( data.posts ) {
posts = data.posts.map( item => {
if ( 0 === item.id ) {
return {
label: 'All Posts',
value: item.followers,
};
}
return {
label: item.title,
link: item.url,
labelIcon: 'external',
value: item.followers,
};
} );
}
return { page, pages, total, posts };
},
statsComments( data, query, siteId, site ) {
if ( ! data ) {
return null;
}
const adminUrl = site ? site.options.admin_url : null;
let authors = [];
if ( data.authors ) {
authors = data.authors.map( author => {
return {
label: author.name,
value: author.comments,
iconClassName: 'avatar-user',
icon: parseAvatar( author.gravatar ),
link: adminUrl + 'edit-comments.php' + author.link,
className: 'module-content-list-item-large',
actions: [
{
type: 'follow',
data: author.follow_data ? author.follow_data.params : false,
},
],
};
} );
}
let posts = [];
if ( data.posts ) {
posts = data.posts.map( post => {
return {
label: post.name,
value: post.comments,
page: site ? '/stats/post/' + post.id + '/' + site.slug : null,
actions: [
{
type: 'link',
data: post.link,
},
],
};
} );
}
return { authors, posts };
},
/**
* Returns a normalized statsVideo array, ready for use in stats-module
*
* @param {object} payload Stats response payload
* @returns {Array} Parsed data array
*/
statsVideo( payload ) {
if ( ! payload ) {
return null;
}
let data = [];
if ( payload.data ) {
data = payload.data
.map( item => {
return { period: item[ 0 ], value: item[ 1 ] };
} )
.slice( Math.max( payload.data.length - 10, 1 ) );
}
let pages = [];
if ( payload.pages ) {
pages = payload.pages.map( item => {
return {
label: item,
link: item,
};
} );
}
return { pages, data };
},
/**
* Returns a normalized statsTopAuthors array, ready for use in stats-module
*
* @param {object} data Stats data
* @param {object} query Stats query
* @param {number} siteId Site ID
* @param {object} site Site Object
* @returns {Array} Normalized stats data
*/
statsTopAuthors( data, query = {}, siteId, site ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const authorsData = get( data, [ 'days', startOf, 'authors' ], [] );
return authorsData.map( item => {
const record = {
label: item.name,
iconClassName: 'avatar-user',
icon: parseAvatar( item.avatar ),
children: null,
value: item.views,
className: 'module-content-list-item-large',
};
if ( item.posts && item.posts.length > 0 ) {
record.children = item.posts.map( child => {
return {
label: child.title,
value: child.views,
page: site ? '/stats/post/' + child.id + '/' + site.slug : null,
actions: [
{
type: 'link',
data: child.url,
},
],
children: null,
};
} );
}
return record;
} );
},
/**
* Returns a normalized statsTags array, ready for use in stats-module
*
* @param {object} data Stats data
* @returns {Array} Parsed data array
*/
statsTags( data ) {
if ( ! data || ! data.tags ) {
return [];
}
const getTagTypeIcon = type => {
return type === 'category' ? 'folder' : type;
};
return data.tags.map( item => {
let children;
const hasChildren = item.tags.length > 1;
const labels = item.tags.map( tagItem => {
return {
label: tagItem.name,
labelIcon: getTagTypeIcon( tagItem.type ),
link: hasChildren ? null : tagItem.link,
};
} );
if ( hasChildren ) {
children = item.tags.map( tagItem => {
return {
label: tagItem.name,
labelIcon: getTagTypeIcon( tagItem.type ),
value: null,
children: null,
link: tagItem.link,
};
} );
}
return {
label: labels,
link: labels.length > 1 ? null : labels[ 0 ].link,
value: item.views,
children: children,
};
} );
},
/*
* Returns a normalized statsClicks array, ready for use in stats-module
*
* @param {Object} data Stats data
* @param {Object} query Stats query
* @return {Array} Parsed data array
*/
statsClicks( data, query ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const dataPath = query.summarize ? [ 'summary', 'clicks' ] : [ 'days', startOf, 'clicks' ];
const statsData = get( data, dataPath, [] );
return statsData.map( item => {
const hasChildren = item.children && item.children.length > 0;
const newRecord = {
label: item.name,
value: item.views,
children: null,
link: item.url,
icon: item.icon,
labelIcon: hasChildren ? null : 'external',
};
if ( item.children ) {
newRecord.children = item.children.map( child => {
return {
label: child.name,
value: child.views,
children: null,
link: child.url,
labelIcon: 'external',
};
} );
}
return newRecord;
} );
},
/*
* Returns a normalized statsReferrers array, ready for use in stats-module
*
* @param {Object} data Stats data
* @param {Object} query Stats query
* @param {Int} siteId Site ID
* @return {Array} Parsed data array
*/
statsReferrers( data, query, siteId ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const dataPath = query.summarize ? [ 'summary', 'groups' ] : [ 'days', startOf, 'groups' ];
const statsData = get( data, dataPath, [] );
const parseItem = item => {
let children;
if ( item.children && item.children.length > 0 ) {
children = item.children.map( parseItem );
}
const record = {
label: item.name,
value: item.views,
link: item.url,
labelIcon: children ? null : 'external',
children,
};
if ( item.icon ) {
record.icon = item.icon;
}
return record;
};
return statsData.map( item => {
let actions = [];
if (
( item.url && -1 !== item.url.indexOf( item.name ) ) ||
( ! item.url && item.name === item.group && -1 !== item.name.indexOf( '.' ) )
) {
actions = [
{
type: 'spam',
data: {
siteID: siteId,
domain: item.name,
},
},
];
}
return {
...parseItem( { ...item, children: item.results, views: item.total } ),
actions,
actionMenu: actions.length,
};
} );
},
statsVisits( payload ) {
return parseChartData( payload, [ 'visits', 'likes', 'visitors', 'comments', 'posts' ] );
},
statsOrders( payload ) {
return {
data: parseOrdersChartData( payload ),
deltas: parseOrderDeltas( payload ),
};
},
statsStoreReferrers( payload ) {
return parseStoreStatsReferrers( payload );
},
statsTopSellers( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data;
},
statsTopCategories( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data;
},
statsTopCoupons( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data;
},
statsTopEarners( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return payload.data;
},
statsAds( payload ) {
if ( ! payload || ! payload.data ) {
return [];
}
return parseChartData( payload, [ 'impressions', 'revenue', 'cpm' ] );
},
/*
* Returns a normalized statsSearchTerms array, ready for use in stats-module
*
* @param {Object} data Stats data
* @param {Object} query Stats query
* @return {Array} Parsed data array
*/
statsSearchTerms( data, query ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const dataPath = query.summarize ? [ 'summary' ] : [ 'days', startOf ];
const searchTerms = get( data, dataPath.concat( [ 'search_terms' ] ), [] );
const encryptedSearchTerms = get(
data,
dataPath.concat( [ 'encrypted_search_terms' ] ),
false
);
const result = searchTerms.map( day => {
return {
label: day.term,
className: 'user-selectable',
value: day.views,
};
} );
if ( encryptedSearchTerms ) {
result.push( {
label: translate( 'Unknown Search Terms' ),
value: encryptedSearchTerms,
link: 'http://en.support.wordpress.com/stats/#search-engine-terms',
labelIcon: 'external',
} );
}
return result;
},
/*
* Returns a normalized statsFileDownloads array, ready for use in stats-module
*
* @param {Object} data Stats data
* @param {Object} query Stats query
* @return {Array} Parsed data array
*/
statsFileDownloads( data, query ) {
if ( ! data || ! query.period || ! query.date ) {
return [];
}
const { startOf } = rangeOfPeriod( query.period, query.date );
const statsData = get( data, [ 'days', startOf, 'files' ], [] );
return statsData.map( item => {
return {
label: item.relative_url,
shortLabel: item.filename,
page: null,
value: item.downloads,
link: item.download_url,
linkTitle: item.relative_url,
labelIcon: 'external',
};
} );
},