[AAI-92 Amsterdam] Update license
[aai/sparky-fe.git] / src / utils / DateTimeChartUtil.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 let moment = require('moment');
24 let d3Scale = require('d3-scale');
25 let d3Time = require('d3-time');
26
27 /**
28  * Converts specified time (ms since epoc) into a
29  * MM/DD/YYYY format for the local timezone
30  */
31 export function dateFormatLocalTimeZoneMMDDYYYY(time) {
32   return moment(time).format('L');
33 };
34
35 /**
36  * Build a map of 'ticks' to be used on a graph axis based on the date range
37  * identified by the specified JSON attribute (ticks will be on a daily basis)
38  * @param data - the array of JSON data
39  * @param attrKey - the attribute within the JSON containing the date
40  * @returns - a map of ticks, one for each day within the range of dates
41  *   covered by the specified data
42  */
43 export function getTicks(data, attrKey) {
44   if (!data || !data.length) {
45     return [];
46   }
47   
48   const domain = [new Date(data[0][attrKey]),
49     new Date(data[data.length - 1][attrKey])];
50   const scale = d3Scale.scaleTime().domain(domain).range([0, 1]);
51   const ticks = scale.ticks(d3Time.timeDay, 1);
52   
53   return ticks.map(entry => +entry);
54 };
55
56 /**
57  * Merges ticks into a list of data points
58  * @param data - the list of data points (each point is a date/value pair)
59  * @param ticks - a map of date based points to merge with the list of data
60  * @param attrKey - the attribute which contains teh date value
61  * @returns - an integrated list of data points and ticks
62  */
63 export function getTicksData(data, ticks, attrKey) {
64   if (!data || !data.length) {
65     return [];
66   }
67   
68   const dataMap = new Map(data.map((i) => [i[attrKey], i]));
69   ticks.forEach(function (item) {
70     if (!dataMap.has(item)) {
71       let jsonObj = {};
72       jsonObj[attrKey] = item;
73       data.push(jsonObj);
74     }
75   });
76   return data;
77 };
78
79 /**
80  * Sort an array of JSON data by a specified attribute
81  * @param data - array of JSON objects to be sorted
82  * @param field - the attribute to sort on
83  * @returns - the sorted array
84  */
85 export function sortDataByField(data, field) {
86   return data.sort(function (a, b) {
87     return a[field] - b[field];
88   });
89 };