Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mooshu1x2
committed
Jul 22, 2016
1 parent
ab8b9fa
commit 47e4ea1
Showing
14 changed files
with
460 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
from distill import es | ||
from distill.utils.query_builder import QueryBuilder | ||
from flask import jsonify | ||
from elasticsearch import Elasticsearch, TransportError | ||
|
||
class Hist (object): | ||
""" | ||
Distill's statistics package. Apply statistical algorithms to User Ale log data segmented with | ||
Stout. Need to query/filter by session or user id. | ||
""" | ||
|
||
def __init__ (self): | ||
# parse out query | ||
pass | ||
|
||
# @staticmethod | ||
# def filter (app, app_type=None, q=''): | ||
|
||
# field = q.get ("field") if q.get ("field") else "" | ||
# size = q.get ("size") if q.get ("size") else 10 | ||
|
||
# query = { "aggs" : { | ||
# "count_by_type" : { | ||
# "filter" : { "term" : { field : }} | ||
# "terms" : { | ||
# "field" : field, | ||
# "size" : 100 | ||
# } | ||
# } | ||
# } | ||
# } | ||
|
||
# d = {} | ||
# # try: | ||
# response = es.search (index=app, doc_type=app_type, body=query) | ||
# # for tag in response['aggregations']['count_by_type']['buckets']: | ||
# # d [tag ['key']] = tag ['doc_count'] | ||
# # except TransportError as e: | ||
# # d ['error'] = e.info | ||
# # except Exception as e: | ||
# # d ['error'] = str (e) | ||
# # return jsonify (d) | ||
# return jsonify (response) | ||
|
||
@staticmethod | ||
def terms (app, app_type=None, q=''): | ||
""" | ||
Group by field (find all elements ) | ||
""" | ||
field = q.get ("field") if q.get ("field") else "" | ||
segment = q.get ("seg") if q.get ("seg") else "*" | ||
size = q.get ("size") if q.get ("size") else 10000 | ||
numhits = q.get ("numhits") if q.get ("numhits") else 10 | ||
|
||
query = { "aggs" : { | ||
"count_by_type" : { | ||
"terms" : { | ||
"field" : field, | ||
"size" : size # maximum number of keys (unique fields) | ||
}, | ||
"aggs" : { | ||
"top" : { # arbitrary name | ||
"top_hits" : { | ||
"size" : numhits, # number of logs in subgroup | ||
"_source" : { # segment on fields - return only subgroup based on field | ||
"include" : [ | ||
segment | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
d = {} | ||
# try: | ||
response = es.search (index=app, doc_type=app_type, body=query) | ||
# for tag in response['aggregations']['count_by_type']['buckets']: | ||
# d [tag ['key']] = tag ['doc_count'] | ||
# except TransportError as e: | ||
# d ['error'] = e.info | ||
# except Exception as e: | ||
# d ['error'] = str (e) | ||
# return jsonify (d) | ||
return jsonify (response) | ||
|
||
@staticmethod | ||
def unique_terms (app, app_type=None, q=""): | ||
""" | ||
Aggregate the number of unique terms in a field. Missing values are counted and marked as "N/A". | ||
.. todo:: | ||
Need to incorporate QueryBuilder library instead of manually generating queries. | ||
:param app: [string] application name | ||
:param app_type: [string] application type | ||
:param field: [string] field to search against for unique values | ||
:param size: [int] the top size terms returned in the result. Default value is 10. | ||
:param min_hits: [int] return tags which have been found in min_hits or more. Default value is 1. | ||
:return: [dict] dictionary of results | ||
""" | ||
|
||
field = q.get ("field") if q.get ("field") else "" | ||
size = q.get ("size") if q.get ("size") else 10000 | ||
min_hits = q.get ("min_hits") if q.get ("min_hits") else 0 | ||
|
||
print field | ||
query = { "aggs" : { | ||
"terms_agg" : { | ||
"terms" : { | ||
"field" : field, | ||
"size" : size, | ||
"min_doc_count" : min_hits, | ||
"missing" : "N/A" | ||
} | ||
} | ||
} | ||
} | ||
|
||
d = {} | ||
try: | ||
response = es.search (index=app, doc_type=app_type, body=query) | ||
for tag in response['aggregations']['terms_agg']['buckets']: | ||
d [tag ['key']] = tag ['doc_count'] | ||
except TransportError as e: | ||
d ['error'] = e.info | ||
except Exception as e: | ||
d ['error'] = str (e) | ||
return jsonify (d) | ||
|
||
@staticmethod | ||
def histogram (app, app_type=None, q=""): | ||
""" | ||
Only works on numerical data. | ||
""" | ||
field = q.get ("field") if q.get ("field") else "" | ||
|
||
interval = 50 | ||
query = { "aggs" : { | ||
"hist_agg" : { | ||
"histogram" : { | ||
"field" : field, | ||
"interval" : interval | ||
} | ||
} | ||
} | ||
} | ||
|
||
d = {} | ||
try: | ||
response = es.search (index=app, doc_type=app_type, body=query) | ||
for tag in response['aggregations']['hist_agg']['buckets']: | ||
d [tag ['key']] = tag ['doc_count'] | ||
except TransportError as e: | ||
d ['error'] = e.info | ||
except Exception as e: | ||
d ['error'] = str (e) | ||
return jsonify (d) | ||
|
||
def get_value (): | ||
return 0 | ||
|
||
def _parse_msg (query): | ||
# should have form ?measure=name&field=f1, f2&event=a,b | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.