Updating versions of Sparky FE files
[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  * Converts specified time (ms since epoc) into a
37  * YYYY-MM-DD format for the local timezone
38  */
39 export function dateFormatLocalTimeZoneYYYYMMDD(time) {
40   return moment(time).format('YYYY-MM-DD');
41 };
42
43 /**
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
50  */
51 export function getTicks(data, attrKey) {
52   if (!data || !data.length) {
53     return [];
54   }
55
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);
60
61   return ticks.map(entry => +entry);
62 };
63
64 /**
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
70  */
71 export function getTicksData(data, ticks, attrKey) {
72   if (!data || !data.length) {
73     return [];
74   }
75
76   const dataMap = new Map(data.map((i) => [i[attrKey], i]));
77   ticks.forEach(function (item) {
78     if (!dataMap.has(item)) {
79       let jsonObj = {};
80       jsonObj[attrKey] = item;
81       data.push(jsonObj);
82     }
83   });
84   return data;
85 };
86
87 /**
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
92  */
93 export function sortDataByField(data, field) {
94   return data.sort(function (a, b) {
95     return a[field] - b[field];
96   });
97 };