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

[mds-stream-processor] Add geo labeling for mds-stream-processor #231

Merged
merged 7 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions packages/mds-stream-processor/labelers/geography-labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,30 @@
limitations under the License.
*/

import db from '@mds-core/mds-db'
import { UUID, Nullable, Telemetry } from '@mds-core/mds-types'
import { pointInShape } from '@mds-core/mds-utils'
import { MessageLabeler } from './types'

export interface GeographyLabel {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a geography is changed at some point? I know the idea of versioning definitions has been brought up before, not sure where we landed with it though?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We make a new one and the new one references the previous one in the attribute prev_geographies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make it so it only annotates for published geographies, I suppose. Those are immutable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a change to only query for published geographies.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might wanna update your test to reflect it only does published geos then.

geography_id: UUID[]
geography_ids: UUID[]
}

export const GeographyLabeler: () => MessageLabeler<
{ telemetry?: Nullable<Telemetry> },
GeographyLabel
> = () => async ({ telemetry }) => {
// TODO: Add Point-in-polygon checks
return { geography_id: telemetry ? [] : [] }
const gps = telemetry?.gps
if (gps) {
const { lat, lng } = gps

const geographies = await db.readGeographies({ get_published: true })

const geography_ids = geographies
.filter(({ geography_json }) => pointInShape({ lat, lng }, geography_json))
.map(({ geography_id }) => geography_id)

return { geography_ids }
}
return { geography_ids: [] }
}
3 changes: 3 additions & 0 deletions packages/mds-stream-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
"@mds-core/mds-types": "0.1.23",
"@mds-core/mds-utils": "0.1.26"
},
"devDependencies": {
"sinon": "7.5.0"
},
"license": "Apache-2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { GeographyLabeler } from '@mds-core/mds-stream-processor/labelers/geography-labeler'
import * as utils from '@mds-core/mds-utils'
import db from '@mds-core/mds-db'
import Sinon from 'sinon'
import { Geography } from '@mds-core/mds-types'
import { v4 as uuid } from 'uuid'
import assert from 'assert'

const mockGeographies: Geography[] = Array.from({ length: 100 }, () => ({
geography_id: uuid(),
geography_json: {
type: 'FeatureCollection',
features: []
},
name: uuid() // Usually intended to be a human readable name, but we just need a random string here.
}))

const mockTelemetry = {
provider_id: uuid(),
device_id: uuid(),
timestamp: utils.now(),
gps: { lat: 34.0522, lng: 118.2437 }
}

describe('GeographyLabeler tests', async () => {
it('Tests all matched geographies are included in list', async () => {
Sinon.replace(utils, 'pointInShape', () => true)
Sinon.replace(db, 'readGeographies', async () => mockGeographies)

const { geography_ids } = await GeographyLabeler()({ telemetry: mockTelemetry })

geography_ids.forEach(geography_id =>
assert(mockGeographies.find(geography => geography.geography_id === geography_id))
)
})

it('Tests that absent gps in telemetry elicits no matches', async () => {
const { geography_ids } = await GeographyLabeler()({})
assert(geography_ids.length === [].length)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LatencyLabeler } from '@mds-core/mds-stream-processor/labelers/latency-labeler'
import assert from 'assert'

describe('LatencyLabeler Tests', async () => {
it('Tests LatencyLabeler detects latency > 0', async () => {
const mockTimeChunks = Array.from({ length: 100 }, (_, index) => ({
timestamp: 1000 * index,
recorded: 1000 * (index + 1)
}))
const latencyLabels = await Promise.all(mockTimeChunks.map(chunk => LatencyLabeler()(chunk)))
latencyLabels.forEach(({ latency_ms }) => assert(latency_ms === 1000))
})

it('Tests LatencyLabeler returns latency == 0', async () => {
const mockTimeChunks = Array.from({ length: 100 }, (_, index) => ({
timestamp: 1000 * index,
recorded: 1000 * index
}))
const latencyLabels = await Promise.all(mockTimeChunks.map(chunk => LatencyLabeler()(chunk)))
latencyLabels.forEach(({ latency_ms }) => assert(latency_ms === 0))
})
})