Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to reduce withOwner:true #145

Open
lpatiny opened this issue Aug 27, 2018 · 1 comment
Open

Allow to reduce withOwner:true #145

lpatiny opened this issue Aug 27, 2018 · 1 comment

Comments

@lpatiny
Copy link
Member

lpatiny commented Aug 27, 2018

While it does not make sense to reduce data that belongs to many different users it would be interesting to still allow the user to decide how he wants to reduce in this case.

I would like to create a view statistics but I don't want that those statistics are available to everybody. So I would like to allow the following code bug currently the only function possible is _count (and if withOwner: true my reduce is ignored.

customViews.statsByOwner = {
    map: function(doc) {
      if (doc.$type !== 'entry') return;
      for (var i = 0; i < doc.$owners.length; i++) {
        emit(['[email protected]', doc.$kind, doc.$owners[i]], doc);
      }
    },
    reduce: function(keys, values, rereduce) {
      if (!rereduce) {
        var result = {
          entries: {
            total: values.length,
            reaction: values.filter(function(value) {
              return value.$kind === 'reaction';
            }).length,
            sample: values.filter(function(value) {
              return value.$kind === 'sample';
            }).length,
            analysisRequest: values.filter(function(value) {
              return value.$kind === 'analysisRequest';
            }).length
          },
          attachments: {
            number: 0,
            size: 0
          }
        };
        values.forEach(function(value) {
          if (value._attachments) {
            var keys = Object.keys(value._attachments);
            for (var i = 0; i < keys.length; i++) {
              result.attachments.number++;
              result.attachments.size += value._attachments[keys[i]].length;
            }
          }
        });
        return result;
      } else {
        var result = values[0];
        for (var i = 1; i < values.length; i++) {
          var value = values[i];
          result.entries.total += value.entries.total;
          result.entries.sample += value.entries.sample;
          result.entries.reaction += value.entries.reaction;
          result.entries.analysisRequest += value.entries.analysisRequest;
          result.attachments.number += value.attachments.number;
          result.attachments.size += value.attachments.size;
        }
        return result;
      }
    },
    designDoc: 'stats',
    withOwner: true
@targos
Copy link
Member

targos commented Aug 27, 2018

It's not just about allowing it. The problem is that for the reduce, you need to take care of the fact that the same document will be emitted multiple times (once for each owner).

@stropitek stropitek removed their assignment Sep 14, 2021
@targos targos removed their assignment Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants