<commit_before>// Licensed under the Apache License. See footer for details.

var path = require("path")

var async    = require("async")
var request  = require("request")
var lruCache = require("lru-cache")

var sampleCC = require("./sample-data/current-conditions")

//------------------------------------------------------------------------------
exports.create = createService

// Creates caches for the current (1 hour) and historical (1 year) weather data
var CacheCurrentCond  = lruCache({max: 500,  maxAge: 1000 * 60 * 60})
var CacheHistoricCond = lruCache({max: 3000, maxAge: 1000 * 60 * 60 * 24 * 365})

//------------------------------------------------------------------------------
function createService(url, apiKey) {
  return new Service(url, apiKey)
}

//------------------------------------------------------------------------------
function Service(url, apiKey) {
  this.url    = url
  this.apiKey = apiKey
}

Service.prototype.getCurrentConditions  = Service__getCurrentConditions
Service.prototype.getHistoricConditions = Service__getHistoricConditions
Service.prototype.getPastConditions = Service_getPastConditions
Service.prototype.predictConditions = Service_predictConditions

//------------------------------------------------------------------------------
// Gets weather data for lat/lon on current date
function Service__getCurrentConditions(lat, lon, cb) {
  // Gets cached version of current weather if present
  var cacheKey = lat + "," + lon
  var result = CacheCurrentCond.get(cacheKey)
  if (result) return cb(null, result)

  // Builds REST URL for requesting current weather
  var url = this.url + "/v1/geocode/" +
        lat + "/" + lon +
        "/observations/current.json"

  var requestOpts = {
    url: url,
    qs: {
      apiKey:   this.apiKey,
      language: "en-US",
      units:    "e"
    }
  }

  // Caches and returns current weather data for input lat/lon
  request(requestOpts, function(err, message, result) {
    cb(null, result)
  })
}

//------------------------------------------------------------------------------
// Gets weather data for lat/lon on past 10 years on current date
function Service__getHistoricConditions(lat, lon, cb) {
  var dates = []
  var date = new Date()
  var year = date.getYear()  + 1900
  var mon  = date.getMonth() + 1
  var day  = date.getDate()

  // Adjusts date if Feb 28
  if ((mon == 2) && (day == 29)) day = 28

  // Adds dates from past 10 years to dates[] array
  for (var i=1; i<11; i++) {
    var hDate = "" + (year-i) + "" + right(mon,0,2) + "" + right(day,0,2)
    dates.push(hDate)
  }

  var url    = this.url
  var apiKey = this.apiKey

  var dateReqs = dates.map(function(date, i) {
    return function(cb) { getOneHC(url, apiKey, lat, lon, dates[i], cb) }
  })<commit_msg>Replaced caching for current weather<commit_after>// Licensed under the Apache License. See footer for details.

var path = require("path")

var async    = require("async")
var request  = require("request")
var lruCache = require("lru-cache")

var sampleCC = require("./sample-data/current-conditions")

//------------------------------------------------------------------------------
exports.create = createService

// Creates caches for the current (1 hour) and historical (1 year) weather data
var CacheCurrentCond  = lruCache({max: 500,  maxAge: 1000 * 60 * 60})
var CacheHistoricCond = lruCache({max: 3000, maxAge: 1000 * 60 * 60 * 24 * 365})

//------------------------------------------------------------------------------
function createService(url, apiKey) {
  return new Service(url, apiKey)
}

//------------------------------------------------------------------------------
function Service(url, apiKey) {
  this.url    = url
  this.apiKey = apiKey
}

Service.prototype.getCurrentConditions  = Service__getCurrentConditions
Service.prototype.getHistoricConditions = Service__getHistoricConditions
Service.prototype.getPastConditions = Service_getPastConditions
Service.prototype.predictConditions = Service_predictConditions

//------------------------------------------------------------------------------
// Gets weather data for lat/lon on current date
function Service__getCurrentConditions(lat, lon, cb) {
  // Gets cached version of current weather if present
  var cacheKey = lat + "," + lon
  var result = CacheCurrentCond.get(cacheKey)
  if (result) return cb(null, result)

  // Builds REST URL for requesting current weather
  var url = this.url + "/v1/geocode/" +
        lat + "/" + lon +
        "/observations/current.json"

  var requestOpts = {
    url: url,
    qs: {
      apiKey:   this.apiKey,
      language: "en-US",
      units:    "e"
    }
  }

  // Caches and returns current weather data for input lat/lon
  request(requestOpts, function(err, message, result) {
    if (err !== null && err != undefined)
      CacheCurrentCond.set(cacheKey, result)
    cb(null, result)
  })
}

//------------------------------------------------------------------------------
// Gets weather data for lat/lon on past 10 years on current date
function Service__getHistoricConditions(lat, lon, cb) {
  var dates = []
  var date = new Date()
  var year = date.getYear()  + 1900
  var mon  = date.getMonth() + 1
  var day  = date.getDate()

  // Adjusts date if Feb 28
  if ((mon == 2) && (day == 29)) day = 28

  // Adds dates from past 10 years to dates[] array
  for (var i=1; i<11; i++) {
    var hDate = "" + (year-i) + "" + right(mon,0,2) + "" + right(day,0,2)
    dates.push(hDate)
  }

  var url    = this.url
  var apiKey = this.apiKey

  var dateReqs = dates.map(function(date, i) {
    return function(cb) { getOneHC(url, apiKey, lat, lon, dates[i], cb) }
  })
{'fraction_non_alphanumeric': 0.24497354497354498, 'fraction_numerical': 0.01904761904761905, 'mean_word_length': 1.9815983175604626, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3671219', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1582, 'n_words': 481}