Skip to content

Commit 3c6bed3

Browse files
committed
refs skygragon#81: add session command.
Signed-off-by: Eric Wang <[email protected]>
1 parent 9195bcf commit 3c6bed3

File tree

6 files changed

+171
-5
lines changed

6 files changed

+171
-5
lines changed

lib/commands/session.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict';
2+
var prompt = require('prompt');
3+
var sprintf = require('sprintf-js').sprintf;
4+
5+
var h = require('../helper');
6+
var chalk = require('../chalk');
7+
var log = require('../log');
8+
var core = require('../core');
9+
var session = require('../session');
10+
11+
const cmd = {
12+
command: 'session [keyword]',
13+
desc: 'Manage sessions',
14+
builder: function(yargs) {
15+
return yargs
16+
.option('c', {
17+
alias: 'create',
18+
type: 'boolean',
19+
describe: 'Create session',
20+
default: false
21+
})
22+
.option('d', {
23+
alias: 'delete',
24+
type: 'boolean',
25+
describe: 'Delete session',
26+
default: false
27+
})
28+
.option('e', {
29+
alias: 'enable',
30+
type: 'boolean',
31+
describe: 'Enable/activate session',
32+
default: false
33+
})
34+
.positional('keyword', {
35+
type: 'string',
36+
describe: 'Session name or id',
37+
default: ''
38+
})
39+
.example(chalk.yellow('leetcode session'), 'Show all cache')
40+
.example(chalk.yellow('leetcode session xxx'), 'Show session by keyword')
41+
.example('', '')
42+
.example(chalk.yellow('leetcode session -c xxx'), 'Create session with name')
43+
.example(chalk.yellow('leetcode session -e xxx'), 'Enable session by keyword')
44+
.example(chalk.yellow('leetcode session -d xxx'), 'Delete session by keyword');
45+
}
46+
};
47+
48+
function printSessions(e, sessions) {
49+
if (e) return log.fail(e);
50+
51+
log.info(chalk.gray(sprintf(' %6s %5s %18s %28s %16s',
52+
'Active', 'Id', 'Name', 'AC Questions', 'AC Submits')));
53+
log.info(chalk.gray('-'.repeat(80)));
54+
55+
for (let s of sessions) {
56+
let questionRate = 0;
57+
let submissionRate = 0;
58+
if (s.submitted_questions > 0)
59+
questionRate = s.ac_questions * 100 / s.submitted_questions;
60+
if (s.total_submitted > 0)
61+
submissionRate = s.total_acs * 100 / s.total_submitted;
62+
63+
log.printf(' %s %8d %-26s %s (%6s %%) %s (%6s %%)',
64+
s.is_active ? h.prettyState('ac') : ' ',
65+
s.id,
66+
s.name || 'Anonymous Session',
67+
chalk.green(sprintf('%6s', s.ac_questions)),
68+
sprintf('%.2f', questionRate),
69+
chalk.green(sprintf('%6s', s.total_acs)),
70+
sprintf('%.2f', submissionRate));
71+
}
72+
}
73+
74+
cmd.handler = function(argv) {
75+
session.argv = argv;
76+
77+
if (argv.create)
78+
return core.createSession(argv.keyword, printSessions);
79+
80+
core.getSessions(function(e, sessions) {
81+
if (e) return log.fail(e);
82+
83+
if (argv.keyword) {
84+
const key = Number(argv.keyword) || argv.keyword;
85+
sessions = sessions.filter(x => x.name === key || x.id === key);
86+
const session = sessions[0];
87+
if (!session) return log.fail('Session not found!');
88+
89+
if (argv.enable && !session.is_active) {
90+
core.activateSession(session, function(e, sessions) {
91+
if (e) return log.fail(e);
92+
require('../session').deleteCodingSession();
93+
printSessions(e, sessions);
94+
});
95+
return;
96+
}
97+
98+
if (argv.delete) {
99+
log.info([
100+
chalk.red.bold('CAREFUL! This action CANNOT be undone!'),
101+
'\nThis will permanently delete all your submissions',
102+
'and progress associated with this session.',
103+
'\nAre you sure you want to delete this session?\n',
104+
'\nPlease type in the session\'s',
105+
chalk.yellow.bold('number of accepted submissions'),
106+
'to confirm.\n'
107+
].join(' '));
108+
109+
prompt.colors = false;
110+
prompt.message = '';
111+
prompt.start();
112+
prompt.get([{name: 'answer', type: 'integer', required: true}], function(e, x) {
113+
if (x.answer !== session.total_acs) return;
114+
return core.deleteSession(session, printSessions);
115+
});
116+
return;
117+
}
118+
}
119+
printSessions(null, sessions);
120+
});
121+
};
122+
123+
module.exports = cmd;

lib/commands/stat.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ function printLine(key, done, all) {
4949
const n = 30;
5050
const percent = (all > 0) ? done / all : 0;
5151
const x = Math.ceil(n * percent);
52-
log.printf(' %s\t%3d/%-3d (%.2f%%) %s%s',
53-
h.prettyLevel(key), done, all, 100 * percent,
54-
chalk.green('█'.repeat(x)),
55-
chalk.red('░'.repeat(n - x)));
52+
log.printf(' %s\t%3d/%-3d (%6s %%) %s%s',
53+
h.prettyLevel(key), done, all,
54+
sprintf('%.2f', 100 * percent),
55+
chalk.green('█'.repeat(x)),
56+
chalk.red('░'.repeat(n - x)));
5657
}
5758

5859
function showProgress(problems) {

lib/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const DEFAULT_CONFIG = {
3535
problem: 'https://leetcode.com/problems/$slug/description/',
3636
problem_detail: 'https://leetcode.com/graphql',
3737
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
38+
session: 'https://leetcode.com/session/',
3839
submit: 'https://leetcode.com/problems/$slug/submit/',
3940
submissions: 'https://leetcode.com/api/submissions/$slug',
4041
submission: 'https://leetcode.com/submissions/detail/$id/',

lib/plugins/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ plugin.logout = function(user, purge) {
7878
if (!user) user = session.getUser();
7979
if (purge) session.deleteUser();
8080
// NOTE: need invalidate any user related cache
81-
cache.del(h.KEYS.problems);
81+
session.deleteCodingSession();
8282
return user;
8383
};
8484

lib/plugins/leetcode.js

+37
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,43 @@ plugin.getFavorites = function(cb) {
377377
});
378378
};
379379

380+
function runSession(method, data, cb) {
381+
const opts = makeOpts(config.sys.urls.session);
382+
opts.json = true;
383+
opts.method = method;
384+
opts.body = data;
385+
386+
const spin = h.spin('Waiting session result');
387+
request(opts, function(e, resp, body) {
388+
spin.stop();
389+
e = checkError(e, resp, 200);
390+
return cb(e, body.sessions);
391+
});
392+
}
393+
394+
plugin.getSessions = function(cb) {
395+
log.debug('running leetcode.getSessions');
396+
runSession('POST', {}, cb);
397+
};
398+
399+
plugin.activateSession = function(session, cb) {
400+
log.debug('running leetcode.activateSession');
401+
const data = {func: 'activate', target: session.id};
402+
runSession('PUT', data, cb);
403+
};
404+
405+
plugin.createSession = function(name, cb) {
406+
log.debug('running leetcode.createSession');
407+
const data = {func: 'create', name: name};
408+
runSession('PUT', data, cb);
409+
};
410+
411+
plugin.deleteSession = function(session, cb) {
412+
log.debug('running leetcode.deleteSession');
413+
const data = {target: session.id};
414+
runSession('DELETE', data, cb);
415+
};
416+
380417
plugin.signin = function(user, cb) {
381418
log.debug('running leetcode.signin');
382419
const spin = h.spin('Signing in leetcode.com');

lib/session.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ session.deleteUser = function() {
3030
cache.del(h.KEYS.user);
3131
};
3232

33+
session.deleteCodingSession = function() {
34+
cache.del(h.KEYS.problems);
35+
};
36+
3337
session.isLogin = function() {
3438
return this.getUser() !== null;
3539
};

0 commit comments

Comments
 (0)