Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / 1
1 function(head, req) {
2   var ddoc = this;
3   var Mustache = require("lib/mustache");
4   var List = require("vendor/couchapp/lib/list");
5   var path = require("vendor/couchapp/lib/path").init(req);
6   var Atom = require("vendor/couchapp/lib/atom");
7
8   var within = 0;
9         if ( 'within' in req.query ) {
10                 within = Number(req.query['within']);
11                 log("within: " + within);
12         } else {
13                 within = 60 * 60 * 24;  // within the last day.
14         }
15   var ts = (new Date()).getTime()/1000 - within; // convert milliseconds to seconds
16
17   var indexPath = path.list('rpms','rpm-to-host',{descending:true, limit:10});
18   var feedPath = path.list('rpms','rpm-to-host',{descending:true, limit:10, format:"atom"});
19   var commentsFeed = path.list('comments','comments',{descending:true, limit:10, format:"atom"});
20
21   function check_field(query, field, def) { 
22         var ret = def;
23         if ( field in query ) {
24                 ret = query[field];
25                 log(field + ": " + ret);
26         }
27         return ret;
28   }
29
30   var path_parts = req.path;
31
32   // The first matching format is sent, so reordering functions changes 
33   // thier priority. In this case HTML is the preferred format, so it comes first.
34   provides("text", function() {
35                 var row, first_row = true;
36                 var more_props = true;
37                 var header;
38                 var hostname = check_field(req.query, 'hostname', false);
39                 var rpm = check_field(req.query, 'rpm', false);
40                 var show_hosts = check_field(req.query, 'show_hosts', false);
41                 var show_rpms = check_field(req.query, 'show_rpms', false);
42                 while (row = getRow()) {
43                         if ( row.key[0] < ts ) { continue; }
44                 } 
45
46                 return '\n';
47   });
48   provides("html", function() {
49     var key = "";
50     // render the html head using a template
51         //
52     var stash = {
53       header : {
54             title : 'blah blah blah',
55         index : indexPath,
56         blogName : ddoc.blog.title,
57         feedPath : feedPath,
58         commentsFeed : commentsFeed
59       },
60       scripts : {},
61       db : req.path[0],
62       design : req.path[2],
63       feedPath : feedPath,
64       newPostPath : path.show("edit"),
65       assets : path.asset(),
66           rpmversion : check_field(req.query, 'rpmversion', 'List all'),
67
68       nodes : List.withRows(function(row) {
69         var node = row.value;
70         key = row.key;
71         return {
72           name : key[1],
73           val  : row.value,
74           link : path.list('rpmlist',{rpmversion : key[1], group : true, startkey : [key[1]], endkey : [key[1],{}]}),
75         };
76       }),
77     };
78     return Mustache.to_html(ddoc.templates.rpms, stash, ddoc.templates.partials, List.send);
79   });
80          // link : path.list('rpms','rpm-to-host', {rpmversion : key[1], group : true, startkey : [key[1]], endkey : [key[1],{}]}),
81
82   // if the client requests an atom feed and not html, 
83   // we run this function to generate the feed.
84   provides("atom", function() {    
85     var path = require("vendor/couchapp/lib/path").init(req);
86     var markdown = require("vendor/couchapp/lib/markdown");
87     var textile = require("vendor/textile/textile");
88
89     // we load the first row to find the most recent change date
90     var row = getRow();
91     
92     // generate the feed header
93     var feedHeader = Atom.header({
94       updated : (row ? new Date(row.value.created_at) : new Date()),
95       title : ddoc.blog.title,
96       feed_id : path.absolute(indexPath),
97       feed_link : path.absolute(feedPath),
98     });
99     
100     // send the header to the client
101     send(feedHeader);
102
103     // loop over all rows
104     if (row) {
105       do {
106         if (row.value.format == "markdown") {
107           var html = markdown.encode(row.value.body);
108         } else if (row.value.format == "textile") {
109           var html = textile.encode(row.value.body);
110         } else {
111           var html = Mustache.escape(row.value.html);
112         }
113         // generate the entry for this row
114         var feedEntry = Atom.entry({
115           entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)),
116           title : row.value.title,
117           content : html,
118           updated : new Date(row.value.created_at),
119           author : row.value.author,
120           alternate : path.absolute(path.show('post', row.id))
121         });
122         // send the entry to client
123         send(feedEntry);
124       } while (row = getRow());
125     }
126
127     // close the loop after all rows are rendered
128     return "</feed>";
129   });
130 };