Repository: google/timesketch

Issue:
IndexError: cannot do a non-empty take from an empty axes. (when domain_count_array is empty)
I don't know why domain_count_array is empty but in this case an error is raised:
```
[2019-05-15 15:57:25,067: ERROR/ForkPoolWorker-1] Task timesketch.lib.tasks.run_sketch_analyzer[87c2fee5-d10c-4a92-8d28-a6acc970a7fe] raised unexpected: IndexError('cannot do a non-empty take from an empty axes.',)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/celery/app/trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/timesketch-20190207-py3.6.egg/timesketch/__init__.py", line 181, in __call__
    return TaskBase.__call__(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/celery/app/trace.py", line 648, in __protected_call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/timesketch-20190207-py3.6.egg/timesketch/lib/tasks.py", line 334, in run_sketch_analyzer
    result = analyzer.run_wrapper()
  File "/usr/local/lib/python3.6/dist-packages/timesketch-20190207-py3.6.egg/timesketch/lib/analyzers/interface.py", line 37, in wrapper
    func_return = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/timesketch-20190207-py3.6.egg/timesketch/lib/analyzers/interface.py", line 403, in run_wrapper
    result = self.run()
  File "/usr/local/lib/python3.6/dist-packages/timesketch-20190207-py3.6.egg/timesketch/lib/analyzers/domain.py", line 71, in run
    domain_20th_percentile = int(numpy.percentile(domain_count_array, 20))
  File "/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py", line 3707, in percentile
    a, q, axis, out, overwrite_input, interpolation, keepdims)
  File "/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py", line 3826, in _quantile_unchecked
    interpolation=interpolation)
  File "/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py", line 3405, in _ureduce
    r = func(a, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py", line 3941, in _quantile_ureduce_func
    x1 = take(ap, indices_below, axis=axis) * weights_below
  File "/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py", line 189, in take
    return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
  File "/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py", line 56, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)
IndexError: cannot do a non-empty take from an empty axes.```

Should I add something here https://github.com/google/timesketch/blob/7244f821b9c257d42402115f6a39cab266f0a84c/timesketch/lib/analyzers/domain.py#L70
in order to set at 0 for example in case domain_count_array returns empty?



Discussion:

Resolving patch:
diff --git a/timesketch/lib/analyzers/domain.py b/timesketch/lib/analyzers/domain.py
--- a/timesketch/lib/analyzers/domain.py
+++ b/timesketch/lib/analyzers/domain.py
@@ -67,6 +67,10 @@ def run(self):
             tld = '.'.join(domain.split('.')[-2:])
             tld_counter[tld] += 1
 
+        # Exit early if there are no domains in the data set to analyze.
+        if not domain_counter:
+            return 'No domains to analyze.'
+
         domain_count_array = numpy.array(list(domain_counter.values()))
         domain_20th_percentile = int(numpy.percentile(domain_count_array, 20))
         domain_85th_percentile = int(numpy.percentile(domain_count_array, 85))


Test patch: