1from chart.plotviewer import plot_settings
  2from chart.plotviewer.plot_utils import PlotType, build_request
  3from chart_rest.response_handlers.response_handlers import ResponseMode
  4from chart_rest.response_handlers.correlation.correlation import handle_correlation
  5from chart_rest.response_handlers.limits.limits import handle_limits
  6from chart_rest.response_handlers.histogram.histogram import handle_histogram
  7from chart_rest.response_handlers.bit_histogram.bit_histogram import handle_bit_histogram
  8from chart_rest.response_handlers.csv.csv import handle_csv
  9from chart_rest.response_handlers.single_linear.single_linear import handle_single_linear
 10from chart_rest.response_handlers.textual_bar.textual_bar import handle_textual_bar
 11from chart_rest.response_handlers.geolocation import handle_geolocation
 12
 13
 14def build_pspec(request):
 15    """
 16    Transforms CHART-rest request into a request format
 17    supported by CHART build_request.
 18
 19    Parameters is a comma-separated list of <table>.<field> combinations.
 20    This is transformed into datapoint definition, i.e. dp[index][property].
 21
 22    Domain is defined with format <scid>.<cfid>. Depending on CHART project
 23    the SID argument can be either "scid" or "sid" so both are used.
 24    """
 25    request.GET._mutable = True
 26
 27    if "type" not in request.GET:
 28        request.GET["type"] = "auto"
 29
 30    if "mode" not in request.GET and request.GET["type"] != PlotType.CSV:
 31        request.GET["mode"] = ResponseMode.VEGA.name
 32
 33    if "gridx" not in request.GET:
 34        request.GET["gridx"] = 0
 35
 36    if "gridy" not in request.GET:
 37        request.GET["gridy"] = 0
 38
 39    if "csv_time" not in request.GET and request.GET["type"] == PlotType.CSV:
 40        request.GET["csv_time"] = "UNIX"
 41
 42    if "domain" in request.GET:
 43        parts = request.GET["domain"].split(".")
 44        request.GET["sid"] = parts[0]
 45        request.GET["scid"] = parts[0]
 46
 47        if len(parts) > 1:
 48            request.GET["cfid"] = parts[1]
 49
 50    if "parameters" in request.GET:
 51        parameters = request.GET["parameters"].split(",")
 52        for i in range(0, len(parameters)):
 53            param_features = parameters[i].split("::")
 54
 55            # There are parameter features
 56            if len(param_features) > 1:
 57                for j in range(1, len(param_features)):
 58                    request.GET[f"dp[{i}][" + param_features[j] + "]"] = True
 59
 60            parts = param_features[0].split(".")
 61
 62            # Handle as event if table is EVENTS or EVENT
 63            if parts[0] == "EVENTS" or parts[0] == "EVENT":
 64                request.GET[f"dp[{i}][eclass]"] = parts[1]
 65                # Handle event property
 66                if len(parts) > 2:
 67                    request.GET[f"dp[{i}][prop]"] = parts[2]
 68            else:
 69                request.GET[f"dp[{i}][table]"] = parts[0]
 70                request.GET[f"dp[{i}][field]"] = parts[1]
 71                if len(parts) > 2:
 72                    request.GET[f"dp[{i}][shift]"] = parts[2]
 73
 74    pspec = build_request(request)
 75
 76    # Adding CDAT specific settings here instead of in plot_utils
 77    pspec['show_ool'] = 'ool' in request.GET
 78    pspec['shift_axes'] = 'shift_axes' in request.GET
 79    return pspec
 80
 81
 82def single_linear(pspec):
 83    return handle_single_linear(pspec)
 84
 85def textual_bar(pspec):
 86    return handle_textual_bar(pspec)
 87
 88def correlation(pspec):
 89    return handle_correlation(pspec)
 90
 91
 92def limits(pspec):
 93    return handle_limits(pspec)
 94
 95
 96def histogram(
 97            pspec,
 98            min_val=None,
 99            max_val=None,
100            logscale=False
101            ):
102
103    result = handle_histogram(
104            pspec,
105            min_val=min_val,
106            max_val=max_val,
107            logscale=logscale)
108
109    return result
110
111
112def bit_histogram(pspec):
113    return handle_bit_histogram(pspec)
114
115
116def csv(pspec):
117    return handle_csv(pspec)
118
119def geolocation_map(pspec):
120    return handle_geolocation(pspec)