1 | //- JavaScript source code
|
2 |
|
3 | //- couch-log-ddoc.js ~~
|
4 | // ~~ (c) SRW, 31 Mar 2013
|
5 | // ~~ last updated 20 Mar 2014
|
6 |
|
7 | (function () {
|
8 | ;
|
9 |
|
10 | // Pragmas
|
11 |
|
12 | /*global exports: false, getRow: false, send: false, sum: false */
|
13 |
|
14 | /*jshint maxparams: 2, quotmark: single, strict: true */
|
15 |
|
16 | /*jslint couch: true, indent: 4, maxlen: 80, nomen: true, unparam: true */
|
17 |
|
18 | /*properties
|
19 | 'as-array', box_frequency, hasOwnProperty, _id, identity, ip,
|
20 | ip_frequency, ips, lists, map, method, reduce, replace, shows, slice,
|
21 | split, stringify, timestamp, updates, url, value, views
|
22 | */
|
23 |
|
24 | // Out-of-scope definitions
|
25 |
|
26 | exports._id = '_design/app';
|
27 |
|
28 | exports.lists = {
|
29 | 'as-array': function () {
|
30 | // This function can be useful for dumping the entire contents of the
|
31 | // database for export into another database:
|
32 | //
|
33 | // $ curl -o couch-dump.json \
|
34 | // localhost:5984/traffic/_design/app/_list/as-array/identity
|
35 | //
|
36 | var first, row;
|
37 | first = true;
|
38 | row = getRow();
|
39 | send('[');
|
40 | while (row !== null) {
|
41 | if (first === true) {
|
42 | first = false;
|
43 | send(JSON.stringify(row.value));
|
44 | } else {
|
45 | send(',' + JSON.stringify(row.value));
|
46 | }
|
47 | row = getRow();
|
48 | }
|
49 | send(']');
|
50 | return;
|
51 | }
|
52 | };
|
53 |
|
54 | exports.shows = {};
|
55 |
|
56 | exports.updates = {};
|
57 |
|
58 | exports.views = {
|
59 |
|
60 | box_frequency: {
|
61 | map: function (doc) {
|
62 | // This function needs documentation.
|
63 | var pattern;
|
64 | if (doc.hasOwnProperty('url')) {
|
65 | pattern = /^\/box\/([\w\-]+)\?/;
|
66 | doc.url.replace(pattern, function (match, box) {
|
67 | // This function needs documentation.
|
68 | emit(box, 1);
|
69 | return;
|
70 | });
|
71 | return;
|
72 | }
|
73 | return;
|
74 | },
|
75 | reduce: function (key, values) {
|
76 | // This function needs documentation.
|
77 | return sum(values);
|
78 | }
|
79 | },
|
80 |
|
81 | identity: {
|
82 | map: function (doc) {
|
83 | // This function needs documentation.
|
84 | if (doc._id.slice(0, 8) !== '_design/') {
|
85 | emit(null, {
|
86 | ip: doc.ip,
|
87 | method: doc.method,
|
88 | timestamp: doc.timestamp,
|
89 | url: doc.url
|
90 | });
|
91 | }
|
92 | return;
|
93 | }
|
94 | },
|
95 |
|
96 | ip_frequency: {
|
97 | map: function (doc) {
|
98 | // This function needs documentation.
|
99 | if (doc.hasOwnProperty('ip')) {
|
100 | emit(doc.ip.split(', ')[0], 1);
|
101 | }
|
102 | return;
|
103 | },
|
104 | reduce: function (key, values) {
|
105 | // This function needs documentation.
|
106 | return sum(values);
|
107 | }
|
108 | }
|
109 |
|
110 | };
|
111 |
|
112 | // That's all, folks!
|
113 |
|
114 | return;
|
115 |
|
116 | }());
|
117 |
|
118 | //- vim:set syntax=javascript:
|