How to use Truncate method of internal Package

Best Ginkgo code snippet using internal.Truncate

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

truncate.go

Source: truncate.go Github

copy
1package topic
2
3import (
4	"fmt"
5
6	"github.com/ovh/tat"
7	"github.com/ovh/tat/tatcli/internal"
8	"github.com/spf13/cobra"
9)
10
11var force bool
12
13func init() {
14	cmdTopicTruncate.Flags().BoolVarP(&force, "force", "", false, "--force : truncate without asking confirmation")
15}
16
17var cmdTopicTruncate = &cobra.Command{
18	Use:   "truncate",
19	Short: "Remove all messages in a topic, only for tat admin and administrators on topic : tatcli topic truncate <topic> [--force]",
20	Run: func(cmd *cobra.Command, args []string) {
21		if len(args) == 1 {
22			topicTruncate(args[0])
23		} else {
24			internal.Exit("Invalid argument: tatcli topic truncate --help\n")
25		}
26	},
27}
28
29func topicTruncate(topic string) {
30	j := tat.TopicNameJSON{Topic: topic}
31	if force {
32		out, err := internal.Client().TopicTruncate(j)
33		internal.Check(err)
34		if internal.Verbose {
35			internal.Print(out)
36		}
37	} else {
38		fmt.Print("Are you really sure ? You will delete all messages even if a user has a message in his tasks. Please enter again topic name to confirm: ")
39		var confirmTopic string
40		fmt.Scanln(&confirmTopic)
41
42		if confirmTopic == topic {
43			fmt.Printf("Please enter 'yes' to confirm removing all messages from %s: ", topic)
44			var confirmYes string
45			fmt.Scanln(&confirmYes)
46			if confirmYes == "yes" {
47				out, err := internal.Client().TopicTruncate(j)
48				internal.Check(err)
49				internal.Print(out)
50				return
51			}
52		} else {
53			fmt.Printf("Error. You enter %s instead of %s\n", confirmTopic, topic)
54		}
55		fmt.Println("Nothing done")
56	}
57}
58
Full Screen

truncate_test.go

Source: truncate_test.go Github

copy
1// Copyright 2015-2018 Kuzzle
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// 		http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package collection_test
16
17import (
18	"fmt"
19
20	"github.com/kuzzleio/sdk-go/collection"
21
22	"testing"
23
24	"github.com/kuzzleio/sdk-go/internal"
25	"github.com/kuzzleio/sdk-go/kuzzle"
26	"github.com/kuzzleio/sdk-go/types"
27	"github.com/stretchr/testify/assert"
28)
29
30func TestTruncateIndexNull(t *testing.T) {
31	k, _ := kuzzle.NewKuzzle(&internal.MockedConnection{}, nil)
32	nc := collection.NewCollection(k)
33	err := nc.Truncate("", "collection", nil)
34	assert.NotNil(t, err)
35}
36
37func TestTruncateCollectionNull(t *testing.T) {
38	k, _ := kuzzle.NewKuzzle(&internal.MockedConnection{}, nil)
39	nc := collection.NewCollection(k)
40	err := nc.Truncate("index", "", nil)
41	assert.NotNil(t, err)
42}
43
44func TestTruncateError(t *testing.T) {
45	c := &internal.MockedConnection{
46		MockSend: func(query []byte, options types.QueryOptions) *types.KuzzleResponse {
47			return &types.KuzzleResponse{Error: types.KuzzleError{Message: "Unit test error"}}
48		},
49	}
50	k, _ := kuzzle.NewKuzzle(c, nil)
51
52	nc := collection.NewCollection(k)
53	err := nc.Truncate("index", "collection", nil)
54	assert.NotNil(t, err)
55}
56
57func TestTruncate(t *testing.T) {
58	c := &internal.MockedConnection{
59		MockSend: func(query []byte, options types.QueryOptions) *types.KuzzleResponse {
60			return &types.KuzzleResponse{Result: []byte(`{
61				"acknowledged": true
62			}`)}
63		},
64	}
65	k, _ := kuzzle.NewKuzzle(c, nil)
66
67	nc := collection.NewCollection(k)
68	err := nc.Truncate("index", "collection", nil)
69	assert.Nil(t, err)
70}
71
72func ExampleCollection_Truncate() {
73	c := &internal.MockedConnection{}
74	k, _ := kuzzle.NewKuzzle(c, nil)
75
76	nc := collection.NewCollection(k)
77	err := nc.Truncate("index", "collection", nil)
78
79	if err != nil {
80		fmt.Println(err.Error())
81		return
82	}
83}
84
Full Screen

stat.go

Source: stat.go Github

copy
1package services
2
3import "stock/utils"
4
5var StatPathMap = map[string]string{
6	"other":"其它",
7	"/pub/coin_price/:coin/:vs_coin/:timestamp":                                 "获取币价换算,多节点签名版",
8	"/pub/dex/ftx_chart_prices/:coin_type/:count/:interval/:timestamp":          "获取杠杆btc代币不同时间区间的价格图表信息",
9	"/pub/dex/ftx_price/:coin_type/:data_type/:timestamp":                       "获取ftx token价格信息",
10	"/pub/dex/lp_price/:pair/:timestamp":                                        "获取lp价格信息",
11	"/pub/dex/stock_chart_prices/:coin_type/:count/:interval/:timestamp":        "获取股票不同时间区间的价格图表信息",
12	"/pub/dex/token_chain_price/:token/:data_type/:timestamp":                   "获取token链上价格信息",
13	"/pub/dex/token_chart_prices/:token/:count/:interval/:timestamp":            "获取token不同时间区间的价格图表信息",
14	"/pub/dex/token_day_datas/:token/:days/:timestamp":                          "获取token相应天数的统计图表信息",
15	"/pub/dex/token_price/:token/:data_type/:timestamp":                         "获取token价格信息",
16	"/pub/internal/coin_price/:coin/:vs_coin":                                   "获取币价换算,内部单节点",
17	"/pub/internal/dex/ftx_price/:coin_type/:timestamp":                         "获取ftx coin最近一小时最高最低价格信息,内部单节点",
18	"/pub/internal/dex/lp_price/:pair/:timestamp":                               "获取lp价格信息,内部单节点:",
19	"/pub/internal/dex/token_chain_price/:token/:timestamp":                     "获取token最近一小时最高最低价格信息,内部单节点模式",
20	"/pub/internal/dex/token_info/:token/:timestamp":                            "获取token信息,内部单节点",
21	"/pub/internal/dex/token_price/:token/:timestamp":                           "获取token最近一小时最高最低价格信息,内部单节点",
22	"/pub/internal/stock_avgprice":                                              "获取股票平均价格共识:",
23	"/pub/internal/token_avgprice":                                              "获取token平均价格共识:",
24	"/pub/stock/aggre_info/:code/:data_type/:timestamp":                         "获取共识美股价格:",
25	"/pub/stock/any_api":                                                        "当前节点any-api",
26	"/pub/stock/any_apis":                                                       "所有节点any-api",
27	"/pub/stock/info/:code/:data_type/:timestamp":                               "获取美股价格",
28	"/pub/stock/market_status/:timestamp":                                       "获取美股市场开盘状态",
29	"/pub/stock/stat":                                                           "当前节点状态:记录数,钱包地址",
30	"/pub/stock/stats":                                                          "所有节点状态:记录数,钱包地址",
31	"/pub/dex/pair/token_chart_prices/:pair/:token/:count/:interval/:timestamp": "从Pair获取token不同时间区间的价格图表信息",
32	"/pub/dex/pair/token_price/:pair/:token/:data_type/:timestamp":              "从Pair获取token价格信息",
33	"/pub/internal/dex/pair/token_info/:pair/:token/:timestamp":                 "从pair获取token信息,内部单节点",
34	"/pub/internal/dex/pair/token_price/:pair/:token/:timestamp":                "从Pair获取token最近一小时最高最低价格信息,内部单节点",
35	"/pub/dex/token/token_chart_supply/:token/:amount/:timestamp":               "获取某个token的totalSupply的变化量",
36}
37var StatPath2IDMap = map[string]int{
38	"/pub/coin_price/:coin/:vs_coin/:timestamp":                                 1,
39	"/pub/dex/ftx_chart_prices/:coin_type/:count/:interval/:timestamp":          2,
40	"/pub/dex/ftx_price/:coin_type/:data_type/:timestamp":                       3,
41	"/pub/dex/lp_price/:pair/:timestamp":                                        4,
42	"/pub/dex/stock_chart_prices/:coin_type/:count/:interval/:timestamp":        5,
43	"/pub/dex/token_chain_price/:token/:data_type/:timestamp":                   6,
44	"/pub/dex/token_chart_prices/:token/:count/:interval/:timestamp":            7,
45	"/pub/dex/token_day_datas/:token/:days/:timestamp":                          8,
46	"/pub/dex/token_price/:token/:data_type/:timestamp":                         9,
47	"/pub/internal/coin_price/:coin/:vs_coin":                                   10,
48	"/pub/internal/dex/ftx_price/:coin_type/:timestamp":                         11,
49	"/pub/internal/dex/lp_price/:pair/:timestamp":                               12,
50	"/pub/internal/dex/token_chain_price/:token/:timestamp":                     13,
51	"/pub/internal/dex/token_info/:token/:timestamp":                            14,
52	"/pub/internal/dex/token_price/:token/:timestamp":                           15,
53	"/pub/internal/stock_avgprice":                                              16,
54	"/pub/internal/token_avgprice":                                              17,
55	"/pub/stock/aggre_info/:code/:data_type/:timestamp":                         18,
56	"/pub/stock/any_api":                                                        19,
57	"/pub/stock/any_apis":                                                       20,
58	"/pub/stock/info/:code/:data_type/:timestamp":                               21,
59	"/pub/stock/market_status/:timestamp":                                       22,
60	"/pub/stock/stat":                                                           23,
61	"/pub/stock/stats":                                                          24,
62	"/pub/dex/pair/token_chart_prices/:pair/:token/:count/:interval/:timestamp": 25,
63	"/pub/dex/pair/token_price/:pair/:token/:data_type/:timestamp":              26,
64	"/pub/internal/dex/pair/token_info/:pair/:token/:timestamp":                 27,
65	"/pub/internal/dex/pair/token_price/:pair/:token/:timestamp":                28,
66	"/pub/dex/token/token_chart_supply/:token/:amount/:timestamp":               29,
67}
68var StatID2PathMap = map[int]string{ }
69var StatID2ResMap = map[int]string{ }
70
71func init(){
72	StatPath2IDMap["other"]=100
73	for key, value := range StatPath2IDMap {
74		StatID2PathMap[value]=key
75		StatID2ResMap[value]=StatPathMap[key]
76	}
77}
78
79func ApiStat()(map[string]interface{},error) {
80	res := map[string]interface{}{}
81	allStat := map[string]interface{}{}
82	err := utils.Orm.Raw(
83		`select substr(from_unixtime(min(timestamp)),1,10) begin_date,
84       sum(stats.counter)                                                   counter
85from api_stats stats
86where stats.is_internal = 0`).First(&allStat).Error
87	res["allStat"] = allStat
88
89	type apiRankStat struct{
90		PathId int
91		Counter int
92		PathName string
93	}
94	apiRankStats := []*apiRankStat{}
95	err = utils.Orm.Raw(
96		`select stats.path_id,
97       sum(stats.counter) counter
98from api_stats stats
99 where stats.is_internal=0 and stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
100group by stats.path_id  order by counter desc;`).
101		Find(&apiRankStats).Error
102	if err == nil {
103		for _, item := range apiRankStats {
104			item.PathName=StatID2ResMap[item.PathId]
105		}
106		res["apiRankStats"] = apiRankStats
107	}
108
109	day30Stat := []map[string]interface{}{}
110	err = utils.Orm.Raw(
111		`select substr(from_unixtime(aa.timespan),1,10) daystr,
112       sum((case when is_internal = 1 then 0  else aa.counter end)) as oss,
113       sum((case when is_internal = 1 then aa.counter else 0 end)) as internal
114from (
115         select stats.is_internal,
116                (truncate(timestamp / (3600 * 24), 0) * 3600 * 24) as timespan,
117                sum(stats.counter)                                                 counter
118         from api_stats stats
119#          where stats.is_internal = 0
120         where stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
121         group by stats.is_internal, timespan
122     ) aa
123group by daystr`).
124		Find(&day30Stat).Error
125	if err == nil {
126		res["day30Stat"] = day30Stat
127	}
128
129	day30HourRankStat := []map[string]interface{}{}
130	err = utils.Orm.Raw(
131		`select concat( hour(from_unixtime(truncate(timestamp / (3600), 0) * 3600)),'点') as hour_name,
132#                 stats.is_internal,
133#                 hour(date_add(
134#                         FROM_UNIXTIME(0), interval
135#                         (truncate(timestamp / (3600), 0) * 3600) +
136#                         TIMESTAMPDIFF(SECOND, NOW(), UTC_TIMESTAMP()) SECOND
137#                     ))             timespan,
138       sum(stats.counter)                                             counter
139from api_stats stats
140where stats.is_internal = 0
141  and stats.timestamp > truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24 - (3600 * 24) * 30
142group by hour_name
143order by counter desc`).
144		Find(&day30HourRankStat).Error
145	if err == nil {
146		res["day30HourRankStat"] = day30HourRankStat
147	}
148
149	last48HourStat:= []map[string]interface{}{}
150	err = utils.Orm.Raw(
151		`select from_unixtime(truncate(timestamp / (3600), 0) * 3600) as datestr,
152sum(stats.counter)                                             counter
153from api_stats stats
154where stats.is_internal = 0
155and stats.timestamp > truncate(unix_timestamp() / (3600), 0) * 3600 - 3600 * 48
156group by datestr;`).
157		Find(&last48HourStat).Error
158	if err == nil {
159		res["last48HourStat"] = last48HourStat
160	}
161
162	last48Hour10MinStat:= []map[string]interface{}{}
163	err = utils.Orm.Raw(
164		`select from_unixtime(truncate(timestamp / (600), 0) * 600) as datestr,
165sum(stats.counter)                                             counter
166from api_stats stats
167where stats.is_internal = 0
168and stats.timestamp > truncate(unix_timestamp() / (600), 0) * 600 - 3600 * 48
169group by datestr`).
170		Find(&last48Hour10MinStat).Error
171	if err == nil {
172		res["last48Hour10MinStat"] = last48Hour10MinStat
173	}
174	return res, err
175}
176
177/*
178
179-- 总访问次数
180select substr(from_unixtime(min(timestamp)),1,10) begin_date,
181       sum(stats.counter)                                                   counter
182from api_stats stats
183where stats.is_internal = 0;
184
185
186-- 最近30天访问最多的api
187select stats.path_id,
188       sum(stats.counter) counter
189from api_stats stats
190 where stats.is_internal=0 and stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
191group by stats.path_id  order by counter desc;
192
193
194
195-- 最近30天每天访问次数统计
196select substr(from_unixtime(aa.timespan),1,10) daystr,
197       sum((case when is_internal = 1 then 0  else aa.counter end)) as oss,
198       sum((case when is_internal = 1 then aa.counter else 0 end)) as internal
199from (
200         select stats.is_internal,
201                (truncate(timestamp / (3600 * 24), 0) * 3600 * 24) as timespan,
202                sum(stats.counter)                                                 counter
203         from api_stats stats
204#          where stats.is_internal = 0
205         where stats.timestamp>truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24- (3600 * 24)*30
206         group by stats.is_internal, timespan
207     ) aa
208group by daystr;
209
210
211-- 最近30天小时次数排名.
212select concat( hour(from_unixtime(truncate(timestamp / (3600), 0) * 3600)),'点') as hour_name,
213#                 stats.is_internal,
214#                 hour(date_add(
215#                         FROM_UNIXTIME(0), interval
216#                         (truncate(timestamp / (3600), 0) * 3600) +
217#                         TIMESTAMPDIFF(SECOND, NOW(), UTC_TIMESTAMP()) SECOND
218#                     ))             timespan,
219       sum(stats.counter)                                             counter
220from api_stats stats
221where stats.is_internal = 0
222  and stats.timestamp > truncate(unix_timestamp() / (3600 * 24), 0) * 3600 * 24 - (3600 * 24) * 30
223group by hour_name
224order by counter desc
225
226
227-- 最近48小时每小时访问量
228select from_unixtime(truncate(timestamp / (3600), 0) * 3600) as datestr,
229sum(stats.counter)                                             counter
230from api_stats stats
231where stats.is_internal = 0
232and stats.timestamp > truncate(unix_timestamp() / (3600), 0) * 3600 - 3600 * 48
233group by datestr;
234# order by counter desc;
235
236
237-- 最近48小时每10分钟访问量
238select from_unixtime(truncate(timestamp / (600), 0) * 600) as datestr,
239sum(stats.counter)                                             counter
240from api_stats stats
241where stats.is_internal = 0
242and stats.timestamp > truncate(unix_timestamp() / (600), 0) * 600 - 3600 * 48
243group by datestr
244*/
245
246
247
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in

Trigger Truncate code on LambdaTest Cloud Grid

Execute automation tests with Truncate on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)