Browse Source

Rename modules and types (#860)

pull/1/head
Alexander Velikiy 4 years ago committed by GitHub
parent
commit
f98ac05ad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      analytics/analytics/analytic_types/cache.py
  2. 0
      analytics/analytics/analytic_types/detector.py
  3. 2
      analytics/analytics/analytic_unit_worker.py
  4. 2
      analytics/analytics/detectors/anomaly_detector.py
  5. 2
      analytics/analytics/detectors/detector.py
  6. 2
      analytics/analytics/detectors/pattern_detector.py
  7. 2
      analytics/analytics/detectors/threshold_detector.py
  8. 12
      analytics/analytics/utils/common.py
  9. 4
      analytics/analytics/utils/meta.py
  10. 0
      analytics/scripts/build-dist.sh
  11. 2
      analytics/tests/test_detectors.py
  12. 2
      server/src/index.ts
  13. 2
      server/src/models/analytic_units/db.ts
  14. 4
      server/src/models/grafana_panel_model.ts
  15. 16
      server/src/routes/panel_router.ts
  16. 4
      server/src/services/data_service/index.ts
  17. 2
      server/src/services/data_service/migrations.ts
  18. 8
      server/src/services/grafana_service.ts

2
analytics/analytics/analytic_types/cache.py

@ -1,7 +1,7 @@
from typing import Optional, List, Dict
from analytic_types.segment import AnomalyDetectorSegment
from analytic_types.detector_typing import Bound
from analytic_types.detector import Bound
from utils.meta import JSONClass, SerializableList

0
analytics/analytics/analytic_types/detector_typing.py → analytics/analytics/analytic_types/detector.py

2
analytics/analytics/analytic_unit_worker.py

@ -9,7 +9,7 @@ import utils
from utils import get_intersected_chunks, get_chunks, prepare_data
from analytic_types import ModelCache, TimeSeries
from analytic_types.detector_typing import DetectionResult
from analytic_types.detector import DetectionResult
logger = logging.getLogger('AnalyticUnitWorker')

2
analytics/analytics/detectors/anomaly_detector.py

@ -7,7 +7,7 @@ from typing import Optional, Union, List, Tuple, Generator
import operator
from analytic_types import AnalyticUnitId, ModelCache
from analytic_types.detector_typing import DetectionResult, ProcessingResult, Bound
from analytic_types.detector import DetectionResult, ProcessingResult, Bound
from analytic_types.data_bucket import DataBucket
from analytic_types.segment import Segment, AnomalyDetectorSegment
from analytic_types.cache import AnomalyCache

2
analytics/analytics/detectors/detector.py

@ -3,7 +3,7 @@ from pandas import DataFrame
from typing import Optional, Union, List
from analytic_types import ModelCache, TimeSeries, AnalyticUnitId
from analytic_types.detector_typing import DetectionResult, ProcessingResult
from analytic_types.detector import DetectionResult, ProcessingResult
from analytic_types.segment import Segment

2
analytics/analytics/detectors/pattern_detector.py

@ -11,7 +11,7 @@ from detectors import Detector
from analytic_types.data_bucket import DataBucket
from utils import convert_pd_timestamp_to_ms
from analytic_types import AnalyticUnitId, ModelCache
from analytic_types.detector_typing import DetectionResult
from analytic_types.detector import DetectionResult
from analytic_types.segment import Segment
import utils

2
analytics/analytics/detectors/threshold_detector.py

@ -6,7 +6,7 @@ import numpy as np
from typing import Optional, List
from analytic_types import ModelCache, AnalyticUnitId
from analytic_types.detector_typing import DetectionResult, ProcessingResult
from analytic_types.detector import DetectionResult, ProcessingResult
from analytic_types.segment import Segment
from detectors import ProcessingDetector
from time import time

12
analytics/analytics/utils/common.py

@ -17,6 +17,7 @@ from analytic_types.segment import Segment
SHIFT_FACTOR = 0.05
CONFIDENCE_FACTOR = 0.5
SMOOTHING_FACTOR = 5
MEASUREMENT_ERROR = 0.05
def exponential_smoothing(series: pd.Series, alpha: float, last_smoothed_value: Optional[float] = None) -> pd.Series:
@ -391,7 +392,7 @@ def find_parameters(segment_data: pd.Series, segment_from_index: int, pat_type:
segment = flat_segment.dropna()
segment_median, segment_max_line, segment_min_line = utils.get_distribution_density(segment)
height = 0.95 * (segment_max_line - segment_min_line)
length = utils.find_length(segment_data, segment_min_line, segment_max_line, pat_type)
length = utils.get_pattern_length(segment_data, segment_min_line, segment_max_line, pat_type)
return height, length
def find_pattern_center(segment_data: pd.Series, segment_from_index: int, pattern_type: str):
@ -404,14 +405,15 @@ def find_pattern_center(segment_data: pd.Series, segment_from_index: int, patter
segment_cent_index = math.ceil((len(segment_data)) / 2)
return segment_cent_index
def find_length(segment_data: pd.Series, segment_min_line: float, segment_max_line: float, pat_type: str) -> int:
x_abscissa = np.arange(0, len(segment_data))
def get_pattern_length(segment_data: pd.Series, segment_min_line: float, segment_max_line: float, pat_type: str) -> int:
# TODO: move function to jump & drop merged model
segment_max = max(segment_data)
segment_min = min(segment_data)
# TODO: use better way
if segment_min_line <= segment_min:
segment_min_line = segment_min * 1.05
segment_min_line = segment_min * (1 + MEASUREMENT_ERROR)
if segment_max_line >= segment_max:
segment_max_line = segment_max * 0.95
segment_max_line = segment_max * (1 - MEASUREMENT_ERROR)
min_line = []
max_line = []
for i in range(len(segment_data)):

4
analytics/analytics/utils/meta.py

@ -1,6 +1,6 @@
from inspect import signature, Parameter
from functools import wraps
from typing import Optional
from typing import Optional, List
import re
@ -76,6 +76,6 @@ def JSONClass(target_class):
target_class.from_json = from_json
return target_class
class SerializableList(list):
class SerializableList(List[dict]):
def to_json(self):
return list(map(lambda s: s.to_json(), self))

0
analytics/scripts/build_dist.sh → analytics/scripts/build-dist.sh

2
analytics/tests/test_detectors.py

@ -2,7 +2,7 @@ import unittest
import pandas as pd
from detectors import pattern_detector, threshold_detector, anomaly_detector
from analytic_types.detector_typing import DetectionResult, ProcessingResult, Bound
from analytic_types.detector import DetectionResult, ProcessingResult, Bound
from analytic_types.segment import Segment
from tests.test_dataset import create_dataframe, create_list_of_timestamps
from utils import convert_pd_timestamp_to_ms

2
server/src/index.ts

@ -10,7 +10,7 @@ import * as ProcessService from './services/process_service';
import { HASTIC_PORT, PACKAGE_VERSION, GIT_INFO, ZMQ_CONNECTION_STRING, HASTIC_INSTANCE_NAME } from './config';
import { applyDBMigrations } from './migrations';
import { applyDBMigrations } from './services/data_service/migrations';
import * as Koa from 'koa';
import * as Router from 'koa-router';

2
server/src/models/analytic_units/db.ts

@ -41,7 +41,7 @@ export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> {
return db.insertOne(obj);
}
export async function insertMany(analyticUnits: any[]): Promise<AnalyticUnitId[]> {
export async function insertMany(analyticUnits: AnalyticUnit[]): Promise<AnalyticUnitId[]> {
return db.insertMany(analyticUnits);
}

4
server/src/models/panel_model.ts → server/src/models/grafana_panel_model.ts

@ -3,14 +3,14 @@ import * as AnalyticUnitCache from '../models/analytic_unit_cache_model';
import * as DetectionSpan from '../models/detection_model';
import * as Segment from '../models/segment_model';
export type PanelTemplate = {
export type GrafanaPanelTemplate = {
analyticUnits: AnalyticUnit.AnalyticUnit[],
caches: AnalyticUnitCache.AnalyticUnitCache[],
detectionSpans: DetectionSpan.DetectionSpan[],
segments: Segment.Segment[]
}
export type TemplateVariables = {
export type GrafanaTemplateVariables = {
grafanaUrl: string,
panelId: string,
datasourceUrl: string

16
server/src/routes/panel_router.ts

@ -1,10 +1,10 @@
import { PanelTemplate, TemplateVariables } from '../models/panel_model';
import { exportPanel, importPanel } from '../services/export_service';
import { GrafanaPanelTemplate, GrafanaTemplateVariables } from '../models/grafana_panel_model';
import { exportPanel, importPanel } from '../services/grafana_service';
import * as Router from 'koa-router';
async function exportPanelTemplate(ctx: Router.IRouterContext) {
async function exportGrafanaPanelTemplate(ctx: Router.IRouterContext) {
let panelId = ctx.request.query.panelId;
if(panelId === undefined) {
throw new Error('Cannot export analytic units with undefined panelId');
@ -14,10 +14,10 @@ async function exportPanelTemplate(ctx: Router.IRouterContext) {
ctx.response.body = panelTemplate;
}
async function importPanelTemplate(ctx: Router.IRouterContext) {
async function importGrafanaPanelTemplate(ctx: Router.IRouterContext) {
const { panelTemplate, templateVariables } = ctx.request.body as {
panelTemplate: PanelTemplate,
templateVariables: TemplateVariables
panelTemplate: GrafanaPanelTemplate,
templateVariables: GrafanaTemplateVariables
};
// TODO: move to model
@ -50,5 +50,5 @@ async function importPanelTemplate(ctx: Router.IRouterContext) {
export var router = new Router();
router.get('/template', exportPanelTemplate);
router.post('/template', importPanelTemplate);
router.get('/template', exportGrafanaPanelTemplate);
router.post('/template', importGrafanaPanelTemplate);

4
server/src/services/data_service.ts → server/src/services/data_service/index.ts

@ -1,5 +1,5 @@
import { getDbQueryWrapper, dbCollection, DBType } from './data_layer';
import * as config from '../config';
import { getDbQueryWrapper, dbCollection, DBType } from '../data_layer';
import * as config from '../../config';
import * as nedb from 'nedb';
import * as fs from 'fs';

2
server/src/migrations.ts → server/src/services/data_service/migrations.ts

@ -7,7 +7,7 @@
Note: do not import code from other modules here because it can be changed
*/
import { Collection, makeDBQ } from './services/data_service';
import { Collection, makeDBQ } from './index';
import * as _ from 'lodash';

8
server/src/services/export_service.ts → server/src/services/grafana_service.ts

@ -1,4 +1,4 @@
import { PanelTemplate, TemplateVariables } from '../models/panel_model';
import { GrafanaPanelTemplate, GrafanaTemplateVariables } from '../models/grafana_panel_model';
import * as AnalyticUnit from '../models/analytic_units';
import * as AnalyticUnitCache from '../models/analytic_unit_cache_model';
@ -6,7 +6,7 @@ import * as DetectionSpan from '../models/detection_model';
import * as Segment from '../models/segment_model';
export async function exportPanel(panelId: string): Promise<PanelTemplate> {
export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate> {
const analyticUnits = await AnalyticUnit.findMany({ panelId });
const analyticUnitIds = analyticUnits.map(analyticUnit => analyticUnit.id);
const analyticUnitTemplates = analyticUnits.map(analyticUnit => analyticUnit.toTemplate());
@ -26,8 +26,8 @@ export async function exportPanel(panelId: string): Promise<PanelTemplate> {
}
export async function importPanel(
panelTemplate: PanelTemplate,
variables: TemplateVariables
panelTemplate: GrafanaPanelTemplate,
variables: GrafanaTemplateVariables
): Promise<void> {
panelTemplate.analyticUnits.forEach(analyticUnit => {
analyticUnit.grafanaUrl = variables.grafanaUrl;
Loading…
Cancel
Save