-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [NET-1290]: Store sample message (#17)
Added new `sampleMessage` endpoint. It returns a sample message from a specified stream. - In each crawl iteration, we store one random message for each stream, replacing any previous sample from earlier iteration - We collect samples from public streams only The sample message can be in JSON or binary format. JSON is returned as a plain string and binary as base64-encoded string.
- Loading branch information
Showing
11 changed files
with
336 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Arg, Query, Resolver } from 'type-graphql' | ||
import { Inject, Service } from 'typedi' | ||
import { ContentType, Message } from '../entities/Message' | ||
import { MessageRepository } from '../repository/MessageRepository' | ||
import { toStreamID } from '@streamr/protocol' | ||
import { binaryToUtf8 } from '@streamr/utils' | ||
|
||
@Resolver() | ||
@Service() | ||
export class MessageResolver { | ||
|
||
private repository: MessageRepository | ||
|
||
constructor( | ||
@Inject() repository: MessageRepository | ||
) { | ||
this.repository = repository | ||
} | ||
|
||
@Query(() => Message, { nullable: true }) | ||
async sampleMessage( | ||
@Arg("stream", { nullable: false }) streamId: string | ||
): Promise<Message | null> { | ||
const message = await this.repository.getSampleMessage(toStreamID(streamId)) | ||
if (message !== null) { | ||
return { | ||
content: (message.contentType === ContentType.JSON) | ||
? binaryToUtf8(message.content) | ||
: Buffer.from(message.content).toString('base64'), | ||
contentType: message.contentType | ||
} | ||
} else { | ||
return null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Field, ObjectType } from 'type-graphql' | ||
|
||
export enum ContentType { | ||
JSON = 'JSON', | ||
BINARY = 'BINARY' | ||
} | ||
|
||
/* eslint-disable indent */ | ||
@ObjectType() | ||
export class Message { | ||
@Field(() => String, { description: 'JSON string if contentType is JSON, otherwise base64-encoded binary content' }) | ||
content!: string | ||
@Field() | ||
contentType!: ContentType | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Inject, Service } from 'typedi' | ||
import { ConnectionPool } from './ConnectionPool' | ||
import { StreamID } from '@streamr/protocol' | ||
import { ContentType } from '../entities/Message' | ||
import { StreamMessage, ContentType as StreamMessageContentType } from '@streamr/protocol' | ||
|
||
export interface MessageRow { | ||
content: Uint8Array | ||
contentType: ContentType | ||
} | ||
|
||
export const convertStreamMessageToMessageRow = (msg: StreamMessage): MessageRow => { | ||
let contentType | ||
if (msg.contentType === StreamMessageContentType.JSON) { | ||
contentType = ContentType.JSON | ||
} else if (msg.contentType === StreamMessageContentType.BINARY) { | ||
contentType = ContentType.BINARY | ||
} else { | ||
throw new Error(`Assertion failed: unknown content type ${msg.contentType}`) | ||
} | ||
return { | ||
content: msg.content, | ||
contentType | ||
} | ||
} | ||
|
||
@Service() | ||
export class MessageRepository { | ||
|
||
private readonly connectionPool: ConnectionPool | ||
|
||
constructor( | ||
@Inject() connectionPool: ConnectionPool | ||
) { | ||
this.connectionPool = connectionPool | ||
} | ||
|
||
async getSampleMessage(streamId: StreamID): Promise<MessageRow | null> { | ||
const rows = await this.connectionPool.queryOrExecute<MessageRow>( | ||
'SELECT content, contentType FROM sample_messages WHERE streamId=? LIMIT 1', | ||
[streamId] | ||
) | ||
if (rows.length === 1) { | ||
return rows[0] | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
async replaceSampleMessage(message: MessageRow | null, streamId: StreamID): Promise<void> { | ||
if (message !== null) { | ||
await this.connectionPool.queryOrExecute( | ||
'REPLACE INTO sample_messages (streamId, content, contentType) VALUES (?, ?, ?)', | ||
[ | ||
streamId, | ||
Buffer.from(message.content), | ||
message.contentType | ||
] | ||
) | ||
} else { | ||
await this.connectionPool.queryOrExecute( | ||
'DELETE FROM sample_messages WHERE streamId=?', | ||
[streamId] | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.