Evgeny Smyshlyaev
6 years ago
committed by
GitHub
13 changed files with 115 additions and 166 deletions
@ -0,0 +1,35 @@ |
|||||||
|
import { Collection, makeDBQ } from '../services/data_service'; |
||||||
|
|
||||||
|
const db = makeDBQ(Collection.ANALYTIC_UNITS); |
||||||
|
|
||||||
|
export async function convertPanelUrlToPanelId() { |
||||||
|
const analyticUnits = await db.findMany({ panelUrl: { $exists: true } }); |
||||||
|
console.log(`Found ${analyticUnits.length} analytic units with panelUrl field`); |
||||||
|
if(analyticUnits.length === 0) { |
||||||
|
console.log('Nothing to migrate'); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const panelUrlRegex = /^(.+)\/d\/(\w+)\/.+panelId=(\d+)/; |
||||||
|
const newPanelUrlRegex = /^(.+)\/dashboard\/(\w+).+panelId=(\d+)/; |
||||||
|
const updatedAnalyticUnits = analyticUnits.map(analyticUnit => { |
||||||
|
const parsedPanelUrl = analyticUnit.panelUrl.match(panelUrlRegex) || analyticUnit.panelUrl.match(newPanelUrlRegex); |
||||||
|
const grafanaUrl = parsedPanelUrl[1]; |
||||||
|
const dashboardId = parsedPanelUrl[2]; |
||||||
|
const oldPanelId = parsedPanelUrl[3]; |
||||||
|
const panelId = `${dashboardId}/${oldPanelId}`; |
||||||
|
|
||||||
|
return { |
||||||
|
_id: analyticUnit._id, |
||||||
|
grafanaUrl, |
||||||
|
panelId |
||||||
|
}; |
||||||
|
}); |
||||||
|
|
||||||
|
console.log(updatedAnalyticUnits); |
||||||
|
await updatedAnalyticUnits.forEach(analyticUnit => db.updateOne(analyticUnit._id, { |
||||||
|
panelUrl: undefined, |
||||||
|
...analyticUnit |
||||||
|
})); |
||||||
|
} |
||||||
|
|
@ -1,67 +0,0 @@ |
|||||||
import { AnalyticUnitId } from './analytic_unit_model'; |
|
||||||
|
|
||||||
import { Collection, makeDBQ } from '../services/data_service'; |
|
||||||
|
|
||||||
let db = makeDBQ(Collection.PANELS); |
|
||||||
|
|
||||||
|
|
||||||
export type PanelId = string; |
|
||||||
|
|
||||||
export class Panel { |
|
||||||
constructor( |
|
||||||
public panelUrl: string, |
|
||||||
public analyticUnits: AnalyticUnitId[], |
|
||||||
public id?: PanelId |
|
||||||
) { |
|
||||||
if(this.panelUrl === undefined) { |
|
||||||
throw new Error('panelUrl is undefined'); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public toObject() { |
|
||||||
return { |
|
||||||
_id: this.id, |
|
||||||
panelUrl: this.panelUrl, |
|
||||||
analyticUnits: this.analyticUnits |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
static fromObject(obj: any): Panel { |
|
||||||
if(obj === undefined) { |
|
||||||
throw new Error('obj is undefined'); |
|
||||||
} |
|
||||||
return new Panel( |
|
||||||
obj.panelUrl, |
|
||||||
obj.analyticUnits, |
|
||||||
obj._id |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
export type FindOneQuery = { |
|
||||||
panelUrl: string |
|
||||||
} |
|
||||||
|
|
||||||
export async function findOne(query: FindOneQuery): Promise<Panel> { |
|
||||||
let panel = await db.findOne(query); |
|
||||||
if(panel === null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
return Panel.fromObject(panel); |
|
||||||
} |
|
||||||
|
|
||||||
export async function insertAnalyticUnit(panelUrl: string, analyticUnitId: AnalyticUnitId) { |
|
||||||
const panel = await db.findOne({ panelUrl }); |
|
||||||
|
|
||||||
return db.updateOne({ panelUrl }, { |
|
||||||
analyticUnits: panel.analyticUnits.concat(analyticUnitId) |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
export async function removeAnalyticUnit(panelUrl: string, analyticUnitId: AnalyticUnitId) { |
|
||||||
const panel = await db.findOne({ panelUrl }); |
|
||||||
|
|
||||||
return db.updateOne({ panelUrl }, { |
|
||||||
analyticUnits: panel.analyticUnits.filter(analyticUnit => analyticUnit !== analyticUnitId) |
|
||||||
}); |
|
||||||
} |
|
@ -1,43 +0,0 @@ |
|||||||
import { AnalyticUnitId } from '../models/analytic_unit_model'; |
|
||||||
import * as Panel from '../models/panel_model'; |
|
||||||
|
|
||||||
import * as Router from 'koa-router'; |
|
||||||
|
|
||||||
|
|
||||||
async function getAnalyticUnits(ctx: Router.IRouterContext) { |
|
||||||
let panelUrl: string = ctx.request.query.panelUrl; |
|
||||||
if(panelUrl === undefined || panelUrl === '') { |
|
||||||
throw new Error('panelUrl is missing'); |
|
||||||
} |
|
||||||
const analyticUnits = await Panel.findOne({ panelUrl }); |
|
||||||
ctx.response.body = { analyticUnits }; |
|
||||||
} |
|
||||||
|
|
||||||
async function addAnalyticUnit(ctx: Router.IRouterContext) { |
|
||||||
let { panelUrl, analyticUnitId } = ctx.request.body as { |
|
||||||
panelUrl: string, analyticUnitId: AnalyticUnitId |
|
||||||
}; |
|
||||||
await Panel.insertAnalyticUnit(panelUrl, analyticUnitId); |
|
||||||
ctx.response.body = { |
|
||||||
code: 200, |
|
||||||
message: 'Success' |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
async function deleteAnalyticUnit(ctx: Router.IRouterContext) { |
|
||||||
let { panelUrl, analyticUnitId } = ctx.request.body as { |
|
||||||
panelUrl: string, analyticUnitId: AnalyticUnitId |
|
||||||
}; |
|
||||||
// TODO: stop task when analytic unit is removed
|
|
||||||
await Panel.removeAnalyticUnit(panelUrl, analyticUnitId); |
|
||||||
ctx.response.body = { |
|
||||||
code: 200, |
|
||||||
message: 'Success' |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
export const router = new Router(); |
|
||||||
|
|
||||||
router.get('/', getAnalyticUnits); |
|
||||||
router.post('/', addAnalyticUnit); |
|
||||||
router.delete('/', deleteAnalyticUnit); |
|
Loading…
Reference in new issue