From 590f3fdf766a9ea47e32fcc3b969038be4de2934 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sun, 12 Aug 2018 22:31:07 +0300 Subject: [PATCH] strict any checks --- server/src/config.ts | 2 +- server/src/controllers/alerts_controller.ts | 2 +- server/src/controllers/analytics_controller.ts | 8 ++++---- server/src/models/segment_model.ts | 2 +- server/src/routes/segments_router.ts | 2 +- server/src/services/analytics_service.ts | 8 ++++---- server/src/services/data_service.ts | 18 +++++++++--------- server/src/services/notification_service.ts | 6 +++--- server/src/services/process_service.ts | 6 +++--- server/tsconfig.json | 4 ++-- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/server/src/config.ts b/server/src/config.ts index 41fc9db..6a486f5 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -21,7 +21,7 @@ export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002'); export const ANLYTICS_PING_INTERVAL = 500; // ms -function getConfigField(field, defaultVal?) { +function getConfigField(field: string, defaultVal?: any) { let val = defaultVal; if(process.env[field] !== undefined) { diff --git a/server/src/controllers/alerts_controller.ts b/server/src/controllers/alerts_controller.ts index 0989ae2..0eaba4f 100644 --- a/server/src/controllers/alerts_controller.ts +++ b/server/src/controllers/alerts_controller.ts @@ -2,7 +2,7 @@ * Alarting is not supported yet */ -throw new console.error("Not supported"); +throw new Error('not supported'); // import { runPredict } from './analytics_controller'; diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 00afba3..ad3db56 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -3,7 +3,7 @@ import * as AnalyticUnit from '../models/analytic_unit_model' import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service'; -const taskMap = {}; +const taskMap = new Map(); let nextTaskId = 0; let analyticsService: AnalyticsService = undefined; @@ -14,9 +14,9 @@ function onTaskResult(taskResult: any) { let status = taskResult.status; if(status === 'SUCCESS' || status === 'FAILED') { if(taskId in taskMap) { - let resolver = taskMap[taskId]; + let resolver: any = taskMap.get(taskId); resolver(taskResult); - delete taskMap[taskId]; + taskMap.delete(taskId); } } } @@ -49,7 +49,7 @@ export function terminate() { analyticsService.close(); } -async function runTask(task): Promise { +async function runTask(task: any): Promise { // let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId); // task.metric = { // datasource: anomaly.metric.datasource, diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index be91f16..4abb5f2 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -68,7 +68,7 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis return segs.map(Segment.fromObject); } -export async function insertSegments(id: AnalyticUnitId, segments: Segment[]) { +export async function insertSegments(segments: Segment[]) { return db.insertMany(segments.map(s => s.toObject())); } diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 2194fac..64ff66a 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -27,7 +27,7 @@ async function updateSegments(ctx: Router.IRouterContext) { try { let segmentsUpdate = ctx.request.body; let id = segmentsUpdate.id; - let addedIds = insertSegments(id, segmentsUpdate.addedSegments); + let addedIds = insertSegments(segmentsUpdate.addedSegments); // removeSegments(id, segmentsUpdate.removedSegments); ctx.response.body = { addedIds }; runLearning(id); diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 7686135..940207f 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -29,8 +29,8 @@ export class AnalyticsService { private _requester: any; private _ready: boolean = false; private _pingResponded = false; - private _zmqConnectionString = null; - private _ipcPath = null; + private _zmqConnectionString: string = null; + private _ipcPath: string = null; private _analyticsPinger: NodeJS.Timer = null; private _isClosed = false; @@ -55,7 +55,7 @@ export class AnalyticsService { strMessage = 'PING'; } return new Promise((resolve, reject) => { - this._requester.send(strMessage, undefined, (err) => { + this._requester.send(strMessage, undefined, (err: any) => { if(err) { reject(err); } else { @@ -184,7 +184,7 @@ export class AnalyticsService { } } - private _onAnalyticsMessage(data: any, error) { + private _onAnalyticsMessage(data: any) { if(data.toString() === 'PONG') { this._pingResponded = true; if(!this._ready) { diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index c39da2f..725a69e 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -52,7 +52,7 @@ const db = new Map(); let dbInsertOne = (collection: Collection, doc: object) => { return new Promise((resolve, reject) => { - db[collection].insert(doc, (err, newDoc) => { + db.get(collection).insert(doc, (err, newDoc: any) => { if(err) { reject(err); } else { @@ -64,7 +64,7 @@ let dbInsertOne = (collection: Collection, doc: object) => { let dbInsertMany = (collection: Collection, docs: object[]) => { return new Promise((resolve, reject) => { - db[collection].insert(docs, (err, newDocs: any[]) => { + db.get(collection).insert(docs, (err, newDocs: any[]) => { if(err) { reject(err); } else { @@ -77,7 +77,7 @@ let dbInsertMany = (collection: Collection, docs: object[]) => { let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => { query = wrapIdToQuery(query); return new Promise((resolve, reject) => { - db[collection].update(query, updateQuery, { /* options */ }, (err: Error) => { + db.get(collection).update(query, updateQuery, { /* options */ }, (err: Error) => { if(err) { reject(err); } else { @@ -90,7 +90,7 @@ let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: let dbFindOne = (collection: Collection, query: string | object) => { query = wrapIdToQuery(query); return new Promise((resolve, reject) => { - db[collection].findOne(query, (err, doc) => { + db.get(collection).findOne(query, (err, doc) => { if(err) { reject(err); } else { @@ -103,7 +103,7 @@ let dbFindOne = (collection: Collection, query: string | object) => { let dbFindMany = (collection: Collection, query: string[] | object) => { query = wrapIdsToQuery(query); return new Promise((resolve, reject) => { - db[collection].findOne(query, (err, docs) => { + db.get(collection).findOne(query, (err, docs: any[]) => { if(err) { reject(err); } else { @@ -116,7 +116,7 @@ let dbFindMany = (collection: Collection, query: string[] | object) => { let dbRemoveOne = (collection: Collection, id: string) => { let query = { _id: id }; return new Promise((resolve, reject) => { - db[collection].remove(query, (err, numRemoved) => { + db.get(collection).remove(query, (err, numRemoved) => { if(err) { reject(err); } else { @@ -133,7 +133,7 @@ let dbRemoveOne = (collection: Collection, id: string) => { let dbRemoveMany = (collection: Collection, query: string[] | object) => { query = wrapIdsToQuery(query); return new Promise((resolve, reject) => { - db[collection].remove(query, (err, numRemoved) => { + db.get(collection).remove(query, (err, numRemoved) => { if(err) { reject(err); } else { @@ -161,5 +161,5 @@ function checkDataFolders(): void { checkDataFolders(); // TODO: it's better if models request db which we create if it`s needed -db[Collection.ANALYTIC_UNITS] = new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true }); -db[Collection.SEGMENTS] = new nedb({ filename: config.SEGMENTS_DATABASE_PATH, autoload: true }); +db.set(Collection.ANALYTIC_UNITS, new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true })); +db.set(Collection.SEGMENTS, new nedb({ filename: config.SEGMENTS_DATABASE_PATH, autoload: true })); diff --git a/server/src/services/notification_service.ts b/server/src/services/notification_service.ts index 126ca10..6160ef9 100644 --- a/server/src/services/notification_service.ts +++ b/server/src/services/notification_service.ts @@ -1,11 +1,11 @@ -import { findById } from '../models/analytic_unit_model'; +import { findById, AnalyticUnitId } from '../models/analytic_unit_model'; import axios from 'axios'; // TODO: send notification with payload without dep to AnalyticUnit -export async function sendNotification(predictorId, active) { - let anomalyName = (await findById(predictorId)).name +export async function sendNotification(id: AnalyticUnitId, active: boolean) { + let anomalyName = (await findById(id)).name console.log('Notification ' + anomalyName); let notification = { diff --git a/server/src/services/process_service.ts b/server/src/services/process_service.ts index 6d0a237..b3e882e 100644 --- a/server/src/services/process_service.ts +++ b/server/src/services/process_service.ts @@ -1,6 +1,6 @@ -var exitHandlers = [] +var exitHandlers: (() => void)[] = []; var exitHandled = false; /** @@ -12,7 +12,7 @@ export function registerExitHandler(callback: () => void) { exitHandlers.push(callback); } -function exitHandler(options, err?) { +function exitHandler(options: any, err?: any) { if(exitHandled) { return; } @@ -24,7 +24,7 @@ function exitHandler(options, err?) { process.exit(); } -function catchException(options, err) { +function catchException(options: any, err: any) { console.log('Server exception:'); console.log(err); exitHandler({ exit: true }); diff --git a/server/tsconfig.json b/server/tsconfig.json index e28057c..f51b103 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { "sourceMap": true, - "noImplicitAny": false, + "noImplicitAny": true, "module": "commonjs", "target": "es6", - "allowJs": true + "allowJs": true, } }