-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcontext_menu_helpers.js
162 lines (147 loc) · 5.79 KB
/
context_menu_helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import dateMath from '@elastic/datemath';
import moment from 'moment';
import {
reportGenerationInProgressModal,
reportGenerationSuccess,
reportGenerationFailure,
permissionsMissingOnGeneration,
} from './context_menu_ui';
import { timeRangeMatcher } from '../utils/utils';
import { unhashUrl } from '../../../../../src/plugins/opensearch_dashboards_utils/public';
const getReportSourceURL = (baseURI) => {
const url = baseURI.substr(0, baseURI.indexOf('?'));
const reportSourceId = url.substr(url.lastIndexOf('/') + 1, url.length);
return reportSourceId;
};
export const contextMenuViewReports = () =>
window.location.assign('reports-dashboards#/');
export const getTimeFieldsFromUrl = () => {
const url = unhashUrl(window.location.href);
let [, fromDateString, toDateString] = url.match(timeRangeMatcher);
fromDateString = fromDateString.replace(/[']+/g, '');
// convert time range to from date format in case time range is relative
const fromDateFormat = dateMath.parse(fromDateString);
toDateString = toDateString.replace(/[']+/g, '');
const toDateFormat = dateMath.parse(toDateString);
const timeDuration = moment.duration(
dateMath.parse(toDateString).diff(dateMath.parse(fromDateString))
);
return {
time_from: fromDateFormat,
time_to: toDateFormat,
time_duration: timeDuration.toISOString(),
};
};
export const contextMenuCreateReportDefinition = (baseURI) => {
const reportSourceId = getReportSourceURL(baseURI);
let reportSource = '';
const timeRanges = getTimeFieldsFromUrl();
// check report source
if (/\/app\/dashboards/.test(baseURI)) {
reportSource = 'dashboard:';
} else if (/\/app\/visualize/.test(baseURI)) {
reportSource = 'visualize:';
} else if (/\/app\/discover/.test(baseURI)) {
reportSource = 'discover:';
}
reportSource += reportSourceId.toString();
window.location.assign(
`reports-dashboards#/create?previous=${reportSource}?timeFrom=${timeRanges.time_from.toISOString()}?timeTo=${timeRanges.time_to.toISOString()}`
);
};
export const displayLoadingModal = () => {
const opensearchDashboardsBody = document.getElementById(
'opensearch-dashboards-body'
);
if (opensearchDashboardsBody) {
try {
const loadingModal = document.createElement('div');
loadingModal.innerHTML = reportGenerationInProgressModal();
opensearchDashboardsBody.appendChild(loadingModal.children[0]);
} catch (e) {
console.log('error displaying loading modal:', e);
}
}
};
export const addSuccessOrFailureToast = (status, reportSource) => {
const generateToast = document.querySelectorAll('.euiGlobalToastList');
if (generateToast) {
try {
const generateInProgressToast = document.createElement('div');
if (status === 'success') {
generateInProgressToast.innerHTML = reportGenerationSuccess();
setTimeout(function () {
document.getElementById('reportSuccessToast').style.display = 'none';
}, 6000); // closes toast automatically after 6s
} else if (status === 'failure') {
generateInProgressToast.innerHTML = reportGenerationFailure();
setTimeout(function () {
document.getElementById('reportFailureToast').style.display = 'none';
}, 6000);
} else if (status === 'timeoutFailure') {
generateInProgressToast.innerHTML = reportGenerationFailure(
'Error generating report.',
`Timed out generating on-demand report from ${reportSource}. Try again later.`
);
setTimeout(function () {
document.getElementById('reportFailureToast').style.display = 'none';
}, 6000);
} else if (status === 'permissionsFailure') {
generateInProgressToast.innerHTML = permissionsMissingOnGeneration();
setTimeout(function () {
document.getElementById(
'permissionsMissingErrorToast'
).style.display = 'none';
}, 6000);
}
generateToast[0].appendChild(generateInProgressToast.children[0]);
} catch (e) {
console.log('error displaying toast', e);
}
}
};
export const replaceQueryURL = (pageUrl) => {
// we unhash the url in case OpenSearch Dashboards advanced UI setting 'state:storeInSessionStorage' is turned on
const unhashedUrl = new URL(unhashUrl(pageUrl));
let queryUrl = unhashedUrl.pathname + unhashedUrl.hash;
let [, fromDateString, toDateString] = queryUrl.match(timeRangeMatcher);
fromDateString = fromDateString.replace(/[']+/g, '');
// convert time range to from date format in case time range is relative
const fromDateFormat = dateMath.parse(fromDateString);
toDateString = toDateString.replace(/[']+/g, '');
const toDateFormat = dateMath.parse(toDateString);
// replace to and from dates with absolute date
queryUrl = queryUrl.replace(
fromDateString,
"'" + fromDateFormat.toISOString() + "'"
);
queryUrl = queryUrl.replace(
toDateString + '))',
"'" + toDateFormat.toISOString() + "'))"
);
return queryUrl;
};