312e19eea000a83ad80cc702c5d674b0278a1609
[aai/sparky-fe.git] / src / utils / DateTimeChartUtil.js
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 let moment = require('moment');
27 let d3Scale = require('d3-scale');
28 let d3Time = require('d3-time');
29
30 /**
31  * Converts specified time (ms since epoc) into a
32  * MM/DD/YYYY format for the local timezone
33  */
34 export function dateFormatLocalTimeZoneMMDDYYYY(time) {
35   return moment(time).format('L');
36 };
37
38 /**
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
45  */
46 export function getTicks(data, attrKey) {
47   if (!data || !data.length) {
48     return [];
49   }
50   
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);
55   
56   return ticks.map(entry => +entry);
57 };
58
59 /**
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
65  */
66 export function getTicksData(data, ticks, attrKey) {
67   if (!data || !data.length) {
68     return [];
69   }
70   
71   const dataMap = new Map(data.map((i) => [i[attrKey], i]));
72   ticks.forEach(function (item) {
73     if (!dataMap.has(item)) {
74       let jsonObj = {};
75       jsonObj[attrKey] = item;
76       data.push(jsonObj);
77     }
78   });
79   return data;
80 };
81
82 /**
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
87  */
88 export function sortDataByField(data, field) {
89   return data.sort(function (a, b) {
90     return a[field] - b[field];
91   });
92 };