每分钟/每小时记录 API 调用的最佳方式

发布于 2024-09-11 14:43:52 字数 885 浏览 3 评论 0原文

我们在 Rails Web 服务中使用反向地理编码,并且在通过 geokit 使用 Google 反向地理编码器时遇到了配额问题。我们还实施了 simple-geo 服务,我希望能够跟踪我们每分钟/小时发出的请求数。

对于跟踪我们的反向地理编码调用有什么建议吗?

我们的代码如下所示。你会做其中任何一个吗?

  • 每天在后台添加自定义记录器和进程
  • 使用我不知道的超级神奇的 gem,它可以轻松地进行配额和评级
  • 插入数据库调用并在那里进行查询。

注意:我不需要实时数据,只是想知道每小时内我们通常的请求和每小时的最大请求是多少。 (以及每月请求总数)

def use_simplegeo(lat, lng)
  SimpleGeo::Client.set_credentials(SIMPLE_GEO_OAUTHTOKEN, SIMPLE_GEO_OAUTHSECRET)
  # maybe do logging/tracking here?
  nearby_address = SimpleGeo::Client.get_nearby_address(lat, lng)

  located_location = LocatedLocation.new
  located_location.city = nearby_address[:place_name]
  located_location.county = nearby_address[:county_name]
  located_location.state = nearby_address[:state_code]
  located_location.country = nearby_address[:country]
  return located_location

end

谢谢!

We are using reverse-geocoding in a rails webservice, and have run into quota problems when using the Google reverse geocoder through geokit. We are also implementing the simple-geo service, and I want to be able to track how many requests per minute/hour we are making.

Any suggestions for tracking our reverse-geocoding calls?

Our code will look something like the following. Would you do any of these?

  • Add a custom logger and process in the background daily
  • Use a super-fantastic gem that I don't know about that does quotas and rating easily
  • Insert into database a call and do queries there.

Note: I don't need the data in real-time, just want to be able to know in an hourly period, what's our usual and max requests per hour. (and total monthly requests)

def use_simplegeo(lat, lng)
  SimpleGeo::Client.set_credentials(SIMPLE_GEO_OAUTHTOKEN, SIMPLE_GEO_OAUTHSECRET)
  # maybe do logging/tracking here?
  nearby_address = SimpleGeo::Client.get_nearby_address(lat, lng)

  located_location = LocatedLocation.new
  located_location.city = nearby_address[:place_name]
  located_location.county = nearby_address[:county_name]
  located_location.state = nearby_address[:state_code]
  located_location.country = nearby_address[:country]
  return located_location

end

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2024-09-18 14:43:52

这里的第一部分不是回答您所问的问题,但如果之前没有考虑过的话,我会有所帮助。

您是否考虑过不使用服务器(即通过 Geokit)进行反向地理编码,而是由客户端完成?换句话说,一些 Javascript 加载到用户的浏览器中,代表您的服务进行 Google 地理编码器 API 调用。

如果您的应用程序可以支持这种方法,那么这有很多优点:

  • 您可以解决配额问题,因为您的分布式用户每个都有自己的每日配额并且不会消耗您的配额
  • 您不会花费自己的服务器资源来执行此操作

如果您仍然想记录地理编码器查询,并且担心对主应用程序数据库的性能影响,那么您可以考虑以下选项之一:

  1. 只需创建一个单独的数据库(或多个数据库)用于日志记录(写入密集型)并执行它同步。可能是关系型的,但也许 MongoDB 或 Redis 可能会
  2. 记录到文件系统(使用自定义记录器),然后将这些数据批量写入结构化、可查询的存储中。如果效果更好的话,存储可以是外部的,例如亚马逊的 S3。
  3. 每次进行地理编码时,只需将记录写入 SimpleGeo 并向这些记录添加自定义元数据,以将它们与您自己的模型联系起来

The first part here is not answering the question you are asking but my be helpful if haven't considered it before.

Have you looked at not doing your reverse geocoding using your server (i.e. through Geokit) but instead having this done by the client? In other words some Javascript loaded into the user's browser making Google geocoder API calls on behalf of your service.

If your application could support this approach than this has a number of advantages:

  • You get around the quota problem because your distributed users each have their own daily quota and don't consume yours
  • You don't expend server resources of your own doing this

If you still would like to log your geocoder queries and you are concerned about the performance impact to your primary application database then you might consider one of the following options:

  1. Just create a separate database (or databases) for logging (which write intensive) and do it synchronously. Could be relational but perhaps MongoDB or Redis might work either
  2. Log to the file system (with a custom logger) and then cron these in batches into structured, queriable storage later. The storage could be external such as on Amazon's S3 if that works better.
  3. Just write a record into SimpleGeo each time you do a Geocode and add custom meta-data to those records to tie them back to your own model(s)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文