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'.freeze,
  template: 'template'.freeze,
  status: 'status'.freeze,
  title: 'title'.freeze,
  tags: 'tags'.freeze,
}.freeze
DefaultSize =

default page size

25
ResultsEntry =

Entry search results fields

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

Index search results fields

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

Log search results fields

{
  caseid: 'caseid'.freeze,
  log: 'log'.freeze,
  time: 'time'.freeze,
  user: 'user'.freeze,
  entry: ['entry'.freeze, :sub, 'num'.freeze].freeze,
  index: ['index'.freeze, :sub, 'num'.freeze].freeze,
  action: ['action'.freeze, :sub, 'num'.freeze].freeze,
  files: ['files_hash'.freeze, :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



155
156
157
158
159
160
# File 'lib/icfs/cache_elastic.rb', line 155

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



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

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



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

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



844
845
846
# File 'lib/icfs/cache_elastic.rb', line 844

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

#_agg_terms(name, field, sub) ⇒ Object

terms bucket aggregation



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

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



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

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

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

bool query



931
932
933
934
935
936
937
938
939
940
941
942
# File 'lib/icfs/cache_elastic.rb', line 931

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



1010
1011
1012
# File 'lib/icfs/cache_elastic.rb', line 1010

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

#_query_exists(field, val) ⇒ Object

Exists query



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

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

#_query_keyw(field, val) ⇒ Object

keyword query



899
900
901
902
903
904
905
906
907
# File 'lib/icfs/cache_elastic.rb', line 899

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



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

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

#_query_nested(field, query) ⇒ Object

Nested query



446
447
448
449
450
451
452
453
# File 'lib/icfs/cache_elastic.rb', line 446

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

#_query_prefix(field, val) ⇒ Object

prefix string query



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

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

#_query_term(field, val) ⇒ Object

Term query



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

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

#_query_times(field, val_gt, val_lt) ⇒ Object

times query



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

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



543
544
545
# File 'lib/icfs/cache_elastic.rb', line 543

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

#action_search(query) ⇒ Object

Search for actions

Parameters:

  • query (Hash)

    the query



559
560
561
562
563
564
565
566
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
# File 'lib/icfs/cache_elastic.rb', line 559

def action_search(query)

  # build the query
  task_must = [
    _query_match('tasks.title'.freeze, query[:title])
  ].compact
  task_filter = [
    _query_term('tasks.assigned'.freeze, query[:assigned]),
    _query_term('tasks.status'.freeze, query[:status]),
    _query_term('tasks.flag'.freeze, query[:flag]),
    _query_times('tasks.time'.freeze, query[:after], query[:before]),
    _query_term('tasks.tags'.freeze, query[:tags]),
  ].compact
  must = [
    _query_nested(
      'tasks'.freeze,
      _query_bool(task_must, task_filter, nil, nil)
    )
  ]
  filter = [
    _query_term('caseid'.freeze, 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'.freeze,
            'filter' => _query_term(
              'tasks.assigned'.freeze, query[:assigned])
          }
        }
      },
      { '_id' => { 'order' => 'desc' } }
    ]
  end

  # paging
  _page(query, req)

  # run the search
  url = @map[:action] + '/_search'.freeze
  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



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
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
# File 'lib/icfs/cache_elastic.rb', line 1112

def action_tags(query)

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

  # run the search
  url = @map[:action] + '/_search'.freeze
  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



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

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

#case_read(cid) ⇒ String

Read a case

Parameters:

  • cid (String)

    caseid

Returns:

  • (String)

    JSON encoded item



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

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

#case_search(query) ⇒ Object

Search for cases

Parameters:

  • query (Hash)

    the query



256
257
258
259
260
261
262
263
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
# File 'lib/icfs/cache_elastic.rb', line 256

def case_search(query)

  # build the query
  must = [
    _query_match('title'.freeze, query[:title]),
  ].compact
  filter = [
    _query_term('tags'.freeze, query[:tags]),
    _query_term('status'.freeze, query[:status]),
    _query_term('template'.freeze, query[:template]),
  ].compact
  access = [
    _query_term('access.grant'.freeze, query[:grantee]),
    _query_term('access.perm'.freeze, query[:perm]),
  ].compact
  unless access.empty?
    qu = (access.size == 1) ? access[0] : _query_bool(nil, access, nil, nil)
    filter << _query_nested('access'.freeze, 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'.freeze
  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



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
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
# File 'lib/icfs/cache_elastic.rb', line 1060

def case_tags(query)

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

  # run the search
  url = @map[:case] + '/_search'.freeze
  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



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

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



207
208
209
# File 'lib/icfs/cache_elastic.rb', line 207

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

#current_write(cid, item) ⇒ Object

Write current

Parameters:

  • cid (String)

    caseid

  • item (String)

    JSON encoded item



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

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



430
431
432
# File 'lib/icfs/cache_elastic.rb', line 430

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

#entry_search(query) ⇒ Object

Search for entries

Parameters:

  • query (Hash)

    the query



459
460
461
462
463
464
465
466
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
# File 'lib/icfs/cache_elastic.rb', line 459

def entry_search(query)

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



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
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
# File 'lib/icfs/cache_elastic.rb', line 1018

def (query)

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

  # run the search
  url = @map[:entry] + '/_search'.freeze
  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



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

def entry_write(cid, enum, item)
  _write(:entry, '%s.%d'.freeze % [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



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

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

#index_search(query) ⇒ Object

Search for Indexes

Parameters:

  • query (Hash)

    the query



651
652
653
654
655
656
657
658
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
# File 'lib/icfs/cache_elastic.rb', line 651

def index_search(query)

  # build the query
  must = [
    _query_match('title'.freeze, query[:title]),
    _query_match('content'.freeze, query[:content]),
  ].compact
  filter = [
    _query_term('caseid'.freeze, query[:caseid]),
    _query_term('tags'.freeze, query[:tags]),
    _query_prefix('title.raw'.freeze, 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'.freeze
  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



729
730
731
732
733
734
735
736
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
# File 'lib/icfs/cache_elastic.rb', line 729

def index_tags(query)

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

  # run the search
  url = @map[:index] + '/_search'.freeze
  body = JSON.generate(req)
  head = { 'Content-Type' => 'application/json'.freeze }
  resp = @es.run_request(:get, url, body, head)
  raise 'search failed'.freeze 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



636
637
638
# File 'lib/icfs/cache_elastic.rb', line 636

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

#lock_release(cid) ⇒ Object

Release a case lock

Parameters:

  • cid (String)

    caseid



195
196
197
198
199
200
201
# File 'lib/icfs/cache_elastic.rb', line 195

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

#lock_take(cid) ⇒ Object

Take a case lock

Parameters:

  • cid (String)

    caseid



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/icfs/cache_elastic.rb', line 172

def lock_take(cid)

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

#log_read(cid, lnum) ⇒ String

Read a log

Parameters:

  • cid (String)

    caseid

  • lnum (Integer)

    the log number

Returns:

  • (String)

    JSON encoded item



771
772
773
# File 'lib/icfs/cache_elastic.rb', line 771

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

#log_search(query) ⇒ Object

Search for a log

Parameters:

  • query (Hash)

    the query



800
801
802
803
804
805
806
807
808
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
# File 'lib/icfs/cache_elastic.rb', line 800

def log_search(query)

  # build the query
  filter = [
    _query_term('caseid'.freeze, query[:caseid]),
    _query_times('times'.freeze, query[:after], query[:before]),
    _query_term('user'.freeze, query[:user]),
    _query_term('entry.num'.freeze, query[:entry]),
    _query_term('index.num'.freeze, query[:index]),
    _query_term('action.num'.freeze, 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'.freeze
  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



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

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

#stats(query) ⇒ Object

Analyze stats

Parameters:

  • query (Hash)

    the query



948
949
950
951
952
953
954
955
956
957
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
# File 'lib/icfs/cache_elastic.rb', line 948

def stats(query)

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

  # build the query
  filt = [
    _query_term('caseid'.freeze, query[:caseid]),
    _query_times('time'.freeze, 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'.freeze
  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