Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何优化多个google api日历freeBussy查询_Go_Google Api_Google Calendar Api - Fatal编程技术网

如何优化多个google api日历freeBussy查询

如何优化多个google api日历freeBussy查询,go,google-api,google-calendar-api,Go,Google Api,Google Calendar Api,我正在从事一个GoRESTAPI项目,在该项目中,我收到一个具有不同间隔的POST请求。这反映了商店的营业时间,以下是一个示例: { ... "intervals": [ { "day": "1600347600", "starthours": "800", "endhours&quo

我正在从事一个GoRESTAPI项目,在该项目中,我收到一个具有不同间隔的POST请求。这反映了商店的营业时间,以下是一个示例:

{
    ...
    "intervals": [
        {
            "day": "1600347600",
            "starthours": "800",
            "endhours": "1200"
        },
        {
            "day": "1600434000",
            "starthours": "1300",
            "endhours": "1700"
        },
        {
            "day": "1600520400",
            "starthours": "800",
            "endhours": "1200"
        },
        ...
    ]
}

我向谷歌提出请求的代码如下:

    // Some code to get the token
    
    //We treat every interval obj sent on the params
    if len(sc.Intervals) > 0 {
        for _, i := range sc.Intervals {
            //This is a necessary step as the format of the dates that the client gives me
            //are 
            dStart, dEnd, err := getDateStartAndEnd(i)
            if err != nil {
                log.Error(err)
                return responseInterval, err
            }
            //0. build search criteria
            var query = events.SearchCriteria{
                Email:          sc.Owner.Email,
                FreeBussyQuery: calendar.FreeBusyRequest{
                    TimeMin: dStart.Format(time.RFC3339),
                    TimeMax: dEnd.Format(time.RFC3339),
                },
            }
            isFree, err := calendarService.Freebusy.Query(setCalendarId(query)).Do()
            if err != nil {
                log.Error(err)
            }
            //In this function I do two things, get all the "slots" of 15m availables
            //in an given interval, then compare them to the bussy answer of Google
            //to fill a new slice with free "slots"
            freeIntervales := findFreeIntervales(isFree,dStart,dEnd)
            responseInterval.Interval = append(responseInterval.Interval,freeIntervales)
        }
    }
虽然我承认这可能不是最有效的算法,但我这里的主要问题是,比如说,以8个间隔执行请求需要5秒钟

有没有更好的方法查询谷歌日历? 或者我应该在这里修改我的逻辑?我曾想过只进行一次多次间隔的查询来检查谷歌日历,但这似乎是不可能的


一个可能的解决办法是我最早和最晚的时间提出一个独特的要求。这是一个可行的解决方案吗?

最后,我意识到为什么我只能打五个电话给谷歌,而我只能打一个?这戏剧性地加快了速度。以前需要5-6秒,现在是900-1400毫秒

我只是简单地记下了第一次intervale的第一个日期,以及最后一次intervale的最后一个日期,然后让Google给我这个范围内所有繁忙的细节

    if len(sc.Intervals) > 0 {

    dStartResult, _, err:= getDateStartAndEnd(sc.Intervals[0])
    if err != nil {
        log.Error(err)
    }
    _, dEndResultLast, err:= getDateStartAndEnd(sc.Intervals[len(sc.Intervals)-1])
    if err != nil {
        log.Error(err)
    }

    //0. build search criteria
    var query = events.SearchCriteria{
        Email:          sc.Owner.Email,
        FreeBussyQuery: calendar.FreeBusyRequest{
            TimeMin: dStartResult.Format(time.RFC3339),
            TimeMax: dEndResultLast.Format(time.RFC3339),
        },
    }
    isFree, err := calendarService.Freebusy.Query(setCalendarId(query)).Do()
    if err != nil {
        log.Error(err)
    }

    for _, i := range sc.Intervals {
        ....
        //Do some magic to get the free intervals