Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kneth committed Sep 13, 2023
1 parent 81b969d commit 10c390a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 34 deletions.
1 change: 1 addition & 0 deletions examples/example-node-telemetry/Charts/Dashboard.charts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"exportVersion":6,"dashboards":{"dashboard-1":{"description":"","embedding":{},"filters":[],"layout":[{"i":"item-1","x":0,"y":0,"w":2,"h":2}],"title":"Dashboard"}},"items":{"item-1":{"calculatedFields":[],"channels":{"x":{"channelType":"value","inferredType":"Date","field":"timestamp","type":"temporal"},"y":{"channelType":"value","inferredType":"Number","field":"loadAvg","type":"quantitative","transformedType":"Number"}},"chartType":"Line Continuous","convertedFields":[{"fieldPath":"loadAvg","convertTo":"double","arrayParentPaths":["loadAvg"]}],"customisations":{"options":{},"axes":{"y":{}},"channels":{"x":{"dateTimeFormatting":{"enabled":true,"value":"DD-MMM-YYYY"}},"y":{"numberFormatting":{"enabled":true,"value":"Default"}}},"conditionalFormatting":[]},"dashboardId":"dashboard-1","dataSourceId":"data-source-1","description":"","filters":[],"iconValue":"line-continuous","interactiveFiltering":"highlight","itemType":"chart","lookupFields":[],"meta":{},"missedFields":[],"query":null,"queryId":null,"reductions":{"y":[{"dimensionality":1,"field":"loadAvg","type":"Array element by index","arguments":[0]}]},"sample":false,"title":"","embedding":{}}},"dataSources":{"data-source-1":{"alias":"SensorReading","collection":"SensorReading","database":"telemetry","deployment":"Cluster0","sourceType":"cluster"}},"queries":{}}
7 changes: 4 additions & 3 deletions examples/example-node-telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To sync Realm data you must first:

After running the client and seeing the available collection in Atlas, [set write permissions](https://www.mongodb.com/docs/atlas/app-services/rules/roles/#define-roles---permissions) for the collection.

The schema to use (App Services / Data Access / Schema):
The following schema will automatically be created in **Development Mode** and you can find it at `App Services / Data Access / Schema` in the Atlas App Service UI:

```json
{
Expand Down Expand Up @@ -102,10 +102,11 @@ The schema to use (App Services / Data Access / Schema):
}
```

Data can be visualized by [Charts](https://www.mongodb.com/products/charts). An example is shown below.
### Visualize Data

![An example on how Charts can visualize incoming data](./charts-example.png)
Data can be visualized by [Charts](https://www.mongodb.com/products/charts). An example from a [dashboard](Charts/Dashboard.charts) is shown below.

![An example on how Charts can visualize incoming data](Charts/charts-example.png)

## How to build and run

Expand Down
31 changes: 31 additions & 0 deletions examples/example-node-telemetry/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/example-node-telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "A demonstration of how to use data ingest to store and visualize sensor data",
"main": "src/app.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"build": "tsc -p tsconfig.json",
"lint": "eslint --ext .js,.mjs,.ts .",
"start": "npm run build && node dist/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
20 changes: 10 additions & 10 deletions examples/example-node-telemetry/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
//
////////////////////////////////////////////////////////////////////////////

import os from "node:os";
import * as os from "node:os";
import { machineId } from "node-machine-id";

import createDebug from "debug";
export const debug = createDebug("realm:telemetry");

import Realm from "realm";
import { MachineInfo } from "./models/machine_info";
import { SensorReading } from "./models/sensor_reading";
import * as Realm from "realm";
import { MachineInfo } from "./models/MachineInfo";
import { SensorReading } from "./models/SensorReading";
import { config } from "./config";

const tenSeconds = 10 * 1000;
const INSERT_DATA_INTERVAL = 10_000 as const;

/**
* Samples system's load and memory
Expand All @@ -45,11 +45,11 @@ function readSensorData() {
* the platform/operating system and its version/release
* @returns machine identifier, platform, and version/release
*/
async function readMachineInfo() {
async function readMachineInfo(): Promise<MachineInfo> {
const identifier = await machineId();
const platform = os.platform();
const release = os.release();
return { identifier, platform, release };
return { identifier, platform, release } as MachineInfo;
}

async function main() {
Expand Down Expand Up @@ -80,14 +80,14 @@ async function main() {
const intervalId = setInterval(() => {
const now = new Date();
const measurement = readSensorData();
const obj: SensorReading = {
const obj = {
timestamp: now,
machineInfo,
...measurement,
};
} as unknown as SensorReading;
debug("Writing new sensor reading");
realm.write(() => realm.create(SensorReading, obj));
}, tenSeconds);
}, INSERT_DATA_INTERVAL);

// Catch CTRL-C in a nice way
process.stdin.resume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
//
////////////////////////////////////////////////////////////////////////////

import Realm, { ObjectSchema } from "realm";
import * as Realm from "realm";

export class MachineInfo extends Realm.Object {
identifier!: string;
platform!: string;
release!: string;

static schema: ObjectSchema = {
static schema: Realm.ObjectSchema = {
name: "MachineInfo",
embedded: true,
properties: {
identifier: "string",
platform: "string",
release: "string",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
//
////////////////////////////////////////////////////////////////////////////

import Realm, { BSON, ObjectSchema } from "realm";
import { MachineInfo } from "./machine_info";
import * as Realm from "realm";
import { MachineInfo } from "./MachineInfo";

export class SensorReading extends Realm.Object {
_id!: BSON.ObjectId;
_id!: Realm.BSON.ObjectId;
timestamp!: Date;
uptime!: number;
freemem!: number;
loadAvg!: Realm.List<number>;
machineInfo!: MachineInfo;

static schema: ObjectSchema = {
static schema: Realm.ObjectSchema = {
name: "SensorReading",
asymmetric: true,
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
_id: { type: "objectId", default: () => new Realm.BSON.ObjectId() },
timestamp: "date",
uptime: "float",
freemem: "int",
Expand Down
11 changes: 0 additions & 11 deletions examples/example-node-telemetry/tsconfig.build.json

This file was deleted.

8 changes: 6 additions & 2 deletions examples/example-node-telemetry/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"compilerOptions": {
"target": "es2018",
"module": "CommonJS",
"moduleResolution": "node",
"noEmit": true,
"noEmit": false,
"forceConsistentCasingInFileNames": true,
"types": [
"node",
],
"lib": [
"es2019"
]
],
"declaration": true,
"outDir": "./dist",
"skipLibCheck": true
},
"include": [
"src/**/*.ts"
Expand Down

0 comments on commit 10c390a

Please sign in to comment.