A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/developer/products/mongodb/time-series-cpp/ below:

MongoDB Time Series With C++

Time series data is a set of data points collected at regular intervals. It’s a common use case in many industries such as finance, IoT, and telecommunications. MongoDB provides powerful features for handling time series data, and in this tutorial, we will show you how to build a C++ console application that uses MongoDB to store time series data, related to the Air Quality Index (AQI) for a given location. We will also take a look at MongoDB Charts to visualize the data saved in the time series.

Libraries used in this tutorial:

This tutorial uses Microsoft Windows 11 and Microsoft Visual Studio 2022 but the code used in this tutorial should work on any operating system and IDE, with minor changes.
  1. Microsoft Visual Studio setup with MongoDB C and C++ Driver installed. Follow the instructions in Getting Started with MongoDB and C++ to install MongoDB C/C++ drivers and set up the dev environment in Visual Studio.
  2. Your machine’s IP address is whitelisted. Note: You can add 0.0.0.0/0 as the IP address, which should allow access from any machine. This setting is not recommended for production use.

Launch powershell/terminal as an administrator and execute commands shared below.

1git clone https://github.com/Microsoft/vcpkg.git2cd vcpkg3./bootstrap-vcpkg.sh4./vcpkg integrate install
1./vcpkg install cpr:x64-windows

This tutorial assumes we are working with x64 architecture. If you are targeting x86, please use this command:

Note: Below warning (if encountered) can be ignored.

1# this is heuristically generated, and may not be correct2find_package(cpr CONFIG REQUIRED)3target_link_libraries(main PRIVATE cpr::cpr)
Source code available here In this tutorial, we will build an Air Quality Index (AQI) monitor that will save the AQI of a given location to a time series collection.

The AQI is a measure of the quality of the air in a particular area, with higher numbers indicating worse air quality. The AQI is based on a scale of 0 to 500 and is calculated based on the levels of several pollutants in the air.

Once we have set up a Visual Studio solution, let’s start with adding the necessary headers and writing the helper functions.

Note: Since we installed the cpr library with vcpkg, it automatically adds the needed include paths and dependencies to Visual Studio.

Navigate to the Solution Explorer panel, right-click on the solution name, and click “Properties.” Go to Configuration Properties > Debugging > Environment to add these environment variables as shown below.

1#pragma once234#include <mongocxx/client.hpp>5#include <bsoncxx/builder/list.hpp>6#include <bsoncxx/builder/stream/document.hpp>7#include <bsoncxx/json.hpp>8#include <mongocxx/uri.hpp>9#include <mongocxx/instance.hpp>101112#include <iostream>13#include <cpr/cpr.h>141516using namespace std;171819std::string getEnvironmentVariable(std::string environmentVarKey)20{21	char* pBuffer = nullptr;22	size_t size = 0;23	auto key = environmentVarKey.c_str();24	25	// Use the secure version of getenv, ie. _dupenv_s to fetch environment variable.  26	if (_dupenv_s(&pBuffer, &size, key) == 0 && pBuffer != nullptr)27	{28		std::string environmentVarValue(pBuffer);29		free(pBuffer);30		return environmentVarValue;31	}32	else33	{34		return "";35	}36}373839int getAQI(std::string city, std::string apiToken)40{41	// Call the API to get the air quality index.42	std::string aqiUrl = "https://api.waqi.info/feed/" + city + "/?token=" + apiToken;43	auto aqicnResponse = cpr::Get(cpr::Url{ aqiUrl });444546	// Get the AQI from the response47	if(aqicnResponse.text.empty())48	{49		cout << "Error: Response is empty." << endl;50		return -1;51	}52	bsoncxx::document::value aqicnResponseBson = bsoncxx::from_json(aqicnResponse.text);53	auto aqi = aqicnResponseBson.view()["data"]["aqi"].get_int32().value;54	return aqi;55}565758void saveToCollection(mongocxx::collection& collection, int aqi)59{60	auto timeStamp = bsoncxx::types::b_date(std::chrono::system_clock::now());616263	bsoncxx::builder::stream::document aqiDoc = bsoncxx::builder::stream::document{};64	aqiDoc << "timestamp" << timeStamp << "aqi" << aqi;65	collection.insert_one(aqiDoc.view());666768	// Log to the console window.69	cout << " TimeStamp: " << timeStamp << " AQI: " << aqi << endl;70}

With all the helper functions in place, let’s write the main function that will drive this application.

1int main()2{3	// Get the required parameters from environment variable.4	auto mongoURIStr = getEnvironmentVariable("MONGODB_URI");5	auto apiToken = getEnvironmentVariable("AQICN_TOKEN");6	std::string city = "Delhi";7	static const mongocxx::uri mongoURI = mongocxx::uri{ mongoURIStr };8910	if (mongoURI.to_string().empty() || apiToken.empty())11	{12		cout << "Invalid URI or API token. Please check the environment variables." << endl;13		return 0;14	}151617	// Create an instance.18	mongocxx::instance inst{};19	mongocxx::options::client client_options;20	auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 };21	client_options.server_api_opts(api);22	mongocxx::client conn{ mongoURI, client_options };232425	// Setup Database and Collection.26	const string dbName = "AQIMonitor";27	const string timeSeriesCollectionName = "AQIMonitorCollection";282930	// Setup Time Series collection options.31	bsoncxx::builder::document timeSeriesCollectionOptions =32	{33	   "timeseries",34		{35		  "timeField", "timestamp",36		  "granularity", "minutes"37		}38	};394041	auto aqiMonitorDB = conn[dbName];42	auto aqiMonitorCollection = aqiMonitorDB.has_collection(timeSeriesCollectionName)43		? aqiMonitorDB[timeSeriesCollectionName]44		: aqiMonitorDB.create_collection(timeSeriesCollectionName, timeSeriesCollectionOptions.view().get_document().value);454647	// Fetch and update AQI every 30 minutes.48	while (true)49	{ 50		auto aqi = getAQI(city, apiToken);51		saveToCollection(aqiMonitorCollection, aqi);52		std::this_thread::sleep_for(std::chrono::minutes(30));53	}545556	return 0;57}

When this application is executed, you can see the below activity in the console window.

You can also see the time series collection in Atlas reflecting any change made via the console application.

Visualizing the data with MongoDB Charts

We can make use of MongoDB Charts to visualize the AQI data and run aggregation on top of it.

Step 1: Go to MongoDB Charts and click on “Add Dashboard” to create a new dashboard — name it “AQI Monitor”.

Step 2: Click on “Add Chart”.

Step 3: In the “Select Data Source” dialog, go to the “Project” tab and navigate to the time series collection created by our code.

Step 4: Change the chart type to “Continuous Line”. We will use this chart to display the AQI trends over time.

Step 5: Drag and drop the “timestamp” and “aqi” fields into the X axis and Y axis respectively. You can customize the look and feel (like labels, color, and data format) in the “Customize” tab. Click “Save and close” to save the chart.

Step 6: Let’s add another chart to display the maximum AQI — click on “Add Chart” and select the same data source as before.

Step 7: Change the chart type to “Number”.

Step 8: Drag and drop the “aqi” field into “Aggregation” and change Aggregate to “MAX”.

Step 9: We can customize the chart text to change color based on the AQI values. Let’s make the text green if AQI is less than or equal to 100, and red otherwise. We can perform this action with the conditional formatting option under Customize tab.

Step 10: Similarly, we can add charts for minimum and average AQI. The dashboard should finally look something like this:

Tip: Change the dashboard’s auto refresh settings from “Refresh settings” button to choose a refresh time interval of your choice for the charts.

With this article, we covered creating an application in C++ that writes data to a MongoDB time series collection, and used it further to create a MongoDB Charts dashboard to visualize the data in a meaningful way. The application can be further expanded to save other parameters like PM2.5 and temperature. Now that you've learned how to create an application using the MongoDB C++ driver and MongoDB time series, put your new skills to the test by building your own unique application. Share your creation with the community and let us know how it turned out!

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4