|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package tasks |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/http" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/apache/incubator-devlake/core/errors" |
| 25 | + "github.com/apache/incubator-devlake/core/plugin" |
| 26 | + helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 27 | +) |
| 28 | + |
| 29 | +func newDailyUsageDateRangeIterator(since *time.Time) *helper.QueueIterator { |
| 30 | + startMs, endMs := computeUsageTimeRangeMs(since, time.Now().UTC()) |
| 31 | + iter := helper.NewQueueIterator() |
| 32 | + for _, chunk := range splitDailyUsageTimeRangeMs(startMs, endMs, cursorDailyUsageMaxDays) { |
| 33 | + iter.Push(chunk) |
| 34 | + } |
| 35 | + return iter |
| 36 | +} |
| 37 | + |
| 38 | +func dailyUsagePostBody(reqData *helper.RequestData) map[string]interface{} { |
| 39 | + input := reqData.Input.(cursorTimeRangeInput) |
| 40 | + page := 1 |
| 41 | + if state, ok := reqData.CustomData.(cursorPageState); ok && state.Page > 0 { |
| 42 | + page = state.Page |
| 43 | + } else if reqData.Pager.Page > 0 { |
| 44 | + page = reqData.Pager.Page |
| 45 | + } |
| 46 | + return map[string]interface{}{ |
| 47 | + "startDate": input.StartDateMs, |
| 48 | + "endDate": input.EndDateMs, |
| 49 | + "page": page, |
| 50 | + "pageSize": reqData.Pager.Size, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// CollectDailyUsage collects per-user per-day adoption metrics from POST /teams/daily-usage-data. |
| 55 | +func CollectDailyUsage(taskCtx plugin.SubTaskContext) errors.Error { |
| 56 | + data, ok := taskCtx.TaskContext().GetData().(*CursorTaskData) |
| 57 | + if !ok { |
| 58 | + return errors.Default.New("task data is not CursorTaskData") |
| 59 | + } |
| 60 | + apiClient, err := CreateApiClient(taskCtx.TaskContext(), data.Connection) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + rawArgs := helper.RawDataSubTaskArgs{ |
| 66 | + Ctx: taskCtx, |
| 67 | + Table: rawDailyUsageTable, |
| 68 | + Options: rawParamsFromTaskData(data), |
| 69 | + } |
| 70 | + |
| 71 | + collector, err := helper.NewStatefulApiCollector(rawArgs) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + err = collector.InitCollector(helper.ApiCollectorArgs{ |
| 77 | + ApiClient: apiClient, |
| 78 | + Input: newDailyUsageDateRangeIterator(collector.GetSince()), |
| 79 | + Method: http.MethodPost, |
| 80 | + UrlTemplate: "teams/daily-usage-data", |
| 81 | + PageSize: cursorApiPageSize, |
| 82 | + RequestBody: dailyUsagePostBody, |
| 83 | + GetNextPageCustomData: nextPostPage, |
| 84 | + ResponseParser: parseDailyUsageResponse, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + return collector.Execute() |
| 91 | +} |
0 commit comments