Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor repository #149

Merged
merged 59 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
3c82f2f
fix: nits
h13i32maru Jul 31, 2020
238bfc2
refactor(StreamRepo):
h13i32maru Jul 31, 2020
138cd93
refactor(StreamRepo):
h13i32maru Jul 31, 2020
7dfa9c6
refactor(StreamRepo):
h13i32maru Aug 1, 2020
adcbd23
refactor(StreamRepo):
h13i32maru Aug 1, 2020
2bad1d3
refactor(StreamRepo):
h13i32maru Aug 1, 2020
50c1584
refactor(StreamRepo):
h13i32maru Aug 1, 2020
dc07165
refactor(StreamRepo):
h13i32maru Aug 1, 2020
1ca4bf6
refactor(StreamRepo):
h13i32maru Aug 1, 2020
c88fea0
refactor(StreamRepo):
h13i32maru Aug 1, 2020
f5081be
refactor(StreamRepo):
h13i32maru Aug 1, 2020
e57bb00
refactor(StreamRepo):
h13i32maru Aug 1, 2020
eb726e5
refactor(StreamRepo):
h13i32maru Aug 1, 2020
ae12f7d
refactor(StreamRepo):
h13i32maru Aug 1, 2020
ca4b6bb
refactor(StreamRepo):
h13i32maru Aug 1, 2020
c6c26fd
refactor(FilterHistoryRepo):
h13i32maru Aug 1, 2020
9c37bed
refactor(FilterHistoryRepo):
h13i32maru Aug 1, 2020
0df5a99
refactor(StreamRepo):
h13i32maru Aug 1, 2020
de9adf3
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
498447d
refactor:
h13i32maru Aug 1, 2020
67f7e7c
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
5d17553
refactor(StreamIssueRepo):
h13i32maru Aug 1, 2020
a9e1f5e
refactor(SubscriptionIssueRepo):
h13i32maru Aug 1, 2020
c9496ac
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
1ff6872
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
933087b
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
c3eee22
fix(Stream): query index calc
h13i32maru Aug 1, 2020
2e76678
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
8a9af6d
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
5b4678d
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
be72387
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
54b1f49
refactor(StreamPolling):
h13i32maru Aug 1, 2020
8bb448b
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
9d10f56
refactor(SystemStreamRepo):
h13i32maru Aug 1, 2020
e135662
refactor(IssueRepo):
h13i32maru Aug 1, 2020
efceb1f
refactor(IssueRepo):
h13i32maru Aug 1, 2020
4e4059d
refactor(IssueRepo):
h13i32maru Aug 1, 2020
2b7e4ce
refactor(IssueRepo):
h13i32maru Aug 1, 2020
30f82e5
refactor(IssueRepo):
h13i32maru Aug 1, 2020
8d245b9
refactor(IssueRepo):
h13i32maru Aug 1, 2020
f0d0d98
refactor(IssueRepo):
h13i32maru Aug 1, 2020
0730d1e
refactor(IssueRepo):
h13i32maru Aug 1, 2020
457a361
refactor(IssueRepo):
h13i32maru Aug 1, 2020
59168df
refactor(VersionRepo):
h13i32maru Aug 1, 2020
8e2c123
refactor(IssueRepo):
h13i32maru Aug 1, 2020
3d9d5d6
refactor(IssueRepo):
h13i32maru Aug 1, 2020
19e50f8
refactor(IssueRepo):
h13i32maru Aug 1, 2020
bff9d34
refactor(IssueRepo):
h13i32maru Aug 1, 2020
b44e56c
refactor: nits
h13i32maru Aug 2, 2020
cab2570
refactor(IssueRepo):
h13i32maru Aug 2, 2020
8bcf72c
refactor(IssueRepo):
h13i32maru Aug 2, 2020
5166b9f
refactor(IssueRepo):
h13i32maru Aug 2, 2020
c0852ca
refactor(IssueRepo):
h13i32maru Aug 2, 2020
d71e3e7
refactor(IssueRepo):
h13i32maru Aug 2, 2020
eedb2d0
refactor(IssueRepo):
h13i32maru Aug 2, 2020
e45dc8d
refactor(StreamIssueRepo):
h13i32maru Aug 2, 2020
992dac7
refactor(DBIPC): show slow query
h13i32maru Aug 2, 2020
13aa0f8
refactor(Repo):
h13i32maru Aug 2, 2020
f9e5f9a
refactor(IssueRepo):
h13i32maru Aug 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(IssueRepo):
  • Loading branch information
h13i32maru committed Aug 1, 2020
commit efceb1f36258cf2d51028b7682a5a73ae39cf2c2
5 changes: 5 additions & 0 deletions src/IPC/DBIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ type SQLRowReturn<T> = {
};

class _DBIPC {
private log: boolean = false;

// exec
async exec(sql: SQLParams['sql'], params?: SQLParams['params']): Promise<SQLRunReturn> {
if (this.log) console.log(sql, params);
const p: SQLParams = {sql, params};
return ipcRenderer.invoke(ChannelNames.exec, p);
}
Expand All @@ -40,6 +43,7 @@ class _DBIPC {

// select
async select<T = any>(sql: SQLParams['sql'], params?: SQLParams['params']): Promise<SQLRowsReturn<T>> {
if (this.log) console.log(sql, params);
const p: SQLParams = {sql, params};
return ipcRenderer.invoke(ChannelNames.select, p);
}
Expand All @@ -50,6 +54,7 @@ class _DBIPC {

// selectSingle
async selectSingle<T = any>(sql: SQLParams['sql'], params?: SQLParams['params']): Promise<SQLRowReturn<T>> {
if (this.log) console.log(sql, params);
const p: SQLParams = {sql, params};
return ipcRenderer.invoke(ChannelNames.selectSingle, p);
}
Expand Down
11 changes: 4 additions & 7 deletions src/Renderer/Infra/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class Stream {
if (this.hasError) return;

// build search query
const searchedAt = DateUtil.localToUTCString(new Date());
this.queries = await this.buildSearchQueries();
if (!this.queries.length) return;
const queries = this.queries.map(query =>{
Expand All @@ -46,8 +45,7 @@ export class Stream {
});

// 初回はデータを取りすぎないようにする
const {count: currentIssuesCount} = await StreamIssueRepo.totalCount(this.id);
const maxSearchingCount = currentIssuesCount === 0 ? PerPage : MaxSearchingCount;
const maxSearchingCount = this.searchedAt ? MaxSearchingCount : PerPage;

// search
const {error} = await this.search(queries, maxSearchingCount);
Expand All @@ -59,9 +57,9 @@ export class Stream {

// すべて取得したときにsearchedAtを更新する
if (this.queryIndex === 0 && this.page === 1) {
this.searchedAt = searchedAt;
this.searchedAt = DateUtil.localToUTCString(new Date());
if (this.id > 0) { // hack:
const {error} = await StreamRepo.updateSearchedAt(this.id, searchedAt);
const {error} = await StreamRepo.updateSearchedAt(this.id, this.searchedAt);
if (error) {
console.error(error);
this.hasError = true;
Expand Down Expand Up @@ -144,8 +142,7 @@ export class Stream {
}
}

const {updatedIssueIds} = await IssueRepo.import(issues);
const {error: e1} = await StreamIssueRepo.createBulk(this.id, issues);
const {error: e1, updatedIssueIds} = await IssueRepo.createBulk(this.id, issues);
if (e1) return {error: e1};

if (updatedIssueIds.length) {
Expand Down
103 changes: 61 additions & 42 deletions src/Renderer/Repository/IssueRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,37 @@ import moment from 'moment';
import {IssueEvent} from '../Event/IssueEvent';
import {IssueFilter} from './Issue/IssueFilter';
import {IssueEntity} from '../Type/IssueEntity';
import {RemoteIssueEntity} from '../Type/RemoteIssueEntity';
import {GitHubUtil} from '../Util/GitHubUtil';
import {StreamIssueRepo} from './StreamIssueRepo';

// todo: refactor
class _IssueRepo {
async getIssues(streamId: number): Promise<{error?: Error; issues?: IssueEntity[]}> {
const {error, rows} = await DBIPC.select<IssueEntity>(`
select
t1.*
from
issues as t1
inner join
streams_issues as t2 on t1.id = t2.issue_id
where
t2.stream_id = ?
`, [streamId]);
private async relations(issues: IssueEntity[]) {
for (const issue of issues) {
issue.value = JSON.parse(issue.value as any);
// // todo: this hack is for old github response
// // we must add value.assignee before `issue.value = value`.
// // because issue.value is setter/getter, so setter behavior is special.
// if (!value.assignees) {
// value.assignees = value.assignee ? [value.assignee] : [];
// }
}
}

async getIssues(issueIds: number[]): Promise<{error?: Error; issues?: IssueEntity[]}> {
const {error, rows: issues} = await DBIPC.select<IssueEntity>(`select * from issues where id in (${issueIds.join(',')})`);
if (error) return {error};

return {issues: rows};
await this.relations(issues);
return {issues};
}

async getIssue(issueId: number): Promise<{error?: Error; issue?: IssueEntity}> {
const {error, issues} = await this.getIssues([issueId]);
if (error) return {error};

return {issue: issues[0]};
}

async getTotalCount(): Promise<{error?: Error; count?: number}>{
Expand All @@ -49,47 +62,49 @@ class _IssueRepo {
return {count: row.count};
}

async import(issues: any[], defaultReadAt: string = null): Promise<{error?: Error; updatedIssueIds?: number[]}> {
async createBulk(streamId: number, issues: RemoteIssueEntity[]): Promise<{error?: Error; updatedIssueIds?: number[]}> {
const updatedIds = [];

for (const issue of issues) {
const paths = issue.url.split('/').reverse();
const user = paths[3];
const repo = `${paths[3]}/${paths[2]}`;

if (!issue.assignees) {
if (issue.assignee) {
issue.assignees = [JSON.parse(JSON.stringify(issue.assignee))];
} else {
issue.assignees = [];
}
}

const res = await DBIPC.selectSingle('select * from issues where id = ?', [issue.id]);
const currentIssue = res.row;
// const paths = issue.url.split('/').reverse();
// const user = paths[3];
// const repo = `${paths[3]}/${paths[2]}`;
const {repo, user} = GitHubUtil.getInfo(issue.url);

// if (!issue.assignees) {
// if (issue.assignee) {
// issue.assignees = [JSON.parse(JSON.stringify(issue.assignee))];
// } else {
// issue.assignees = [];
// }
// }

const res = await this.getIssue(issue.id);
if (res.error) return {error: res.error};
const currentIssue = res.issue;
const params = [
issue.id,
issue.pull_request ? 'pr' : 'issue',
issue.title,
issue.created_at,
issue.updated_at,
issue.closed_at ? issue.closed_at : null,
currentIssue ? currentIssue.read_at : defaultReadAt,
issue.closed_at || null,
currentIssue?.read_at || null,
issue.number,
user,
repo,
issue.user.login, // author
issue.assignees.length ? issue.assignees.map((assignee)=> `<<<<${assignee.login}>>>>`).join('') : null, // hack: assignees format
issue.labels.length ? issue.labels.map((label)=> `<<<<${label.name}>>>>`).join('') : null, // hack: labels format
issue.milestone ? issue.milestone.title : null,
issue.milestone ? issue.milestone.due_on : null,
issue.milestone?.title || null,
issue.milestone?.due_on || null,
issue.html_url,
issue.body,
JSON.stringify(issue)
];

if (currentIssue) {
await DBIPC.exec(`
const {error} = await DBIPC.exec(`
update
issues
set
Expand All @@ -115,9 +130,10 @@ class _IssueRepo {
id = ${issue.id}
`, params);

if (error) return {error};
if (issue.updated_at > currentIssue.updated_at) updatedIds.push(issue.id);
} else {
await DBIPC.exec(`
const {error} = await DBIPC.exec(`
insert into
issues
(
Expand All @@ -144,23 +160,26 @@ class _IssueRepo {
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, params);

if (!defaultReadAt) updatedIds.push(issue.id);
if (error) return {error};
updatedIds.push(issue.id);
}
}

// see StreamsIssuesTable
// limit max records
const max = ConfigRepo.getConfig().database.max;
await DBIPC.exec(`
delete from
issues
where
id in (select id from issues order by updated_at desc limit ${max}, 1000)
`);
await DBIPC.exec(`delete from issues where id in (select id from issues order by updated_at desc limit ${max}, 1000)`);

// create stream-issue
const issueIds = issues.map(issue => issue.id);
const res = await this.getIssues(issueIds);
if (res.error) return {error: res.error};
const {error} = await StreamIssueRepo.createBulk(streamId, res.issues);
if (error) return {error};

return {updatedIssueIds: updatedIds};
}

isRead(issue) {
isRead(issue: IssueEntity): boolean {
return issue && issue.read_at !== null && issue.read_at >= issue.updated_at;
}

Expand Down
19 changes: 9 additions & 10 deletions src/Renderer/Repository/SubscriptionIssuesRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {SubscriptionIssueEntity} from '../Type/SubscriptionIssueEntity';
import {ConfigRepo} from './ConfigRepo';
import {GitHubClient} from '../Infra/GitHubClient';
import {IssueRepo} from './IssueRepo';
import {StreamIssueRepo} from './StreamIssueRepo';
import {StreamPolling} from '../Infra/StreamPolling';
import {SystemStreamEvent} from '../Event/SystemStreamEvent';
import {SystemStreamId} from './SystemStreamRepo';
import {DateUtil} from '../Util/DateUtil';
import {GitHubUtil} from '../Util/GitHubUtil';

class _SubscriptionIssuesRepo {
async getAllSubscriptionIssues(): Promise<{error?: Error; subscriptionIssues?: SubscriptionIssueEntity[]}>{
Expand All @@ -31,16 +31,15 @@ class _SubscriptionIssuesRepo {
if (subscriptionIssue) return;

// get issue
const {repo, issueNumber} = this.getRepoAndIssueNumber(url);
const {repo, issueNumber} = GitHubUtil.getInfo(url);
const github = ConfigRepo.getConfig().github;
const client = new GitHubClient(github.accessToken, github.host, github.pathPrefix, github.https);
const res = await client.request(`/repos/${repo}/issues/${issueNumber}`);
if (res.error) return {error: res.error};
const issue = res.body;

// create
await IssueRepo.import([issue]);
const {error} = await StreamIssueRepo.createBulk(SystemStreamId.subscription, [issue]);
const {error} = await IssueRepo.createBulk(SystemStreamId.subscription, [issue]);
if (error) return {error};

const createdAt = DateUtil.localToUTCString(new Date());
Expand Down Expand Up @@ -72,12 +71,12 @@ class _SubscriptionIssuesRepo {
return {};
}

private getRepoAndIssueNumber(url: string): {repo: string; issueNumber: number} {
const urlPaths = url.split('/').reverse();
const repo = `${urlPaths[3]}/${urlPaths[2]}`;
const issueNumber = parseInt(urlPaths[0], 10);
return {repo, issueNumber};
}
// private getRepoAndIssueNumber(url: string): {repo: string; issueNumber: number} {
// const urlPaths = url.split('/').reverse();
// const repo = `${urlPaths[3]}/${urlPaths[2]}`;
// const issueNumber = parseInt(urlPaths[0], 10);
// return {repo, issueNumber};
// }
}

export const SubscriptionIssuesRepo = new _SubscriptionIssuesRepo();
8 changes: 4 additions & 4 deletions src/Renderer/Repository/SystemStreamRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ class _SystemStreamRepo {
// return StreamPolling.getSystemStreamQueries(streamId);
// }

async isSubscription(url) {
const res = await DBIPC.selectSingle('select * from subscription_issues where url = ?', [url]);
return !!res.row;
}
// async isSubscription(url) {
// const res = await DBIPC.selectSingle('select * from subscription_issues where url = ?', [url]);
// return !!res.row;
// }

// async subscribe(url) {
// const already = await this.isSubscription(url);
Expand Down
4 changes: 3 additions & 1 deletion src/Renderer/Type/IssueEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {RemoteIssueEntity} from './RemoteIssueEntity';

export type IssueEntity = {
id: number;
type: string;
Expand All @@ -21,5 +23,5 @@ export type IssueEntity = {
body: string;
read_body: string;
prev_read_body: string;
value: string;
value: RemoteIssueEntity;
}
31 changes: 31 additions & 0 deletions src/Renderer/Type/RemoteIssueEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type RemoteIssueEntity = {
id: number;
pull_request: any;
title: string;
number: number;
user: RemoteUserEntity;
labels: RemoteLabelEntity[];
milestone: RemoteMilestoneEntity;
created_at: string;
updated_at: string;
closed_at: string;
draft: boolean;
url: string;
html_url: string;
body: string;
assignees: RemoteUserEntity[];
assignee: RemoteUserEntity;
}

type RemoteUserEntity = {
login: string;
}

type RemoteLabelEntity = {
name: string;
}

type RemoteMilestoneEntity = {
title: string;
due_on: string;
}
11 changes: 11 additions & 0 deletions src/Renderer/Util/GitHubUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class _GitHubUtil {
getInfo(url: string): {repo: string; issueNumber: number, user: string} {
const urlPaths = url.split('/').reverse();
const repo = `${urlPaths[3]}/${urlPaths[2]}`;
const issueNumber = parseInt(urlPaths[0], 10);
const user = urlPaths[3];
return {repo, issueNumber, user};
}
}

export const GitHubUtil = new _GitHubUtil();