Class: ICFS::CacheElastic

Inherits:
Cache
  • Object
show all
Includes:
Elastic
Defined in:
lib/icfs/cache_elastic.rb

Overview

Implements Cache using Elasticsearch

Constant Summary collapse

ResultsCase =

the Case results fields

{
  caseid: 'caseid',
  template: 'template',
  status: 'status',
  title: 'title',
  tags: 'tags',
}.freeze
DefaultSize =

default page size

25
ResultsEntry =

Entry search results fields

{
  caseid: 'caseid',
  entry: 'entry',
  time: 'time',
  title: 'title',
  tags: 'tags',
  perms: ['perms', :empty],
  action: ['action', :zero],
  indexes: ['index', :size],
  files: ['files', :size],
  stats: ['stats', :size],
}.freeze
ResultsIndex =

Index search results fields

{
  caseid: 'caseid',
  index: 'index',
  title: 'title',
  tags: 'tags',
}.freeze
ResultsLog =

Log search results fields

{
  caseid: 'caseid',
  log: 'log',
  time: 'time',
  user: 'user',
  entry: ['entry', :sub, 'num'].freeze,
  index: ['index', :sub, 'num'].freeze,
  action: ['action', :sub, 'num'].freeze,
  case: ['case', :sub, 'set'].freeze,
  files: ['files_hash', :size].freeze,
}.freeze

Instance Method Summary collapse

Methods included from Elastic

#create

Methods inherited from Cache

#supports

Constructor Details

#initialize(map, es) ⇒ CacheElastic

New instance

Parameters:

  • map (Hash)

    Symbol to String of the indexes. Must provide :case, :log, :entry, :action, :current, and :lock

  • es (Faraday)

    Faraday instance to the Elasticsearch cluster



163
164
165
166
167
168
# File 'lib/icfs/cache_elastic.rb', line 163

def initialize(map, es)
  @map = map
  @es = es
  @name = '%s:%d' % [Socket.gethostname, Process.pid]
  @name.freeze
end

Instance Method Details

#_agg_filter(name, qu, sub) ⇒ Object

filter bucket aggregation



872
873
874
875
876
# File 'lib/icfs/cache_elastic.rb', line 872

def _agg_filter(name, qu, sub)
  ag = { name => { 'filter' => qu } }
  ag[name]['aggs'] = sub if sub
  return ag
end

#_agg_nested(name, field, sub) ⇒ Object

nested bucket aggregation



882
883
884
885
886
# File 'lib/icfs/cache_elastic.rb', line 882

def _agg_nested(name, field, sub)
  ag = { name => { 'nested' => { 'path' => field } } }
  ag[name]['aggs'] = sub if sub
  return ag
end

#_agg_stats(name, field) ⇒ Object

stats metric aggregation



854
855
856
# File 'lib/icfs/cache_elastic.rb', line 854

def _agg_stats(name, field)
  { name => { 'stats' => { 'field' => field } } }
end

#_agg_terms(name, field, sub) ⇒ Object

terms bucket aggregation



862
863
864
865
866
# File 'lib/icfs/cache_elastic.rb', line 862

def _agg_terms(name, field, sub)
  ag = { name => { 'terms' => { 'field' => field } } }
  ag[name]['aggs'] = sub if sub
  return ag
end

#_query_allObject

match all query



256
257
258
# File 'lib/icfs/cache_elastic.rb', line 256

def _query_all()
  { 'match_all' => {} }
end

#_query_bool(must, filter, should, must_not) ⇒ Object

bool query



941
942
943
944
945
946
947
948
949
950
951
952
# File 'lib/icfs/cache_elastic.rb', line 941

def _query_bool(must, filter, should, must_not)
  qu = {}
  qu['must'] = must if(must && !must.empty?)
  qu['filter'] = filter if(filter && !filter.empty?)
  qu['should'] = should if(should && !should.empty?)
  qu['must_not'] = must_not if(must_not && !must_not.empty?)
  if qu.empty?
    return { 'match_all' => {} }
  else
    return { 'bool' => qu }
  end
end

#_query_constant(filter) ⇒ Object

constant score



1020
1021
1022
# File 'lib/icfs/cache_elastic.rb', line 1020

def _query_constant(filter)
  {'constant_score' => { 'filter' => filter } }
end

#_query_exists(field, val) ⇒ Object

Exists query



901
902
903
904
# File 'lib/icfs/cache_elastic.rb', line 901

def _query_exists(field, val)
  return nil if val.nil?
  { 'exists' => { 'field' => field } }
end

#_query_keyw(field, val) ⇒ Object

keyword query



909
910
911
912
913
914
915
916
917
# File 'lib/icfs/cache_elastic.rb', line 909

def _query_keyw(field, val)
  return nil if val.nil?
  if val.is_a?(Array)
    qu = { 'terms' => { field => val } }
  else
    qu = {'term' => { field => val } }
  end
  return qu
end

#_query_match(field, val) ⇒ Object

match query



247
248
249
250
# File 'lib/icfs/cache_elastic.rb', line 247

def _query_match(field, val)
  return nil if !val
  { 'match' => { field => { 'query' => val } } }
end

#_query_nested(field, query) ⇒ Object

Nested query



454
455
456
457
458
459
460
461
# File 'lib/icfs/cache_elastic.rb', line 454

def _query_nested(field, query)
  {
    'nested' => {
      'path' => field,
      'query' => query
    }
  }
end

#_query_prefix(field, val) ⇒ Object

prefix string query



933
934
935
936
# File 'lib/icfs/cache_elastic.rb', line 933

def _query_prefix(field, val)
  return nil if val.nil?
  return { 'prefix' => { field => val } }
end

#_query_term(field, val) ⇒ Object

Term query



892
893
894
895
# File 'lib/icfs/cache_elastic.rb', line 892

def _query_term(field, val)
  return nil if val.nil?
  { 'term' => { field => val } }
end

#_query_times(field, val_gt, val_lt) ⇒ Object

times query



922
923
924
925
926
927
928
# File 'lib/icfs/cache_elastic.rb', line 922

def _query_times(field, val_gt, val_lt)
  return nil if( val_gt.nil? && val_lt.nil? )
  tq = {}
  tq['gt'] = val_gt if val_gt
  tq['lt'] = val_lt if val_lt
  return {'range' => { field => tq } }
end

#action_read(cid, anum) ⇒ String

Read an action

Parameters:

  • cid (String)

    caseid

  • anum (Integer)

    the action number

Returns:

  • (String)

    JSON encoded item



551
552
553
# File 'lib/icfs/cache_elastic.rb', line 551

def action_read(cid, anum)
  _read(:action, '%s.%d' % [cid, anum])
end

#action_search(query) ⇒ Object

Search for actions

Parameters:

  • query (Hash)

    the query



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/icfs/cache_elastic.rb', line 567

def action_search(query)

  # build the query
  task_must = [
    _query_match('tasks.title', query[:title])
  ].compact
  task_filter = [
    _query_term('tasks.assigned', query[:assigned]),
    _query_term('tasks.status', query[:status]),
    _query_term('tasks.flag', query[:flag]),
    _query_times('tasks.time', query[:after], query[:before]),
    _query_term('tasks.tags', query[:tags]),
  ].compact
  must = [
    _query_nested(
      'tasks',
      _query_bool(task_must, task_filter, nil, nil)
    )
  ]
  filter = [
    _query_term('caseid', query[:caseid])
  ].compact
  req = { 'query' => _query_bool(must, filter, nil, nil) }

  # sort
  case query[:sort]
  when 'time_desc'
    srt = 'desc'
  when 'time_asc'
    srt = 'asc'
  else
    srt = query[:title] ? nil : 'desc'
  end
  if srt
    req['sort'] = [
      {
        'tasks.time' => {
          'order' => srt,
          'nested' => {
            'path' => 'tasks',
            'filter' => _query_term(
              'tasks.assigned', query[:assigned])
          }
        }
      },
      { '_id' => { 'order' => 'desc' } }
    ]
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:action] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  return _results(resp, query) do |src|
    tsk = src['tasks'].select{|tk| tk['assigned'] == query[:assigned]}.first
    {
      caseid: src['caseid'],
      action: src['action'],
      status: tsk['status'],
      flag: tsk['flag'],
      title: tsk['title'],
      time: tsk['time'],
      tags: tsk['tags'],
    }
  end
end

#action_tags(query) ⇒ Object

List tags used on action tasks

Parameters:

  • query (Hash)

    the query



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/icfs/cache_elastic.rb', line 1122

def action_tags(query)

  # build the query
  task_filter = [
    _query_term('tasks.assigned', query[:assigned]),
    _query_term('tasks.status', query[:status]),
    _query_term('tasks.flag', query[:flag]),
    _query_times('tasks.time', query[:after], query[:before]),
  ].compact
  qu_filt = _query_bool(nil, task_filter, nil, nil)
  ag = _agg_terms('tags', 'tasks.tags', nil)
  ag = _agg_filter('filt', qu_filt, ag)
  ag = _agg_nested('nest', 'tasks', ag)
  if query[:caseid]
    qu = _query_term('caseid', query[:caseid])
  else
    qu = _query_all()
  end
  req = {
    'query' => qu,
    'aggs' => ag,
    'size' => 0
  }

  # run the search
  url = @map[:action] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  # extract tags
  rh = JSON.parse(resp.body)
  rh = rh['aggregations']['nest']['filt']['tags']['buckets']
  list =  rh.map do |hh|
    {
      object: {
        tag: hh['key'],
        count: hh['doc_count'],
      }
    }
  end

  return {
    query: query,
    list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]}
  }
end

#action_write(cid, anum, item) ⇒ Object

Write an action

Parameters:

  • cid (String)

    caseid

  • anum (Integer)

    the action number

  • item (String)

    JSON encoded item



559
560
561
# File 'lib/icfs/cache_elastic.rb', line 559

def action_write(cid, anum, item)
  _write(:action, '%s.%d' % [cid, anum], item)
end

#case_read(cid) ⇒ String

Read a case

Parameters:

  • cid (String)

    caseid

Returns:

  • (String)

    JSON encoded item



231
232
233
# File 'lib/icfs/cache_elastic.rb', line 231

def case_read(cid)
  _read(:case, cid)
end

#case_search(query) ⇒ Object

Search for cases

Parameters:

  • query (Hash)

    the query



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/icfs/cache_elastic.rb', line 264

def case_search(query)

  # build the query
  must = [
    _query_match('title', query[:title]),
  ].compact
  filter = [
    _query_term('tags', query[:tags]),
    _query_term('status', query[:status]),
    _query_term('template', query[:template]),
  ].compact
  access = [
    _query_term('access.grant', query[:grantee]),
    _query_term('access.perm', query[:perm]),
  ].compact
  unless access.empty?
    qu = (access.size == 1) ? access[0] : _query_bool(nil, access, nil, nil)
    filter << _query_nested('access', qu)
  end
  req = { 'query' => _query_bool(must, filter, nil, nil) }

  # highlight
  hl = {}
  hl['title'] = {} if query[:title]
  req['highlight'] = { 'fields' => hl } unless hl.empty?

  # sort
  unless query[:title]
    req['sort'] = { 'caseid' => 'asc' }
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:case] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  return _results(resp, query, ResultsCase)
end

#case_tags(query) ⇒ Object

Get list of tags for cases

Parameters:

  • query (Hash)

    the query



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/icfs/cache_elastic.rb', line 1070

def case_tags(query)

  # build the query
  filter = [
    _query_term('status', query[:status]),
    _query_term('template', query[:template]),
  ].compact
  access = [
    _query_term('access.grant', query[:grantee]),
    _query_term('access.perm', query[:perm]),
  ].compact
  unless access.empty?
    qu = (access.size == 1) ? access[0] : _query_bool(nil, access, nil, nil)
    filter << _query_nested('access', qu)
  end
  qu = _query_bool(nil, filter, nil, nil)
  ag = _agg_terms('tags', 'tags', nil)
  req = {
    'query' => qu,
    'aggs' => ag,
    'size' => 0
  }

  # run the search
  url = @map[:case] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  # extract tags
  rh = JSON.parse(resp.body)
  rh = rh['aggregations']['tags']['buckets']
  list = rh.map do |hh|
    {
      object: {
        tag: hh['key'],
        count: hh['doc_count'],
      }
    }
  end

  return {
    query: query,
    list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag] }
  }
end

#case_write(cid, item) ⇒ Object

Write a case

Parameters:

  • cid (String)

    caseid

  • item (String)

    JSON encoded item



239
240
241
# File 'lib/icfs/cache_elastic.rb', line 239

def case_write(cid, item)
  _write(:case, cid, item)
end

#current_read(cid) ⇒ String

Read current

Parameters:

  • cid (String)

    caseid

Returns:

  • (String)

    JSON encoded item



215
216
217
# File 'lib/icfs/cache_elastic.rb', line 215

def current_read(cid)
  _read(:current, cid)
end

#current_write(cid, item) ⇒ Object

Write current

Parameters:

  • cid (String)

    caseid

  • item (String)

    JSON encoded item



223
224
225
# File 'lib/icfs/cache_elastic.rb', line 223

def current_write(cid, item)
  _write(:current, cid, item)
end

#entry_read(cid, enum) ⇒ String

Read an entry

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    the entry number

Returns:

  • (String)

    JSON encoded item



438
439
440
# File 'lib/icfs/cache_elastic.rb', line 438

def entry_read(cid, enum)
  _read(:entry, '%s.%d' % [cid, enum])
end

#entry_search(query) ⇒ Object

Search for entries

Parameters:

  • query (Hash)

    the query



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/icfs/cache_elastic.rb', line 467

def entry_search(query)

  # build the query
  must = [
    _query_match('title', query[:title]),
    _query_match('content', query[:content]),
  ].compact
  filter = [
    _query_term('tags', query[:tags]),
    _query_term('caseid', query[:caseid]),
    _query_times('time', query[:after], query[:before]),
    _query_term('action', query[:action]),
    _query_term('index', query[:index]),
  ].compact
  stats = [
    _query_term('stats.name', query[:stat]),
    _query_term('stats.credit', query[:credit]),
  ].compact
  unless stats.empty?
    qu = (stats.size == 1) ? stats[0] : _query_bool(nil, stats, nil, nil)
    filter << _query_nested('stats', qu)
  end
  req = { 'query' => _query_bool(must, filter, nil, nil) }

  # highlight
  hl = {}
  hl['title'] = {} if query[:title]
  hl['content'] = {} if query[:content]
  req['highlight'] = { 'fields' => hl } unless hl.empty?

  # sort
  case query[:sort]
  when 'time_desc'
    req['sort'] = [
      { 'time' => 'desc' },
      { '_id' => 'desc' },
    ]
  when 'time_asc'
    req['sort'] = [
      { 'time' => 'asc' },
      { '_id' => 'desc' },
    ]
  when nil
    if !query[:title] && !query[:content]
     req['sort'] = [
        { 'time' => 'desc' },
        { '_id' => 'desc' },
      ]
    end
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:entry] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  return _results(resp, query, ResultsEntry)
end

#entry_tags(query) ⇒ Object

List tags used on Entries

Parameters:

  • query (Hash)

    the query



1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/icfs/cache_elastic.rb', line 1028

def (query)

  # build the query
  ag = _agg_terms('tags', 'tags', nil)
  qu = _query_term('caseid', query[:caseid])
  qu = _query_constant(qu)
  req = {
    'query' => qu,
    'aggs' => ag,
    'size' => 0
  }

  # run the search
  url = @map[:entry] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  # extract tags
  rh = JSON.parse(resp.body)
  rh = rh['aggregations']['tags']['buckets']
  list = rh.map do |hh|
    {
      object: {
        caseid: query[:caseid],
        tag: hh['key'],
        count: hh['doc_count'],
      }
    }
  end

  return {
    query: query,
    list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]}
  }
end

#entry_write(cid, enum, item) ⇒ Object

Write an entry

Parameters:

  • cid (String)

    caseid

  • enum (Integer)

    the entry number

  • item (String)

    JSON encoded item



446
447
448
# File 'lib/icfs/cache_elastic.rb', line 446

def entry_write(cid, enum, item)
  _write(:entry, '%s.%d' % [cid, enum], item)
end

#index_read(cid, xnum) ⇒ String

Read an Index

Parameters:

  • cid (String)

    caseid

  • xnum (Integer)

    the index number

Returns:

  • (String)

    JSON encoded item



652
653
654
# File 'lib/icfs/cache_elastic.rb', line 652

def index_read(cid, xnum)
  _read(:index, '%s.%d' % [cid, xnum])
end

#index_search(query) ⇒ Object

Search for Indexes

Parameters:

  • query (Hash)

    the query



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/icfs/cache_elastic.rb', line 659

def index_search(query)

  # build the query
  must = [
    _query_match('title', query[:title]),
    _query_match('content', query[:content]),
  ].compact
  filter = [
    _query_term('caseid', query[:caseid]),
    _query_term('tags', query[:tags]),
    _query_prefix('title.raw', query[:prefix]),
  ].compact
  req = { 'query' => _query_bool(must, filter, nil, nil) }

  # highlight
  hl = {}
  hl['title'] = {} if query[:title]
  hl['content'] = {} if query[:content]
  req['highlight'] = { 'fields' => hl } unless hl.empty?

  # sort
  case query[:sort]
  when 'index_asc'
    req['sort'] = [
      { 'index' => 'asc' },
      { '_id' => 'desc' },
    ]
  when 'index_desc'
    req['sort'] = [
      { 'index' => 'desc' },
      { '_id' => 'desc' },
    ]
  when 'title_desc'
    req['sort'] = [
      { 'title.raw' => 'desc' },
      { '_id' => 'desc' },
    ]
  when 'title_asc'
    req['sort'] = [
      { 'title.raw' => 'asc' },
      { '_id' => 'desc' },
    ]
  else
    # default if not a title/content query
    if must.empty?
      req['sort'] = [
        { 'title.raw' => 'asc' },
        { '_id' => 'desc' },
      ]
    end
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:index] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  return _results(resp, query, ResultsIndex)
end

#index_tags(query) ⇒ Object

List tags used in indexes

Parameters:

  • query (Hash)

    the query



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/icfs/cache_elastic.rb', line 737

def index_tags(query)

  # build the query
  ag = _agg_terms('tags', 'tags', nil)
  qu = _query_term('caseid', query[:caseid])
  qu = _query_constant(qu)
  req = {
    'query' => qu,
    'aggs' => ag,
    'size' => 0
  }

  # run the search
  url = @map[:index] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  # extract tags
  rh = JSON.parse(resp.body)
  rh = rh['aggregations']['tags']['buckets']
  list = rh.map do |hh|
    {
      object: {
        caseid: query[:caseid],
        tag: hh['key'],
        count: hh['doc_count'],
      }
    }
  end

  return {
    query: query,
    list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]}
  }
end

#index_write(cid, xnum, item) ⇒ Object

Write an Index

Parameters:

  • cid (String)

    caseid

  • xnum (Integer)

    the index number

  • item (String)

    JSON encoded item



644
645
646
# File 'lib/icfs/cache_elastic.rb', line 644

def index_write(cid, xnum, item)
  _write(:index, '%s.%d' % [cid, xnum], item)
end

#lock_release(cid) ⇒ Object

Release a case lock

Parameters:

  • cid (String)

    caseid



203
204
205
206
207
208
209
# File 'lib/icfs/cache_elastic.rb', line 203

def lock_release(cid)
  url = '%s/_doc/%s' % [@map[:lock], CGI.escape(cid)]
  resp = @es.run_request(:delete, url, '', {})
  if !resp.success?
    raise('Elasticsearch lock release failed: %s' % cid)
  end
end

#lock_take(cid) ⇒ Object

Take a case lock

Parameters:

  • cid (String)

    caseid



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/icfs/cache_elastic.rb', line 180

def lock_take(cid)

  json = '{"client":"%s"}' % @name
  url = '%s/_doc/%s/_create' % [@map[:lock], CGI.escape(cid)]
  head = {'Content-Type' => 'application/json'}.freeze

  # try to take
  tries = 5
  while tries > 0
    resp = @es.run_request(:put, url, json, head)
    return true if resp.success?
    tries = tries - 1
    sleep(0.1)
  end

  # failed to take lock
  raise('Elasticsearch lock take failed: %s' % cid)
end

#log_read(cid, lnum) ⇒ String

Read a log

Parameters:

  • cid (String)

    caseid

  • lnum (Integer)

    the log number

Returns:

  • (String)

    JSON encoded item



779
780
781
# File 'lib/icfs/cache_elastic.rb', line 779

def log_read(cid, lnum)
  _read(:log, '%s.%d' % [cid, lnum])
end

#log_search(query) ⇒ Object

Search for a log

Parameters:

  • query (Hash)

    the query



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/icfs/cache_elastic.rb', line 809

def log_search(query)

  # build the query
  filter = [
    _query_term('caseid', query[:caseid]),
    _query_times('times', query[:after], query[:before]),
    _query_term('user', query[:user]),
    _query_exists('case.set', query[:case_edit]),
    _query_term('entry.num', query[:entry]),
    _query_term('index.num', query[:index]),
    _query_term('action.num', query[:action]),
  ].compact
  req = { 'query' => _query_bool(nil, filter, nil, nil) }

  # sort
  case query[:sort]
  when 'time_desc', nil
    req['sort'] = [
      { 'time' => 'desc' },
      { '_id' => 'desc' },
    ]
  when 'time_asc'
    req['sort'] = [
      { 'time' => 'asc' },
      { '_id' => 'desc' },
    ]
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:log] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  return _results(resp, query, ResultsLog)
end

#log_write(cid, lnum, item) ⇒ Object

Write a log

Parameters:

  • cid (String)

    caseid

  • lnum (Integer)

    the log number

  • item (String)

    JSON encoded item



787
788
789
# File 'lib/icfs/cache_elastic.rb', line 787

def log_write(cid, lnum, item)
  _write(:log, '%s.%d' % [cid, lnum], item)
end

#stats(query) ⇒ Object

Analyze stats

Parameters:

  • query (Hash)

    the query



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/icfs/cache_elastic.rb', line 958

def stats(query)

  # aggs
  ag = _agg_stats('vals', 'stats.value')
  ag = _agg_terms('stats', 'stats.name', ag)
  if query[:credit]
    cd = _query_term('stats.credit', query[:credit])
    ag = _agg_filter('credit', cd, ag)
  end
  ag = _agg_nested('nested', 'stats', ag)

  # build the query
  filt = [
    _query_term('caseid', query[:caseid]),
    _query_times('time', query[:after], query[:before]),
  ].compact
  qu = _query_bool(nil, filt, nil, nil)

  # the request
  req = {
    'query' => qu,
    'aggs' => ag,
    'size' => 0,
  }

  # run the search
  url = @map[:entry] + '/_search'
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json' }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed' if !resp.success?

  # extract stats
  rh = JSON.parse(resp.body)
  if query[:credit]
    rh = rh['aggregations']['nested']['credit']['stats']['buckets']
  else
    rh = rh['aggregations']['nested']['stats']['buckets']
  end
  list = rh.map do |hh|
    {
      object: {
        stat: hh['key'],
        sum: hh['vals']['sum'],
        count: hh['vals']['count'],
        min: hh['vals']['min'],
        max: hh['vals']['max'],
      }
    }
  end

  # return the results
  return {
    query: query,
    list: list
  }
end