javascript 106-1 NTUSC选举黑客马拉松;天真地编写代码以使事情有效

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 106-1 NTUSC选举黑客马拉松;天真地编写代码以使事情有效相关的知识,希望对你有一定的参考价值。

const Promise = require('bluebird');
const express = require('express');

const models = require('./models');

const app = express();

app.use('/static', express.static(__dirname + '/public'));

app.get('/clients', (req, resp) => {
    // SELECT *, (TIME_TO_SEC(TIMEDIFF(NOW(3), `last_ping`)) - 8 * 3600) as timer FROM `client` order by timer asc
    const exprDelay = models.db.fn('TIMEDIFF', models.db.fn('NOW', 3), models.db.col('last_ping'));
    models.Client.findAll({
        attributes: [
            'id',
            'name',
            'last_ping',
            [ exprDelay, 'delay_time' ]
        ],
        order: [[models.db.col('delay_time'), 'DESC']]
    }).then(rows => {
        tableContent = rows.map(d => {
            let cl = '';
            let mat = (d.get('delay_time') || '').match(/(\d+?):(\d+?):(\d+?).(\d+)/);
            delay_time = 0;
            if (mat) {
                delay_time = (mat[2] * 60000 + (mat[3] - 0) * 1000 + (mat[4] - 0)) / 1000;
                delay_time = ('' + delay_time).slice(0, 7);
            }
            if (delay_time > 300) {
                cl = 'danger';
            } else if (delay_time > 60) {
                cl = 'warning';
            }
            let last_ping = d.last_ping ? d.last_ping.toISOString() : '';
            return `<tr class="${cl}"><td>${d.id}<td>${d.name}<td>${last_ping}<td style="text-align: right">${delay_time}`
        }).join('');

        html = `<!DOCTYPE html>
        <html>
        <head><link rel="stylesheet" href="/static/css/bootstrap.min.css">
        <title>票點狀況</title>
        <meta http-equiv="refresh" content="3">
        <body style="font-size: 24px; margin-top: 2em;">
        <div class="container">
        <table class="table"><tbody>${tableContent}</table></div>`;
        resp.send(html);
    });
});

const enumClient = [
    '一活', '二活', '普通', '新生', '共同', '大一女', '長興', '社科院', '水源', '醫學院', '公衛學院'
];

const enumDepcode = [
    '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'
];

const enumDepname = [
    '文', '理', '社科', '醫', '工', '生農', '管', '公衛', '電資', '法律', '生科'
];

app.get('/distr', (req, resp) => {
    models.db.query('SELECT client.name, LEFT(ballot.dept, 1) as depcode, count(*) as cnt FROM `ballot`, `client` '
        + 'WHERE client.id = ballot.client_id AND ballot.commit = 1 GROUP BY client.id, depcode')
    .spread((rows, _) => {
        let pivot = [];
        enumClient.forEach((_, i) => {
            pivot[i] = [];
            for (var j = 0; j < enumDepcode.length; j++) {
                pivot[i][j] = '';
            }
        });

        rows.forEach(row => {
            let idx = enumClient.indexOf(row.name);
            let idx2 = enumDepcode.indexOf(row.depcode);
            if (idx < 0) {
                console.log('WARN: ', row, 'not found in any client');
            }
            if (idx2 < 0) {
                console.log('WARN: ', row, 'not found in any dept');
            }
            pivot[idx][idx2] = row.cnt;
        });

        return pivot;
    }).then(pivot => {

        tableHead = '<tr><th>' + enumDepcode.map((d, i) => '<th style="text-align: center;">' + d + '<br>' + enumDepname[i]).join('');

        tot = [];
        enumDepcode.forEach((_, j) => { tot[j] = 0; });

        tableContent = pivot.map((row, i) => {
            let cont = row.map((cell, j) => {
                if (cell != '')
                    tot[j] += cell;
                return '<td>' + cell;
            }).join('');
            return `<tr class="row" data-client="${enumClient[i]}">${cont}`;
        }).join('');


        tableFoot = `<tr class="row info" data-client="Total">` + tot.map(x => `<td>${x}`).join('');
        let grand_tot = tot.reduce((pv, v) => pv + v, 0);

        html = `<!DOCTYPE html>
        <html>
        <head><link rel="stylesheet" href="/static/css/bootstrap.min.css">
        <title>票種分佈表</title>
        <meta http-equiv="refresh" content="20">
        <style>
        .row:before { content: attr(data-client); display: block; position: absolute; left: .2em; line-height: 1.42857; padding: 8px; }
        </style>
        <body style="font-size: 24px; margin-top: 2em;">
        <div class="container">
        <table class="table table-striped table-hover" style="text-align: center;">
        <tfoot>${tableFoot}
        <thead>${tableHead}
        <tbody>${tableContent}</table>Grand total: ${grand_tot}</div>`;

        resp.send(html);
    });
})

app.listen(9487, () => {
    console.log('Running at port 9487...');
});

以上是关于javascript 106-1 NTUSC选举黑客马拉松;天真地编写代码以使事情有效的主要内容,如果未能解决你的问题,请参考以下文章

luogu P3765 总统选举(线段树维护摩尔投票+平衡树)

为什么总有人黑 JavaScript?就真的那么差吗?——爱创课堂

javascript调试进入VM****文件如何黑盒这些文件? [复制]

这些JavaScript编程黑科技,装逼指南,高逼格代码,让你惊叹不已

JavaScript 最新特性实现的三大黑科技

黑苹果理事长袁岳入选第四届“中国创翼”创业创新大赛专家委员会