Attempt to make *hastic* client which works with old version of hastic-server https://code.corpglory.net/hastic/hastic-server
based on new version of hastic client from https://code.corpglory.net/hastic/hastic
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
87 lines
2.4 KiB
2 years ago
|
// TODO: https://github.com/hastic/hastic-grafana-app/blob/c67bd8af140105c36f24c875187929869e48e51e/src/panel/graph_panel/services/analytic_service.ts
|
||
|
|
||
|
import { API_URL } from "@/config";
|
||
|
import axios from 'axios';
|
||
|
|
||
|
import { getGenerator } from '@/utils';
|
||
|
|
||
|
import {
|
||
|
AnalyticUnitType, AnlyticUnitConfig,
|
||
|
PatternConfig, ThresholdConfig, AnomalyConfig
|
||
|
} from "@/types/analytic_units";
|
||
|
import { AnomalyHSR } from "@/types";
|
||
|
import { AnalyticStatus } from "@/store";
|
||
|
|
||
|
import _ from 'lodash';
|
||
|
|
||
|
|
||
|
const ANALYTICS_API_URL = API_URL + "analytics/";
|
||
|
|
||
|
export async function getStatus(): Promise<AnalyticStatus> {
|
||
|
const uri = ANALYTICS_API_URL + `status`;
|
||
|
try {
|
||
|
const res = await axios.get<{ status: string }>(uri);
|
||
|
const data = res.data;
|
||
|
return {
|
||
|
available: true,
|
||
|
message: data.status
|
||
|
};
|
||
|
} catch (e) {
|
||
|
return {
|
||
|
available: false,
|
||
|
message: e.message
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export async function getConfig(): Promise<[AnalyticUnitType, AnlyticUnitConfig]> {
|
||
|
const uri = ANALYTICS_API_URL + `config`;
|
||
|
const res = await axios.get(uri);
|
||
|
|
||
|
const data = res['data'];
|
||
|
|
||
|
let analyticUnitType = AnalyticUnitType.ANOMALY;
|
||
|
let analyticUnitConfig = undefined;
|
||
|
if(data['Pattern'] !== undefined) {
|
||
|
analyticUnitType = AnalyticUnitType.PATTERN;
|
||
|
analyticUnitConfig = data['Pattern'] as PatternConfig
|
||
|
}
|
||
|
if(data['Threshold'] !== undefined) {
|
||
|
analyticUnitType = AnalyticUnitType.THRESHOLD;
|
||
|
analyticUnitConfig = data['Threshold'] as ThresholdConfig
|
||
|
}
|
||
|
if(data['Anomaly'] !== undefined) {
|
||
|
analyticUnitType = AnalyticUnitType.ANOMALY;
|
||
|
analyticUnitConfig = data['Anomaly'] as AnomalyConfig
|
||
|
}
|
||
|
|
||
|
if(analyticUnitConfig === undefined) {
|
||
|
throw new Error("unknows config type" + _.keys(data));
|
||
|
}
|
||
|
|
||
|
return [analyticUnitType, analyticUnitConfig];
|
||
|
}
|
||
|
|
||
|
export async function patchConfig(patchObj: any) {
|
||
|
const uri = ANALYTICS_API_URL + `config`;
|
||
|
await axios.patch(uri, patchObj);
|
||
|
}
|
||
|
|
||
|
export function getStatusGenerator(): AsyncIterableIterator<AnalyticStatus> {
|
||
|
return getGenerator<AnalyticStatus>(100, getStatus);
|
||
|
}
|
||
|
|
||
|
|
||
|
export async function getHSRAnomaly(from: number, to: number): Promise<AnomalyHSR> {
|
||
|
if(from >= to) {
|
||
|
throw new Error("`from` can`t be less than `to`");
|
||
|
}
|
||
|
|
||
|
const uri = ANALYTICS_API_URL + `hsr/?from=${from}&to=${to}`;
|
||
|
const res = await axios.get(uri);
|
||
|
|
||
|
const values = res["data"]["AnomalyHSR"];
|
||
|
|
||
|
return values as AnomalyHSR;
|
||
|
}
|