Skip to content

Commit

Permalink
Including data generators
Browse files Browse the repository at this point in the history
  • Loading branch information
HikmaMuneer committed Apr 4, 2024
1 parent 44478e0 commit 0e809c9
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,69 @@ sql.connect(config)
console.error('Database connection failed: ',err);
});

// Function to generate random values for temperature, humidity, and air pressure
function generateWeatherData() {
const temperature = (Math.random() * (40 - 10) + 10).toFixed(2); // Random temperature between 10 and 40
const humidity = (Math.random() * (100 - 20) + 20).toFixed(2); // Random humidity between 20 and 100
const airPressure = (Math.random() * (1100 - 900) + 900).toFixed(2); // Random air pressure between 900 and 1100
return { temperature, humidity, airPressure };
}

// Function to insert weather data into the database for a specific district
async function insertWeatherDataForDistrict(district) {
try {
// Connect to the database
const pool = await sql.connect(config);

// Generate random weather data
const weatherData = generateWeatherData();

// Update the status of previous data for this district to 0
await pool.request()
.input('district', sql.NVarChar, district)
.query('UPDATE districts SET status = 0 WHERE name = @district AND status = 1');


// Insert the generated data into the database for the specified district
await pool.request()
.input('district', sql.NVarChar, district)
.input('temperature', sql.VarChar, weatherData.temperature.toString())
.input('humidity', sql.VarChar, weatherData.humidity.toString())
.input('airPressure', sql.VarChar, weatherData.airPressure.toString())
.query('INSERT INTO districts (name, temperature, humidity, air_pressure, status) VALUES (@district, @temperature, @humidity, @airPressure, 1)');

console.log(`Weather data updated successfully for district: ${district}`);
} catch (error) {
console.error(`Error updating data for district ${district}:`, error.message);
}
}


// Function to update weather data for all districts
async function updateWeatherDataForAllDistricts() {
try {
// Connect to the database
const pool = await sql.connect(config);

// Query all districts from the database
const result = await pool.request().query('SELECT name FROM districts');

// Close the database connection
await sql.close();

// Iterate over each district and update weather data
for (const district of result.recordset) {
await insertWeatherDataForDistrict(district.name);
}
} catch (error) {
console.error('Error updating weather data for all districts:', error.message);
}
}

// Schedule the weather data update to run every 5 minutes
setInterval(updateWeatherDataForAllDistricts, 5 * 60 * 1000);


//Endpoint to get all data
app.get('/weather', async (req, res) => {
try {
Expand Down Expand Up @@ -51,7 +114,7 @@ app.get('/weather/:district', async (req, res) => {
await sql.connect(config);

// Query to select data from the weather table based on district
const result = await sql.query`SELECT * FROM districts WHERE name = ${district}`;
const result = await sql.query`SELECT * FROM districts WHERE name = ${district} AND status = 1;`;

// Close the database connection
await sql.close();
Expand All @@ -64,9 +127,7 @@ app.get('/weather/:district', async (req, res) => {
}
});

// app.get('/', (req, res) => {
// res.send('Hello, World!');
// });


app.listen(port, () => {
console.log(`Server is running on port ${port}`);
Expand Down

0 comments on commit 0e809c9

Please sign in to comment.