cleanup
This commit is contained in:
@@ -8,23 +8,70 @@ const predictionSchema = new mongoose.Schema({
|
||||
},
|
||||
predictedPower: {
|
||||
type: Number,
|
||||
required: true
|
||||
required: true,
|
||||
min: 0
|
||||
},
|
||||
weatherData: {
|
||||
type: Object,
|
||||
required: true
|
||||
date: String,
|
||||
temp_min: Number,
|
||||
temp_max: Number,
|
||||
humidity: Number,
|
||||
wind_speed: Number,
|
||||
icon: String,
|
||||
description: String,
|
||||
timestamp: Date
|
||||
},
|
||||
confidence: {
|
||||
type: Number,
|
||||
required: true
|
||||
required: true,
|
||||
min: 0,
|
||||
max: 1
|
||||
},
|
||||
factors: {
|
||||
cloudImpact: {
|
||||
type: Number,
|
||||
required: true,
|
||||
min: 0,
|
||||
max: 1
|
||||
},
|
||||
tempImpact: {
|
||||
type: Number,
|
||||
required: true,
|
||||
min: 0,
|
||||
max: 1
|
||||
},
|
||||
rainImpact: {
|
||||
type: Number,
|
||||
required: true,
|
||||
min: 0,
|
||||
max: 1
|
||||
},
|
||||
weatherFactor: {
|
||||
type: Number,
|
||||
required: true,
|
||||
min: 0,
|
||||
max: 1
|
||||
}
|
||||
},
|
||||
lastUpdated: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
default: Date.now,
|
||||
index: true
|
||||
}
|
||||
}, {
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
// Create compound index for efficient querying
|
||||
predictionSchema.index({ date: 1, lastUpdated: 1 });
|
||||
predictionSchema.index({ date: 1, lastUpdated: -1 });
|
||||
|
||||
export const Prediction = mongoose.models.Prediction || mongoose.model('Prediction', predictionSchema);
|
||||
predictionSchema.methods.isStale = function() {
|
||||
const sixHoursAgo = new Date(Date.now() - 6 * 60 * 60 * 1000);
|
||||
return this.lastUpdated < sixHoursAgo;
|
||||
};
|
||||
|
||||
predictionSchema.statics.getFreshPredictions = function() {
|
||||
const sixHoursAgo = new Date(Date.now() - 6 * 60 * 60 * 1000);
|
||||
return this.find({ lastUpdated: { $gte: sixHoursAgo } }).sort({ date: 1 });
|
||||
};
|
||||
|
||||
export const Prediction = mongoose.models.Prediction || mongoose.model('Prediction', predictionSchema);
|
||||
@@ -11,15 +11,12 @@ let client
|
||||
let clientPromise
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// In development mode, use a global variable so that the value
|
||||
// is preserved across module reloads caused by HMR (Hot Module Replacement).
|
||||
if (!global._mongoClientPromise) {
|
||||
client = new MongoClient(uri, options)
|
||||
global._mongoClientPromise = client.connect()
|
||||
}
|
||||
clientPromise = global._mongoClientPromise
|
||||
} else {
|
||||
// In production mode, it's best to not use a global variable.
|
||||
client = new MongoClient(uri, options)
|
||||
clientPromise = client.connect()
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ export async function fetchWeatherData() {
|
||||
)
|
||||
const data = await response.json()
|
||||
|
||||
// Process the data to get daily forecasts
|
||||
const dailyForecasts = data.list.reduce((acc, item) => {
|
||||
const date = new Date(item.dt * 1000)
|
||||
const day = date.toISOString().split('T')[0]
|
||||
@@ -67,10 +66,8 @@ export async function getWeatherData() {
|
||||
const { db } = await connectToDatabase()
|
||||
const collection = db.collection('weather_data')
|
||||
|
||||
// Get the latest weather data
|
||||
const latestData = await collection.findOne({}, { sort: { timestamp: -1 } })
|
||||
|
||||
// If no data exists or data is older than 2 hours, fetch new data
|
||||
if (!latestData || (Date.now() - latestData.timestamp.getTime() > 2 * 60 * 60 * 1000)) {
|
||||
const newData = await fetchWeatherData()
|
||||
await collection.insertOne({
|
||||
|
||||
Reference in New Issue
Block a user