2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 let moment = require('moment');
24 let d3Scale = require('d3-scale');
25 let d3Time = require('d3-time');
28 * Converts specified time (ms since epoc) into a
29 * MM/DD/YYYY format for the local timezone
31 export function dateFormatLocalTimeZoneMMDDYYYY(time) {
32 return moment(time).format('L');
36 * Converts specified time (ms since epoc) into a
37 * YYYY-MM-DD format for the local timezone
39 export function dateFormatLocalTimeZoneYYYYMMDD(time) {
40 return moment(time).format('YYYY-MM-DD');
44 * Build a map of 'ticks' to be used on a graph axis based on the date range
45 * identified by the specified JSON attribute (ticks will be on a daily basis)
46 * @param data - the array of JSON data
47 * @param attrKey - the attribute within the JSON containing the date
48 * @returns - a map of ticks, one for each day within the range of dates
49 * covered by the specified data
51 export function getTicks(data, attrKey) {
52 if (!data || !data.length) {
56 const domain = [new Date(data[0][attrKey]),
57 new Date(data[data.length - 1][attrKey])];
58 const scale = d3Scale.scaleTime().domain(domain).range([0, 1]);
59 const ticks = scale.ticks(d3Time.timeDay, 1);
61 return ticks.map(entry => +entry);
65 * Merges ticks into a list of data points
66 * @param data - the list of data points (each point is a date/value pair)
67 * @param ticks - a map of date based points to merge with the list of data
68 * @param attrKey - the attribute which contains teh date value
69 * @returns - an integrated list of data points and ticks
71 export function getTicksData(data, ticks, attrKey) {
72 if (!data || !data.length) {
76 const dataMap = new Map(data.map((i) => [i[attrKey], i]));
77 ticks.forEach(function (item) {
78 if (!dataMap.has(item)) {
80 jsonObj[attrKey] = item;
88 * Sort an array of JSON data by a specified attribute
89 * @param data - array of JSON objects to be sorted
90 * @param field - the attribute to sort on
91 * @returns - the sorted array
93 export function sortDataByField(data, field) {
94 return data.sort(function (a, b) {
95 return a[field] - b[field];