Skip to content

Commit

Permalink
Merge pull request #16 from gaveshalabs/dev
Browse files Browse the repository at this point in the history
Filter weather data
  • Loading branch information
lihini authored Jan 10, 2024
2 parents bdf5a95 + a142c5a commit 9e0eb05
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/modules/weather-data/weather-data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export class WeatherDataController {
async findAllByWeatherStationId(
@Query('weather_station_id', new ParseUUIDPipe({ version: '4' }))
weatherStationId: string,
@Query('from_date') dateFrom?: string, // Optional parameter
@Query('to_date') dateTo?: string, // Optional parameter
): Promise<GetWeatherDatumDto[]> {
return await this.weatherDataService.findAllByWeatherStationId(
weatherStationId,
dateFrom,
dateTo,
);
}

Expand Down
51 changes: 42 additions & 9 deletions src/modules/weather-data/weather-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ export class WeatherDataService {
transformWeatherDatum(datum) {
return {
_id: datum._id,
author_user_id: datum.metadata.author_user_id,
sensor_id: datum.metadata.sensor_id,
weather_station_id: datum.metadata.weather_station_id,
coordinates: datum.metadata.coordinates,
author_user_id: datum.metadata?.author_user_id,
sensor_id: datum.metadata?.sensor_id,
weather_station_id: datum.metadata?.weather_station_id,
coordinates: datum.metadata?.coordinates,
timestamp: datum.timestamp,
temperature: datum.temperature,
humidity: datum.humidity,
Expand Down Expand Up @@ -201,26 +201,59 @@ export class WeatherDataService {
});
}

/**
* Retrieves weather data by weather station ID within a specified date range.
* If no date range is provided, retrieves all weather data for the specified weather station.
*
* @param weatherStationId - The ID of the weather station.
* @param dateFrom - Optional. The starting date of the date range (inclusive)
* @param dateTo - Optional. The ending date of the date range (inclusive)
* @returns A promise that resolves to an array of GetWeatherDatumDto objects representing the weather data.
*/
async findAllByWeatherStationId(
weatherStationId: string,
dateFrom?: string,
dateTo?: string,
): Promise<GetWeatherDatumDto[]> {
// Convert date strings to actual Date objects, if they are defined
const fromDate = dateFrom ? new Date(dateFrom) : null;
const toDate = dateTo ? new Date(dateTo) : null;

// Create the query object with the weather station ID
const query: any = { 'metadata.weather_station_id': weatherStationId };

// If both dates are defined, use the $gte (greater than or equal) and $lte (less than or equal) operators.
// Inclusive ranges.
if (fromDate && toDate) {
query.timestamp = { $gte: fromDate, $lte: toDate };
} else if (fromDate) {
// If only the fromDate is defined, use $gte operator
query.timestamp = { $gte: fromDate };
} else if (toDate) {
// If only the toDate is defined, use $lte operator
query.timestamp = { $lte: toDate };
}

const data = (await this.weatherDatumModel
.find({ weather_station_id: weatherStationId })
.find(query)
.exec()) as WeatherDatum[];

return data.map((datum) => {
return this.transformWeatherDatum(datum);
});
// Transform and return the data
return data.map((datum) => this.transformWeatherDatum(datum));
}

async findLatestByWeatherStationId(
weatherStationId: string,
): Promise<GetWeatherDatumDto> {
const datum = (await this.weatherDatumModel
.findOne({ weather_station_id: weatherStationId })
.findOne({ "metadata.weather_station_id": weatherStationId })
.sort({ timestamp: -1 })
.exec()) as WeatherDatum;

if (!datum) {
return null;
}

return this.transformWeatherDatum(datum);
}

Expand Down

0 comments on commit 9e0eb05

Please sign in to comment.