<commit_before>package models

import (
	"encoding/json"
	"github.com/vivowares/eywa/Godeps/_workspace/src/gopkg.in/olivere/elastic.v3"
	"github.com/vivowares/eywa/connections"
	. "github.com/vivowares/eywa/utils"
	"time"
)

var HistoryLength = 100

type ConnectionStatus struct {
	ChannelName  string                 `json:"channel"`
	Status       string                 `json:"status"`
	ConnectedAt  *time.Time             `json:"connected_at,omitempty"`
	LastPingedAt *time.Time             `json:"last_pinged_at,omitempty"`
	Identifier   string                 `json:"identifier"`
	Metadata     map[string]interface{} `json:"metadata,omitempty"`
	Histories    []*ConnectionHistory   `json:"histories,omitempty"`
}

type ConnectionHistory struct {
	Activity       string    `json:"activity"`
	Timestamp      time.Time `json:"timestamp"`
	ConnectionType string    `json:"connection_type"`
	Duration       *int64    `json:"duration,omitempty"`
}

func FindConnectionStatus(ch *Channel, devId string, withHistory bool) *ConnectionStatus {
	s := &ConnectionStatus{
		ChannelName: ch.Name,
		Status:      "offline",
		Identifier:  devId,
		Histories:   make([]*ConnectionHistory, 0),
	}

	conn, found := connections.FindConnection(devId)
	if found {
		s.Status = "online"
		ct := conn.CreatedAt().UTC()
		s.ConnectedAt = &ct
		pt := conn.LastPingedAt().UTC()
		s.LastPingedAt = &pt
		s.Metadata = conn.Metadata()
	}

	if withHistory {
		boolQ := elastic.NewBoolQuery()
		mustQs := make([]elastic.Query, 0)
		mustQs = append(mustQs, elastic.NewTermQuery("device_id", devId))
		boolQ.Must(mustQs...)

		orQs := make([]elastic.Query, 0)
		orQs = append(orQs, elastic.NewTermQuery("message_type", "start"))
		orQs = append(orQs, elastic.NewTermQuery("message_type", "close"))
		boolQ.Should(orQs...)

		resp, err := IndexClient.Search().Index(GlobIndexName(ch)).Type(IndexTypeActivities).Query(boolQ).Sort("timestamp", false).Size(HistoryLength).Do()
		if err == nil {
			for _, hit := range resp.Hits.Hits {
				var t map[string]interface{}
				err = json.Unmarshal(*hit.Source, &t)
				if err == nil {
					var d *int64
					if _d, ok := t["duration"].(int64); ok {
						d = &_d
					}
					s.Histories = append(s.Histories, &ConnectionHistory{
						Activity: t["message_type"].(string),
						Timestamp: time.Unix(
							MilliSecToSec(int64(t["timestamp"].(float64))),
							MilliSecToNano(int64(t["timestamp"].(float64))),
						).UTC(),
						ConnectionType: t["connection_type"].(string),
						Duration:       d,
					})
				}
			}
		}
	}

	return s
}<commit_msg>fixed the hidden duration of connection status<commit_after>package models

import (
	"encoding/json"
	"github.com/vivowares/eywa/Godeps/_workspace/src/gopkg.in/olivere/elastic.v3"
	"github.com/vivowares/eywa/connections"
	. "github.com/vivowares/eywa/utils"
	"time"
)

var HistoryLength = 100

type ConnectionStatus struct {
	ChannelName  string                 `json:"channel"`
	Status       string                 `json:"status"`
	ConnectedAt  *time.Time             `json:"connected_at,omitempty"`
	LastPingedAt *time.Time             `json:"last_pinged_at,omitempty"`
	Identifier   string                 `json:"identifier"`
	Metadata     map[string]interface{} `json:"metadata,omitempty"`
	Histories    []*ConnectionHistory   `json:"histories,omitempty"`
}

type ConnectionHistory struct {
	Activity       string    `json:"activity"`
	Timestamp      time.Time `json:"timestamp"`
	ConnectionType string    `json:"connection_type"`
	Duration       *int64    `json:"duration,omitempty"`
}

func FindConnectionStatus(ch *Channel, devId string, withHistory bool) *ConnectionStatus {
	s := &ConnectionStatus{
		ChannelName: ch.Name,
		Status:      "offline",
		Identifier:  devId,
		Histories:   make([]*ConnectionHistory, 0),
	}

	conn, found := connections.FindConnection(devId)
	if found {
		s.Status = "online"
		ct := conn.CreatedAt().UTC()
		s.ConnectedAt = &ct
		pt := conn.LastPingedAt().UTC()
		s.LastPingedAt = &pt
		s.Metadata = conn.Metadata()
	}

	if withHistory {
		boolQ := elastic.NewBoolQuery()
		mustQs := make([]elastic.Query, 0)
		mustQs = append(mustQs, elastic.NewTermQuery("device_id", devId))
		boolQ.Must(mustQs...)

		orQs := make([]elastic.Query, 0)
		orQs = append(orQs, elastic.NewTermQuery("message_type", "start"))
		orQs = append(orQs, elastic.NewTermQuery("message_type", "close"))
		boolQ.Should(orQs...)

		resp, err := IndexClient.Search().Index(GlobIndexName(ch)).Type(IndexTypeActivities).Query(boolQ).Sort("timestamp", false).Size(HistoryLength).Do()
		if err == nil {
			for _, hit := range resp.Hits.Hits {
				var t map[string]interface{}
				err = json.Unmarshal(*hit.Source, &t)
				if err == nil {
					var d *int64
					if _f, ok := t["duration"].(float64); ok {
						_d := int64(_f)
						d = &_d
					}
					s.Histories = append(s.Histories, &ConnectionHistory{
						Activity: t["message_type"].(string),
						Timestamp: time.Unix(
							MilliSecToSec(int64(t["timestamp"].(float64))),
							MilliSecToNano(int64(t["timestamp"].(float64))),
						).UTC(),
						ConnectionType: t["connection_type"].(string),
						Duration:       d,
					})
				}
			}
		}
	}

	return s
}
{'fraction_non_alphanumeric': 0.14904679376083188, 'fraction_numerical': 0.008472944348160986, 'mean_word_length': 3.4545454545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5973160', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1721, 'n_words': 320}