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 * Build a map of 'ticks' to be used on a graph axis based on the date range
40 * identified by the specified JSON attribute (ticks will be on a daily basis)
41 * @param data - the array of JSON data
42 * @param attrKey - the attribute within the JSON containing the date
43 * @returns - a map of ticks, one for each day within the range of dates
44 * covered by the specified data
46 export function getTicks(data, attrKey) {
47 if (!data || !data.length) {
51 const domain = [new Date(data[0][attrKey]),
52 new Date(data[data.length - 1][attrKey])];
53 const scale = d3Scale.scaleTime().domain(domain).range([0, 1]);
54 const ticks = scale.ticks(d3Time.timeDay, 1);
56 return ticks.map(entry => +entry);
60 * Merges ticks into a list of data points
61 * @param data - the list of data points (each point is a date/value pair)
62 * @param ticks - a map of date based points to merge with the list of data
63 * @param attrKey - the attribute which contains teh date value
64 * @returns - an integrated list of data points and ticks
66 export function getTicksData(data, ticks, attrKey) {
67 if (!data || !data.length) {
71 const dataMap = new Map(data.map((i) => [i[attrKey], i]));
72 ticks.forEach(function (item) {
73 if (!dataMap.has(item)) {
75 jsonObj[attrKey] = item;
83 * Sort an array of JSON data by a specified attribute
84 * @param data - array of JSON objects to be sorted
85 * @param field - the attribute to sort on
86 * @returns - the sorted array
88 export function sortDataByField(data, field) {
89 return data.sort(function (a, b) {
90 return a[field] - b[field];