Debian Codegraph @

blazar_dashboard/static/leases/js/calendar/lease_chart.js

raw
1 (function(window, horizon, $, undefined) {
2 'use strict';
3
4 const CHART_TITLE_HEIGHT = 68;
5 const ROW_HEIGHT = 60;
6
7 const selector = '#blazar-calendar-host';
8 const pluralResourceType = gettext("Hosts");
9 if ($(selector).length < 1) return;
10 const calendarElement = $(selector);
11 const form = $('form[name="blazar-calendar-controls"]');
12
13 function init() {
14 calendarElement.addClass('loaded');
15 $.getJSON("resources.json")
16 .done(function(resp) {
17 const rowAttr = resp.row_attr;
18 // For this row shows up at all, we need at least 1 data point.
19 const reservationsById = {}
20 resp.reservations.forEach(function(reservation){
21 if(!(reservation.reservation_id in reservationsById)){
22 reservationsById[reservation.reservation_id] = reservation
23 reservation.name = reservation.reservation_id
24 reservation.data = []
25 }
26 const newReservation = {
27 'start_date': new Date(reservation.start_date),
28 'end_date': new Date(reservation.end_date),
29 'x': reservation[rowAttr],
30 'y': [
31 new Date(reservation.start_date).getTime(),
32 new Date(reservation.end_date).getTime()
33 ],
34 }
35 reservationsById[reservation.reservation_id].data.push(newReservation)
36 })
37 reservationsById["0"] = {"name": "0", "data": []}
38 resp.resources.forEach(function(resource){
39 reservationsById["0"].data.push({x: resource[rowAttr], y: [0, 0]})
40 })
41 const allReservations = Object.values(reservationsById)
42 constructCalendar(allReservations, computeTimeDomain(7), resp.resources)
43 })
44 .fail(function() {
45 calendarElement.html(`<div class="alert alert-danger">${gettext("Unable to load reservations")}.</div>`);
46 });
47
48 function constructCalendar(rows, timeDomain, resources){
49 calendarElement.empty();
50 const options = {
51 series: rows,
52 chart: {
53 type: 'rangeBar',
54 toolbar: {show: false},
55 zoom: {enabled: false, type: 'xy'},
56 height: ROW_HEIGHT * resources.length + CHART_TITLE_HEIGHT,
57 width: "100%",
58 },
59 plotOptions: { bar: {horizontal: true, rangeBarGroupRows: true}},
60 xaxis: { type: 'datetime' },
61 legend: { show: false },
62 tooltip: {
63 custom: function({series, seriesIndex, dataPointIndex, w}) {
64 const datum = rows[seriesIndex];
65 const resourcesReserved = datum.data.map(function(el){ return el.x }).join("<br>");
66 const project_dt = "";
67 if(datum.project_id){
68 project_dt = `<dt>${gettext("Project")}</dt>
69 <dd>${datum.project_id}</dd>`;
70 }
71 return `<div class='tooltip-content'><dl>
72 ${project_dt}
73 <dt>${pluralResourceType}</dt>
74 <dd>${resourcesReserved}</dd>
75 <dt>${gettext("Reserved")}</dt>
76 <dd>${datum.start_date} <strong>${gettext("to")}</strong> ${datum.end_date}</dd>
77 </dl></div>`;
78 }
79 },
80 annotations: {
81 xaxis: [
82 {
83 x: new Date().getTime(),
84 borderColor: '#00E396',
85 }
86 ]
87 },
88 }
89 const chart = new ApexCharts(document.querySelector(selector), options);
90 chart.render();
91
92 setTimeDomain(timeDomain, chart); // Also sets the yaxis limits
93
94 $('input[data-datepicker]', form).datepicker({
95 dateFormat: 'mm/dd/yyyy'
96 });
97
98 $('input', form).on('change', function() {
99 if (form.hasClass('time-domain-processed')) {
100 const timeDomain = getTimeDomain();
101 // If invalid ordering is chosen, set period to 1 day
102 if (timeDomain[0] >= timeDomain[1]) {
103 timeDomain[1] = d3.time.day.offset(timeDomain[0], +1);
104 }
105 setTimeDomain(timeDomain, chart);
106 }
107 });
108
109 $('.calendar-quickdays').click(function() {
110 const days = parseInt($(this).data("calendar-days"));
111 if (!isNaN(days)) {
112 const timeDomain = computeTimeDomain(days);
113 setTimeDomain(timeDomain, chart);
114 }
115 })
116 }
117
118 function computeTimeDomain(days) {
119 const padFraction = 1/8; // chart default is 3 hours for 1 day
120 return [
121 d3.time.day.offset(Date.now(), -days * padFraction),
122 d3.time.day.offset(Date.now(), days * (1 + padFraction))
123 ];
124 }
125
126 function setTimeDomain(timeDomain, chart) {
127 // Set the input elements
128 form.removeClass('time-domain-processed');
129 $('#dateStart').datepicker('setDate', timeDomain[0]);
130 $('#timeStartHours').val(timeDomain[0].getHours());
131 $('#dateEnd').datepicker('setDate', timeDomain[1]);
132 $('#timeEndHours').val(timeDomain[1].getHours());
133 form.addClass('time-domain-processed');
134 const options = { yaxis: {min: timeDomain[0].getTime(), max: timeDomain[1].getTime()}}
135 chart.updateOptions(options)
136 }
137
138 function getTimeDomain() {
139 const timeDomain = [
140 $('#dateStart').datepicker('getDate'),
141 $('#dateEnd').datepicker('getDate')
142 ];
143
144 timeDomain[0].setHours($('#timeStartHours').val());
145 timeDomain[0].setMinutes(0);
146 timeDomain[1].setHours($('#timeEndHours').val());
147 timeDomain[1].setMinutes(0);
148
149 return timeDomain;
150 }
151 }
152
153 horizon.addInitFunction(init);
154
155 })(window, horizon, jQuery);