2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 let moment = require('moment');
22 let d3Scale = require('d3-scale');
23 let d3Time = require('d3-time');
26 * Converts specified time (ms since epoc) into a
27 * MM/DD/YYYY format for the local timezone
29 export function dateFormatLocalTimeZoneMMDDYYYY(time) {
30 return moment(time).format('L');
34 * Converts specified time (ms since epoc) into a
35 * YYYY-MM-DD format for the local timezone
37 export function dateFormatLocalTimeZoneYYYYMMDD(time) {
38 return moment(time).format('YYYY-MM-DD');
42 * Build a map of 'ticks' to be used on a graph axis based on the date range
43 * identified by the specified JSON attribute (ticks will be on a daily basis)
44 * @param data - the array of JSON data
45 * @param attrKey - the attribute within the JSON containing the date
46 * @returns - a map of ticks, one for each day within the range of dates
47 * covered by the specified data
49 export function getTicks(data, attrKey) {
50 if (!data || !data.length) {
54 const domain = [new Date(data[0][attrKey]),
55 new Date(data[data.length - 1][attrKey])];
56 const scale = d3Scale.scaleTime().domain(domain).range([0, 1]);
57 const ticks = scale.ticks(d3Time.timeDay, 1);
59 return ticks.map(entry => +entry);
63 * Merges ticks into a list of data points
64 * @param data - the list of data points (each point is a date/value pair)
65 * @param ticks - a map of date based points to merge with the list of data
66 * @param attrKey - the attribute which contains teh date value
67 * @returns - an integrated list of data points and ticks
69 export function getTicksData(data, ticks, attrKey) {
70 if (!data || !data.length) {
74 const dataMap = new Map(data.map((i) => [i[attrKey], i]));
75 ticks.forEach(function (item) {
76 if (!dataMap.has(item)) {
78 jsonObj[attrKey] = item;
86 * Sort an array of JSON data by a specified attribute
87 * @param data - array of JSON objects to be sorted
88 * @param field - the attribute to sort on
89 * @returns - the sorted array
91 export function sortDataByField(data, field) {
92 return data.sort(function (a, b) {
93 return a[field] - b[field];