2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 let moment = require('moment');
27 let d3Scale = require('d3-scale');
28 let d3Time = require('d3-time');
31 * Converts specified time (ms since epoc) into a
32 * MM/DD/YYYY format for the local timezone
34 export function dateFormatLocalTimeZoneMMDDYYYY(time) {
35 return moment(time).format('L');
39 * Converts specified time (ms since epoc) into a
40 * YYYY-MM-DD format for the local timezone
42 export function dateFormatLocalTimeZoneYYYYMMDD(time) {
43 return moment(time).format('YYYY-MM-DD');
47 * Build a map of 'ticks' to be used on a graph axis based on the date range
48 * identified by the specified JSON attribute (ticks will be on a daily basis)
49 * @param data - the array of JSON data
50 * @param attrKey - the attribute within the JSON containing the date
51 * @returns - a map of ticks, one for each day within the range of dates
52 * covered by the specified data
54 export function getTicks(data, attrKey) {
55 if (!data || !data.length) {
59 const domain = [new Date(data[0][attrKey]),
60 new Date(data[data.length - 1][attrKey])];
61 const scale = d3Scale.scaleTime().domain(domain).range([0, 1]);
62 const ticks = scale.ticks(d3Time.timeDay, 1);
64 return ticks.map(entry => +entry);
68 * Merges ticks into a list of data points
69 * @param data - the list of data points (each point is a date/value pair)
70 * @param ticks - a map of date based points to merge with the list of data
71 * @param attrKey - the attribute which contains teh date value
72 * @returns - an integrated list of data points and ticks
74 export function getTicksData(data, ticks, attrKey) {
75 if (!data || !data.length) {
79 const dataMap = new Map(data.map((i) => [i[attrKey], i]));
80 ticks.forEach(function (item) {
81 if (!dataMap.has(item)) {
83 jsonObj[attrKey] = item;
91 * Sort an array of JSON data by a specified attribute
92 * @param data - array of JSON objects to be sorted
93 * @param field - the attribute to sort on
94 * @returns - the sorted array
96 export function sortDataByField(data, field) {
97 return data.sort(function (a, b) {
98 return a[field] - b[field];