Skip to content

Commit

Permalink
feat(api): add necessary fields and connect to ui
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Jun 15, 2021
1 parent a085681 commit f895484
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 498 deletions.
2 changes: 1 addition & 1 deletion packages/api/nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"watch": ["src"],
"ext": "ts",
"ignore": [".git", "node_modules"],
"exec": "npx ts-node --require dotenv/config ./src/server.ts"
"exec": "npx ts-node --require dotenv/config ./src/index.ts"
}
7 changes: 2 additions & 5 deletions packages/api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ export class MongoManager {
client: MongoClient

async start (uri?: string): Promise<Db | null> {
uri =
uri ||
`mongodb://${process.env.MONGO_DATABASE_USERNAME}:${process.env.MONGO_DATABASE_PASSWORD}@${process.env.DB_HOSTNAME}:${process.env.MONGO_PORT}/${process.env.MONGO_INITDB_DATABASE}`

return this.connect(uri, process.env.MONGO_INITDB_DATABASE)
const mongoDbUri = `${uri || process.env.MONGO_URI}`
return this.connect(mongoDbUri, process.env.MONGO_INITDB_DATABASE)
}

async connect (uri: string, name: string): Promise<Db | null> {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createServer } from './server'

async function main () {
const mongoManager = new MongoManager()
const db = await mongoManager.start()
const db = await mongoManager.start(process.env.MONGO_URI)
const server = await createServer(db)

server.listen().then(({ url }) => {
Expand Down
10 changes: 4 additions & 6 deletions packages/api/src/repository/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ export class FeedRepository {
return this._normalizeId(response.value)
}

async get (name: string) {
const a = await this.collection.findOne({
name
})

return this._normalizeId(a)
async get (id: string) {
return this._normalizeId(
await this.collection.findOne({ _id: new ObjectId(id) })
)
}

private _normalizeId (feed: FeedDbObject) {
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FeedRepository } from './repository/Feed'
import { ResultRequestRepository } from './repository/ResultRequest'
type Context = {
feedRepository: FeedRepository
resultRequestRepository: ResultRequestRepository
resultRequestRepository: ResultRequestRepository
}

const resolvers = {
Expand All @@ -12,7 +12,7 @@ const resolvers = {
},

feed: async (_parent, args, { feedRepository }: Context) => {
return await feedRepository.get(args.name)
return await feedRepository.get(args.id)
}
},
Feed: {
Expand Down
104 changes: 62 additions & 42 deletions packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,77 @@ import { Db } from 'mongodb'
import { FeedRepository } from './repository/Feed'
import { ResultRequestRepository } from './repository/ResultRequest'

// async function addSampleData(feedRepository: FeedRepository, resultRequestRepository: ResultRequestRepository) {
// await feedRepository.collection.drop()
// await resultRequestRepository.collection.drop()
async function addSampleData (
feedRepository: FeedRepository,
resultRequestRepository: ResultRequestRepository
) {
await feedRepository.collection.drop()
await resultRequestRepository.collection.drop()

// const feed1 = await feedRepository.insert({
// address: "1",
// name: "btc/eur",
// requests: []
// })
const feed1 = await feedRepository.insert({
name: 'btc/eur',
address: '1',
lastResult: '1000.0',
label: '$',
network: 'mainnet',
requests: []
})

// const resultRequest1 = await resultRequestRepository.insert({
// result: 1000.00,
// feedId: feed1._id.toString(),
// requestId: '1',
// timestamp: Date.now().toString(),
// })
// await feedRepository.addResultRequest(feed1._id, resultRequest1._id)
const resultRequest1 = await resultRequestRepository.insert({
feedId: feed1._id.toString(),
address: '0x00000000',
result: '1000.0',
label: '$',
requestId: '1',
timestamp: Date.now().toString(),
drTxHash: 'drTxHash12345'
})
await feedRepository.addResultRequest(feed1._id, resultRequest1._id)

// const resultRequest2 = await resultRequestRepository.insert({
// result: 2000.00,
// feedId: feed1._id.toString(),
// requestId: '2',
// timestamp: Date.now().toString(),
// })
// await feedRepository.addResultRequest(feed1._id, resultRequest2._id)
const resultRequest2 = await resultRequestRepository.insert({
feedId: feed1._id.toString(),
address: '0x00000000',
result: '2000.0',
label: '$',
requestId: '1',
timestamp: Date.now().toString(),
drTxHash: 'drTxHash12345'
})
await feedRepository.addResultRequest(feed1._id, resultRequest2._id)

// const feed2 = await feedRepository.insert({
// address: "2",
// name: "btc/usd",
// requests: []
// })
// const resultRequest3 = await resultRequestRepository.insert({
// result: 3000.00,
// feedId: feed2._id.toString(),
// requestId: '3',
// timestamp: Date.now().toString(),
// })
// await feedRepository.addResultRequest(feed2._id, resultRequest3._id)
const feed2 = await feedRepository.insert({
name: 'btc/usd',
address: '2',
lastResult: '2000.0',
label: '$',
network: 'mainnet',
requests: []
})
const resultRequest3 = await resultRequestRepository.insert({
feedId: feed1._id.toString(),
address: '0x00000000',
result: '3000.0',
label: '$',
requestId: '1',
timestamp: Date.now().toString(),
drTxHash: 'drTxHash12345'
})
await feedRepository.addResultRequest(feed2._id, resultRequest3._id)

// await feedRepository.insert({
// address: "3",
// name: "eth/eur",
// requests: []
// })
// }
await feedRepository.insert({
name: 'btc/usd',
address: '3',
lastResult: '3000.0',
label: '$',
network: 'mainnet',
requests: []
})
}

export async function createServer (db: Db): Promise<ApolloServer> {
const feedRepository = new FeedRepository(db)
const resultRequestRepository = new ResultRequestRepository(db)

// addSampleData(feedRepository, resultRequestRepository)
addSampleData(feedRepository, resultRequestRepository)

const context = () => {
return { feedRepository, resultRequestRepository }
Expand Down
11 changes: 8 additions & 3 deletions packages/api/src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ const typeDefs = gql`
id: String! @id
name: String! @column
address: String! @column
lastResult: String @column
label: String! @column
network: String! @column
requests: [ResultRequest]! @link
lastResult: Float! @column
}
type ResultRequest @entity {
id: String! @id
feedId: String! @column
result: Float! @column
result: String! @column
label: String! @column
requestId: String! @column
address: String! @column
timestamp: String! @column
drTxHash: String! @column
# request: DataRequest @embedded
error: String
}
Expand All @@ -27,7 +32,7 @@ const typeDefs = gql`
type Query {
feeds: [Feed]!
feed(name: String!): Feed
feed(id: String!): Feed
}
`

Expand Down
26 changes: 15 additions & 11 deletions packages/api/test/feeds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ describe('feeds', function () {
.collection('feed')
.insertOne(feedExample)
const resultRequestExample1 = {
result: 1111.0,
result: '1111.0',
feedId: feedResponse.ops[0]._id.toString(),
requestId: '1',
timestamp: '1623085320000'
}
const resultRequestExample2 = {
result: 2222.0,
result: '2222.0',
feedId: feedResponse.ops[0]._id.toString(),
requestId: '1',
timestamp: '1623085329000'
Expand Down Expand Up @@ -167,29 +167,33 @@ describe('feeds', function () {
name: 'btc/usd',
requests: []
}
await state.mongoManager.db.collection('feed').insertOne(feedExample)
const result = await state.mongoManager.db
.collection('feed')
.insertOne(feedExample)

const { _id } = result.ops[0]

const GET_FEED = gql`
query Feed($name: String!) {
feed(name: $name) {
query Feed($id: String!) {
feed(id: $id) {
id
address
name
address
requests {
id
feedId
}
}
}
`
const {
data: { feed }
} = await state.testClient.query({
const result2 = await state.testClient.query({
query: GET_FEED,
variables: {
name: 'btc/usd'
id: _id.toString()
}
})

const feed = result2.data.feed

expect(feed).toHaveProperty('address', feedExample.address)
expect(feed).toHaveProperty('name', feedExample.name)
expect(feed).toHaveProperty('requests', feedExample.requests)
Expand Down
1 change: 1 addition & 0 deletions packages/ui/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 1 addition & 0 deletions packages/ui/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
14 changes: 12 additions & 2 deletions packages/ui/apollo/queries/feed.gql
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
query feed($name: String!) {
feed(name: $name) {
query feed($id: String!) {
feed(id: $id) {
id
name
address
lastResult
requests {
feedId,
result,
drTxHash,
label,
address,
requestId,
timestamp,
}
}
}
3 changes: 3 additions & 0 deletions packages/ui/apollo/queries/feeds.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
id
name
address
lastResult
network
label
}
}
5 changes: 4 additions & 1 deletion packages/ui/components/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<div ref="container">
<div v-show="tooltip" class="tooltip">
<span class="value">
{{ value }} <span class="date"> {{ date }}</span></span
{{ formatNumber(value) }} <span class="date"> {{ date }}</span></span
>
<span class="name"> {{ name }} </span>
</div>
</div>
</template>

<script>
import { formatNumber } from '@/utils/formatNumber'
export default {
name: 'Chart',
props: {
Expand Down Expand Up @@ -102,6 +104,7 @@ export default {
this.date = this.dateToString(this.data[this.data.length - 1].time)
},
methods: {
formatNumber,
setData() {
this.lineChart.setData(this.data)
},
Expand Down

0 comments on commit f895484

Please sign in to comment.