app_analytics.go 6.3 KB
Newer Older
P
Palash Nigam 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
package app

import (
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/appbaseio/abc/appbase/common"
	"github.com/olekukonko/tablewriter"
)

type analyticsResults struct {
	Count json.Number `json:"count"`
	Key   string      `json:"key"`
}

type analyticsVolumeResults struct {
	Count     json.Number `json:"count"`
	Key       json.Number `json:"key"`
	DateAsStr string      `json:"key_as_string"`
}

type overviewAnalyticsBody struct {
	NoResultSearches []analyticsResults       `json:"noResultSearches"`
	PopularSearches  []analyticsResults       `json:"popularSearches"`
	SearchVolume     []analyticsVolumeResults `json:"searchVolume"`
}

//ShowOverview .......
func ShowOverview(body io.ReadCloser) error {

	var res overviewAnalyticsBody
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	// Display the overview results
	fmt.Println("Analytics(Overview) Results:")

	// Display NoResultSearches results
	noResultTable := tablewriter.NewWriter(os.Stdout)
	noResultTable.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.NoResultSearches {
		noResultTable.Append([]string{common.JSONNumberToString(elements.Count), elements.Key})
	}
	noResultTable.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("No Result Searches")
	noResultTable.Render()

	// Display PopularSearches results
	popularSearchesTable := tablewriter.NewWriter(os.Stdout)
	popularSearchesTable.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.PopularSearches {
		popularSearchesTable.Append([]string{common.JSONNumberToString(elements.Count), elements.Key})
	}
	popularSearchesTable.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("No Result Searches")
	popularSearchesTable.Render()

	// Display SearcheVolume results
	searchVolumeTable := tablewriter.NewWriter(os.Stdout)
	searchVolumeTable.SetHeader([]string{"Count", "Key", "Date-As-Str"})
	for _, elements := range res.SearchVolume {
		searchVolumeTable.Append([]string{common.JSONNumberToString(elements.Count), common.JSONNumberToString(elements.Key), elements.DateAsStr})
	}
	searchVolumeTable.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Search Volume Results")
	searchVolumeTable.Render()

	return nil
}

type latencyResults struct {
	Count json.Number `json:"count"`
	Key   json.Number `json:"key"`
}

type latency struct {
	Latency []latencyResults `json:"latency"`
}

//ShowLatency .......
func ShowLatency(body io.ReadCloser) error {
	var res latency
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)

	if err != nil {
		fmt.Println(err)
		return err
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.Latency {
		table.Append([]string{common.JSONNumberToString(elements.Count), common.JSONNumberToString(elements.Key)})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics(Latency) Results:")
	table.Render()

	return nil
}

type geoIP struct {
	GeoIP []analyticsResults `json:"aggrByCountry"`
}

//ShowGeoIP .......
func ShowGeoIP(body io.ReadCloser) error {
	var res geoIP
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.GeoIP {
		table.Append([]string{common.JSONNumberToString(elements.Count), elements.Key})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics(GeoIP) Results:")
	table.Render()

	return nil
}

type analyticsPopularResults struct {
	Count  json.Number `json:"count"`
	Key    string      `json:"key"`
	Source string      `json:"source"`
}

type popularResults struct {
	PopularResults []analyticsPopularResults `json:"popularResults"`
}

//ShowPopularResults .......
func ShowPopularResults(body io.ReadCloser) error {
	var res popularResults
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	// TODO refine output

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key", "Source"})

	for _, elements := range res.PopularResults {
		table.Append([]string{common.JSONNumberToString(elements.Count), elements.Key, elements.Source})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics(Popular) Results:")
	table.Render()

	return nil
}

type popularSearches struct {
	Results []analyticsResults `json:"popularSearches"`
}

//ShowPopularSearches .......
func ShowPopularSearches(body io.ReadCloser) error {
	var res popularSearches
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.Results {
		table.Append([]string{common.JSONNumberToString(elements.Count), elements.Key})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics Popular Searches:")
	table.Render()

	return nil
}

type noResultSearches struct {
	Results []analyticsResults `json:"noResultSearches"`
}

//ShowNoResultSearches .......
func ShowNoResultSearches(body io.ReadCloser) error {
	var res noResultSearches
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key"})

	for _, elements := range res.Results {
		table.Append([]string{common.JSONNumberToString(elements.Count), elements.Key})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics No Result Searches:")
	table.Render()

	return nil
}

type analyticsPopularFilters struct {
	Count json.Number `json:"count"`
	Key   string      `json:"key"`
	Value string      `json:"value"`
}

type popularFilters struct {
	Results []analyticsPopularFilters `json:"popularFilters"`
}

//ShowPopularFilters .......
func ShowPopularFilters(body io.ReadCloser) error {
	var res popularFilters
	dec := json.NewDecoder(body)
	err := dec.Decode(&res)
	if err != nil {
		fmt.Println(err)
		return err
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Count", "Key", "Value"})

	for _, elements := range res.Results {
		table.Append([]string{common.JSONNumberToString(elements.Count), elements.Key, elements.Value})
	}
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("Analytics Popular Filters:")
	table.Render()

	return nil
}