Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / 1
diff --git a/web/query/lists/1 b/web/query/lists/1
new file mode 100644 (file)
index 0000000..b19ac9a
--- /dev/null
@@ -0,0 +1,130 @@
+function(head, req) {
+  var ddoc = this;
+  var Mustache = require("lib/mustache");
+  var List = require("vendor/couchapp/lib/list");
+  var path = require("vendor/couchapp/lib/path").init(req);
+  var Atom = require("vendor/couchapp/lib/atom");
+
+  var within = 0;
+       if ( 'within' in req.query ) {
+               within = Number(req.query['within']);
+               log("within: " + within);
+       } else {
+               within = 60 * 60 * 24;  // within the last day.
+       }
+  var ts = (new Date()).getTime()/1000 - within; // convert milliseconds to seconds
+
+  var indexPath = path.list('rpms','rpm-to-host',{descending:true, limit:10});
+  var feedPath = path.list('rpms','rpm-to-host',{descending:true, limit:10, format:"atom"});
+  var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"});
+
+  function check_field(query, field, def) { 
+       var ret = def;
+       if ( field in query ) {
+               ret = query[field];
+               log(field + ": " + ret);
+       }
+       return ret;
+  }
+
+  var path_parts = req.path;
+
+  // The first matching format is sent, so reordering functions changes 
+  // thier priority. In this case HTML is the preferred format, so it comes first.
+  provides("text", function() {
+               var row, first_row = true;
+               var more_props = true;
+               var header;
+               var hostname = check_field(req.query, 'hostname', false);
+               var rpm = check_field(req.query, 'rpm', false);
+               var show_hosts = check_field(req.query, 'show_hosts', false);
+               var show_rpms = check_field(req.query, 'show_rpms', false);
+               while (row = getRow()) {
+                       if ( row.key[0] < ts ) { continue; }
+               } 
+
+               return '\n';
+  });
+  provides("html", function() {
+    var key = "";
+    // render the html head using a template
+       //
+    var stash = {
+      header : {
+           title : 'blah blah blah',
+        index : indexPath,
+        blogName : ddoc.blog.title,
+        feedPath : feedPath,
+        commentsFeed : commentsFeed
+      },
+      scripts : {},
+      db : req.path[0],
+      design : req.path[2],
+      feedPath : feedPath,
+      newPostPath : path.show("edit"),
+      assets : path.asset(),
+         rpmversion : check_field(req.query, 'rpmversion', 'List all'),
+
+      nodes : List.withRows(function(row) {
+        var node = row.value;
+        key = row.key;
+        return {
+          name : key[1],
+          val  : row.value,
+          link : path.list('rpmlist',{rpmversion : key[1], group : true, startkey : [key[1]], endkey : [key[1],{}]}),
+        };
+      }),
+    };
+    return Mustache.to_html(ddoc.templates.rpms, stash, ddoc.templates.partials, List.send);
+  });
+         // link : path.list('rpms','rpm-to-host', {rpmversion : key[1], group : true, startkey : [key[1]], endkey : [key[1],{}]}),
+
+  // if the client requests an atom feed and not html, 
+  // we run this function to generate the feed.
+  provides("atom", function() {    
+    var path = require("vendor/couchapp/lib/path").init(req);
+    var markdown = require("vendor/couchapp/lib/markdown");
+    var textile = require("vendor/textile/textile");
+
+    // we load the first row to find the most recent change date
+    var row = getRow();
+    
+    // generate the feed header
+    var feedHeader = Atom.header({
+      updated : (row ? new Date(row.value.created_at) : new Date()),
+      title : ddoc.blog.title,
+      feed_id : path.absolute(indexPath),
+      feed_link : path.absolute(feedPath),
+    });
+    
+    // send the header to the client
+    send(feedHeader);
+
+    // loop over all rows
+    if (row) {
+      do {
+        if (row.value.format == "markdown") {
+          var html = markdown.encode(row.value.body);
+        } else if (row.value.format == "textile") {
+          var html = textile.encode(row.value.body);
+        } else {
+          var html = Mustache.escape(row.value.html);
+        }
+        // generate the entry for this row
+        var feedEntry = Atom.entry({
+          entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)),
+          title : row.value.title,
+          content : html,
+          updated : new Date(row.value.created_at),
+          author : row.value.author,
+          alternate : path.absolute(path.show('post', row.id))
+        });
+        // send the entry to client
+        send(feedEntry);
+      } while (row = getRow());
+    }
+
+    // close the loop after all rows are rendered
+    return "</feed>";
+  });
+};