Skip to content

Commit

Permalink
Merge pull request #339 from datacite/feature_get_metrics
Browse files Browse the repository at this point in the history
Feature get metrics from GraphQL API
  • Loading branch information
kjgarza authored Aug 28, 2019
2 parents ab6ed24 + ebe4455 commit d219ea0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/graphql/types/dataset_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class DatasetType < BaseObject
implements DoiItem
implements MetricInterface

field :usage_reports, DatasetUsageReportConnectionWithMetaType, null: false, description: "Usage reports for this dataset", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/doi_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module DoiItem
field :url, String, null: true, description: "The URL registered for the resource"
field :client, ClientType, null: true, description: "The client account managing this resource"
field :provider, ProviderType, null: true, description: "The provider account managing this resource"

def creators(first: nil)
Array.wrap(object.creators[0...first]).map do |c|
Hashie::Mash.new(
Expand Down
28 changes: 28 additions & 0 deletions app/graphql/types/metric_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module MetricInterface
include GraphQL::Schema::Interface

field :view_count, Integer, null: true, description: "The count of DOI views according to the COUNTER code of Practice"
field :download_count, Integer, null: true, description: "The count of DOI dowloands according to the COUNTER code of Practice"
field :citation_count, Integer, null: true, description: "The count of DOI events that represents citations"

def aggregation_results(**args)
Event.query(nil, doi: doi_from_url(args[:id]), "page[size]": 0, aggregations: "metrics_aggregations", source_id: args[:source_id] || nil).response.aggregations
end

def view_count
meta = aggregation_results(id: object.identifier).views.dois.buckets
meta.first.fetch("total_by_type", {}).fetch("value", nil) if meta.any?
end

def download_count
meta = aggregation_results(id: object.identifier).downloads.dois.buckets
meta.first.fetch("total_by_type", {}).fetch("value", nil) if meta.any?
end

def citation_count
meta = aggregation_results(id: object.identifier).citations.dois.buckets
meta.first.fetch("unique_citations", {}).fetch("value", nil) if meta.any?
end
end
1 change: 1 addition & 0 deletions app/graphql/types/publication_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class PublicationType < BaseObject
implements DoiItem
implements MetricInterface

field :datasets, PublicationDatasetConnectionWithMetaType, null: false, description: "Referenced datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/software_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class SoftwareType < BaseObject
implements DoiItem
implements MetricInterface

field :datasets, SoftwareDatasetConnectionWithMetaType, null: false, description: "Referenced datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
Expand Down
4 changes: 2 additions & 2 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def self.metrics_aggregations
views: {
filter: views_filter,
aggs: { dois: {
terms: { field: 'obj_id', size: 50, min_doc_count: 1 }
terms: { field: 'obj_id', size: 50, min_doc_count: 1} , aggs: { "total_by_type" => { sum: { field: 'total' }}}
}}
},
views_histogram: {
Expand All @@ -264,7 +264,7 @@ def self.metrics_aggregations
downloads: {
filter: downloads_filter,
aggs: { dois: {
terms: { field: 'obj_id', size: 50, min_doc_count: 1 }
terms: { field: 'obj_id', size: 50, min_doc_count: 1} , aggs: { "total_by_type" => { sum: { field: 'total' }}}
}}
},
downloads_histogram: {
Expand Down

0 comments on commit d219ea0

Please sign in to comment.