Browse Source

Init es support.

pull/99/head
cweijan 5 years ago
parent
commit
50c473f671
  1. 1
      package.json
  2. BIN
      resources/icon/es.png
  3. 10
      src/common/constants.ts
  4. 40
      src/model/es/esNode.ts
  5. 26
      src/model/es/indexNode.ts
  6. 16
      src/provider/treeDataProvider.ts
  7. 3
      src/service/connect/connection.ts
  8. 42
      src/service/connect/esConnection.ts
  9. 62
      src/vue/connect/index.vue

1
package.json

@ -918,6 +918,7 @@
"@antv/g2": "^4.0.9",
"@types/pg": "^7.14.7",
"@types/tedious": "^4.0.3",
"axios": "^0.21.1",
"command-exists": "^1.2.9",
"date-format": "^3.0.0",
"deepmerge": "^3.2.0",

BIN
resources/icon/es.png

After

Width: 48  |  Height: 48  |  Size: 699 B

10
src/common/constants.ts

@ -48,12 +48,20 @@ export enum Confirm {
export enum DatabaseType {
MYSQL = "MySQL", PG = "PostgreSQL",
MSSQL = "SqlServer", ORACLE = "Oracle",
ES = "ElasticSearch"
}
export enum ModelType {
/**
* ElasticSearch
*/
ES_CONNECTION = "esConnection",ES_INDEX="esIndex",
/**
* database
*/
CONNECTION = "connection", DATABASE = "database", USER_GROUP = "userGroup", USER = "user",
TABLE = "table", COLUMN = "column", INFO = "info", TABLE_GROUP = "tableGroup",
VIEW = "view", VIEW_GROUP = "viewGroup",SYSTEM_VIEW_GROUP = "systemViewGroup", TRIGGER_GROUP = "triggerGroup", TRIGGER = "trigger",
VIEW = "view", VIEW_GROUP = "viewGroup", SYSTEM_VIEW_GROUP = "systemViewGroup", TRIGGER_GROUP = "triggerGroup", TRIGGER = "trigger",
PROCEDURE_GROUP = "procedureGroup", PROCEDURE = "procedure", FUNCTION_GROUP = "functionGroup", FUNCTION = "function",
QUERY_GROUP = "queryGroup", QUERY = "query",
DIAGRAM_GROUP = "diagramGroup", DIAGRAM = "diagram"

40
src/model/es/esNode.ts

@ -0,0 +1,40 @@
import * as path from "path";
import { Constants, ModelType } from "../../common/constants";
import { ConnectionManager } from "../../service/connectionManager";
import { Node } from "../interface/node";
import axios from "axios";
import { IndexNode } from "./indexNode";
import { InfoNode } from "../other/infoNode";
export class EsNode extends Node {
public iconPath: string = path.join(Constants.RES_PATH, "icon/es.png");
public contextValue: string = ModelType.ES_CONNECTION;
constructor(readonly uid: string, readonly parent: Node) {
super(uid)
this.init(parent)
this.cacheSelf()
const lcp = ConnectionManager.getLastConnectionOption(false);
if (lcp && lcp.getConnectId() == this.getConnectId()) {
this.iconPath = path.join(Constants.RES_PATH, "icon/connection-active.svg");
this.description = `Active`
}
}
async getChildren(): Promise<Node[]> {
return axios.get("http://localhost:9200/_cat/indices").then(res => {
let indexes = [];
const results = res.data.match(/[^\r\n]+/g);
for (const result of results) {
indexes.push(new IndexNode(result, this))
}
return indexes;
}).catch(err => {
return [new InfoNode(err)]
})
}
}

26
src/model/es/indexNode.ts

@ -0,0 +1,26 @@
import * as path from "path";
import { Constants, ModelType } from "@/common/constants";
import { ConnectionManager } from "@/service/connectionManager";
import { Node } from "../interface/node";
export class IndexNode extends Node {
public iconPath: string = path.join(Constants.RES_PATH, "icon/server.png");
public contextValue: string = ModelType.ES_INDEX;
constructor(readonly info: string, readonly parent: Node) {
super(null)
this.init(parent)
const [health, status, index, uuid, pri, rep, docsCount, docsDeleted, storeSize, priStoreSize] = info.split(/\s+/)
this.label = index
const lcp = ConnectionManager.getLastConnectionOption(false);
if (lcp && lcp.getConnectId() == this.getConnectId()) {
this.iconPath = path.join(Constants.RES_PATH, "icon/connection-active.svg");
this.description = `Active`
}
}
}

16
src/provider/treeDataProvider.ts

@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { CacheKey, ConfigKey } from "../common/constants";
import { CacheKey, ConfigKey, DatabaseType } from "../common/constants";
import { ConnectionManager } from "../service/connectionManager";
import { DatabaseCache } from "../service/common/databaseCache";
import { ConnectionNode } from "../model/database/connectionNode";
@ -9,6 +9,8 @@ import { UserGroup } from "../model/database/userGroup";
import { Global } from "../common/global";
import { NodeUtil } from "@/model/nodeUtil";
import { InfoNode } from "@/model/other/infoNode";
import { EsConnection } from "@/service/connect/esConnection";
import { EsNode } from "@/model/es/esNode";
export class DbTreeDataProvider implements vscode.TreeDataProvider<Node> {
@ -66,7 +68,7 @@ export class DbTreeDataProvider implements vscode.TreeDataProvider<Node> {
ConnectionManager.removeConnection(connectId)
await targetContext.update(CacheKey.ConectionsKey, NodeUtil.removeParent(connections));
DbTreeDataProvider.refresh();
}
/**
@ -95,25 +97,19 @@ export class DbTreeDataProvider implements vscode.TreeDataProvider<Node> {
return this.instance;
}
public async getConnectionNodes(): Promise<ConnectionNode[]> {
public async getConnectionNodes(): Promise<Node[]> {
let globalConnections = this.context.globalState.get<{ [key: string]: Node }>(CacheKey.ConectionsKey, {});
let workspaceConnections = this.context.workspaceState.get<{ [key: string]: Node }>(CacheKey.ConectionsKey, {});
const connections = { ...globalConnections, ...workspaceConnections };
// temp duplicate uid solution
const idMap = {};
return Object.keys(connections).map(key => {
const connection = new ConnectionNode(key, connections[key]);
idMap[connection.uid] = true;
const connection = connections[key].dbType == DatabaseType.ES ? new EsNode(key, connections[key]) : new ConnectionNode(key, connections[key]);
if (typeof connections[key].global == "undefined") {
// Compatible with older versions, will remove in the feature
connections[key].global = true;
}
if (idMap[connection.uid]) {
idMap[connection.uid] = idMap[connection.uid] + new Date().getTime()
}
return connection;
})

3
src/service/connect/connection.ts

@ -1,6 +1,7 @@
import { DatabaseType } from "@/common/constants";
import { Node } from "@/model/interface/node";
import { FieldInfo } from "mysql2";
import { EsConnection } from "./esConnection";
import { MSSqlConnnection } from "./mssqlConnection";
import { MysqlConnection } from "./mysqlConnection";
import { PostgreSqlConnection } from "./postgreSqlConnection";
@ -36,6 +37,8 @@ export function create(opt: Node) {
return new MSSqlConnnection(opt)
case DatabaseType.PG:
return new PostgreSqlConnection(opt)
case DatabaseType.ES:
return new EsConnection(opt);
}
return new MysqlConnection(opt)
}

42
src/service/connect/esConnection.ts

@ -0,0 +1,42 @@
import axios from "axios";
import { Node } from "@/model/interface/node";
import { IConnection, queryCallback } from "./connection";
export class EsConnection implements IConnection {
private url: string;
private conneted: boolean;
constructor(opt: Node) {
this.url = `http://${opt.host}:${opt.port}`
}
query(sql: string, callback?: queryCallback): void;
query(sql: string, values: any, callback?: queryCallback): void;
query(sql: any, values?: any, callback?: any) {
throw new Error("Method not implemented.");
}
connect(callback: (err: Error) => void): void {
axios.get(`${this.url}/_cluster/health`).then(res => {
this.conneted = true;
callback(null)
}).catch(err => {
callback(err)
})
}
beginTransaction(callback: (err: Error) => void): void {
throw new Error("Method not implemented.");
}
rollback(): void {
throw new Error("Method not implemented.");
}
commit(): void {
throw new Error("Method not implemented.");
}
end(): void {
}
isAlive(): boolean {
return this.conneted;
}
}

62
src/vue/connect/index.vue

@ -16,9 +16,10 @@
<section class="mb-2">
<label class="block font-bold" for="connection-type">Connection Type</label>
<el-radio v-model="connectionOption.dbType" label="MySQL">MySQL</el-radio>
<el-radio v-model="connectionOption.dbType" label="PostgreSQL">PostgreSQL</el-radio>
<el-radio v-model="connectionOption.dbType" label="SqlServer">SQL Server</el-radio>
<el-radio v-model="connectionOption.dbType" label="MySQL">MySQL</el-radio>
<el-radio v-model="connectionOption.dbType" label="PostgreSQL">PostgreSQL</el-radio>
<el-radio v-model="connectionOption.dbType" label="SqlServer">SQL Server</el-radio>
<el-radio v-model="connectionOption.dbType" label="ElasticSearch">ElasticSearch</el-radio>
</el-select>
</section>
@ -32,25 +33,29 @@
<input class="w-full field__input" id="connection-port" placeholder="The port of connection" required type="number" v-model="connectionOption.port" />
</section>
<section class="mb-2">
<label class="block font-bold" for="connection-user">Username</label>
<input class="w-full field__input" id="connection-user" placeholder="Username" required v-model="connectionOption.user" />
</section>
<template v-if="connectionOption.dbType!='ElasticSearch'">
<section class="mb-2">
<label class="block font-bold" for="connection-password">Password</label>
<input class="w-full field__input" id="connection-password" placeholder="Password" type="password" v-model="connectionOption.password" />
</section>
<section class="mb-2">
<label class="block font-bold" for="connection-user">Username</label>
<input class="w-full field__input" id="connection-user" placeholder="Username" required v-model="connectionOption.user" />
</section>
<section class="mb-2">
<label class="block font-bold" for="databases">Databases</label>
<input class="w-full field__input" id="databases" placeholder="Default is all databases" v-model="connectionOption.database" />
</section>
<section class="mb-2">
<label class="block font-bold" for="connection-password">Password</label>
<input class="w-full field__input" id="connection-password" placeholder="Password" type="password" v-model="connectionOption.password" />
</section>
<section class="mb-2">
<label class="block font-bold" for="timezone">Timezone</label>
<input class="w-full field__input" id="timezone" placeholder="+HH:MM" v-model="connectionOption.timezone" />
</section>
<section class="mb-2">
<label class="block font-bold" for="databases">Databases</label>
<input class="w-full field__input" id="databases" placeholder="Default is all databases" v-model="connectionOption.database" />
</section>
<section class="mb-2">
<label class="block font-bold" for="timezone">Timezone</label>
<input class="w-full field__input" id="timezone" placeholder="+HH:MM" v-model="connectionOption.timezone" />
</section>
</template>
<section class="flex items-center mb-2">
<label class="mr-2 font-bold" for="global">Global</label>
@ -187,20 +192,23 @@ export default {
"connectionOption.dbType"(value) {
switch (value) {
case "MySQL":
this.connectionOption.user='root';
this.connectionOption.port=3306;
this.connectionOption.user = "root"
this.connectionOption.port = 3306
break
case "PostgreSQL":
this.connectionOption.user='postgres';
this.connectionOption.port=5432;
this.connectionOption.user = "postgres"
this.connectionOption.port = 5432
break
case "Oracle":
this.connectionOption.user='system';
this.connectionOption.port=1521;
this.connectionOption.user = "system"
this.connectionOption.port = 1521
break
case "SqlServer":
this.connectionOption.user='sa';
this.connectionOption.port=1433;
this.connectionOption.user = "sa"
this.connectionOption.port = 1433
break
case "ElasticSearch":
this.connectionOption.port = 9200
break
}
},

Loading…
Cancel
Save