Add scripts to create myops-getqueryview:
[myops.git] / web / query / lists / rpms.js
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('rpms','rpm-to-host', {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
81   // if the client requests an atom feed and not html, 
82   // we run this function to generate the feed.
83   provides("atom", function() {    
84     var path = require("vendor/couchapp/lib/path").init(req);
85     var markdown = require("vendor/couchapp/lib/markdown");
86     var textile = require("vendor/textile/textile");
87
88     // we load the first row to find the most recent change date
89     var row = getRow();
90     
91     // generate the feed header
92     var feedHeader = Atom.header({
93       updated : (row ? new Date(row.value.created_at) : new Date()),
94       title : ddoc.blog.title,
95       feed_id : path.absolute(indexPath),
96       feed_link : path.absolute(feedPath),
97     });
98     
99     // send the header to the client
100     send(feedHeader);
101
102     // loop over all rows
103     if (row) {
104       do {
105         if (row.value.format == "markdown") {
106           var html = markdown.encode(row.value.body);
107         } else if (row.value.format == "textile") {
108           var html = textile.encode(row.value.body);
109         } else {
110           var html = Mustache.escape(row.value.html);
111         }
112         // generate the entry for this row
113         var feedEntry = Atom.entry({
114           entry_id : path.absolute('/'+encodeURIComponent(req.info.db_name)+'/'+encodeURIComponent(row.id)),
115           title : row.value.title,
116           content : html,
117           updated : new Date(row.value.created_at),
118           author : row.value.author,
119           alternate : path.absolute(path.show('post', row.id))
120         });
121         // send the entry to client
122         send(feedEntry);
123       } while (row = getRow());
124     }
125
126     // close the loop after all rows are rendered
127     return "</feed>";
128   });
129 };