Permalink
Browse files

Add views, forks to plunk stats. Other goodies. Also fixed filearts/p…

  • Loading branch information...
1 parent 8a556bd commit 83ddc252804bc876b27b25f31b25bf7cae73a1e4 @ggoodman ggoodman committed Apr 15, 2013
Showing with 343 additions and 60 deletions.
  1. +18 −0 .npmignore
  2. +6 −0 database/schema/plunk.coffee
  3. +0 −3 database/schema/session.coffee
  4. +1 −0 errors.coffee
  5. +62 −7 index.coffee
  6. +4 −2 package.json
  7. +155 −31 resources/plunks.coffee
  8. +33 −12 resources/sessions.coffee
  9. +33 −0 resources/tags.coffee
  10. +22 −4 resources/users.coffee
  11. +9 −1 server.js
View
@@ -0,0 +1,18 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+
+npm-debug.log
+
+.c9revisions
@@ -42,6 +42,7 @@ PlunkSchema = new Schema
updated_at: { type: Date, 'default': Date.now }
token: { type: String, 'default': genid.bind(null, 16) }
'private': { type: Boolean, 'default': false }
+ template: { type: Boolean, 'default': false }
source: {}
files: [PlunkFileSchema]
user: { type: Schema.ObjectId, ref: "User", index: true }
@@ -50,10 +51,15 @@ PlunkSchema = new Schema
forks: [{ type: String, ref: "Plunk", index: true }]
tags: [{ type: String, index: true}]
voters: [{ type: Schema.ObjectId, ref: "Users", index: true }]
+ rememberers: [{ type: Schema.ObjectId, ref: "Users", index: true }]
history: [PlunkHistorySchema]
+ views: { type: Number, 'default': 0 }
+ forked: { type: Number, 'default': 0 }
PlunkSchema.index(score: -1, updated_at: -1)
PlunkSchema.index(thumbs: -1, updated_at: -1)
+PlunkSchema.index(views: -1, updated_at: -1)
+PlunkSchema.index(forked: -1, updated_at: -1)
PlunkSchema.virtual("url").get -> apiUrl + "/plunks/#{@_id}"
PlunkSchema.virtual("raw_url").get -> runUrl + "/plunks/#{@_id}/"
@@ -32,9 +32,6 @@ SessionSchema.virtual("age").get -> Date.now() - @last_access
SessionSchema.plugin(lastModified)
-SessionSchema.statics.prune = (max_age = 1000 * 60 * 60 * 24 * 7 * 2, cb = ->) ->
- @find({}).where("last_access").lt(new Date(Date.now() - max_age)).remove()
-
SessionSchema.set "toJSON",
virtuals: true
getters: true
View
@@ -45,6 +45,7 @@ errorTypes =
httpCode: 404
ImpossibleError:
message: "Impossibru"
+ initialize: (err) -> console.error("[ERR] #{@message}", err)
exports[name] = createErrorClass(name, errDef) for name, errDef of errorTypes
View
@@ -2,6 +2,10 @@ express = require("express")
nconf = require("nconf")
cors = require("cors")
+#memwatch = require("memwatch");
+
+#memwatch.on "leak", (d) -> console.log "[LEAK]", d
+
# Set defaults in nconf
require "./configure"
@@ -15,6 +19,7 @@ app = module.exports = express()
apiUrl = nconf.get("url:api")
+{Session} = require("./database")
errorHandler = (err, req, res, next) ->
@@ -34,11 +39,33 @@ app.use app.router
app.use errorHandler
app.use express.errorHandler()
+###
+hd = null
+
+app.get "/debug/start", (req, res, next) ->
+ if hd then res.send 400, "Heap diff in progress"
+ else
+ hd = new memwatch.HeapDiff
+ res.send 200, "Heap diff started"
+app.get "/debug/end", (req, res, next) ->
+ if hd then res.json hd.end()
+ else res.send 400, "Heap diff not in progress"
+
+ hd = null
+
+app.get "/debug/gc", (req, res, next) ->
+ memwatch.gc()
+
+ res.send "Garbage collected"
+###
# Sessions
sessions = require "./resources/sessions"
+# Users
+users = require "./resources/users"
+
app.get "/sessions", sessions.findOrCreate
app.post "/sessions", sessions.create
@@ -47,6 +74,7 @@ app.get "/sessions/:id", sessions.read
app.post "/sessions/:id/user", sessions.withSession, sessions.setUser
app.del "/sessions/:id/user", sessions.withSession, sessions.unsetUser
+
# Plunks
plunks = require "./resources/plunks"
@@ -58,6 +86,18 @@ app.get "/plunks/trending", plunks.createListing (req, res) ->
app.get "/plunks/popular", plunks.createListing (req, res) ->
baseUrl: "#{apiUrl}/plunks/popular"
sort: "-thumbs -updated_at"
+app.get "/plunks/views", plunks.createListing (req, res) ->
+ baseUrl: "#{apiUrl}/plunks/views"
+ sort: "-views -updated_at"
+app.get "/plunks/forked", plunks.createListing (req, res) ->
+ baseUrl: "#{apiUrl}/plunks/forked"
+ sort: "-forked -updated_at"
+
+app.get "/plunks/remembered", users.withCurrentUser, plunks.createListing (req, res) ->
+ sort: "-updated_at"
+ ignorePrivate: true
+ query: {rememberers: req.currentUser._id}
+ baseUrl: "#{apiUrl}/users/#{req.params.login}/remembered"
app.post "/plunks", validateSchema(plunks.schema.create), plunks.create
app.get "/plunks/:id", plunks.withPlunk, plunks.read
@@ -67,6 +107,9 @@ app.del "/plunks/:id", plunks.withPlunk, plunks.ownsPlunk, plunks.destroy
app.post "/plunks/:id/thumb", plunks.withPlunk, plunks.setThumbed
app.del "/plunks/:id/thumb", plunks.withPlunk, plunks.unsetThumbed
+app.post "/plunks/:id/remembered", plunks.withPlunk, plunks.setRemembered
+app.del "/plunks/:id/remembered", plunks.withPlunk, plunks.unsetRemembered
+
forkSchema = (req) ->
if req.apiVersion is 0 then plunks.schema.create
else if req.apiVersion is 1 then plunks.schema.fork
@@ -78,17 +121,21 @@ app.get "/plunks/:id/forks", plunks.createListing (req, res) ->
sort: "-updated_at"
-# Users
-users = require "./resources/users"
app.get "/users/:login", users.withUser, users.read
app.get "/users/:login/plunks", users.withUser, plunks.createListing (req, res) ->
+ sort: "-updated_at"
query: {user: req.user._id}
baseUrl: "#{apiUrl}/users/#{req.params.login}/plunks"
app.get "/users/:login/thumbed", users.withUser, plunks.createListing (req, res) ->
+ sort: "-updated_at"
query: {voters: req.user._id}
baseUrl: "#{apiUrl}/users/#{req.params.login}/thumbed"
+app.get "/users/:login/remembered", users.withUser, plunks.createListing (req, res) ->
+ sort: "-updated_at"
+ query: {rememberers: req.user._id}
+ baseUrl: "#{apiUrl}/users/#{req.params.login}/remembered"
###
@@ -124,24 +171,32 @@ app.del "/catalogue/packages/:name/maintainers", packages.withOwnPackage, packag
#app.post "/catalogue/packages/:name/versions/:semver", validateSchema(packages.schema.versions.update), packages.withOwnPackage, packages.updateVersion
#app.del "/catalogue/packages/:name/versions/:semver", packages.withOwnPackage, packages.destroyVersion
+# Tags
+tags = require "./resources/tags"
+
+app.get "/tags", tags.list
+
+
app.get "/robots.txt", (req, res, next) ->
res.send """
User-Agent: *
Disallow: /
"""
app.get "/favicon.ico", (req, res, next) ->
- console.log "Requesting favicon.ico"
res.send("")
app.all "*", (req, res, next) -> next(new apiErrors.NotFound)
-Session = require("./database").Session
PRUNE_FREQUENCY = 1000 * 60 * 60 * 6 # Prune the sessions every 6 hours
pruneSessions = ->
- console.log "Pruning sessions"
- Session.prune()
+ console.log "[INFO] Pruning sessions"
+ sessions.prune (err, numDocs) ->
+ if err then console.log "[ERR] Pruning failed", err.message
+ else console.log "[OK] Pruned #{numDocs} sessions"
+
+ setTimeout pruneSessions, PRUNE_FREQUENCY
-setInterval pruneSessions, PRUNE_FREQUENCY
+pruneSessions()
View
@@ -5,7 +5,7 @@
"api.plnkr.co",
"api.plunker.co"
],
- "version": "0.4.0-7",
+ "version": "0.4.1-11",
"private": true,
"description": "The RESTful backend that powers Plunker",
"main": "server.js",
@@ -22,7 +22,9 @@
"nconf": "~0.6.7",
"genid": "~0.1.0",
"googlediff": "~0.1.0",
- "semver": "~1.1.4"
+ "semver": "~1.1.4",
+ "nodefly": "~0.1.34",
+ "memwatch": "~0.2.2"
},
"devDependencies": {
"coffee-script": "~1.6.1"
Oops, something went wrong.

0 comments on commit 83ddc25

Please sign in to comment.