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.
36 lines
1.2 KiB
36 lines
1.2 KiB
5 years ago
|
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
|
||
|
}));
|
||
|
}
|
||
|
|