Module: DwcOccurrencesHelper

Defined in:
app/helpers/dwc_occurrences_helper.rb

Instance Method Summary collapse

Instance Method Details

#collector_global_id_metadata(dwc_occurrences = nil, project_id = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/dwc_occurrences_helper.rb', line 81

def (dwc_occurrences = nil, project_id = nil)
  records = dwc_occurrences
  project_id ||= sessions_current_project_id

  # Anticipate multiple sources of records in future (e.g. AssertedDistribution)
  records ||= ::DwcOccurrence.where(project_id: project_id)

  a = ::Person
    .left_joins(:identifiers)
    .joins(:dwc_occurrences)
    .order(:cached)
    .merge(records)

  with_global_id = a.where("identifiers.type ILIKE 'Identifier::Global%'").distinct.pluck(:id, :cached)
  without_global_id =  a.where("(identifiers.id is null) OR identifiers.type not ILIKE 'Identifier::Global%'").distinct.pluck(:id, :cached)

  return {
    without_global_id: without_global_id,
    with_global_id: with_global_id
  }
end

#dwc_column(dwc_occurrence) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/dwc_occurrences_helper.rb', line 17

def dwc_column(dwc_occurrence)
  return nil if dwc_occurrence.nil?

  r = []
  dwc_occurrence.dwc_occurrence_object.dwc_occurrence_attributes(false).each do |k, v|
#    CollectionObject::DwcExtensions::DWC_OCCURRENCE_MAP.keys.each do |k|
    next if [:footprintWKT].include?(k)
    if !v.blank?
      r.push tag.tr( (tag.td(k) + tag.td(v)).html_safe )
    end
  end

  r.push tag.tr( (tag.td('basisOfRecord') + tag.td(dwc_occurrence.basisOfRecord)).html_safe )
  tag.table(r.join.html_safe)
end

#dwc_occurrence_health_tag(dwc_occurrence) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/dwc_occurrences_helper.rb', line 3

def dwc_occurrence_health_tag(dwc_occurrence)
  a = [ ]
  if dwc_occurrence.nil?
    a.push tag.span('Darwin Core Occurrence not built', class: [:feedback, 'feedback-thin', 'feedback-danger'])
  else
    a.push tag.span('Referenced data are younger than this record', class: [:feedback, 'feedback-thin', 'feedback-warning']) if dwc_occurrence.is_stale?
    a.push tag.span('A new version of the DwC builder is available', class: [:feedback, 'feedback-thin', 'feedback-warning']) if DateTime.parse(::Export::Dwca::INDEX_VERSION.last).to_i > dwc_occurrence.updated_at.to_i
  end
  a.push tag.span('Up-to-date', class: [:feedback, 'feedback-info']) if a.empty?

  a.push tag.span('TW DwcOccurrence id (internal index/use only): ' + dwc_occurrence.to_param, class: [:feedback, 'feedback-secondary'])
  a.join(' ').html_safe
end

#dwc_occurrences_metadata(dwc_occurrences = nil, project_id: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/dwc_occurrences_helper.rb', line 33

def (dwc_occurrences = nil, project_id: nil)
  records = dwc_occurrences
  project_id ||= sessions_current_project_id

  # Anticipate multiple sources of records in future (e.g. AssertedDistribution)
  collection_objects = CollectionObject.where(project_id: project_id)
  records ||= DwcOccurrence.where(project_id: project_id) # .where(dwc_occurrence_object_type: 'CollectionObject').all
  data = collection_objects.joins(:dwc_occurrence)

  a = {
    health: {
      'Kingdom rank present': TaxonName.where(project_id: project_id).with_rank_class_including('Kingdom').any?,
      'DwcOccurrence records without occurrenceID (on collection objects)': records.where(occurrenceID: nil).count,
      'Total collection objects': collection_objects.count,
      'Collection objects without UUids': collection_objects.count - collection_objects.joins(:identifiers).where("identifiers.type ilike 'Identifier::Global::Uuid%'").distinct.count,
      'Collection objects without DwcOccurrence records': collection_objects.where.missing(:dwc_occurrence).count
    },

    # updated_at takes 'updated_at > ?'  to mean "between the time provided for ? and NOW

    collection_objects: {
      record_total: collection_objects.count,
      enumerated_total: collection_objects.sum(:total),
      freshness: {
        never: collection_objects.where.missing(:dwc_occurrence).count,
        one_day: data.where("dwc_occurrences.updated_at >= ?", 1.day.ago).count,
        one_week: data.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.day.ago, 1.week.ago).count,
        one_month: data.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.week.ago, 1.month.ago).count,
        one_year: data.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.month.ago, 1.year.ago).count,
        greater_than_one_year: data.where("dwc_occurrences.updated_at < ?", 1.year.ago).count
      },
    },

    index: { # the dwc_occurrences
      record_total: records.count,
      enumerated_total: records.sum(:individualCount),
      freshness: {
        never: 0,
        one_day: records.where("dwc_occurrences.updated_at >= ?", 1.day.ago).count,
        one_week: records.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.day.ago, 1.week.ago).count,
        one_month: records.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.week.ago, 1.month.ago).count,
        one_year: records.where("dwc_occurrences.updated_at <= ? and dwc_occurrences.updated_at >= ?", 1.month.ago, 1.year.ago).count,
        greater_than_one_year: records.where("dwc_occurrences.updated_at < ?", 1.year.ago).count
      }
    }
  }
end