Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions WordPress/Classes/Stores/StatsPeriodStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ private extension StatsPeriodStore {

setAllAsFetchingOverview()

statsRemote.getData(for: period, endingOn: date) { (summary: StatsSummaryTimeIntervalData?, error: Error?) in
if error != nil {
DDLogInfo("Error fetching summary: \(String(describing: error?.localizedDescription))")
}

DDLogInfo("Stats: Finished fetching summary.")

self.actionDispatcher.dispatch(PeriodAction.receivedSummary(summary))
}

statsRemote.getData(for: period, endingOn: date) { (posts: StatsTopPostsTimeIntervalData?, error: Error?) in
if error != nil {
DDLogInfo("Error fetching posts: \(String(describing: error?.localizedDescription))")
Expand Down Expand Up @@ -729,6 +739,7 @@ private extension StatsPeriodStore {
}

func setAllAsFetchingOverview() {
state.fetchingSummary = true
state.fetchingPostsAndPages = true
state.fetchingReferrers = true
state.fetchingClicks = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,31 @@ private extension SiteStatsPeriodViewModel {
var tableRows = [ImmuTableRow]()
tableRows.append(CellHeaderRow(title: ""))

// TODO: replace with real data
let one = OverviewTabData(tabTitle: StatSection.periodOverviewViews.tabTitle, tabData: 987654321, difference: -987, differencePercent: 5)
let two = OverviewTabData(tabTitle: StatSection.periodOverviewVisitors.tabTitle, tabData: 987654321, difference: 22222, differencePercent: 50)
let three = OverviewTabData(tabTitle: StatSection.periodOverviewLikes.tabTitle, tabData: 987654321, difference: 75324, differencePercent: 27)
let four = OverviewTabData(tabTitle: StatSection.periodOverviewComments.tabTitle, tabData: 987654321, difference: -258547987, differencePercent: -125999)
let summaryData = store.getSummary()?.summaryData ?? []

let viewsData = intervalData(summaryData: summaryData, summaryType: .views)
let viewsTabData = OverviewTabData(tabTitle: StatSection.periodOverviewViews.tabTitle,
tabData: viewsData.count,
difference: viewsData.difference,
differencePercent: viewsData.percentage)

let visitorsData = intervalData(summaryData: summaryData, summaryType: .visitors)
let visitorsTabData = OverviewTabData(tabTitle: StatSection.periodOverviewVisitors.tabTitle,
tabData: visitorsData.count,
difference: visitorsData.difference,
differencePercent: visitorsData.percentage)

let likesData = intervalData(summaryData: summaryData, summaryType: .likes)
let likesTabData = OverviewTabData(tabTitle: StatSection.periodOverviewLikes.tabTitle,
tabData: likesData.count,
difference: likesData.difference,
differencePercent: likesData.percentage)

let commentsData = intervalData(summaryData: summaryData, summaryType: .comments)
let commentsTabData = OverviewTabData(tabTitle: StatSection.periodOverviewComments.tabTitle,
tabData: commentsData.count,
difference: commentsData.difference,
differencePercent: commentsData.percentage)

// Introduced via #11063, to be replaced with real data via #11069
let viewsPeriodStub = ViewsPeriodDataStub()
Expand Down Expand Up @@ -112,12 +132,52 @@ private extension SiteStatsPeriodViewModel {
commentsStyling
]

let row = OverviewRow(tabsData: [one, two, three, four], chartData: chartData, chartStyling: chartStyling, period: lastRequestedPeriod)

let row = OverviewRow(tabsData: [viewsTabData, visitorsTabData, likesTabData, commentsTabData],
chartData: chartData, chartStyling: chartStyling, period: lastRequestedPeriod)
tableRows.append(row)

return tableRows
}

func intervalData(summaryData: [StatsSummaryData],
summaryType: StatsSummaryType) ->
(count: Int, difference: Int, percentage: Int) {

guard let currentInterval = summaryData.last else {
return (0, 0, 0)
}

let previousInterval = summaryData.count >= 2 ? summaryData[summaryData.count-2] : nil

let currentCount: Int
let previousCount: Int
switch summaryType {
case .views:
currentCount = currentInterval.viewsCount
previousCount = previousInterval?.viewsCount ?? 0
case .visitors:
currentCount = currentInterval.visitorsCount
previousCount = previousInterval?.visitorsCount ?? 0
case .likes:
currentCount = currentInterval.likesCount
previousCount = previousInterval?.likesCount ?? 0
case .comments:
currentCount = currentInterval.commentsCount
previousCount = previousInterval?.commentsCount ?? 0
}

let difference = currentCount - previousCount
var roundedPercentage = 0

if previousCount > 0 {
let percentage = (Float(difference) / Float(previousCount)) * 100
roundedPercentage = Int(round(percentage))
}

return (currentCount, difference, roundedPercentage)
}

func postsAndPagesTableRows() -> [ImmuTableRow] {
var tableRows = [ImmuTableRow]()
tableRows.append(CellHeaderRow(title: StatSection.periodPostsAndPages.title))
Expand Down