Skip to content

Commit

Permalink
Merge pull request #212 from codefori/feature/group_history_by_date
Browse files Browse the repository at this point in the history
List query history by date
  • Loading branch information
worksofliam authored Mar 26, 2024
2 parents 2929931 + 521737b commit ad2f9d1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
},
{
"id": "queryHistory",
"name": "Query History",
"name": "Statement History",
"visibility": "visible",
"when": "code-for-ibmi:connected == true"
},
Expand All @@ -240,7 +240,7 @@
"viewsWelcome": [
{
"view": "queryHistory",
"contents": "Query history will appear here."
"contents": "Statement history will appear here."
},
{
"view": "jobManager",
Expand Down Expand Up @@ -413,13 +413,13 @@
},
{
"command": "vscode-db2i.queryHistory.remove",
"title": "Remove query from history",
"title": "Remove statement from history",
"category": "Db2 for i",
"icon": "$(trash)"
},
{
"command": "vscode-db2i.queryHistory.clear",
"title": "Clear query history",
"title": "Clear statement history",
"category": "Db2 for i",
"icon": "$(trash)"
},
Expand Down
25 changes: 24 additions & 1 deletion src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import vscode from 'vscode';
const QUERIES_KEY = `queries`;
const SERVERCOMPONENT_KEY = `serverVersion`

export type QueryList = string[];
export interface QueryHistoryItem {
query: string;
unix: number;
}

export type QueryList = QueryHistoryItem[];

abstract class Storage {
protected readonly globalState;
Expand Down Expand Up @@ -54,6 +59,24 @@ export class ConnectionStorage extends Storage {
return this.set(SERVERCOMPONENT_KEY, name);
}

/**
* Eventually we will want to remove this function, but for now we need to fix the past queries
*/
fixPastQueries() {
const currentList = this.getPastQueries() as (string|QueryHistoryItem)[];
const hasOldFormat = currentList.some(item => typeof item === `string`);
if (hasOldFormat) {
const newList = currentList.map(item => {
if (typeof item === `string`) {
return { query: item, unix: Math.floor(Date.now() / 1000) - 86400 };
} else {
return item;
}
});
return this.setPastQueries(newList);
}
}

getPastQueries() {
return this.get<QueryList>(QUERIES_KEY) || [];
}
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export async function onConnectOrServerInstall(): Promise<boolean> {
Config.setConnectionName(instance.getConnection().currentConnectionName);
determineFeatures();

await Config.fixPastQueries();

await ServerComponent.initialise().then(installed => {
if (installed) {
JobManagerView.setVisible(true);
Expand Down
83 changes: 76 additions & 7 deletions src/views/queryHistoryView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import vscode, { MarkdownString, ThemeIcon, TreeItem, window, workspace } from "vscode";
import { TreeDataProvider } from "vscode";
import { Config } from "../config";
import { QueryHistoryItem } from "../Storage";

const openSqlDocumentCommand = `vscode-db2i.openSqlDocument`;

Expand All @@ -22,7 +23,7 @@ export class queryHistory implements TreeDataProvider<any> {
vscode.commands.registerCommand(`vscode-db2i.queryHistory.prepend`, async (newQuery?: string) => {
if (newQuery && Config.ready) {
let currentList = Config.getPastQueries();
const existingQuery = currentList.findIndex(query => query.trim() === newQuery.trim());
const existingQuery = currentList.findIndex(queryItem => queryItem.query.trim() === newQuery.trim());

// If it exists, remove it
if (existingQuery > 0) {
Expand All @@ -31,7 +32,10 @@ export class queryHistory implements TreeDataProvider<any> {

// If it's at the top, don't add it, it's already at the top
if (existingQuery !== 0) {
currentList.splice(0, 0, newQuery);
currentList.splice(0, 0, {
query: newQuery,
unix: Math.floor(Date.now() / 1000)
});
}

await Config.setPastQueries(currentList);
Expand All @@ -40,11 +44,13 @@ export class queryHistory implements TreeDataProvider<any> {
}
}),

vscode.commands.registerCommand(`vscode-db2i.queryHistory.remove`, async (node: PastQuery) => {
vscode.commands.registerCommand(`vscode-db2i.queryHistory.remove`, async (node: PastQueryNode) => {
if (node && Config.ready) {
let currentList = Config.getPastQueries();
const chosenQuery = node.query;
const existingQuery = currentList.findIndex(query => query.trim() === chosenQuery.trim());
const existingQuery = currentList.findIndex(queryItem =>
queryItem.query.trim() === chosenQuery.trim()
);

// If it exists, remove it
if (existingQuery >= 0) {
Expand Down Expand Up @@ -72,17 +78,80 @@ export class queryHistory implements TreeDataProvider<any> {
return element;
}

async getChildren(): Promise<vscode.TreeItem[]> {
async getChildren(timePeriod?: TimePeriodNode): Promise<vscode.TreeItem[]> {
if (Config.ready) {
return Config.getPastQueries().map(query => new PastQuery(query));
if (timePeriod) {
return timePeriod.getChildren();

} else {
const currentList = Config.getPastQueries();

const day = 60 * 60 * 24;
const week = day * 7;
const month = day * 30;

const now = Math.floor(Date.now() / 1000);
const dayAgo = now - day;
const weekAgo = now - week;
const monthAgo = now - month;

let pastDayQueries: PastQueryNode[] = [];
let pastWeekQueries: PastQueryNode[] = [];
let pastMonthQueries: PastQueryNode[] = [];
let olderQueries: PastQueryNode[] = [];

currentList.forEach(queryItem => {
// The smaller the unix value, the older it is
if (queryItem.unix < monthAgo) {
olderQueries.push(new PastQueryNode(queryItem.query));
} else if (queryItem.unix < weekAgo) {
pastMonthQueries.push(new PastQueryNode(queryItem.query));
} else if (queryItem.unix < dayAgo) {
pastWeekQueries.push(new PastQueryNode(queryItem.query));
} else {
pastDayQueries.push(new PastQueryNode(queryItem.query));
}
});

let nodes: TimePeriodNode[] = [];

if (pastDayQueries.length > 0) {
nodes.push(new TimePeriodNode(`Past day`, pastDayQueries, true));
}
if (pastWeekQueries.length > 0) {
nodes.push(new TimePeriodNode(`Past week`, pastWeekQueries));
}
if (pastMonthQueries.length > 0) {
nodes.push(new TimePeriodNode(`Past month`, pastMonthQueries));
}
if (olderQueries.length > 0) {
nodes.push(new TimePeriodNode(`Older`, olderQueries));
}

return nodes;
}

} else {
return [new TreeItem(`A connection is required for query history`)];
}
}
}

class PastQuery extends vscode.TreeItem {
class TimePeriodNode extends vscode.TreeItem {
constructor(public period: string, private nodes: PastQueryNode[], expanded = false) {
super(period, expanded ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed);

this.contextValue = `timePeriod`;

this.iconPath = new ThemeIcon(`calendar`);
}

getChildren() {
return this.nodes;
}
}

class PastQueryNode extends vscode.TreeItem {
constructor(public query: string) {
super(query.length > 63 ? query.substring(0, 60) + `...` : query);

Expand Down

0 comments on commit ad2f9d1

Please sign in to comment.