Skip to content

Commit

Permalink
Impl [Nuclio] Implement a download button (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-prokopchuk authored Jan 2, 2025
1 parent ba9feaa commit 79d78f4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 31 deletions.
58 changes: 54 additions & 4 deletions src/igz_controls/services/control-panel-logs-data.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
angular.module('iguazio.dashboard-controls')
.factory('ControlPanelLogsDataService', ControlPanelLogsDataService);

function ControlPanelLogsDataService($q, lodash, ElasticsearchService) {
function ControlPanelLogsDataService($q, lodash, ElasticsearchService, ElasticSearchDataService) {
return {
collectLogs: collectLogs,
entriesPaginated: search,
logsPaginated: logsWidthReplicas
};
Expand Down Expand Up @@ -52,8 +53,8 @@
if (queryParams.timeFrame) {
searchFrom = 'now-' + queryParams.timeFrame;
} else {
searchFrom = queryParams.customTimeFrame.from
searchTo = lodash.get(queryParams, 'customTimeFrame.to', 'now')
searchFrom = lodash.get(queryParams, 'customTimeFrame.from', '1970-01-01T00:00:00Z');
searchTo = lodash.get(queryParams, 'customTimeFrame.to', 'now');
}
}

Expand Down Expand Up @@ -113,6 +114,10 @@
});
}

if (queryParams.trackTotalHits) {
config.track_total_hits = true;
}

return ElasticsearchService.search(config)
.then(function (response) {
// Saved log entry can be found in `_source` property
Expand Down Expand Up @@ -140,6 +145,51 @@
});
}

/**
* Collects all the logs using chunks, and saved them as an array of strings.
* @returns {Promise.<Array.<Object>>} a promise resolving to an array of logs.
*/
function collectLogs(query) {
var keepAlive = '5m';
var size = 10000;
var downloadLogsData = [];
var createPitConfig = {
index: 'filebeat*',
keepAlive: keepAlive
};

return ElasticSearchDataService.createPit(createPitConfig).then(function (pitId) {
return getNextPitLogs(pitId, null);
});

function getNextPitLogs(pitId, searchAfter) {
return ElasticSearchDataService.getNextPitLogs(size, query, keepAlive, pitId, searchAfter).then(function (response) {
var hits = response.hits.hits;

if (hits.length > 0) {
var lastHit = lodash.last(hits);

downloadLogsData = downloadLogsData.concat(prepareLogs(hits));

return getNextPitLogs(response.pit_id, lastHit.sort)
} else {
return downloadLogsData;
}
}).catch(function (error) {
throw error;
});
}

function prepareLogs(logs) {
return logs.map(function (logData) {
var log = lodash.get(logData, '_source', {});

return log['@timestamp'] + ' ' + log.name + ' (' + log.level + ') ' +
lodash.get(log, 'message', '') + ' ' + JSON.stringify(lodash.get(log, 'more', {}));
});
}
}

/**
* Mocks the real search function without the need for a running Elasticsearch service, for development
* purposes.
Expand Down Expand Up @@ -195,7 +245,7 @@
logs.replicas = replicas;
}

console.info(queryParams);
// console.info(queryParams);

return $q.when(logs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ such restriction.
function NclVersionExecutionLogController(lodash, $interval, i18next, $i18next, $rootScope, moment, ConfigService,
ControlPanelLogsDataService, ExportService, LoginService,PaginationService) {
var ctrl = this;

var lng = i18next.language;
var MAX_DOWNLOAD_LOGS = 2000000;

var refreshInterval = null;
var initialTimeRange = {
from: null,
Expand All @@ -51,6 +52,7 @@ such restriction.
}
};

ctrl.downloadButtonIsHidden = true;
ctrl.isSplashShowed = {
value: false
};
Expand All @@ -67,6 +69,7 @@ such restriction.
}
};
ctrl.datePreset = initialDatePreset;
ctrl.logsAreDownloading = false;
ctrl.timeRange = initialTimeRange;
ctrl.searchStates = {};
ctrl.selectedReplicas = [];
Expand Down Expand Up @@ -164,7 +167,7 @@ such restriction.
ctrl.$onDestroy = onDestroy;

ctrl.applyFilters = applyFilters;
// ctrl.downloadLogFiles = downloadLogFiles;
ctrl.downloadLogFiles = downloadLogFiles;
ctrl.onCheckboxChange = onCheckboxChange;
ctrl.onRefreshRateChange = onRefreshRateChange;
ctrl.onTimeRangeChange = onTimeRangeChange;
Expand All @@ -189,6 +192,12 @@ such restriction.

ctrl.page.size = ctrl.perPageValues[0].id;

ControlPanelLogsDataService.logsPaginated(0, 100, { query: generateDefaultQuery(), trackTotalHits: true })
.then(function (logs) {
if (logs.length > 0) {
ctrl.downloadButtonIsHidden = logs.total_logs_count >= MAX_DOWNLOAD_LOGS;
}
});
applyFilters();
}

Expand All @@ -213,18 +222,18 @@ such restriction.
searchWithParams(0, ctrl.page.size);
}

// /**
// * Downloads log to the file
// */
// function downloadLogFiles() {
// ExportService.exportLogs(prepareLogs(), ctrl.version.metadata.name);
//
// function prepareLogs() {
// return ctrl.logs.map(function (log) {
// return log['@timestamp'] + ' ' + log.name + ' (' + log.level + ') ' + log.message + ' ' + log.more;
// });
// }
// }
/**
* Downloads log to the file
*/
function downloadLogFiles() {
stopAutoUpdate();

return ControlPanelLogsDataService.collectLogs(generateDefaultQuery())
.then(function (response) {
ExportService.exportLogs(response, ctrl.version.metadata.name);
startAutoUpdate();
});
}

/**
* Triggered when search text was changed
Expand Down Expand Up @@ -277,7 +286,7 @@ such restriction.
function refreshLogs() {
startAutoUpdate();

ControlPanelLogsDataService.logsPaginated(ctrl.page.number, ctrl.page.size, queryParams(), true)
ControlPanelLogsDataService.logsPaginated(ctrl.page.number, ctrl.page.size, queryParams())
.then(function (logs) {
if (logs.length > 0) {
ctrl.logs = lodash.cloneDeep(logs);
Expand Down Expand Up @@ -330,13 +339,12 @@ such restriction.
return;
}

ControlPanelLogsDataService.logsPaginated(ctrl.page.number, ctrl.page.size, queryParamsAutoUpdate(), true)
ControlPanelLogsDataService.logsPaginated(ctrl.page.number, ctrl.page.size, queryParamsAutoUpdate())
.then(function (logs) {
if (logs.length > 0) {
ctrl.logs = lodash.cloneDeep(logs);
ctrl.page.total = logs['total_pages'];

// set lastItemTimeStamp and start autoupdate
onChangePageCallback();
}
});
Expand Down Expand Up @@ -412,12 +420,25 @@ such restriction.
ctrl.filterQuery = lodash.join(queries, ' AND ');
}

/**
* Generates query string without additional filters
*/
function generateDefaultQuery() {
var queries = ['system-id:"' + ConfigService.systemId + '"', '_exists_:nuclio'];

if (!lodash.isEmpty(ctrl.version.metadata.name)) {
queries.push('name:' + ctrl.version.metadata.name);
}
return lodash.join(queries, ' AND ');
}

/**
* Generates query params for ordinary request, for example, when page was changed
* @returns {Object}
*/
function queryParams() {
return {
trackTotalHits: true,
query: ctrl.filterQuery,
timeFrame: ctrl.datePreset,
customTimeFrame: ctrl.timeRange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,17 @@
data-refresh="$ctrl.searchWithParams($ctrl.page.number, $ctrl.page.size)">
</igz-action-item-refresh>
</div>
<!-- <div class="actions-bar-left">-->
<!-- <div class="igz-action-item"-->
<!-- data-ng-click="$ctrl.downloadLogFiles()"-->
<!-- data-uib-tooltip="{{ 'common:DOWNLOAD' | i18next }}"-->
<!-- data-tooltip-placement="bottom"-->
<!-- data-tooltip-popup-delay="300"-->
<!-- data-tooltip-append-to-body="true">-->
<!-- <div class="action-icon igz-icon-download"></div>-->
<!-- </div>-->
<!-- </div>-->
<div class="actions-bar-left" data-ng-hide="$ctrl.downloadButtonIsHidden">
<div class="igz-action-item"
data-ng-click="$ctrl.downloadLogFiles()"
data-ng-class="{'inactive': $ctrl.logsAreDownloading}"
data-uib-tooltip="{{ 'common:DOWNLOAD' | i18next }}"
data-tooltip-placement="bottom"
data-tooltip-popup-delay="300"
data-tooltip-append-to-body="true">
<div class="action-icon igz-icon-download"></div>
</div>
</div>
<igz-actions-panes data-filters-toggle-method="$ctrl.toggleFilters()"
data-show-filter-icon="true"
data-filters-counter="$ctrl.activeFilters"
Expand Down

0 comments on commit 79d78f4

Please sign in to comment.