{"size": 2589, "ext": "js", "max_stars_repo_path": "openstack_lease_it/lease_it/static/js/database.js", "max_stars_repo_name": "LAL/openstack-lease-it", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "openstack_lease_it/lease_it/static/js/database.js", "max_issues_repo_name": "LAL/openstack-lease-it", "max_issues_repo_issues_event_min_datetime": "2017-04-13T16:48:16.000Z", "max_issues_repo_issues_event_max_datetime": "2017-11-22T08:13:39.000Z", "max_forks_repo_path": "openstack_lease_it/lease_it/static/js/database.js", "max_forks_repo_name": "LAL/openstack-lease-it", "max_forks_repo_forks_event_min_datetime": "2017-04-06T09:08:40.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-25T08:15:00.000Z"}
{"max_stars_count": null, "max_issues_count": 11, "max_forks_count": 3, "avg_line_length": 33.1923076923, "max_line_length": 121, "alphanum_fraction": 0.5264580919}
// 7 * 25 * 60 * 60 * 1000 = 604800000
const HEARTBEAT_TIMEOUT = 604800000;

/*
    buildDatabaseView create a full display of Da on div_name
*/
function buildDatabaseView(div_name) {
    var table_columns = [
        { data: 'instance_id' },
        { data: 'heartbeat_at' },
        { data: 'leased_at' },
        { data: 'lease_end' }
    ];
    $('#table-' + div_name).DataTable({
        ajax: {
            url: '/database',
            dataSrc: function(instances) {
                return instances;
            }
        },
        columns: table_columns,
        lengthChange: false,
        pageLength: 25,
        columnDefs: [
            {
                targets: [0],
                render: function ( data, type, row ) {
                    var now = new Date();
                    var heartbeat_date = new Date(row.heartbeat_at);
                    // If a VM as not been seen since 1 week, we can delete it
                    if (heartbeat_date < now - HEARTBEAT_TIMEOUT) {
                        return buildDatabaseRowMenu(data) +
                               formatText(data, MAX_STRING_LENGTH);
                    } else {
                        return formatText(data, MAX_STRING_LENGTH);
                    }
                }
            }],
        drawCallback: function(settings, json) {
            $(".tooltipped").tooltip();
        },
    });
}

/*
    buildDatabaseRowMenu build a menu for each row of Database Table
*/
function buildDatabaseRowMenu(data) {
    var menu = '<a class="btn-floating waves-effect waves-light tiny" onClick="swapDatabaseRowMenu(\'' + data + '\')">' +
               '<i class="material-icons" id="database-icon-' + data + '">chevron_right</i></a> ' +
               '<span hidden id="database-delete-' + data + '">' +
               '<a class="btn-floating waves-effect waves-light red lighten-2"' +
               'onClick="deleteDatabase(\'' + data + '\')">' +
               '<i class="material-icons">delete</i></a></span> ';
    return menu;
}

/*
    swapDatabaseRowMenu swap on/off the delete button
*/
function swapDatabaseRowMenu(button) {
    if ($('#database-delete-' + button).css('display') == 'none') {
        $('#database-icon-' + button).text('chevron_left');
    } else {
        $('#database-icon-' + button).text('chevron_right');
    }
    $('#database-delete-' + button).toggle();
}

/*
    deleteDatabase delete entry in database
*/
function deleteDatabase(instance) {
    return $.getJSON("/database/" + instance, function(data){
    }).success(function(data){
        notify(data);
    });
}