Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Nov 29, 2018
1 parent 908704d commit 0872c82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
16 changes: 8 additions & 8 deletions lib/dbs/activity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert')
var SQL = require('sql-template-strings')
var SQL = require('sql-template-strings')

// constants
// =
Expand Down Expand Up @@ -51,24 +51,24 @@ class ActivityDB {

listGlobalEvents ({limit, lt, gt, lte, gte, reverse} = {}) {
var query = SQL`SELECT * FROM activity`
if (lt) query.append(SQL` WHERE key < ${lt}`)
if (lt) query.append(SQL` WHERE key < ${lt}`)
if (lte) query.append(SQL` WHERE key <= ${lte}`)
if (gt) query.append(SQL` WHERE key > ${gt}`)
if (gt) query.append(SQL` WHERE key > ${gt}`)
if (gte) query.append(SQL` WHERE key >= ${gte}`)
if (!reverse) query.append(SQL` ORDER BY key`)
else query.append(SQL` ORDER BY key DESC`)
else query.append(SQL` ORDER BY key DESC`)
if (limit) query.append(SQL` LIMIT ${limit}`)
return this.sqlite.all(query)
}

listUserEvents (username, opts = {}) {
listUserEvents (username, {limit, lt, gt, lte, gte, reverse} = {}) {
var query = SQL`SELECT * FROM activity WHERE username = ${username}`
if (lt) query.append(SQL` AND key < ${lt}`)
if (lt) query.append(SQL` AND key < ${lt}`)
if (lte) query.append(SQL` AND key <= ${lte}`)
if (gt) query.append(SQL` AND key > ${gt}`)
if (gt) query.append(SQL` AND key > ${gt}`)
if (gte) query.append(SQL` AND key >= ${gte}`)
if (!reverse) query.append(SQL` ORDER BY key`)
else query.append(SQL` ORDER BY key DESC`)
else query.append(SQL` ORDER BY key DESC`)
if (limit) query.append(SQL` LIMIT ${limit}`)
return this.sqlite.all(query)
}
Expand Down
16 changes: 8 additions & 8 deletions lib/dbs/archives.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var EventEmitter = require('events')
var assert = require('assert')
var SQL = require('sql-template-strings')
var SQL = require('sql-template-strings')
var lock = require('../lock')

// exported api
Expand Down Expand Up @@ -54,7 +54,7 @@ class ArchivesDB extends EventEmitter {
if (key && key.key) key = key.key
assert(typeof key === 'string')
await this.sqlite.run(SQL`DELETE FROM archives WHERE key = ${key}`)
this.emit('del', record)
this.emit('del', key)
}

// getters
Expand Down Expand Up @@ -109,7 +109,7 @@ class ArchivesDB extends EventEmitter {
return ArchivesDB.deserialize(res)
}

list ({cursor, limit, reverse, sort, featuredOnly, getExtra} = {}) {
async list ({cursor, limit, reverse, sort, featuredOnly, getExtra} = {}) {
// 'popular' uses a special index
if (sort === 'popular') {
return this._listPopular({cursor, limit, reverse})
Expand All @@ -120,7 +120,7 @@ class ArchivesDB extends EventEmitter {
sort = sort || 'key'
if (cursor) {
if (reverse) query.append(SQL` WHERE ${sort} < ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
if (featuredOnly) {
query.append(SQL` AND isFeatured = 1`)
}
Expand All @@ -136,9 +136,9 @@ class ArchivesDB extends EventEmitter {
// run query and grab additional data
var res = await this.sqlite.all(query)
if (getExtra) {
return Promise.all(res.map({key} => this.getExtraByKey(key)))
return Promise.all(res.map(({key}) => this.getExtraByKey(key)))
} else {
return Promise.all(res.map({key} => this.getByKey(key)))
return Promise.all(res.map(({key}) => this.getByKey(key)))
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ class ArchivesDB extends EventEmitter {
await this.sqlite.run(SQL`
DELETE FROM users_archives WHERE key=${key} AND userid=${userId}
`)
this.emit('remove-hosting-user', {key, userId}, archiveRecord)
this.emit('remove-hosting-user', {key, userId})
}

// internal tracking
Expand All @@ -203,6 +203,7 @@ class ArchivesDB extends EventEmitter {
WHERE users_archives.key IS NULL
`)
}

// helpers
// =

Expand All @@ -219,4 +220,3 @@ class ArchivesDB extends EventEmitter {
}
}
module.exports = ArchivesDB

4 changes: 2 additions & 2 deletions lib/dbs/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ReportsDB extends EventEmitter {
assert(record && typeof record === 'object')
assert(typeof record.id === 'number')
record.updatedAt = Date.now()
var query = SQL`UPDATE archives SET`
var query = SQL`UPDATE reports SET`
for (let k in record) {
if (k === 'id') continue
query.append(SQL` ${k} = ${record[k]}`)
Expand Down Expand Up @@ -80,7 +80,7 @@ class ReportsDB extends EventEmitter {
sort = sort || 'id'
if (cursor) {
if (reverse) query.append(SQL` WHERE ${sort} < ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
}
query.append(SQL` ORDER BY ${sort}`)
if (reverse) query.append(SQL` DESC`)
Expand Down
18 changes: 7 additions & 11 deletions lib/dbs/users.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var assert = require('assert')
var monotonicTimestamp = require('monotonic-timestamp')
var collect = require('stream-collector')
var EventEmitter = require('events')
var moment = require('moment')
var SQL = require('sql-template-strings')
var lock = require('../lock')
var SQL = require('sql-template-strings')
const {
COHORT_STATE_REGISTERED,
COHORT_STATE_ACTIVATED,
Expand All @@ -17,7 +15,6 @@ const {
class UsersDB extends EventEmitter {
constructor (cloud) {
super()
// create levels and indexer
this.cloud = cloud
this.sqlite = cloud.db
}
Expand Down Expand Up @@ -113,22 +110,23 @@ class UsersDB extends EventEmitter {
return record
}

list ({cursor, limit, reverse, sort} = {}) {
async list ({cursor, limit, reverse, sort} = {}) {
// construct query
var query = SQL`SELECT * FROM users`
sort = sort || 'id'
if (cursor) {
if (reverse) query.append(SQL` WHERE ${sort} < ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
else query.append(SQL` WHERE ${sort} > ${cursor}`)
}
query.append(SQL` ORDER BY ${sort}`)
if (reverse) query.append(SQL` DESC`)
if (limit) query.append(SQL` LIMIT ${limit}`)

// run query and attach additional data
var users = this.sqlite.all(query)
await Promise.all(users.map(async (user) => {
user.archives = await this._getArchives(record)
var users = await this.sqlite.all(query)
return Promise.all(users.map(async (user) => {
user.archives = await this._getArchives(user)
return user
}))
}

Expand All @@ -152,7 +150,6 @@ class UsersDB extends EventEmitter {
var userRecord = await this.getByID(userId)
var archive = this.cloud.archiver.getArchive(archiveKey)
if (archive && archive.diskUsage) {
var userRecord = await this.getByID(userId)
userRecord.diskUsage -= archive.diskUsage
await this.put(userRecord)
/* dont await */ this.cloud.archiver.computeUserDiskUsageAndSwarm(userRecord)
Expand Down Expand Up @@ -207,4 +204,3 @@ class UsersDB extends EventEmitter {
}
}
module.exports = UsersDB

0 comments on commit 0872c82

Please sign in to comment.