Skip to content

Commit 5995827

Browse files
committed
chart
1 parent ab75765 commit 5995827

File tree

9 files changed

+64
-15
lines changed

9 files changed

+64
-15
lines changed

src/controllers/StockController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class StockController {
142142
return result
143143
}
144144

145+
async getBlocksCount(ctx: Context) {
146+
const counts = await StockService.getBlocksCount()
147+
ctx.Json({ data: counts })
148+
}
149+
145150
}
146151

147152
export default new StockController

src/controllers/StockHistoryController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ class SharesController {
169169
// }
170170
return stock
171171
}
172+
173+
async getTotal(ctx: Context) {
174+
const total = await StockHistoryService.getTotal()
175+
ctx.Json(total)
176+
}
172177
}
173178

174179
export default new SharesController

src/daos/StockDao.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Equal, Like, Between, FindManyOptions, FindConditions} from "typeorm";
22
import { Context } from '@core/koa'
33
import { Stock } from '../entities/mysql/shares/stock'
44
import { EMarket, EBLock } from '../models/Stocks'
5-
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
5+
import { getSharesManager, getSharesRepository, createSharesQueryBuilder } from '../database/dbUtils';
66

77

88

@@ -70,6 +70,12 @@ class StockDao {
7070
const pages = await getSharesRepository(Stock).findAndCount(options)
7171
return pages
7272
}
73+
74+
async getBlocksCount(): Promise<{total: number, block: EBLock}> {
75+
const counts = await createSharesQueryBuilder(Stock, 'groupByBlocks')
76+
.select('count(*) as total, block').groupBy('block').execute()
77+
return counts
78+
}
7379
}
7480

7581
export default new StockDao

src/daos/StockHistoryDao.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Equal, Like, Between, FindManyOptions} from "typeorm";
1+
import { Equal, Like, Between, LessThanOrEqual, FindManyOptions} from "typeorm";
22
import { StockHistory, History } from '../entities/mysql/shares/stockHistory'
33
import { getSharesManager, getSharesRepository } from '../database/dbUtils';
4-
5-
4+
import Store, { RedisStore } from "../utils/session/store";
65

76
class StockHistoryDao {
87

8+
maxCount = -1
9+
910
async getLastestTrade(stockId: number) {
1011
const lastestTrade = await getSharesRepository(History).findOne({
1112
select: ['timestamp'],
@@ -51,6 +52,25 @@ class StockHistoryDao {
5152
})
5253
return [list as StockHistory[], total]
5354
}
55+
56+
async _getTotal() {
57+
const before = Date.now()
58+
return getSharesRepository(History).count().then(count => {
59+
console.log('get total: ', count)
60+
this.maxCount = count
61+
return this.maxCount
62+
}).then(() => {
63+
console.log(`time:${Date.now() - before}ms`)
64+
})
65+
}
66+
67+
async getTotal() {
68+
if(this.maxCount > 0) {
69+
return this.maxCount
70+
} else {
71+
return await this._getTotal()
72+
}
73+
}
5474
}
5575

5676
export default new StockHistoryDao

src/database/conectDB.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import 'reflect-metadata'
1+
// import 'reflect-metadata'
22
import { createConnection, createConnections, ConnectionOptions } from "typeorm";
3-
import { BlogConf, SharesConf, MongoConf } from '../../conf/db.conf'
3+
import { MysqlConf, MongoConf } from '../../conf/db.conf'
44
import { Entities, ShareEntities } from '../entities/mysql'
55
import { MongoEntities } from '../entities/mongo'
66
import { CONNECT_BLOG, CONNECT_MONGO, CONNECT_SHARES } from './dbUtils';
7+
import stockHistoryDao from '../daos/StockHistoryDao'
78

89
const _PROD_ = process.env.NODE_ENV === 'production'
910

1011
const connectDB = (): Promise<void> => {
1112
const connectOptions = [
12-
{name: CONNECT_BLOG, entities: Entities, database: BlogConf.database},
13-
{name: CONNECT_SHARES, entities: ShareEntities, database: SharesConf.database}
13+
{name: CONNECT_BLOG, entities: Entities, database: CONNECT_BLOG},
14+
{name: CONNECT_SHARES, entities: ShareEntities, database: CONNECT_SHARES}
1415
].map<ConnectionOptions>(db => {
1516
return {
1617
...db,
1718
type : 'mysql',
18-
host : BlogConf.host,
19-
port : BlogConf.port,
20-
username : BlogConf.username,
21-
password : BlogConf.password,
19+
host : MysqlConf.host,
20+
port : MysqlConf.port,
21+
username : MysqlConf.username,
22+
password : MysqlConf.password,
2223
logging : _PROD_ ? false : true,
2324
}
2425
})
2526
return createConnections(connectOptions).then((connect) => {
26-
console.log(`${connectOptions.length} mysql connected successfully!`)
27+
console.log(`${connectOptions.map(c => c.name).join()} ${connectOptions.length} mysql connected successfully!`)
28+
stockHistoryDao._getTotal()
2729
}).catch((err) => {
2830
console.log('mysql failed to connect!', err)
2931
})

src/database/dbUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getManager, getRepository, Entity, ObjectType, EntitySchema, getMongoManager} from "typeorm";
1+
import {getManager, getRepository, ObjectType, EntitySchema, getMongoManager, createQueryBuilder} from "typeorm";
22

33
type TConnectName = 'Blog' | 'Shares' | 'Mongo'
44

@@ -41,4 +41,4 @@ export const getSharesManager = () => getManager(CONNECT_SHARES)
4141
export const getSharesRepository = <Entity>(entity: ObjectType<Entity> | EntitySchema<Entity> | string) => getRepository(entity, CONNECT_SHARES)
4242

4343

44-
44+
export const createSharesQueryBuilder = <Entity>(entity: ObjectType<Entity> | string, alias = 'queryBuilder') => createQueryBuilder(entity, alias, CONNECT_SHARES)

src/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ router
3030
.get('/api/stocks', StockCtrl.pages1)
3131
.get('/api/stocks/:id', StockCtrl.getStock)
3232
.get('/api/stockhistory', StockHistoryCtrl.pages)
33+
.get('/api/stockhistory/total', StockHistoryCtrl.getTotal)
34+
.get('/api/stock/chartCount', StockCtrl.getBlocksCount)
3335
.get('/graphql', KoaGraphql({
3436
schema: RootSchema,
3537
graphql: _PROD_ ? false : true

src/services/StockHistoryService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class StockHistoryService {
5252
]
5353
}
5454
}
55+
56+
async getTotal() {
57+
return await StockHistoryDao.getTotal()
58+
}
5559
}
5660

5761
export default new StockHistoryService

src/services/StockService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class StockService {
4040
const pages = await StockDao.pages(offset, size, code, name, market, block);
4141
return pages
4242
}
43+
44+
async getBlocksCount() {
45+
const counts = await StockDao.getBlocksCount()
46+
return counts
47+
}
4348
}
4449

4550
export default new StockService

0 commit comments

Comments
 (0)