<commit_before>var weather = require('openweathermap')
var db = null;
var city_id = 0;

// http://api.openweathermap.org/data/2.5/forecast?id=2643123&mode=json&appid=
// f3794e46bd7505e6a7746cb0379550ed
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes

exports.config = function(weather_config, city, database) {
    weather.defaults(weather_config);
    db = database;
    city_id = city;
    
    // Ensure the database is ready for use
    db.serialize(function() {
      db.run("CREATE TABLE IF NOT EXISTS daily_weather (timestamp INTEGER PRIMARY KEY, main TEXT, description TEXT, mapping TEXT, temp_min INTEGER, temp_max INTEGER, temp INTEGER)");
      db.run("CREATE TABLE IF NOT EXISTS hourly_weather (timestamp INTEGER PRIMARY KEY, main TEXT, description TEXT, mapping TEXT, temp_min INTEGER, temp_max INTEGER, temp INTEGER)");
      db.run("CREATE TABLE IF NOT EXISTS update_times (timestamp INTEGER)");
    });
}

exports.start = function(update_minutes) {
    if(!db) { 
        console.error("No database has been defined, use config() first!");
        return;
    }
    
    db.get("SELECT * FROM update_times ORDER BY timestamp DESC LIMIT 1", function(err, res) {
        if(res && (getTimestamp() - res.timestamp < (update_minutes * 60 * 1000 * 0.5))) 
            console.log("Not updating as we have updated in the last "+update_minutes*0.5+" minutes.");
        else
            updateWeatherCache();
    });
    setInterval(updateWeatherCache, update_minutes * 60 * 1000);   
}

exports.getWeatherAt = function(timestamp, callback) {

    db.get("SELECT * FROM hourly_weather ORDER BY timestamp DESC LIMIT 1", function(err, res) {
        // We need to check daily if hourly is too old
        var oldestHourly = getTimestamp();
        if(res && res.timestamp)
            oldestHourly = res.timestamp;
       
        if(oldestHourly < timestamp)
            getHourlyWeather(timestamp, callback); 
        else
            getDailyWeather(timestamp, callback); 
    });

}

exports.getDayWeatherAt = function(timestamp, callback) {
    getDailyWeather(timestamp, callback);
}

function getHourlyWeather(timestamp, callback) {
    var hourly = db.prepare("SELECT * FROM hourly_weather WHERE timestamp <= $timestamp ORDER BY timestamp DESC LIMIT 1");
    hourly.get({$timestamp: timestamp}, function(err, res) {
        callback(res);
    });
}

function getDailyWeather(timestamp, callback) {
    var daily = db.prepare("SELECT * FROM daily_weather WHERE timestamp <= $timestamp ORDER BY timestamp DESC LIMIT 1");
    daily.get({$timestamp: timestamp}, function(err, res) {
        callback(res);
    });
}

function getTimestamp() {
    return Math.round(new Date().getTime()/1000);
}

function updateWeatherCache() {
    var insert = db.prepare("INSERT INTO update_times VALUES ($timestamp)");
    console.log("Updating the stored weather information");
    
    // The 3 hours forecast is available for 5 days. Daily forecast is available for 14 days
    /*weather.now({id: MANCHESTER_CITY_ID}, function(data) {      
        weatherCache.now = weatherDataToWeatherObject(data);
    });*/
    
    var insertHourly = db.prepare("INSERT OR IGNORE INTO hourly_weather (timestamp, main, description, mapping, temp_min, temp_max, temp)"
                                + "VALUES ($timestamp, $main, $description, $mapping, $temp_min, $temp_max, $temp)");
    weather.forecast({id: city_id, cnt: 5}, function(reply) {                   
        reply.list.forEach(function(data) {
            insertHourly.run(weatherDataToWeatherObject(data));
        })
        insert.run({$timestamp: getTimestamp()});
        console.log("Update of hourly weather complete");
    });

    var insertDaily = db.prepare("INSERT OR IGNORE INTO daily_weather (timestamp, main, description, mapping, temp_min, temp_max, temp)"
                               + "VALUES ($timestamp, $main, $description, $mapping, $temp_min, $temp_max, $temp)");
    weather.daily({id: city_id, cnt: 14}, function(reply) {
        reply.list.forEach(function(data) {
            insertDaily.run(weatherDataToWeatherObject(data));
        })
        insert.run({$timestamp: getTimestamp()});
        console.log("Update of daily weather complete");
    });
}

function weatherDataToWeatherObject(data) {
    // weather_type: snow, rain, hail, thunder, sunny, cloudy
    var weathermapping = {<commit_msg>Use .info and fix bug with db queries<commit_after>var weather = require('openweathermap')
var db = null;
var city_id = 0;

// http://api.openweathermap.org/data/2.5/forecast?id=2643123&mode=json&appid=
// f3794e46bd7505e6a7746cb0379550ed
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes

exports.config = function(weather_config, city, database) {
    weather.defaults(weather_config);
    db = database;
    city_id = city;
    
    // Ensure the database is ready for use
    db.serialize(function() {
      db.run("CREATE TABLE IF NOT EXISTS daily_weather (timestamp INTEGER PRIMARY KEY, main TEXT, description TEXT, mapping TEXT, temp_min INTEGER, temp_max INTEGER, temp INTEGER)");
      db.run("CREATE TABLE IF NOT EXISTS hourly_weather (timestamp INTEGER PRIMARY KEY, main TEXT, description TEXT, mapping TEXT, temp_min INTEGER, temp_max INTEGER, temp INTEGER)");
      db.run("CREATE TABLE IF NOT EXISTS update_times (timestamp INTEGER)");
    });
}

exports.start = function(update_minutes) {
    if(!db) { 
        console.error("No database has been defined, use config() first!");
        return;
    }
    
    db.get("SELECT * FROM update_times ORDER BY timestamp DESC LIMIT 1", function(err, res) {
        if(res && (getTimestamp() - res.timestamp < (update_minutes * 60 * 1000 * 0.5))) 
            console.info("Not updating weather as we have updated in the last "+update_minutes*0.5+" minutes.");
        else
            updateWeatherCache();
    });
    setInterval(updateWeatherCache, update_minutes * 60 * 1000);   
}

exports.getWeatherAt = function(timestamp, callback) {

    db.get("SELECT * FROM hourly_weather ORDER BY timestamp DESC LIMIT 1", function(err, res) {
        // We need to check daily if hourly is too old
        var oldestHourly = getTimestamp();
        if(res && res.timestamp)
            oldestHourly = res.timestamp;
       
        if(oldestHourly >= timestamp)
            getHourlyWeather(timestamp, callback); 
        else
            getDailyWeather(timestamp, callback); 
    });

}

exports.getDayWeatherAt = function(timestamp, callback) {
    getDailyWeather(timestamp, callback);
}

function getHourlyWeather(timestamp, callback) {
    var hourly = db.prepare("SELECT * FROM hourly_weather WHERE timestamp <= $timestamp ORDER BY timestamp DESC LIMIT 1");
    hourly.get({$timestamp: timestamp}, function(err, res) {
        if(err) 
            console.warn("While searching hourly weather: ", err)
        callback(res, timestamp);
    });
}

function getDailyWeather(timestamp, callback) {
    var daily = db.prepare("SELECT * FROM daily_weather WHERE timestamp <= $timestamp ORDER BY timestamp DESC LIMIT 1");
    daily.get({$timestamp: timestamp}, function(err, res) {
        if(err) 
            console.warn("While searching daily weather: ", err)
        callback(res, timestamp);
    });
}

function getTimestamp() {
    return Math.round(new Date().getTime()/1000);
}

function updateWeatherCache() {
    var insert = db.prepare("INSERT INTO update_times VALUES ($timestamp)");
    console.info("Updating the stored weather information");
    
    // The 3 hours forecast is available for 5 days. Daily forecast is available for 14 days
    /*weather.now({id: MANCHESTER_CITY_ID}, function(data) {      
        weatherCache.now = weatherDataToWeatherObject(data);
    });*/
    
    var insertHourly = db.prepare("INSERT OR IGNORE INTO hourly_weather (timestamp, main, description, mapping, temp_min, temp_max, temp)"
                                + "VALUES ($timestamp, $main, $description, $mapping, $temp_min, $temp_max, $temp)");
    weather.forecast({id: city_id, cnt: 5}, function(reply) {                   
        reply.list.forEach(function(data) {
            insertHourly.run(weatherDataToWeatherObject(data));
        })
        insert.run({$timestamp: getTimestamp()});
        console.info("Update of hourly weather complete");
    });

    var insertDaily = db.prepare("INSERT OR IGNORE INTO daily_weather (timestamp, main, description, mapping, temp_min, temp_max, temp)"
                               + "VALUES ($timestamp, $main, $description, $mapping, $temp_min, $temp_max, $temp)");
    weather.daily({id: city_id, cnt: 14}, function(reply) {
        reply.list.forEach(function(data) {
            insertDaily.run(weatherDataToWeatherObject(data));
        })
        insert.run({$timestamp: getTimestamp()});
        console.info("Update of daily weather complete");
    });
}

function weatherDataToWeatherObject(data) {
    // weather_type: snow, rain, hail, thunder, sunny, cloudy
    var weathermapping = {
{'fraction_non_alphanumeric': 0.11842686699072029, 'fraction_numerical': 0.013919575784357048, 'mean_word_length': 3.2422680412371134, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2606518', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2555, 'n_words': 698}