mirror of
https://github.com/grafana/grafana.git
synced 2025-09-19 04:45:01 +08:00
CLI: Search perf test data (#17422)
* Search perf testdata cli task * Test index * REmoved test index
This commit is contained in:
@ -150,7 +150,7 @@
|
|||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"jest": "jest --notify --watch",
|
"jest": "jest --notify --watch",
|
||||||
"e2e-tests": "jest --runInBand --config=jest.config.e2e.js",
|
"e2e-tests": "jest --runInBand --config=jest.config.e2e.js",
|
||||||
"api-tests": "jest --notify --watch --config=tests/api/jest.js",
|
"api-tests": "jest --notify --watch --config=devenv/e2e-api-tests/jest.js",
|
||||||
"storybook": "cd packages/grafana-ui && yarn storybook",
|
"storybook": "cd packages/grafana-ui && yarn storybook",
|
||||||
"storybook:build": "cd packages/grafana-ui && yarn storybook:build",
|
"storybook:build": "cd packages/grafana-ui && yarn storybook:build",
|
||||||
"themes:generate": "ts-node --project ./scripts/cli/tsconfig.json ./scripts/cli/generateSassVariableFiles.ts",
|
"themes:generate": "ts-node --project ./scripts/cli/tsconfig.json ./scripts/cli/generateSassVariableFiles.ts",
|
||||||
|
@ -7,6 +7,7 @@ import { releaseTask } from './tasks/grafanaui.release';
|
|||||||
import { changelogTask } from './tasks/changelog';
|
import { changelogTask } from './tasks/changelog';
|
||||||
import { cherryPickTask } from './tasks/cherrypick';
|
import { cherryPickTask } from './tasks/cherrypick';
|
||||||
import { precommitTask } from './tasks/precommit';
|
import { precommitTask } from './tasks/precommit';
|
||||||
|
import { searchTestDataSetupTask } from './tasks/searchTestDataSetup';
|
||||||
|
|
||||||
program.option('-d, --depreciate <scripts>', 'Inform about npm script deprecation', v => v.split(','));
|
program.option('-d, --depreciate <scripts>', 'Inform about npm script deprecation', v => v.split(','));
|
||||||
|
|
||||||
@ -72,6 +73,14 @@ program
|
|||||||
await execTask(precommitTask)({});
|
await execTask(precommitTask)({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('searchTestData')
|
||||||
|
.option('-c, --count <number_of_dashboards>', 'Specify number of dashboards')
|
||||||
|
.description('Setup test data for search')
|
||||||
|
.action(async cmd => {
|
||||||
|
await execTask(searchTestDataSetupTask)({ count: cmd.count });
|
||||||
|
});
|
||||||
|
|
||||||
program.parse(process.argv);
|
program.parse(process.argv);
|
||||||
|
|
||||||
if (program.depreciate && program.depreciate.length === 2) {
|
if (program.depreciate && program.depreciate.length === 2) {
|
||||||
|
117
scripts/cli/tasks/searchTestDataSetup.ts
Normal file
117
scripts/cli/tasks/searchTestDataSetup.ts
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import { Task, TaskRunner } from './task';
|
||||||
|
|
||||||
|
interface SearchTestDataSetupOptions {
|
||||||
|
count: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = axios.create({
|
||||||
|
baseURL: 'http://localhost:3000/api',
|
||||||
|
auth: {
|
||||||
|
username: 'admin',
|
||||||
|
password: 'admin2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function getUser(user): Promise<any> {
|
||||||
|
console.log('Creating user ' + user.name);
|
||||||
|
const search = await client.get('/users/search', {
|
||||||
|
params: { query: user.login },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (search.data.totalCount === 1) {
|
||||||
|
user.id = search.data.users[0].id;
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rsp = await client.post('/admin/users', user);
|
||||||
|
user.id = rsp.data.id;
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getTeam(team: any): Promise<any> {
|
||||||
|
// delete if exists
|
||||||
|
const teams = await client.get('/teams/search');
|
||||||
|
for (const existing of teams.data.teams) {
|
||||||
|
if (existing.name === team.name) {
|
||||||
|
console.log('Team exists, deleting');
|
||||||
|
await client.delete('/teams/' + existing.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Creating team ' + team.name);
|
||||||
|
const teamRsp = await client.post(`/teams`, team);
|
||||||
|
team.id = teamRsp.data.teamId;
|
||||||
|
return team;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addToTeam(team: any, user: any): Promise<any> {
|
||||||
|
const members = await client.get(`/teams/${team.id}/members`);
|
||||||
|
console.log(`Adding user ${user.name} to team ${team.name}`);
|
||||||
|
await client.post(`/teams/${team.id}/members`, { userId: user.id });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setDashboardAcl(dashboardId: any, aclList: any) {
|
||||||
|
console.log('Setting Dashboard ACL ' + dashboardId);
|
||||||
|
await client.post(`/dashboards/id/${dashboardId}/permissions`, { items: aclList });
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchTestDataSetupRunnner: TaskRunner<SearchTestDataSetupOptions> = async ({ count }) => {
|
||||||
|
const user1 = await getUser({
|
||||||
|
name: 'searchTestUser1',
|
||||||
|
email: 'searchTestUser@team.com',
|
||||||
|
login: 'searchTestUser1',
|
||||||
|
password: '12345',
|
||||||
|
});
|
||||||
|
|
||||||
|
const team1 = await getTeam({ name: 'searchTestTeam1', email: 'searchtestdata@team.com' });
|
||||||
|
addToTeam(team1, user1);
|
||||||
|
|
||||||
|
// create or update folder
|
||||||
|
const folder: any = {
|
||||||
|
uid: 'search-test-data',
|
||||||
|
title: 'Search test data folder',
|
||||||
|
version: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await client.delete(`/folders/${folder.uid}`);
|
||||||
|
} catch (err) {}
|
||||||
|
|
||||||
|
console.log('Creating folder');
|
||||||
|
|
||||||
|
const rsp = await client.post(`/folders`, folder);
|
||||||
|
folder.id = rsp.data.id;
|
||||||
|
folder.url = rsp.data.url;
|
||||||
|
|
||||||
|
await setDashboardAcl(folder.id, []);
|
||||||
|
|
||||||
|
console.log('Creating dashboards');
|
||||||
|
|
||||||
|
const dashboards: any = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
const dashboard: any = {
|
||||||
|
uid: 'search-test-dash-' + i.toString().padStart(5, '0'),
|
||||||
|
title: 'Search test dash ' + i.toString().padStart(5, '0'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const rsp = await client.post(`/dashboards/db`, {
|
||||||
|
dashboard: dashboard,
|
||||||
|
folderId: folder.id,
|
||||||
|
overwrite: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
dashboard.id = rsp.data.id;
|
||||||
|
dashboard.url = rsp.data.url;
|
||||||
|
|
||||||
|
console.log('Created dashboard ' + dashboard.title);
|
||||||
|
dashboards.push(dashboard);
|
||||||
|
await setDashboardAcl(dashboard.id, [{ userId: 0, teamId: team1.id, permission: 4 }]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const searchTestDataSetupTask = new Task<SearchTestDataSetupOptions>();
|
||||||
|
searchTestDataSetupTask.setName('Search test data setup');
|
||||||
|
searchTestDataSetupTask.setRunner(searchTestDataSetupRunnner);
|
Reference in New Issue
Block a user