ponymail-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/2] incubator-ponymail git commit: stats.lua could use single aggregate query to get first and last years
Date Sun, 11 Dec 2016 02:18:34 GMT
Repository: incubator-ponymail
Updated Branches:
  refs/heads/master 0cb3f31a4 -> 68ea3726e


stats.lua could use single aggregate query to get first and last years

This fixes #276

Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/36e6d594
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/36e6d594
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/36e6d594

Branch: refs/heads/master
Commit: 36e6d59445d58ccd55c19eb14cc6bb7d7c816f8a
Parents: 0cb3f31
Author: Sebb <sebb@apache.org>
Authored: Sun Dec 11 01:35:41 2016 +0000
Committer: Sebb <sebb@apache.org>
Committed: Sun Dec 11 01:35:41 2016 +0000

----------------------------------------------------------------------
 CHANGELOG.md       |  1 +
 site/api/stats.lua | 61 +++++++++++++++----------------------------------
 2 files changed, 19 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/36e6d594/CHANGELOG.md
----------------------------------------------------------------------
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21c175a..372f60b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -53,6 +53,7 @@
 - stored date uses locale-dependent conversion and is ambiguous (#86)
 - search strings not properly quoted. (#76)
 - pminfo.lua creates top100 sender information but it's not used (#282)
+- stats.lua could use single aggregate query to get first and last years (#276)
 
 ## CHANGES in 0.9b:
 

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/36e6d594/site/api/stats.lua
----------------------------------------------------------------------
diff --git a/site/api/stats.lua b/site/api/stats.lua
index 61074a9..6fa0da3 100644
--- a/site/api/stats.lua
+++ b/site/api/stats.lua
@@ -445,41 +445,12 @@ function handle(r)
     -- Get years active
     local NOWISH = math.floor(os.time()/600)
     local FIRSTYEAR_KEY = "firstYear:" .. NOWISH .. ":" .. get.list .. "@" .. get.domain
-    local firstYear = r:ivm_get(FIRSTYEAR_KEY)
-    if (not firstYear or firstYear == "") and not statsOnly then
-        local doc = elastic.raw {
-            query = {
-                bool = {
-                    must = {
-                        {
-                            range = {
-                                date = {
-                                    gt = "1970/01/01 00:00:00",
-                                }
-                            }
-                        },
-                        sterm
-                    }
-                }
-            },
-            sort = {
-                {
-                    date = {
-                        order = "asc"
-                    }
-                }  
-            },
-            size = 1
-        }
-        firstYear = tonumber(os.date("%Y", doc.hits.hits[1] and doc.hits.hits[1]._source.epoch
or os.time()))
-        r:ivm_set(FIRSTYEAR_KEY, firstYear)
-    end
-    
-    -- Get years active
     local LASTYEAR_KEY = "lastYear:" .. NOWISH .. ":" .. get.list .. "@" .. get.domain
+    local firstYear = r:ivm_get(FIRSTYEAR_KEY)
     local lastYear = r:ivm_get(LASTYEAR_KEY)
-    if (not lastYear or lastYear == "")  and not statsOnly then
+    if (not firstYear or firstYear == "" or not lastYear or lastYear == "") and not statsOnly
then
         local doc = elastic.raw {
+            size = 0,
             query = {
                 bool = {
                     must = {
@@ -494,21 +465,25 @@ function handle(r)
                     }
                 }
             },
-            
-            sort = {
-                {
-                    epoch = {
-                        order = "desc"
-                    }
-                }  
-            },
-            size = 1
+            aggs = {
+                first = {
+                   min =  {
+                      field = "date"
+                  }
+              },
+              last = {
+                   max = {
+                    field = "date"
+                  }
+                }
+            }
         }
-        lastYear = tonumber(os.date("%Y", doc.hits.hits[1] and doc.hits.hits[1]._source.epoch
or os.time()))
+        firstYear = tonumber(os.date("%Y", doc.aggregations.first.value/1000))
+        r:ivm_set(FIRSTYEAR_KEY, firstYear)
+        lastYear = tonumber(os.date("%Y", doc.aggregations.last.value/1000))
         r:ivm_set(LASTYEAR_KEY, lastYear)
     end
     
-    
     -- Debug time point 6
     table.insert(t, r:clock() - tnow)
     tnow = r:clock()


Mime
View raw message