def test_prometheus_0(self):

        newUrl = getModelUrl(self.url_prometheus, 'prometheus')
        expected_result = 'http://prometheus-k8s.dev-containers-prometheus-operator-usw2-dev-dev.svc.cluster.local:9090/api/v1/query_range?query=foremast:namespace_app_per_pod%3Acpu_usage_seconds_total_anomaly%7Bnamespace%3D%22dev-containers-foremast-examples-usw2-dev-dev%22%2Capp%3D%22demo%22%7D&start=1553278200&end=1553883000&step=60'
        self.assertEqual(newUrl, expected_result)
 def test_prometheus_1(self):
     newUrl = getModelUrl(self.url_prometheus1, 'prometheus')
     expected_result = 'http://prometheus-k8s.dev-containers-prometheus-operator-usw2-dev-dev.svc.cluster.local:9090/api/v1/query_range?query=foremast:namespace_app_pod_http_server_requests_errors_5xx_anomaly&start=1555013726.658&end=1555027326.658&step=14'
     self.assertEqual(newUrl, expected_result)
    def test_wavefront_0(self):

        newUrl = getModelUrl(self.url_wavefront, 'wavefront')
        expected_result = 'sum(align(60s, mean, ts(custom.iks.foremast.appdynamics.apm.transactions.errors_per_min_anomaly, env=prd and app=qbo-c92 )), app)'

        self.assertEqual(newUrl, expected_result)
def queryData(metricUrl, period, isProphet=False, datasource='prometheus'):
    djson = {}
    ajson = None
    modeDropAnomaly = config.getValueByKey('MODE_DROP_ANOMALY')
    for i in range(RETRY_COUNT):
        try:
            if datasource == 'prometheus':
                if (period != METRIC_PERIOD.HISTORICAL.value):
                    metricUrl = urlEndNow(metricUrl)
                respStr = dorequest(metricUrl)
                djson = json.loads(respStr)
                if period == METRIC_PERIOD.HISTORICAL.value and (
                        modeDropAnomaly is not None
                        and modeDropAnomaly == 'y'):
                    try:
                        modelUrl = getModelUrl(metricUrl, datasource)
                        respStr = dorequest(modelUrl)
                        ajson = json.loads(respStr)
                    except Exception as e1:
                        logger.error(e1.__cause__)

            elif datasource == 'wavefront':
                writeMetricToWaveFront = config.getValueByKey(
                    'METRIC_DESTINATION') == 'wavefront'
                datalist = metricUrl.split("&&")
                if len(datalist) < 4:
                    logger.error("missing wavefront query parameters : " +
                                 metricUrl)
                    return []
                qresult = executeQuery(datalist[0], datalist[1], datalist[2],
                                       datalist[3])
                if period == METRIC_PERIOD.HISTORICAL.value and (
                        modeDropAnomaly is not None
                        and modeDropAnomaly == 'y'):
                    #amonaly result
                    try:
                        aresult = executeQuery(
                            getModelUrl(dequote(datalist[0]), datasource),
                            datalist[1], datalist[2], datalist[3])
                        return convertResponseToMetricInfos(
                            qresult,
                            period,
                            isProphet,
                            aresult,
                            isDestWaveFront=writeMetricToWaveFront)
                    except Exception as e1:
                        logger.error(e1.__cause__)
                return convertResponseToMetricInfos(
                    qresult,
                    period,
                    isProphet,
                    isDestWaveFront=writeMetricToWaveFront)
            break
        except Exception as e:
            logger.error(e.__cause__)
            if i == 2:
                logger.warning("Failed to query  metricUrl: " + metricUrl)
                return []
    ###### TODO remove
    '''
        if period ==  METRIC_PERIOD.HISTORICAL.value :
           djson= getHistoricaljson()
        elif period ==  METRIC_PERIOD.CURRENT.value :
           djson= getCurrentjson()
        else:
           djson= getBaselinejson()
        '''
    if datasource == 'prometheus':
        return convertPromesResponseToMetricInfos(djson, period, isProphet,
                                                  ajson)
    return []